mongoose-auto-increment
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -25,3 +25,4 @@ var mongoose = require('mongoose'); | ||
startAt: 0, | ||
incrementBy: 1 | ||
incrementBy: 1, | ||
incrementOnUpdate: false | ||
}, | ||
@@ -72,3 +73,3 @@ fields = {}, | ||
if (err) return next(err); | ||
if (typeof(doc[settings.field]) !== 'number') | ||
if (typeof(doc[settings.field]) !== 'number' || settings.incrementOnUpdate) | ||
doc[settings.field] = res.c - 1; | ||
@@ -75,0 +76,0 @@ next(); |
{ | ||
"name": "mongoose-auto-increment", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "This plugin allows you to auto-increment any field on any mongoose schema that you wish.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -27,14 +27,23 @@ # mongoose-auto-increment | ||
Let's say you have a field called `sortOrder` and you'd like to increment that instead of `_id`. | ||
bookSchema.plugin(autoIncrement, { model: 'Book', field: 'sequence' }); | ||
bookSchema.plugin(autoIncrement, { model: 'Book', field: 'sortOrder' }); | ||
### Want that field to start at a different number than zero or increment by more than one? | ||
Can't get much simpler than that! | ||
bookSchema.plugin(autoIncrement, { | ||
model: 'Book', | ||
field: 'sequence', | ||
startAt: 100, | ||
incrementBy: 100 | ||
}); | ||
### Want to start the field value at a different number than zero or increment by more than one? | ||
Your first book document would have an `_id` equal to `100`. Your second book document would have an `_id` equal to `200`, and so on. | ||
Let's say for some reason you want to start counting from 100 and you want to increment by 100 each time as well. | ||
### Want your field to increment every time you update it too? | ||
bookSchema.plugin(autoIncrement, { model: 'Book', startAt: 100, incrementBy: 100 }); | ||
Your first book document would have an `_id` equal to `100`. Your second book document would have an `_id` equal to `200`, and so on. | ||
bookSchema.plugin(autoIncrement, { | ||
model: 'Book', | ||
field: 'sequence', | ||
startAt: 100, | ||
incrementBy: 100, | ||
incrementOnUpdate: true | ||
}); |
4559
75
48