Skip to content

Commit

Permalink
feat(AutoIncrementID): support setting "AutoIncrementIDSkipSymbol" on…
Browse files Browse the repository at this point in the history
… the connection

to allow being using with ".create" and on multiple documents
  • Loading branch information
hasezoey committed Sep 1, 2023
1 parent 2ea4988 commit 6d2f447
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions src/autoIncrement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ export function AutoIncrementID(schema: mongoose.Schema<any>, 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(
{
Expand Down
51 changes: 51 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 6d2f447

Please sign in to comment.