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

mongoose-counters

v1.0.2

Published

Autoincrement counter handling for mongoose

Downloads

20

Readme

mongoose-counters

This plugin create fields which autoincrement their value every time a new document is inserted in a collection.

npm GitHub license Codacy Badge npm TypeScript

Installation


yarn add mongoose-counters
npm i mongoose-counters

Setup


This plugin accept a series of options.

  • id: Id of the counter. Is mandatory only for scoped counters but its use is strongly encouraged.
  • incField: The name of the field to increment. Mandatory, default is _id
  • referenceFields: The field to reference for a scoped counter. Optional
  • collectionName: The collection name to mantain the status of the counters. Mandatory, default is counters

Use as you would any Mongoose plugin:

import * as mongoose from 'mongoose';
import mongooseCounter from 'mongoose-counters';

const counter = mongooseCounter(mongoose);
const schema = new mongoose.Schema({ ... });
schema.plugin(counter, { ...options });
const model = model('MyModel', schema);

The increment can be:

  • global: every document has a unique value for the counter field
  • scoped: the counter depends on the value of other field(s)

Global counters

Let's say you want to have an id field in your collection which has an unique auto-incremented value.

The model schema is something like this:

ModelSchema = mongoose.Schema({
  myAttr: String
  ...
});

mongoose.model('ModelName', ModelSchema);

You don't need to define the id field in your schema because the plugin automatically set it for you. The only thing you have to do is to call:

ModelSchema.plugin(counter, { incField: 'id' });

Every time a new model entry is created, the id field will have an incremental number.

If you want to increment the _id field which is special to mongoose, you have to explicitly specify it as a Number and tell mongoose to not interfer:

ModelSchema = mongoose.Schema({
  _id: Number,
  myAttr: String
}, { _id: false });
ModelSchema.plugin(AutoIncrement);

In this case you don't have to specify incField because the default value is _id

Scoped counters

Let say our users are organized for country and city. And we want to save the inhabitant_number according to the two informations. The schema is like this:

UserSchema = mongoose.Schema({
    name: String,
    country: String,
    city: String,
    inhabitant_number: Number
});

Every time a new Parisian is added the counting of Parisians have to be incremented. The inhabitants of New York must not interfer and have their separated counting. We should define a scoped counter which increment the counter depending on the value of other fields.

UserSchema.plugin(AutoIncrement, {id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country','city'] });

Notice that we have to use an id for our sequence, otherwise the plugin will raise an error.

API

resetCounter()

It's possible to programmatically reset a counter through the Model static method counterReset(id, reference, callback). The method take those parameters:

  • id: the counter to reset. It's mandatory
  • reference: Let you reset only a specific reference of the counter, if the counter has referenced fields. Optional. By default it reset all the counters for the id
  • callback: A callback which receive an error in case of any. Mandatory
Model.counterReset('the_counter_id', (err) => { ... });

Model.counterReset('the_counter_id', { ref_field_1: 'ref_value_1', ref_field_2: 'ref_value_2'}, (err) => { ... });

Credits

This plugin is inspired by ramiel/mongoose-sequence.