{"_id":"@ed3ath/mongoose-auto-increment","name":"@ed3ath/mongoose-auto-increment","dist-tags":{"latest":"5.0.1"},"versions":{"5.0.1":{"name":"@ed3ath/mongoose-auto-increment","version":"5.0.1","description":"This plugin allows you to auto-increment any field on any mongoose schema that you wish.","repository":{"type":"git","url":"git://github.com/codetunnel/mongoose-auto-increment.git"},"dependencies":{"extend":"^3.0.0"},"peerDependencies":{"mongoose":"^6.8.4"},"devDependencies":{"async":"*","chai":"*","mocha":"*","mongoose":"^6.8.4"},"keywords":["mongoose","plugin","auto-increment","auto","increment","automatic","autoincrement","auto_increment","autoinc","auto-inc","auto_inc","pureautoinc","mongoose-pureautoinc"],"author":{"name":"Alex Ford","email":"alex.ford@codetunnel.com","url":"Chevex"},"contributors":[{"name":"Nassor Paulino da Silva","email":"nassor@gmail.com","url":"rossan"},{"name":"Misha Koryak","email":"mkoryak@gmail.com","url":"mkoryak"},{"name":"Christopher Hiller","url":"boneskull"},{"name":"tomaskavka","url":"https://github.com/tomaskavka"}],"scripts":{"test":"node_modules/mocha/bin/mocha"},"bugs":{"url":"https://github.com/codetunnel/mongoose-auto-increment/issues"},"gitHead":"c26fac6030eb44ff840be3a12f77e3ac4f8a503f","homepage":"https://github.com/codetunnel/mongoose-auto-increment#readme","_id":"@ed3ath/mongoose-auto-increment@5.0.1","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-HnRmbhXmJQOLb9g21DLSXEVwCpWsJgNH8F8X47aUyMqMYxAetzvie7JPk3Rt1FX6N6nBKWa0oYjRHQazXKCCaA==","shasum":"581db23a2cb2800be87773a4228a635a63e7e4e3","tarball":"https://registry.npmjs.org/@ed3ath/mongoose-auto-increment/-/mongoose-auto-increment-5.0.1.tgz","fileCount":8,"unpackedSize":23105,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD6WtwOaB+LHYTMH+99zcXiDgGuq/VDaXt0Lvk5f78u1wIgYve1BqxgB0RlNiplpmN4bcfgRsfUFtohITt3hY2GPgs="}]},"_npmUser":{"name":"ed3ath","email":"edeath1208@gmail.com"},"directories":{},"maintainers":[{"name":"ed3ath","email":"edeath1208@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mongoose-auto-increment_5.0.1_1684484672713_0.6433662401230764"},"_hasShrinkwrap":false}},"time":{"created":"2023-05-19T08:24:32.637Z","5.0.1":"2023-05-19T08:24:32.877Z","modified":"2023-05-19T08:24:33.094Z"},"maintainers":[{"name":"ed3ath","email":"edeath1208@gmail.com"}],"description":"This plugin allows you to auto-increment any field on any mongoose schema that you wish.","homepage":"https://github.com/codetunnel/mongoose-auto-increment#readme","keywords":["mongoose","plugin","auto-increment","auto","increment","automatic","autoincrement","auto_increment","autoinc","auto-inc","auto_inc","pureautoinc","mongoose-pureautoinc"],"repository":{"type":"git","url":"git://github.com/codetunnel/mongoose-auto-increment.git"},"contributors":[{"name":"Nassor Paulino da Silva","email":"nassor@gmail.com","url":"rossan"},{"name":"Misha Koryak","email":"mkoryak@gmail.com","url":"mkoryak"},{"name":"Christopher Hiller","url":"boneskull"},{"name":"tomaskavka","url":"https://github.com/tomaskavka"}],"author":{"name":"Alex Ford","email":"alex.ford@codetunnel.com","url":"Chevex"},"bugs":{"url":"https://github.com/codetunnel/mongoose-auto-increment/issues"},"readme":"# mongoose-auto-increment\r\n\r\n[![Build Status](https://travis-ci.org/codetunnel/mongoose-auto-increment.png?branch=master)](https://travis-ci.org/codetunnel/mongoose-auto-increment)\r\n[![Dependencies Status](https://gemnasium.com/codetunnel/mongoose-auto-increment.png)](https://gemnasium.com/codetunnel/mongoose-auto-increment)\r\n[![NPM version](https://badge.fury.io/js/mongoose-auto-increment.png)](http://badge.fury.io/js/mongoose-auto-increment)\r\n\r\n> Mongoose plugin that auto-increments any ID field on your schema every time a document is saved.\r\n\r\n---\r\n\r\n> This is the module used by [mongoose-simpledb](https://github.com/codetunnel/mongoose-simpledb) to increment Number IDs. You are perfectly able to use this module by itself if you would like. However, if you're looking to make your life easier when using [mongoose](http://mongoosejs.com/) then I highly recommend you check out simpledb. It's a small wrapper around mongoose but it makes it extremely easy to deal with your models and draws a clear path for how to use mongoose in your application.\r\n\r\n## Getting Started\r\n\r\n> npm install mongoose-auto-increment\r\n\r\nOnce you have the plugin installed it is very simple to use. Just get reference to it, initialize it by passing in your\r\nmongoose connection and pass `autoIncrement.plugin` to the `plugin()` function on your schema.\r\n\r\n> Note: You only need to initialize MAI once.\r\n\r\n````js\r\nvar mongoose = require('mongoose'),\r\n    Schema = mongoose.Schema,\r\n    autoIncrement = require('mongoose-auto-increment');\r\n\r\nvar connection = mongoose.createConnection(\"mongodb://localhost/myDatabase\");\r\n\r\nautoIncrement.initialize(connection);\r\n\r\nvar bookSchema = new Schema({\r\n    author: { type: Schema.Types.ObjectId, ref: 'Author' },\r\n    title: String,\r\n    genre: String,\r\n    publishDate: Date\r\n});\r\n\r\nbookSchema.plugin(autoIncrement.plugin, 'Book');\r\nvar Book = connection.model('Book', bookSchema);\r\n````\r\n\r\nThat's it. Now you can create book entities at will and they will have an `_id` field added of type `Number` and will automatically increment with each new document. Even declaring references is easy, just remember to change the reference property's type to `Number` instead of `ObjectId` if the referenced model is also using the plugin.\r\n\r\n````js\r\nvar authorSchema = new mongoose.Schema({\r\n    name: String\r\n});\r\n    \r\nvar bookSchema = new Schema({\r\n    author: { type: Number, ref: 'Author' },\r\n    title: String,\r\n    genre: String,\r\n    publishDate: Date\r\n});\r\n    \r\nbookSchema.plugin(autoIncrement.plugin, 'Book');\r\nauthorSchema.plugin(autoIncrement.plugin, 'Author');\r\n````\r\n\r\n### Want a field other than `_id`?\r\n\r\n````js\r\nbookSchema.plugin(autoIncrement.plugin, { model: 'Book', field: 'bookId' });\r\n````\r\n\r\n### Want that field to start at a different number than zero or increment by more than one?\r\n\r\n````js\r\nbookSchema.plugin(autoIncrement.plugin, {\r\n    model: 'Book',\r\n    field: 'bookId',\r\n    startAt: 100,\r\n    incrementBy: 100\r\n});\r\n````\r\n\r\nYour first book document would have a `bookId` equal to `100`. Your second book document would have a `bookId` equal to `200`, and so on.\r\n\r\n### Want to know the next number coming up?\r\n\r\n````js\r\nvar Book = connection.model('Book', bookSchema);\r\nBook.nextCount(function(err, count) {\r\n\r\n    // count === 0 -> true\r\n\r\n    var book = new Book();\r\n    book.save(function(err) {\r\n\r\n        // book._id === 0 -> true\r\n\r\n        book.nextCount(function(err, count) {\r\n\r\n            // count === 1 -> true\r\n\r\n        });\r\n    });\r\n});\r\n````\r\n\r\nnextCount is both a static method on the model (`Book.nextCount(...)`) and an instance method on the document (`book.nextCount(...)`).\r\n\r\n### Want to reset counter back to the start value?\r\n\r\n````js\r\nbookSchema.plugin(autoIncrement.plugin, {\r\n    model: 'Book',\r\n    field: 'bookId',\r\n    startAt: 100\r\n});\r\n\r\nvar Book = connection.model('Book', bookSchema),\r\n    book = new Book();\r\n\r\nbook.save(function (err) {\r\n\r\n    // book._id === 100 -> true\r\n\r\n    book.nextCount(function(err, count) {\r\n\r\n        // count === 101 -> true\r\n\r\n        book.resetCount(function(err, nextCount) {\r\n\r\n            // nextCount === 100 -> true\r\n\r\n        });\r\n\r\n    });\r\n\r\n});\r\n````\r\n","readmeFilename":"README.md"}