Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alonsojr1980/mongoose-generator
Easy to use sequencial ID generator plugin for Mongoose
https://github.com/alonsojr1980/mongoose-generator
Last synced: 26 days ago
JSON representation
Easy to use sequencial ID generator plugin for Mongoose
- Host: GitHub
- URL: https://github.com/alonsojr1980/mongoose-generator
- Owner: alonsojr1980
- License: mit
- Created: 2019-05-09T17:55:49.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-05-15T19:20:07.000Z (over 5 years ago)
- Last Synced: 2024-11-16T09:16:12.981Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
![alonso jr](https://img.shields.io/badge/Alonso%20Jr.-www.alonsojr.com.br-crimson.svg)
# @alonsojr1980/mongoose-generator
Plugin for generating sequential field values. It adds a `pre('save')` hook to the attached schema and some useful [static methods](#static-methods).## Basic usage example
```javascript
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}**## Default plugin options
```javascript
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
}
```
## Custom options example```javascript
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}**## Static methods added to the model
```javascript
// 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.## Static methods usage example
```javascript
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
});
```