Plugin for generating sequential field values. It adds a pre('save')
hook to the attached schema and some useful static methods.
const Generator = require('@alonsojr1980/mongoose-generator');
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: String,
age: Number
});
const nameOfTheGenerator = 'users';
UserSchema.plugin(Generator, nameOfTheGenerator);
const model = mongoose.model('User', UserSchema);
const user1 = model({name: "ALONSO"});
user1.save(() => {
console.log(user1._id); //outputs 1
});
The following document will be added to a collection named generators: {"name": "users", "sequence": 1}
const pluginOptions = {
collection: "generators", // name of the collection that'll hold generator docs
name: undefined, // name of the doc in the generators' collection
startAt: 100, // first sequence value, when the generator is created and saved or reseted with gen_id_reset()
increment: 1, // increment of the generator
field: "_id" // field of the model that'll receive the generator's sequence
}
const BusSchema = new mongoose.Schema({
sequentialNumber: Number,
licensePlate: String
});
const pluginOptions = {
collection: "generators",
name: "buses",
startAt: 100,
increment: 1,
field: "sequentialNumber"
}
BusSchema.plugin(Generator, pluginOptions);
const model = mongoose.model('Bus', BusSchema);
const bus1 = model({licensePlate: "BUS-7777"});
bus1.save(() => {
console.log(bus1.sequentialNumber); //outputs 101
});
The following document will be added to a collection named generators:
{"name": "buses", "sequence": 101}
// returns the current sequence value
currentSequence((err, sequence) => {});
// resets the sequence to the startAt value passed in the options object
// or to 0 (default value). Beware of existing documents with that sequence!
gen_id_reset((err, sequence) => {})
Remember that static methods are called in the model, not in the document instance.
const BusSchema = new mongoose.Schema({
sequentialNumber: Number,
licensePlate: String
});
const pluginOptions = {
name: "buses",
startAt: 0
}
BusSchema.plugin(Generator, pluginOptions);
const model = mongoose.model('Bus', BusSchema);
model.gen_id_reset((err, seq) => {
console.log(seq); //outputs 0
});