Skip to content

Commit

Permalink
Fix counterReset error when set 'start_seq' to non-zero number (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeqiang authored and ramiel committed Sep 3, 2019
1 parent 598d01b commit 15ca137
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,14 @@ module.exports = function SequenceFactory(connection) {
Sequence.prototype._resetCounter = function (id, reference, callback) {
const condition = { id };
let cb = callback;
let seq = 0;
if (reference instanceof Function) {
cb = reference;
} else {
condition.reference_value = this._getCounterReferenceField(reference);
}
this._counterModel.updateMany(condition, { $set: { seq: 0 } }, null, cb);
if (this._options.start_seq) seq = this._options.start_seq - 1;
this._counterModel.updateMany(condition, { $set: { seq } }, null, cb);
};

/**
Expand Down
46 changes: 46 additions & 0 deletions test/sequence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ describe('Basic => ', () => {

describe('Reset counter => ', () => {
let ResettableSimple;
let ResettableWithStartSeq;
let ResettableComposed;

beforeAll(() => {
Expand All @@ -470,6 +471,13 @@ describe('Basic => ', () => {
ResettableSimpleSchema.plugin(AutoIncrement, { id: 'resettable_simple_id', inc_field: 'id' });
ResettableSimple = mongoose.model('ResettableSimple', ResettableSimpleSchema);

const ResettableWithStartSeqSchema = new Schema({
id: Number,
val: String,
});
ResettableWithStartSeqSchema.plugin(AutoIncrement, { id: 'resettable_startseq_id', inc_field: 'id', start_seq: 100 });
ResettableWithStartSeq = mongoose.model('ResettableWithStartSeq', ResettableWithStartSeqSchema);

const ResettableComposedSchema = new Schema({
country: String,
city: String,
Expand Down Expand Up @@ -502,6 +510,25 @@ describe('Basic => ', () => {
);
});

beforeEach((done) => {
let count = 0;
const documents = [];

async.whilst(
() => count < 5,

(callback) => {
count += 1;
const t = new ResettableWithStartSeq();
documents.push(t);
t.save(callback);
},

done,

);
});

beforeEach((done) => {
let count = 0;
const documents = [];
Expand Down Expand Up @@ -542,6 +569,7 @@ describe('Basic => ', () => {

it('a model gains a static "counterReset" method', () => {
assert.isFunction(ResettableSimple.counterReset);
assert.isFunction(ResettableWithStartSeq.counterReset);
});

it('after calling it, the counter is 1', (done) => {
Expand All @@ -562,6 +590,24 @@ describe('Basic => ', () => {
});
});

it('after calling it, the counter is 100', (done) => {
ResettableWithStartSeq.counterReset('resettable_startseq_id', (err) => {
if (err) {
done(err);
return;
}
const t = new ResettableWithStartSeq();
t.save((e, saved) => {
if (e) {
done(e);
return;
}
assert.deepEqual(saved.id, 100);
done();
});
});
});

it('for a referenced counter, the counter is 1 for any reference', (done) => {
ResettableComposed.counterReset('resettable_inhabitant_counter', (err) => {
if (err) {
Expand Down

0 comments on commit 15ca137

Please sign in to comment.