{"id":15154435,"url":"https://github.com/alorel/mongoose-auto-increment-reworked","last_synced_at":"2025-09-30T02:30:28.608Z","repository":{"id":57301942,"uuid":"118388104","full_name":"Alorel/mongoose-auto-increment-reworked","owner":"Alorel","description":"An auto-incrementing field generator for Mongoose 4 \u0026 5","archived":true,"fork":false,"pushed_at":"2019-06-06T09:35:10.000Z","size":215,"stargazers_count":17,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-16T18:26:09.248Z","etag":null,"topics":["auto","auto-increment","db","generate","id","increment","incremented","mongo","mongo-db","mongodb","mongoose","numeric"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/Alorel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-22T00:53:20.000Z","updated_at":"2023-01-28T17:53:23.000Z","dependencies_parsed_at":"2022-09-11T08:10:50.065Z","dependency_job_id":null,"html_url":"https://github.com/Alorel/mongoose-auto-increment-reworked","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Fmongoose-auto-increment-reworked","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Fmongoose-auto-increment-reworked/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Fmongoose-auto-increment-reworked/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alorel%2Fmongoose-auto-increment-reworked/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alorel","download_url":"https://codeload.github.com/Alorel/mongoose-auto-increment-reworked/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234687258,"owners_count":18871715,"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":["auto","auto-increment","db","generate","id","increment","incremented","mongo","mongo-db","mongodb","mongoose","numeric"],"created_at":"2024-09-26T17:23:39.785Z","updated_at":"2025-09-30T02:30:23.335Z","avatar_url":"https://github.com/Alorel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![NPM link](https://nodei.co/npm/mongoose-auto-increment-reworked.svg?compact=true)](https://www.npmjs.com/package/mongoose-auto-increment-reworked)\n\n[![Build Status](https://travis-ci.org/Alorel/mongoose-auto-increment-reworked.svg?branch=master)](https://travis-ci.org/Alorel/mongoose-auto-increment-reworked)\n[![Coverage Status](https://coveralls.io/repos/github/Alorel/mongoose-auto-increment-reworked/badge.svg?branch=master)](https://coveralls.io/github/Alorel/mongoose-auto-increment-reworked?branch=master)\n[![Greenkeeper badge](https://badges.greenkeeper.io/Alorel/mongoose-auto-increment-reworked.svg)](https://greenkeeper.io/)\n![Supports Node \u003e= 6](https://img.shields.io/badge/Node-%3E=6-brightgreen.svg)\n\nA rewrite of [mongoose-auto-increment](https://www.npmjs.com/package/mongoose-auto-increment) with optimisations and\ntests updated for the latest versions of Mongoose 4 and 5, as well as new features.\n\n-----\n\n# Table of Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Basic usage](#basic-usage)\n- [Getting the next ID](#getting-the-next-id)\n- [Resetting the ID to its starting value](#resetting-the-id-to-its-starting-value)\n- [Configuration](#configuration)\n  - [Default configuration](#default-configuration)\n- [Getting initialisation information](#getting-initialisation-information)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n# Basic usage\n\nThe plugin creates a pre-save middleware on the provided schema which generates an auto-incrementing ID, therefore it\nmust be applied *before* your schema gets turned into a model.\n\n```javascript\nimport {MongooseAutoIncrementID} from 'mongoose-auto-increment-reworked';\nimport * as mongoose from 'mongoose';\n\nconst MySchema = new mongoose.Schema({\n  someField: {\n    type: String\n  }\n});\n\n/*\n * An optional step - set the name of the schema used by the plugin (defaults to 'IdCounter'). Has no effect if\n * the plugin has already been applied to a schema.\n */\nMongooseAutoIncrementID.initialise('MyCustomName');\n\nconst plugin = new MongooseAutoIncrementID(MySchema, 'MyModel');\n\nplugin.applyPlugin()\n  .then(() =\u003e {\n    // Plugin ready to use! You don't need to wait for this promise - any save queries will just get queued.\n    // Every document will have an auto-incremented number value on _id.\n  })\n  .catch(e =\u003e {\n    // Plugin failed to initialise\n  });\n\n/*\n * Alternatively, just use schema.plugin(). The options passed MUST contain the \"modelName\" key and, optionally,\n * any of the parameters from the configuration section below.\n */\nMySchema.plugin(MongooseAutoIncrementID.plugin, {modelName: 'MyModel'});\n\n// Only turn the schema into the model AFTER applyPlugin has been called. You do not need to wait for the promise to resolve.\nconst MyModel = mongoose.model('MyModel', MySchema);\n```\n\n# Getting the next ID \n\n```javascript\nMyModel._nextCount()\n  .then(count =\u003e console.log(`The next ID will be ${count}`));\n```\n\n# Resetting the ID to its starting value\n\n```javascript\nMyModel._resetCount()\n  .then(val =\u003e console.log(`The counter was reset to ${val}`));\n```\n\n# Configuration\n\nThe plugin's configuration accepts the following options:\n\n```typescript\n/** Plugin configuration */\nexport interface PluginOptions {\n  /**\n   * The field that will be automatically incremented. Do not define this in your schema.\n   * @default _id\n   */\n  field: string;\n  /**\n   * How much every insert should increment the counter by\n   * @default 1\n   */\n  incrementBy: number;\n  /**\n   * The name of the function for getting the next ID number.\n   * Set this to false to prevent the function from being added to the schema's static and instance methods.\n   * @default _nextCount\n   */\n  nextCount: string | false;\n  /**\n   * The name of the function for resetting the ID number\n   * Set this to false to prevent the function from being added to the schema's static and instance methods.\n   * @default _resetCount\n   */\n  resetCount: string | false;\n  /**\n   * The first number that will be generated\n   * @default 1\n   */\n  startAt: number;\n  /**\n   * Whether or not to add a unique index on the field. This option is ignored if the field name is _id.\n   * @default true\n   */\n  unique: boolean;\n}\n```\n\nYou can pass them as the third parameter to the plugin's constructor:\n\n```javascript\nconst options = {\n  field: 'user_id', // user_id will have an auto-incrementing value\n  incrementBy: 2, // incremented by 2 every time\n  nextCount: false, // Not interested in getting the next count - don't add it to the model\n  resetCount: 'reset', // The model and each document can now reset the counter via the reset() method\n  startAt: 1000, // Start the counter at 1000\n  unique: false // Don't add a unique index\n};\n\nnew MongooseAutoIncrementID(MySchema, 'MyModel', options);\n```\n\n## Default configuration\n\nYou can get the current default configuration as follows:\n\n```javascript\nMongooseAutoIncrementID.getDefaults();\n```\n\nAnd set it as follows:\n\n```javascript\nMongooseAutoIncrementID.setDefaults(myNewDefaults);\n```\n\n# Getting initialisation information\n\nYou can get the current initialisation state of the plugin via instance methods:\n\n```javascript\nconst mySchema = new mongoose.Schema({/*...*/});\nconst plugin = new MongooseAutoIncrementID(mySchema, 'MyModel');\nconst promise = plugin.applyPlugin();\n\nconsole.log(plugin.promise === promise); // true\nconsole.log(`Plugin ready: ${plugin.isReady}`);\nconsole.log('Initialisation error: ', plugin.error);\n```\n\nOr via static methods:\n\n```javascript\nMongooseAutoIncrementID.getPromiseFor(mySchema, 'MyModel');\nMongooseAutoIncrementID.isReady(mySchema, 'MyModel');\nMongooseAutoIncrementID.getErrorFor(mySchema, 'MyModel');\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falorel%2Fmongoose-auto-increment-reworked","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falorel%2Fmongoose-auto-increment-reworked","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falorel%2Fmongoose-auto-increment-reworked/lists"}