npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@safer-bwd/mongoose-autonumber

v0.2.2

Published

A Mongoose plugin for auto-increment or auto-number

Downloads

8,300

Readme

@safer-bwd/mongoose-autonumber

Build Status

A Mongoose plugin that adds support for auto-increment or auto-number fields to a Mongoose schema. The plugin supports numbering inside groups and periods.

Install

npm install @safer-bwd/mongoose-autonumber --save

Options

  • counterName string The Mongoose model name for storing counters (optional, default __Counter)

Settings

The plugin adds an option autonumber for String or Number schema types.

  • autonumber (boolean | Object) (optional, default false)
    • autonumber.numerator string The counter name (optional)
    • autonumber.group (string | Function) The path to a Mongoose document grouping property or function to calculate the group (optional)
    • autonumber.period string The periodicity of numbering. Used only with autonumber.date (optional, available values year, month, day, hour, minute)
    • autonumber.date (string | Function) The path to a Mongoose document date property or function to calculate the date. Used only with autonumber.period (optional)
    • autonumber.prefix (string | Function) The path to a Mongoose document prefix property or function to calculate the prefix. Used only with String schema type (optional)
    • autonumber.addLeadingZeros boolean The flag, If true then leading zeros are added. Used only with String schema type and maxlength schema type option (optional)

Usage

The number is set automatically when a new document is saved.

Important: By default, the plugin does not make auto number fields immutable. But you can use schema type option immutable.

Auto increment

import autoNumberPlugin from '@safer-bwd/mongoose-autonumber';
  
const schema = new mongoose.Schema({
  number: {
    type: Number,
    immutable: true,
    autonumber: true,
  }
});
schema.plugin(autoNumberPlugin);
const Order = mongoose.model('Order', schema);

const order1 = new Order();
await order1.save(); // number => 1
const order2 = new Order();
await order2.save(); // number => 2

Increment inside group

import autoNumberPlugin from '@safer-bwd/mongoose-autonumber';
  
const schema = new mongoose.Schema({
  customer: String,
  immutable: true,
  number: {
    type: Number,
    autonumber: {
      group: doc => doc.customer 
    }
  }
});
schema.plugin(autoNumberPlugin);
const Order = mongoose.model('Order', schema);

const order1 = new Order({ customer: 'A' });
await order1.save(); // number => 1
const order2 = new Order({ customer: 'A' });
await order2.save(); // number => 2
const order3 = new Order({ customer: 'B' });
await order3.save(); // number => 1

Increment inside period

import autoNumberPlugin from '@safer-bwd/mongoose-autonumber';
  
const schema = new mongoose.Schema({
  period: Date,
  number: {
    type: Number,
    immutable: true,
    autonumber: {
        period: 'year',
        date: doc => doc.period
    }
  }
});
schema.plugin(autoNumberPlugin);
const Order = mongoose.model('Order', schema);

const order1 = new Order({ period: new Date(2019, 0, 1) });
await order1.save(); // number => 1
const order2 = new Order({ period: new Date(2019, 0, 2) });
await order2.save(); // number => 2
const order3 = new Order({ period: new Date(2020, 0, 1) });
await order3.save(); // number => 1

Prefix and adding leading zeros

In this case maxlength = the total number length (prefix + suffix). If the schema type option maxlength is not set then leading zeros will not be added.

import autoNumberPlugin from '@safer-bwd/mongoose-autonumber';
  
const schema = new mongoose.Schema({
  customer: String,
  number: {
    type: String,
    immutable: true,
    maxlength: 6,
    autonumber: {
      prefix: doc => `${doc.customer}-`,
      addLeadingZeros: true
    }
  }
});
schema.plugin(autoNumberPlugin);
const Order = mongoose.model('Order', schema);

const order1 = new Order({ customer: 'A' });
await order1.save(); // number => 'A-0001'
const order2 = new Order({ customer: 'A' });
await order2.save(); // number => 'A-0002'
const order3 = new Order({ customer: 'B' });
await order3.save(); // number => 'B-0003'