From 6d2f447e3817cd10316f4d4b8608037b0195d252 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Thu, 31 Aug 2023 15:24:34 +0200 Subject: [PATCH] feat(AutoIncrementID): support setting "AutoIncrementIDSkipSymbol" on the connection to allow being using with ".create" and on multiple documents --- README.md | 11 +++++++++- src/autoIncrement.ts | 8 +++++++ test/basic.test.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f230817..c9e08a0 100644 --- a/README.md +++ b/README.md @@ -137,10 +137,19 @@ const doc = new Model(); doc[AutoIncrementIDSkipSymbol] = true; await doc.save(); + +// the symbol can also be set on the connection +Model.db[AutoIncrementIDSkipSymbol] = true; +await Model.create({ _id: something }); +// dont forget to unset it again +Model.db[AutoIncrementIDSkipSymbol] = true; +// or +delete Model.db[AutoIncrementIDSkipSymbol]; ``` Note: `AutoIncrementIDSkipSymbol` can also be set inside hooks, but hooks might be called before others. -Note: `AutoIncrementIDSkipSymbol` cannot be used for `.create`, because `AutoIncrementIDSkipSymbol` works on the current document and `.create({ [AutoIncrementIDSkipSymbol]: true })` will not transfer symbols to the document +Note: `AutoIncrementIDSkipSymbol` cannot be used for `.create` (unless set via connection), because `AutoIncrementIDSkipSymbol` works on the current document and `.create({ [AutoIncrementIDSkipSymbol]: true })` will not transfer symbols to the document. +Note: if `AutoIncrementIDSkipSymbol` is set to `false` on either method, no modification will be done #### incrementBy diff --git a/src/autoIncrement.ts b/src/autoIncrement.ts index e4773c8..248a5e5 100644 --- a/src/autoIncrement.ts +++ b/src/autoIncrement.ts @@ -166,6 +166,14 @@ export function AutoIncrementID(schema: mongoose.Schema, options: AutoIncre return; } + const ownerDocDb = this.db ?? (this as any).ownerDocument().db; + + if (typeof ownerDocDb[AutoIncrementIDSkipSymbol] == 'boolean' && ownerDocDb[AutoIncrementIDSkipSymbol]) { + logger.info('Symbol "AutoIncrementIDSkipSymbol" is set on the connection to "true", skipping'); + + return; + } + const leandoc = await model .findOneAndUpdate( { diff --git a/test/basic.test.ts b/test/basic.test.ts index 8d2c1d6..1ec5c76 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -543,6 +543,57 @@ describe('Basic Suite', () => { expect(foundTracker.count).toEqual(1n); }); + it('should support skipping with symbol on the connection', async () => { + const schema = new mongoose.Schema({ + _id: Number, + somefield: Number, + }); + schema.plugin(AutoIncrementID, {}); + const model = mongoose.model('AutoIncrementID-SomeModel-skiptest-connection', schema); + + // test initial 0 + { + const doc = await model.create({ somefield: 10 }); + expect(doc.somefield).toBe(10); + expect(doc._id).toBe(0); + + await doc.save(); + expect(doc.somefield).toBe(10); + expect(doc._id).toBe(0); + } + + // test add 1 + { + model.db[AutoIncrementIDSkipSymbol] = true; + const doc = await model.create({ _id: 300, somefield: 20 }); + // IMPORTANT: this has to be unset again so that the next ones are not also affected + delete model.db[AutoIncrementIDSkipSymbol]; + expect(doc.somefield).toBe(20); + expect(doc._id).toBe(300); + + await doc.save(); + expect(doc.somefield).toBe(20); + expect(doc._id).toBe(300); + } + + // test add another 1 + { + const doc = await model.create({ somefield: 30 }); + expect(doc.somefield).toBe(30); + expect(doc._id).toBe(1); + + await doc.save(); + expect(doc.somefield).toBe(30); + expect(doc._id).toBe(1); + } + + const trackerModel = mongoose.connection.models['identitycounter']; + expect(Object.getPrototypeOf(trackerModel)).toStrictEqual(mongoose.Model); + + const foundTracker = await trackerModel.findOne({ modelName: model.modelName }).orFail(); + expect(foundTracker.count).toEqual(1n); + }); + it('should throw a error if "overwriteModelName" is a function but returns a empty string', async () => { let fcalled = 0;