Skip to content

Commit

Permalink
Merge tag '4.0.1' into develop
Browse files Browse the repository at this point in the history
4.0.1
  • Loading branch information
ramiel committed Oct 16, 2017
2 parents 904c800 + aa51e51 commit a003482
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js
node_js:
- "6"
- "4"
- "0.12"
- "node"
- "lts/*"
services:
- mongodb
sudo: false
Expand Down
29 changes: 18 additions & 11 deletions lib/sequence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var _ = require('lodash'),
async = require('async'),
mongoose = null,
SequenceArchive = require('./sequence_archive'),
sequenceArchive = SequenceArchive.getSingleton(),
Expand Down Expand Up @@ -254,18 +255,24 @@ Sequence.prototype._resetCounter = function(id, reference, callback){
* @param {Function} callback Called with the sequence counter
*/
Sequence.prototype._setNextCounter = function(doc, callback) {
var id = this.getId();
var referenceValue = this._getCounterReferenceField(doc);
this._counterModel.findOneAndUpdate(
{ id: id, reference_value: referenceValue },
{ $inc: { seq: 1 } },
{ new: true, upsert: true, passRawResult: true },
function(err, counter) {
if (err) return callback(err);
return callback(null, counter.seq);
}

);
var retriable = function(callback) {
var id = this.getId();
var referenceValue = this._getCounterReferenceField(doc);
this._counterModel.findOneAndUpdate(
{ id: id, reference_value: referenceValue },
{ $inc: { seq: 1 } },
{ new: true, upsert: true, passRawResult: true },
function(err, counter) {
if (err) return callback(err);
return callback(null, counter.seq);
}

);
};

async.retry(0, retriable.bind(this), callback);

};

module.exports = function(goose){
Expand Down
130 changes: 102 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-sequence",
"version": "4.0.0",
"version": "4.0.1",
"description": "Very generic autoincrement plugin for mongoose",
"main": "index.js",
"scripts": {
Expand All @@ -23,16 +23,17 @@
},
"license": "GPL-2.0",
"dependencies": {
"async": "^2.5.0",
"lodash": "^4.6.1"
},
"peerDependencies": {
"mongoose": ">=4"
},
"devDependencies": {
"async": "^1.4.2",
"chai": "^3.3.0",
"coveralls": "^2.11.8",
"grunt": "^0.4.5",
"grunt-cli": "^1.2.0",
"grunt-eslint": "^18.0.0",
"grunt-mocha-istanbul": "^3.0.1",
"istanbul": "^0.4.2",
Expand Down
14 changes: 13 additions & 1 deletion test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ describe('Basic => ', function() {
);
});

it('can create multiple document in parallel when the sequence is on _id', function(done) {
async.parallel(
[
function(callback){ this.MainId.create({}, callback); }.bind(this),
function(callback){ this.MainId.create({}, callback); }.bind(this),
function(callback){ this.MainId.create({}, callback); }.bind(this)
],
done
);
});


it('updating a document do not increment the counter', function(done) {
this.SimpleField.findOne({}, function(err, entity) {
var id = entity.id;
Expand Down Expand Up @@ -187,7 +199,7 @@ describe('Basic => ', function() {
var ids = documents.map(function(d) {return d._id;});

try {
assert.deepEqual(ids, [1, 2, 3, 4, 5]);
assert.deepEqual([4, 5, 6, 7, 8], ids);
}catch (e) {
return done(e);
}
Expand Down
Loading

0 comments on commit a003482

Please sign in to comment.