mongoose-increment
Mongoose plugin to generate incremented field
Installation
Install via npm:
$ npm install mongoose-increment
Documentation
Options
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-
)
Methods
instance.nextSequence()
Return a fulfilled promise when increment field has been setted
instance.parseSequence()
Return an object which contain instance prefix, suffix and counter.
hasVersion
is true
instance.nextVersion() is Set the next version from the current document version.
Statics
Model.resetSequence()
Return a fulfilled promise when sequence has been reset
Examples
default option
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true ; TestSchema; var TestIncrement = mongoose; var doc = label: 'label_1' ; doc; // doc saved with `increment_field` === 1
type option
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true ; TestSchema; var TestIncrement = mongoose; var doc = label: 'label_1' ; doc; // doc saved with `increment_field` === '1'
start
and increment
set
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true ; TestSchema; var TestIncrement = mongoose; var doc1 = label: 'label_1' ;doc1; // doc saved with `increment_field` === 300doc1; // => { prefix: '', counter: 300, suffix: '' } var doc2 = label: 'label_2' ;doc2; // `increment_field` === 303doc1; // => { prefix: '', counter: 303, suffix: '' }
resetAfter
and unique
options
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true ; TestSchema; var TestIncrement = mongoose; var doc1 = label: 'label_1' ; doc1; // doc1 saved with `increment_field` === 1 var doc2 = label: 'label_2' ; doc2; // doc2 saved with `increment_field` === 2 var doc3 = label: 'label_3' ; doc3; // doc3 saved with `increment_field` === 1
prefix
and suffix
set
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true flag: type: Boolean required: true ; TestSchema; var TestIncrement = mongoose; var doc1 = label: 'label_1' flag: true ;doc1; // doc saved with `increment_field` === 'P1TRUE'doc1; // => { prefix: 'P', counter: '1', suffix: 'TRUE' } TestIncrement; var doc2 = label: 'label_1' flag: false ;doc2; // doc saved with `increment_field` === 'P1FALSE'doc2; // => { prefix: 'P', counter: '1', suffix: 'FALSE' }
hasVersion
true
var mongoose = ;var mongooseIncrement = ;var increment = ; var TestSchema = label: type: String required: true ; TestSchema; var TestIncrement = mongoose; var doc = label: 'label_1' ; doc; // doc saved with `increment_field` === 1-0-
Note
Inspired by mongoose-auto-increment
Contributing
This project is a work in progress and subject to API changes, please feel free to contribute