Skip to content

Commit

Permalink
fix(index): force increment version of doc
Browse files Browse the repository at this point in the history
  • Loading branch information
borodayev committed Jan 9, 2019
1 parent 3fad063 commit ab8b347
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DiffModel from './DiffModel';
import { getExcludedFields } from './utils';

export default function plugin(schema: MongooseSchema<any>, options?: OptionsT) {
// $FlowFixMe
if (!schema.options.versionKey)
throw new Error(`You must provide 'versionKey' option to your schema or remain it as default`);

Expand All @@ -26,8 +27,8 @@ export default function plugin(schema: MongooseSchema<any>, options?: OptionsT)
});

schema.pre('save', async function() {
// && this._original
if (!this.isNew) {
if (!this.isNew && this._original) {
await this.increment();
const lhs = this._original;
const rhs = this.toObject();
const version = this[versionKey] + 1; // cause we're inside preSave hook
Expand Down

0 comments on commit ab8b347

Please sign in to comment.