Skip to content

Commit

Permalink
feat(autoIncrement::AutoIncrementID): add option to overwrite "modelN…
Browse files Browse the repository at this point in the history
…ame" property in tracker-doc

re #15
  • Loading branch information
hasezoey committed Aug 1, 2022
1 parent d6fdff1 commit bb22a5f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/autoIncrement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export function AutoIncrementID(schema: mongoose.Schema<any>, options: AutoIncre
trackerCollection: 'identitycounters',
trackerModelName: 'identitycounter',
startAt: 0,
overwriteModelName: '',
...options,
};

Expand All @@ -102,7 +103,7 @@ export function AutoIncrementID(schema: mongoose.Schema<any>, options: AutoIncre
schema.pre('save', async function AutoIncrementPreSaveID(): Promise<void> {
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);
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
78 changes: 78 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit bb22a5f

Please sign in to comment.