{"id":14975640,"url":"https://github.com/nodkz/mongoose-plugin-autoinc","last_synced_at":"2025-05-07T09:45:41.647Z","repository":{"id":26869060,"uuid":"111414627","full_name":"nodkz/mongoose-plugin-autoinc","owner":"nodkz","description":"Mongoose plugin that auto-increments any ID field on your schema every time a document is saved","archived":false,"fork":false,"pushed_at":"2022-12-06T20:26:36.000Z","size":2658,"stargazers_count":66,"open_issues_count":31,"forks_count":15,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-22T23:40:46.373Z","etag":null,"topics":["autoincrement","mongoose","mongoose-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodkz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-20T13:34:53.000Z","updated_at":"2023-10-08T00:40:18.000Z","dependencies_parsed_at":"2022-07-27T22:48:33.274Z","dependency_job_id":null,"html_url":"https://github.com/nodkz/mongoose-plugin-autoinc","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodkz%2Fmongoose-plugin-autoinc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodkz%2Fmongoose-plugin-autoinc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodkz%2Fmongoose-plugin-autoinc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodkz%2Fmongoose-plugin-autoinc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodkz","download_url":"https://codeload.github.com/nodkz/mongoose-plugin-autoinc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252854143,"owners_count":21814637,"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":["autoincrement","mongoose","mongoose-plugin"],"created_at":"2024-09-24T13:52:19.408Z","updated_at":"2025-05-07T09:45:41.629Z","avatar_url":"https://github.com/nodkz.png","language":"JavaScript","funding_links":[],"categories":["Packages","🛠 General Utilities"],"sub_categories":["Mongoose"],"readme":"# mongoose-plugin-autoinc\n[![](https://img.shields.io/npm/v/mongoose-plugin-autoinc.svg)](https://www.npmjs.com/package/mongoose-plugin-autoinc)\n[![codecov coverage](https://img.shields.io/codecov/c/github/nodkz/mongoose-plugin-autoinc.svg)](https://codecov.io/github/nodkz/mongoose-plugin-autoinc)\n[![Travis](https://img.shields.io/travis/nodkz/mongoose-plugin-autoinc.svg?maxAge=2592000)](https://travis-ci.org/nodkz/mongoose-plugin-autoinc)\n[![npm](https://img.shields.io/npm/dt/mongoose-plugin-autoinc.svg)](http://www.npmtrends.com/mongoose-plugin-autoinc)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n\n---\n\nThis is a fork of [mongoose-auto-increment](https://github.com/chevex-archived/mongoose-auto-increment) which has not been maintained in a while. Also used fixes and changes from [dashride fork](https://github.com/Dashride/mongoose-auto-increment). This fork addresses the following issues:\n- fix error `'required' is not valid for an index specification` for Mongoose 4\n- does not require established connection for initialization **(deprecate `initialize()` method)**\n- include Flowtype and Typescript declarations\n- tested with Mongoose 5\n- setup automatic package publishing on PR merge with Travis CI and [sematic-release](https://github.com/semantic-release/semantic-release)\n\n## Getting Started\n\n\u003e npm install mongoose-plugin-autoinc\n\nOnce you have the plugin installed it is very simple to use. Just pass `autoIncrement` to the `plugin()` function on your schema.\n\n````js\nimport mongoose from 'mongoose';\nimport { autoIncrement } from 'mongoose-plugin-autoinc';\n\nconst connection = mongoose.createConnection(\"mongodb://localhost/myDatabase\");\n\nconst BookSchema = new mongoose.Schema({\n  author: { type: Schema.Types.ObjectId, ref: 'Author' },\n  title: String,\n  genre: String,\n  publishDate: Date\n});\n\nBookSchema.plugin(autoIncrement, 'Book');\nconst 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\nconst AuthorSchema = new mongoose.Schema({\n  name: String\n});\n\nconst BookSchema = new mongoose.Schema({\n  author: { type: Number, ref: 'Author' },\n  title: String,\n  genre: String,\n  publishDate: Date\n});\n\nBookSchema.plugin(autoIncrement, 'Book');\nAuthorSchema.plugin(autoIncrement, 'Author');\n````\n\n### Want a field other than `_id`?\n\n````js\nBookSchema.plugin(autoIncrement, { 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, {\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\nconst Book = connection.model('Book', BookSchema);\nBook.nextCount().then(count =\u003e {\n  // count === 0 -\u003e true\n  const book = new Book();\n  book.save(err1 =\u003e {\n    // book._id === 0 -\u003e true\n    book.nextCount().then(count =\u003e {\n      // count === 1 -\u003e true\n    });\n  });\n});\n````\n\n`nextCount` 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, {\n  model: 'Book',\n  field: 'bookId',\n  startAt: 100\n});\n\nconst Book = connection.model('Book', BookSchema),\nbook = new Book();\n\nbook.save(err =\u003e {\n  // book._id === 100 -\u003e true\n  book.nextCount().then(count =\u003e {\n    // count === 101 -\u003e true\n    book.resetCount().then(nextCount =\u003e {\n      // nextCount === 100 -\u003e true\n    });\n  });\n});\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodkz%2Fmongoose-plugin-autoinc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodkz%2Fmongoose-plugin-autoinc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodkz%2Fmongoose-plugin-autoinc/lists"}