From bb22a5fda171e36bf8d75defe751a72b4bc17caa Mon Sep 17 00:00:00 2001 From: hasezoey Date: Mon, 1 Aug 2022 14:24:18 +0200 Subject: [PATCH] feat(autoIncrement::AutoIncrementID): add option to overwrite "modelName" property in tracker-doc re https://github.com/typegoose/auto-increment/discussions/15 --- README.md | 9 ++++- src/autoIncrement.ts | 3 +- src/types.ts | 6 ++++ test/basic.test.ts | 78 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f134c66..e3437d7 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ This option is optional, defaults to `identitycounters`. `string` -Set the tracker-model's name (sets the model-name of the tracker model like `mongoose.model(NameHere, ...)`) +Set the tracker-model's name (sets the model-name of the tracker model like `mongoose.model(NameHere, ...)`). This option is optional, defaults to `identitycounter`. @@ -174,6 +174,13 @@ This option is optional, defaults to `identitycounter`. Set the starting number of the counter. (the first document will be this number) +### overwriteModelName + +`string` default's to the documents's model-name + +Overwrite the used value for the `modelName` property on the tracker-document, this can be used when wanting to use the same tracker document across different models. +If the value is falsy, then it will default to the `modelName`. + ## Notes * Please dont add comments with `+1` or something like that, use the Reactions diff --git a/src/autoIncrement.ts b/src/autoIncrement.ts index 083ba73..0bdfb99 100644 --- a/src/autoIncrement.ts +++ b/src/autoIncrement.ts @@ -87,6 +87,7 @@ export function AutoIncrementID(schema: mongoose.Schema, options: AutoIncre trackerCollection: 'identitycounters', trackerModelName: 'identitycounter', startAt: 0, + overwriteModelName: '', ...options, }; @@ -102,7 +103,7 @@ export function AutoIncrementID(schema: mongoose.Schema, options: AutoIncre schema.pre('save', async function AutoIncrementPreSaveID(): Promise { logger.info('AutoIncrementID PreSave'); - const modelName: string = (this.constructor as any).modelName; + const modelName: string = opt.overwriteModelName || (this.constructor as any).modelName; if (!model) { logger.info('Creating idtracker model named "%s"', opt.trackerModelName); diff --git a/src/types.ts b/src/types.ts index b996600..497a44f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,6 +37,12 @@ export interface AutoIncrementIDOptions { * @default 0 */ startAt?: number; + /** + * Overwrite what to use for the `modelName` property in the tracker document + * This can be overwritten when wanting to use a single tracker for multiple models + * Defaults to `document.constructor.modelName` + */ + overwriteModelName?: string; } export interface AutoIncrementIDTrackerSpec { diff --git a/test/basic.test.ts b/test/basic.test.ts index 2a271e8..ef1b2b2 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -209,6 +209,84 @@ describe('Basic Suite', () => { const doc2 = await ParentModel.create({ nested: { someField: 'hello2' } }); expect(doc2.nested._id).toBe(2); }); + + it('should make use of "overwriteModelName"', async () => { + const schema1 = new mongoose.Schema({ + _id: Number, + somefield: Number, + }); + schema1.plugin(AutoIncrementID, { overwriteModelName: 'TestOverwrite' }); + const model1 = mongoose.model('AutoIncrementID-OMN1', schema1); + + const schema2 = new mongoose.Schema({ + _id: Number, + somefield: Number, + }); + schema2.plugin(AutoIncrementID, { overwriteModelName: 'TestOverwrite' }); + const model2 = mongoose.model('AutoIncrementID-OMN2', schema2); + + // test schema1 initial 0 + { + const doc = await model1.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 schema1 add 1 + { + const doc = await model1.create({ somefield: 20 }); + expect(doc.somefield).toBe(20); + expect(doc._id).toBe(1); + + await doc.save(); + expect(doc.somefield).toBe(20); + expect(doc._id).toBe(1); + } + + // test schema2 add 1 & use same tracker + { + const doc = await model2.create({ somefield: 30 }); + expect(doc.somefield).toBe(30); + expect(doc._id).toBe(2); + + await doc.save(); + expect(doc.somefield).toBe(30); + expect(doc._id).toBe(2); + } + + const trackerModel = mongoose.connection.models['identitycounter']; + // expect(trackerModel).toBeInstanceOf(mongoose.connection.Model); // disabled, see https://github.com/Automattic/mongoose/discussions/12179 + + const foundTracker = await trackerModel.findOne({ modelName: 'TestOverwrite' }).orFail(); + expect(foundTracker.count).toEqual(2); + }); + + it('should use modelName if "overwriteModelName" is falsy', async () => { + const schema = new mongoose.Schema({ + _id: Number, + somefield: Number, + }); + schema.plugin(AutoIncrementID, { overwriteModelName: '' }); + const model = mongoose.model('AutoIncrementID-EOMN', schema); + + 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); + + const trackerModel = mongoose.connection.models['identitycounter']; + // expect(trackerModel).toBeInstanceOf(mongoose.connection.Model); // disabled, see https://github.com/Automattic/mongoose/discussions/12179 + + const foundTracker = await trackerModel.findOne({ modelName: 'AutoIncrementID-EOMN' }).orFail(); + expect(foundTracker.count).toEqual(0); + }); }); });