Mongoose plugin to generate incremented field
Install via npm:
$ npm install mongoose-increment
type
{Type} field type (optional, default value isNumber
)modelName
{String} mongoose model namefieldName
{String} mongoose increment field namestart
{Integer} start number for counter (optional, default1
)increment
{Integer} number to increment counter (optional, default1
)prefix
{String/Function} counter prefix (optional, default value is an emptyString
)suffix
{String/Function} counter suffix (optional, default value is an emptyString
)unique
{Boolean} unique field (optional, defaulttrue
)resetAfter
{Integer} reset counter (optional, default0
)hasVersion
{Boolean} has version (optional, defaultfalse
)startVersion
{Integer} start number for version (optional, default1
)delimiterVersion
{String} delimiter for version counter (optional, default-
)
Return a fulfilled promise when increment field has been setted
Return an object which contain instance prefix, suffix and counter.
Set the next version from the current document version.
Return a fulfilled promise when sequence has been reset
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
});
TestSchema.plugin(increment, {
modelName: 'Test_increment',
fieldName: 'increment_field',
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc = new TestIncrement({ label: 'label_1' });
doc.save(); // doc saved with `increment_field` === 1
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
});
TestSchema.plugin(increment, {
type: String,
modelName: 'Test_increment',
fieldName: 'increment_field',
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc = new TestIncrement({ label: 'label_1' });
doc.save(); // doc saved with `increment_field` === '1'
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
});
TestSchema.plugin(increment, {
modelName: 'Test_increment',
fieldName: 'increment_field',
start: 300,
increment: 3,
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc1 = new TestIncrement({ label: 'label_1' });
doc1.save(); // doc saved with `increment_field` === 300
doc1.parseSequence(); // => { prefix: '', counter: 300, suffix: '' }
var doc2 = new TestIncrement({ label: 'label_2' });
doc2.nextSequence(); // `increment_field` === 303
doc1.parseSequence(); // => { prefix: '', counter: 303, suffix: '' }
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
});
TestSchema.plugin(increment, {
modelName: 'Test_increment',
fieldName: 'increment_field',
unique: false,
resetAfter: 2,
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc1 = new TestIncrement({ label: 'label_1' });
doc1.save(); // doc1 saved with `increment_field` === 1
var doc2 = new TestIncrement({ label: 'label_2' });
doc2.save(); // doc2 saved with `increment_field` === 2
var doc3 = new TestIncrement({ label: 'label_3' });
doc3.save(); // doc3 saved with `increment_field` === 1
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
flag: {
type: Boolean,
required: true,
},
});
TestSchema.plugin(increment, {
type: String,
modelName: 'Test_increment',
fieldName: 'increment_field',
prefix: 'P',
suffix: (doc) => {
return doc.flag ? 'TRUE' : 'FALSE';
},
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc1 = new TestIncrement({ label: 'label_1', flag: true });
doc1.save(); // doc saved with `increment_field` === 'P1TRUE'
doc1.parseSequence(); // => { prefix: 'P', counter: '1', suffix: 'TRUE' }
TestIncrement.resetSequence();
var doc2 = new TestIncrement({ label: 'label_1', flag: false });
doc2.save(); // doc saved with `increment_field` === 'P1FALSE'
doc2.parseSequence(); // => { prefix: 'P', counter: '1', suffix: 'FALSE' }
var mongoose = require('mongoose');
var mongooseIncrement = require('mongoose-increment');
var increment = mongooseIncrement(mongoose);
var TestSchema = new mongoose.Schema({
label: {
type: String,
required: true,
},
});
TestSchema.plugin(increment, {
modelName: 'Test_increment',
fieldName: 'increment_field',
hasVersion: true,
startVersion: 0,
});
var TestIncrement = mongoose.model('Test_increment', TestSchema);
var doc = new TestIncrement({ label: 'label_1' });
doc.save(); // doc saved with `increment_field` === 1-0-
Inspired by mongoose-auto-increment
This project is a work in progress and subject to API changes, please feel free to contribute