https://github.com/cozitive/auto-increment-plugin
Mongoose Plugin for Auto Increment Field
https://github.com/cozitive/auto-increment-plugin
mongoose-plugin mongoose-schema
Last synced: 5 months ago
JSON representation
Mongoose Plugin for Auto Increment Field
- Host: GitHub
- URL: https://github.com/cozitive/auto-increment-plugin
- Owner: cozitive
- License: mit
- Created: 2021-02-26T15:18:05.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-28T09:09:37.000Z (over 5 years ago)
- Last Synced: 2025-08-09T01:51:37.992Z (11 months ago)
- Topics: mongoose-plugin, mongoose-schema
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/auto-increment-plugin
- Size: 1010 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# auto-increment-plugin
Mongoose Schema Plugin for Auto Increment Field
## Quick Guide
Create a Mongoose Schema that will add auto increment field:
```js
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
}, {
timestamps: true,
})
```
Then, apply the plugin to the Schema, and create a Mongoose Model:
```js
UserSchema.plugin(AutoIncrementPlugin, {
model_name: 'User',
})
const User = mongoose.model('User', UserSchema)
```
You must specify the name of a model when applying the plugin.
After that, auto-increasing ```id``` field will be created:
```js
await new User({ username: '1' }).save()
const user = await User.findOne({ username: '1' })
console.log(user.id) // 1
```
## Advanced
There are several options for the plugin:
```js
UserSchema.plugin(AutoIncrementPlugin, {
model_name: 'User',
field: 'my_id',
id_model: 'MyId',
})
```
- ```field```: The name of an auto-increment field (Default: ```id```)
- ```id_model```: The name of a model that saves current count (Default: ```model_name + 'Id'```)