{"id":22823235,"url":"https://github.com/eliurkis/mongoose-autoincrement-model","last_synced_at":"2025-03-30T23:42:49.754Z","repository":{"id":57301982,"uuid":"123667139","full_name":"eliurkis/mongoose-autoincrement-model","owner":"eliurkis","description":null,"archived":false,"fork":false,"pushed_at":"2018-03-03T06:52:27.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T06:49:20.102Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eliurkis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-03T06:44:59.000Z","updated_at":"2018-03-03T06:48:23.000Z","dependencies_parsed_at":"2022-09-06T23:31:35.207Z","dependency_job_id":null,"html_url":"https://github.com/eliurkis/mongoose-autoincrement-model","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliurkis%2Fmongoose-autoincrement-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliurkis%2Fmongoose-autoincrement-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliurkis%2Fmongoose-autoincrement-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eliurkis%2Fmongoose-autoincrement-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eliurkis","download_url":"https://codeload.github.com/eliurkis/mongoose-autoincrement-model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246395572,"owners_count":20770240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-12T16:15:58.108Z","updated_at":"2025-03-30T23:42:49.738Z","avatar_url":"https://github.com/eliurkis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-autoincrement-model\n\n\u003e Mongoose plugin that auto-increments any ID field on your schema every time a document is saved.\n\n## Getting Started\n\n\u003e npm install mongoose-auto-increment\n\nOnce you have the plugin installed it is very simple to use. Just get reference to it, initialize it by passing in your\nmongoose connection and pass `autoIncrement.plugin` to the `plugin()` function on your schema.\n\n\u003e Note: You only need to initialize MAI once.\n\n````js\nvar mongoose = require('mongoose'),\n    Schema = mongoose.Schema,\n    autoIncrement = require('mongoose-autoincrement-model');\n\nvar connection = mongoose.createConnection(\"mongodb://localhost/myDatabase\");\n\nautoIncrement.initialize(connection);\n\nvar bookSchema = new Schema({\n    author: { type: Schema.Types.ObjectId, ref: 'Author' },\n    title: String,\n    genre: String,\n    publishDate: Date\n});\n\nbookSchema.plugin(autoIncrement.plugin, 'Book');\nvar Book = connection.model('Book', bookSchema);\n````\n\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.\n\n````js\nvar authorSchema = new mongoose.Schema({\n    name: String\n});\n    \nvar bookSchema = new Schema({\n    author: { type: Number, ref: 'Author' },\n    title: String,\n    genre: String,\n    publishDate: Date\n});\n    \nbookSchema.plugin(autoIncrement.plugin, 'Book');\nauthorSchema.plugin(autoIncrement.plugin, 'Author');\n````\n\n### Want a field other than `_id`?\n\n````js\nbookSchema.plugin(autoIncrement.plugin, { model: 'Book', field: 'bookId' });\n````\n\n### Want that field to start at a different number than zero or increment by more than one?\n\n````js\nbookSchema.plugin(autoIncrement.plugin, {\n    model: 'Book',\n    field: 'bookId',\n    startAt: 100,\n    incrementBy: 100\n});\n````\n\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.\n\n### Want to know the next number coming up?\n\n````js\nvar Book = connection.model('Book', bookSchema);\nBook.nextCount(function(err, count) {\n\n    // count === 0 -\u003e true\n\n    var book = new Book();\n    book.save(function(err) {\n\n        // book._id === 0 -\u003e true\n\n        book.nextCount(function(err, count) {\n\n            // count === 1 -\u003e true\n\n        });\n    });\n});\n````\n\nnextCount is both a static method on the model (`Book.nextCount(...)`) and an instance method on the document (`book.nextCount(...)`).\n\n### Want to reset counter back to the start value?\n\n````js\nbookSchema.plugin(autoIncrement.plugin, {\n    model: 'Book',\n    field: 'bookId',\n    startAt: 100\n});\n\nvar Book = connection.model('Book', bookSchema),\n    book = new Book();\n\nbook.save(function (err) {\n\n    // book._id === 100 -\u003e true\n\n    book.nextCount(function(err, count) {\n\n        // count === 101 -\u003e true\n\n        book.resetCount(function(err, nextCount) {\n\n            // nextCount === 100 -\u003e true\n\n        });\n\n    });\n\n});\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliurkis%2Fmongoose-autoincrement-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feliurkis%2Fmongoose-autoincrement-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feliurkis%2Fmongoose-autoincrement-model/lists"}