New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-auto-increment

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-auto-increment - npm Package Compare versions

Comparing version 2.0.4 to 2.1.0

2

CONTRIBUTING.md

@@ -35,2 +35,2 @@ ## Submitting Issues

To contribute changes to this project please submit them as a [pull request](https://github.com/Chevex/mongoose-auto-increment/pulls). If you don't know how to submit a pull request then see [this article](https://help.github.com/articles/using-pull-requests).
To contribute changes to this project please submit them as a [pull request](https://github.com/Chevex/mongoose-auto-increment/pulls). If you don't know how to submit a pull request then see [this article](https://help.github.com/articles/using-pull-requests).

@@ -108,2 +108,18 @@ // Module Scope

// Declare a function to reset counter at the start value - increment value.
var resetCount = function (callback) {
Counter.findOneAndUpdate(
{ model: settings.model, field: settings.field },
{ count: settings.startAt - settings.incrementBy },
{ new: true }, // new: true specifies that the callback should get the updated counter.
function (err) {
if (err) return callback(err);
callback(null, settings.startAt);
}
);
};
// Add resetCount as both a method on documents and a static on the schema for convenience.
schema.method('resetCount', resetCount);
schema.static('resetCount', resetCount);
// Every time documents in this schema are saved, run this logic.

@@ -110,0 +126,0 @@ schema.pre('save', function (next) {

{
"name": "mongoose-auto-increment",
"version": "2.0.4",
"description": "This plugin allows you to auto-increment any field on any mongoose schema that you wish.",
"repository": {
"type": "git",
"url": "git://github.com/Chevex/mongoose-auto-increment.git"
"name": "mongoose-auto-increment",
"version": "2.1.0",
"description": "This plugin allows you to auto-increment any field on any mongoose schema that you wish.",
"repository": {
"type": "git",
"url": "git://github.com/Chevex/mongoose-auto-increment.git"
},
"dependencies": {
"extend": "*"
},
"peerDependencies": {
"mongoose": "*"
},
"devDependencies": {
"mocha": "*",
"mongoose": "*",
"chai": "*",
"async": "*"
},
"keywords": [
"mongoose",
"plugin",
"auto-increment",
"auto",
"increment",
"automatic",
"autoincrement",
"auto_increment",
"autoinc",
"auto-inc",
"auto_inc",
"pureautoinc",
"mongoose-pureautoinc"
],
"author": {
"name": "Alex Ford (Chevex)",
"email": "alex.ford@codetunnel.com",
"url": "http://CodeTunnel.com"
},
"contributors": [
{
"name": "Nassor Paulino da Silva (rossan)",
"email": "nassor@gmail.com"
},
"dependencies": {
"extend": "*"
{
"name": "Misha Koryak (mkoryak)",
"email": "mkoryak@gmail.com",
"url": "http://ExhibitionNest.com"
},
"peerDependencies": {
"mongoose": "*"
{
"name": "Christopher Hiller (boneskull)",
"url": "http://boneskull.github.io/"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"mongoose": "*"
},
"keywords": [
"mongoose",
"plugin",
"auto-increment",
"auto",
"increment",
"automatic",
"autoincrement",
"auto_increment",
"autoinc",
"auto-inc",
"auto_inc",
"pureautoinc",
"mongoose-pureautoinc"
],
"author": {
"name": "Alex Ford (Chevex)",
"email": "alex.ford@codetunnel.com",
"url": "http://CodeTunnel.com"
},
"contributors": [
{
"name": "Nassor Paulino da Silva (rossan)",
"email": "nassor@gmail.com"
},
{
"name": "Misha Koryak (mkoryak)",
"email": "mkoryak@gmail.com",
"url": "http://ExhibitionNest.com"
},
{
"name": "Christopher Hiller (boneskull)",
"url": "http://boneskull.github.io/"
}
],
"scripts": {
"test": "node_modules/mocha/bin/mocha"
},
"bugs": {
"url": "https://github.com/Chevex/mongoose-auto-increment/issues"
{
"name": "tomaskavka",
"url": "https://github.com/tomaskavka"
}
],
"scripts": {
"test": "node_modules/mocha/bin/mocha"
},
"bugs": {
"url": "https://github.com/Chevex/mongoose-auto-increment/issues"
}
}

@@ -80,3 +80,32 @@ # mongoose-auto-increment

});
nextCount is both a static method on the model (`Book.nextCount(...)`) and an instance method on the document (`book.nextCount(...)`).
### Want to reset counter back to the start value?
bookSchema.plugin(autoIncrement.plugin, {
model: 'Book',
field: 'bookId',
startAt: 100
});
var Book = connection.model('Book', bookSchema),
book = new Book();
book.save(function (err) {
// book._id === 100 -> true
book.nextCount(function(err, count) {
// count === 101 -> true
book.resetCount(function(err, nextCount) {
// nextCount === 100 -> true
});
});
});

@@ -1,11 +0,12 @@

var should = require('should'),
var async = require('async'),
should = require('chai').should(),
mongoose = require('mongoose'),
autoIncrement = require('../index'),
db;
autoIncrement = require('..'),
conn;
before(function (done) {
db = mongoose.createConnection('mongodb://127.0.0.1/mongoose-auto-increment-test');
db.on('error', console.error.bind(console));
db.once('open', function () {
autoIncrement.initialize(db);
conn = mongoose.createConnection('mongodb://127.0.0.1/mongoose-auto-increment-test');
conn.on('error', console.error.bind(console));
conn.once('open', function () {
autoIncrement.initialize(conn);
done();

@@ -16,4 +17,4 @@ });

after(function (done) {
db.db.executeDbCommand({dropDatabase: 1}, function () {
db.close(done);
conn.db.executeDbCommand({ ropDatabase: 1 }, function () {
conn.close(done);
});

@@ -23,6 +24,5 @@ });

afterEach(function (done) {
db.model('User').collection.drop(function (err) {
if (err) return done(err);
delete db.models.User;
db.model('Mongoose-Auto-Increment').collection.drop(done);
conn.model('User').collection.drop(function () {
delete conn.models.User;
return conn.model('Mongoose-Auto-Increment').collection.drop(done);
});

@@ -35,2 +35,3 @@ });

// Arrange
var userSchema = new mongoose.Schema({

@@ -41,16 +42,23 @@ name: String,

userSchema.plugin(autoIncrement.plugin, 'User');
var User = db.model('User', userSchema);
var User = conn.model('User', userSchema),
user = new User({ name: 'Charlie', dept: 'Support' }),
user2 = new User({ name: 'Charlene', dept: 'Marketing' });
var user = new User({ name: 'Charlie', dept: 'Support' });
user.save(function (err) {
should.not.exists(err);
user._id.should.eql(0);
// Act
async.series({
user1: function (cb) {
user.save(cb);
},
user2: function (cb) {
user2.save(cb);
}
}, assert);
var user2 = new User({ name: 'Charlene', dept: 'Marketing' });
user2.save(function (err) {
should.not.exists(err);
user2._id.should.eql(1);
done();
});
});
// Assert
function assert(err, results) {
should.not.exist(err);
results.user1[0].should.have.property('_id', 0);
results.user2[0].should.have.property('_id', 1);
done();
}

@@ -61,2 +69,3 @@ });

// Arrange
var userSchema = new mongoose.Schema({

@@ -67,16 +76,23 @@ name: String,

userSchema.plugin(autoIncrement.plugin, { model: 'User', field: 'userId' });
var User = db.model('User', userSchema);
var User = conn.model('User', userSchema),
user1 = new User({ name: 'Charlie', dept: 'Support' }),
user2 = new User({ name: 'Charlene', dept: 'Marketing' });
var user = new User({ name: 'Charlie', dept: 'Support' });
user.save(function(err) {
should.not.exists(err);
user.userId.should.eql(0);
// Act
async.series({
user1: function (cb) {
user1.save(cb);
},
user2: function (cb) {
user2.save(cb);
}
}, assert);
var user2 = new User({ name: 'Charlene', dept: 'Marketing' });
user2.save(function (err) {
should.not.exists(err);
user2.userId.should.eql(1);
done();
});
});
// Assert
function assert(err, results) {
should.not.exist(err);
results.user1[0].should.have.property('userId', 0);
results.user2[0].should.have.property('userId', 1);
done();
}

@@ -88,2 +104,3 @@ });

// Arrange
var userSchema = new mongoose.Schema({

@@ -94,16 +111,23 @@ name: String,

userSchema.plugin(autoIncrement.plugin, { model: 'User', startAt: 3 });
var User = db.model('User', userSchema);
var User = conn.model('User', userSchema),
user1 = new User({ name: 'Charlie', dept: 'Support' }),
user2 = new User({ name: 'Charlene', dept: 'Marketing' });
var user = new User({ name: 'Charlie', dept: 'Support' });
user.save(function (err) {
should.not.exists(err);
user._id.should.eql(3);
// Act
async.series({
user1: function (cb) {
user1.save(cb);
},
user2: function (cb) {
user2.save(cb);
}
}, assert);
var user2 = new User({ name: 'Charlene', dept: 'Marketing' });
user2.save(function (err) {
should.not.exists(err);
user2._id.should.eql(4);
done();
});
});
// Assert
function assert(err, results) {
should.not.exist(err);
results.user1[0].should.have.property('_id', 3);
results.user2[0].should.have.property('_id', 4);
done();
}

@@ -114,2 +138,3 @@ });

// Arrange
var userSchema = new mongoose.Schema({

@@ -120,23 +145,32 @@ name: String,

userSchema.plugin(autoIncrement.plugin, { model: 'User', incrementBy: 5 });
var User = db.model('User', userSchema);
var User = conn.model('User', userSchema),
user1 = new User({ name: 'Charlie', dept: 'Support' }),
user2 = new User({ name: 'Charlene', dept: 'Marketing' });
var user = new User({ name: 'Charlie', dept: 'Support' });
user.save(function (err) {
should.not.exists(err);
user._id.should.eql(0);
// Act
async.series({
user1: function (cb) {
user1.save(cb);
},
user2: function (cb) {
user2.save(cb);
}
}, assert);
var user2 = new User({ name: 'Charlene', dept: 'Marketing' });
user2.save(function (err) {
should.not.exists(err);
user2._id.should.eql(5);
done();
});
});
// Assert
function assert(err, results) {
should.not.exist(err);
results.user1[0].should.have.property('_id', 0);
results.user2[0].should.have.property('_id', 5);
done();
}
});
describe('nextCount function', function () {
describe('helper function', function () {
it('should return the next count for the model and field (Test 5)', function (done) {
it('nextCount should return the next count for the model and field (Test 5)', function (done) {
// Arrange
var userSchema = new mongoose.Schema({

@@ -147,39 +181,75 @@ name: String,

userSchema.plugin(autoIncrement.plugin, 'User');
var User = db.model('User', userSchema);
var User = conn.model('User', userSchema),
user1 = new User({ name: 'Charlie', dept: 'Support' }),
user2 = new User({ name: 'Charlene', dept: 'Marketing' });;
// Create user and call nextCount.
var user = new User({name: 'Charlie', dept: 'Support'});
user.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(0);
// Act
async.series({
count1: function (cb) {
user1.nextCount(cb);
},
user1: function (cb) {
user1.save(cb);
},
count2: function (cb) {
user1.nextCount(cb);
},
user2: function (cb) {
user2.save(cb);
},
count3: function (cb) {
user2.nextCount(cb);
}
}, assert);
// Now save user and check if its _id is what nextCount said.
user.save(function (err) {
should.not.exists(err);
user._id.should.eql(0);
// Assert
function assert(err, results) {
should.not.exist(err);
results.count1.should.equal(0);
results.user1[0].should.have.property('_id', 0);
results.count2.should.equal(1);
results.user2[0].should.have.property('_id', 1);
results.count3.should.equal(2);
done();
}
// Call nextCount again to ensure it reflects the next number correctly.
user.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(1);
});
// Call nextCount one more time to ensure the value is the same when a save is not performed.
user.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(1);
it('resetCount should cause the count to reset as if there were no documents yet.', function (done) {
// Create a second user and ensure its _id is the value of the last call to nextCount.
var user2 = new User({name: 'Charlene', dept: 'Marketing'});
user2.save(function () {
user2._id.should.eql(1);
User.nextCount(function (err, count) {
should.not.exists(err);
count.should.eql(2);
done();
});
});
});
});
});
// Arrange
var userSchema = new mongoose.Schema({
name: String,
dept: String
});
userSchema.plugin(autoIncrement.plugin, 'User');
var User = conn.model('User', userSchema),
user = new User({name: 'Charlie', dept: 'Support'});
// Act
async.series({
user: function (cb) {
user.save(cb);
},
count1: function (cb) {
user.nextCount(cb);
},
reset: function (cb) {
user.resetCount(cb);
},
count2: function (cb) {
user.nextCount(cb);
}
}, assert);
// Assert
function assert(err, results) {
should.not.exist(err);
results.user[0].should.have.property('_id', 0);
results.count1.should.equal(1);
results.reset.should.equal(0);
results.count2.should.equal(0);
done();
}
});

@@ -186,0 +256,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc