All Projects β†’ Alorel β†’ mongoose-auto-increment-reworked

Alorel / mongoose-auto-increment-reworked

Licence: MIT License
An auto-incrementing field generator for Mongoose 4 & 5

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to mongoose-auto-increment-reworked

ark.db
Small and fast JSON database for Node and browser. πŸ˜‹
Stars: ✭ 65 (+282.35%)
Mutual labels:  mongo, mongoose, db, mongo-db
derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (+217.65%)
Mutual labels:  mongo, mongoose, db
uuid-mongodb
πŸ“‡ Generates and parses MongoDB BSON UUIDs
Stars: ✭ 94 (+452.94%)
Mutual labels:  mongo, mongoose
mongoose-morgan
An npm package for saving morgan log inside MongoDB
Stars: ✭ 14 (-17.65%)
Mutual labels:  mongo, mongoose
mongoose-slug-plugin
Slugs for Mongoose with history and i18n support (uses speakingurl by default, but you can use any slug library such as limax, slugify, mollusc, or slugme)
Stars: ✭ 21 (+23.53%)
Mutual labels:  mongo, mongoose
product-catalog-spring-mvc-demo
A Product Catalog Demonstration Project Using Spring MVC and Mongo DB
Stars: ✭ 16 (-5.88%)
Mutual labels:  mongo, mongo-db
mern-stack-crud
MERN stack (MongoDB, Express, React and Node.js) create read update and delete (CRUD) web application example
Stars: ✭ 142 (+735.29%)
Mutual labels:  mongo, mongoose
react-full-stack-starter
🎈Full-stack React boilerplate using `create-react-app`, Babel, Node.js, and express
Stars: ✭ 22 (+29.41%)
Mutual labels:  mongo, mongoose
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (+1135.29%)
Mutual labels:  mongo, mongoose
NodeExpressCRUD
Node, Express, Mongoose and MongoDB CRUD Web Application
Stars: ✭ 45 (+164.71%)
Mutual labels:  mongo, mongoose
url-incrementer
A web extension for Chrome, Edge, and Firefox. Increment a URL or go to the next page. Supports auto incrementing and advanced toolkit functions like scraping URLs.
Stars: ✭ 27 (+58.82%)
Mutual labels:  auto-increment, increment
server-next
😎 The next generation of RESTful API service and more for Mix Space, powered by @nestjs.
Stars: ✭ 43 (+152.94%)
Mutual labels:  mongo, mongoose
express-mongo-jwt-boilerplate
Express Mongo JsonWebToken boilerplate
Stars: ✭ 100 (+488.24%)
Mutual labels:  mongo, mongoose
auto-commit-msg
A VS Code extension to generate a smart commit message based on file changes
Stars: ✭ 61 (+258.82%)
Mutual labels:  generate, auto
NodeScalableArchitecture
A Scalable Node Architecture/Server. This repository contains a complete implementation of writing scalable nodejs server/architecture on my medium blog.
Stars: ✭ 62 (+264.71%)
Mutual labels:  mongo, mongoose
koa-session-mongoose
Mongoose store for Koa sessions
Stars: ✭ 29 (+70.59%)
Mutual labels:  mongo, mongoose
node-express-mongo-passport-jwt-typescript
A Node.js back end web application with REST API, user JWT authentication and MongoDB data storage using TypeScript
Stars: ✭ 51 (+200%)
Mutual labels:  mongo, mongoose
mongoose-api-generator
Autogenerate a REST API from your Mongoose models
Stars: ✭ 19 (+11.76%)
Mutual labels:  mongoose, db
nutri.gram
Nutrition is the main source of life and although it has been our secondary instinct to check for nutritional value in the food we eat, the effect any diet has on our body and health is consequential. From the fact which connotes the value of nutrition in our diet, springs the idea of nutri.gram. nutri.gram is a mobile application that scans the…
Stars: ✭ 21 (+23.53%)
Mutual labels:  mongo, mongoose
NodeRestApi
Node.js, Express.js and MongoDB REST API App
Stars: ✭ 38 (+123.53%)
Mutual labels:  mongo, mongoose

NPM link

Build Status Coverage Status Greenkeeper badge Supports Node >= 6

A rewrite of mongoose-auto-increment with optimisations and tests updated for the latest versions of Mongoose 4 and 5, as well as new features.


Table of Contents

Basic usage

The plugin creates a pre-save middleware on the provided schema which generates an auto-incrementing ID, therefore it must be applied before your schema gets turned into a model.

import {MongooseAutoIncrementID} from 'mongoose-auto-increment-reworked';
import * as mongoose from 'mongoose';

const MySchema = new mongoose.Schema({
  someField: {
    type: String
  }
});

/*
 * An optional step - set the name of the schema used by the plugin (defaults to 'IdCounter'). Has no effect if
 * the plugin has already been applied to a schema.
 */
MongooseAutoIncrementID.initialise('MyCustomName');

const plugin = new MongooseAutoIncrementID(MySchema, 'MyModel');

plugin.applyPlugin()
  .then(() => {
    // Plugin ready to use! You don't need to wait for this promise - any save queries will just get queued.
    // Every document will have an auto-incremented number value on _id.
  })
  .catch(e => {
    // Plugin failed to initialise
  });

/*
 * Alternatively, just use schema.plugin(). The options passed MUST contain the "modelName" key and, optionally,
 * any of the parameters from the configuration section below.
 */
MySchema.plugin(MongooseAutoIncrementID.plugin, {modelName: 'MyModel'});

// Only turn the schema into the model AFTER applyPlugin has been called. You do not need to wait for the promise to resolve.
const MyModel = mongoose.model('MyModel', MySchema);

Getting the next ID

MyModel._nextCount()
  .then(count => console.log(`The next ID will be ${count}`));

Resetting the ID to its starting value

MyModel._resetCount()
  .then(val => console.log(`The counter was reset to ${val}`));

Configuration

The plugin's configuration accepts the following options:

/** Plugin configuration */
export interface PluginOptions {
  /**
   * The field that will be automatically incremented. Do not define this in your schema.
   * @default _id
   */
  field: string;
  /**
   * How much every insert should increment the counter by
   * @default 1
   */
  incrementBy: number;
  /**
   * The name of the function for getting the next ID number.
   * Set this to false to prevent the function from being added to the schema's static and instance methods.
   * @default _nextCount
   */
  nextCount: string | false;
  /**
   * The name of the function for resetting the ID number
   * Set this to false to prevent the function from being added to the schema's static and instance methods.
   * @default _resetCount
   */
  resetCount: string | false;
  /**
   * The first number that will be generated
   * @default 1
   */
  startAt: number;
  /**
   * Whether or not to add a unique index on the field. This option is ignored if the field name is _id.
   * @default true
   */
  unique: boolean;
}

You can pass them as the third parameter to the plugin's constructor:

const options = {
  field: 'user_id', // user_id will have an auto-incrementing value
  incrementBy: 2, // incremented by 2 every time
  nextCount: false, // Not interested in getting the next count - don't add it to the model
  resetCount: 'reset', // The model and each document can now reset the counter via the reset() method
  startAt: 1000, // Start the counter at 1000
  unique: false // Don't add a unique index
};

new MongooseAutoIncrementID(MySchema, 'MyModel', options);

Default configuration

You can get the current default configuration as follows:

MongooseAutoIncrementID.getDefaults();

And set it as follows:

MongooseAutoIncrementID.setDefaults(myNewDefaults);

Getting initialisation information

You can get the current initialisation state of the plugin via instance methods:

const mySchema = new mongoose.Schema({/*...*/});
const plugin = new MongooseAutoIncrementID(mySchema, 'MyModel');
const promise = plugin.applyPlugin();

console.log(plugin.promise === promise); // true
console.log(`Plugin ready: ${plugin.isReady}`);
console.log('Initialisation error: ', plugin.error);

Or via static methods:

MongooseAutoIncrementID.getPromiseFor(mySchema, 'MyModel');
MongooseAutoIncrementID.isReady(mySchema, 'MyModel');
MongooseAutoIncrementID.getErrorFor(mySchema, 'MyModel');
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].