From ab8b347a92de3a0a5b3c6cd6556a32108a118c0a Mon Sep 17 00:00:00 2001 From: frankast Date: Wed, 9 Jan 2019 17:30:20 +0600 Subject: [PATCH] fix(index): force increment version of doc --- src/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index e381c38..b541a17 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ import DiffModel from './DiffModel'; import { getExcludedFields } from './utils'; export default function plugin(schema: MongooseSchema, options?: OptionsT) { + // $FlowFixMe if (!schema.options.versionKey) throw new Error(`You must provide 'versionKey' option to your schema or remain it as default`); @@ -26,8 +27,8 @@ export default function plugin(schema: MongooseSchema, 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