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

@sudtanj/typegoose-auto-increment

v2.2.5

Published

Automaticly Increment properties updated by sudtanj

Downloads

16

Readme

Typegoose Automatic Increment

Build Status codecov.io npm

Automaticly Increment properties

Basic Usage

Simple

(mongoose)

const schema = new mongoose.Schema({
  somefield: Number
});
schema.plugin(AutoIncrementSimple, [{ field: 'somefield' }]);
const model = mongoose.model('SomeModel', schema);

const doc = await model.create({ somefield: 10 });

await doc.save(); // somefield will be 11

(typegoose)

@plugin(AutoIncrementSimple, [{ field: "someIncrementedField" }])
class SomeClass {
  @prop() // does not need to be empty
  public someIncrementedField: number;
}

const SomeModel = getModelForClass(SomeClass);

const doc = await SomeModel.create({ someIncrementedField: 10 });

await doc.save(); // someIncrementedField will be 11

For Identification

(mongoose)

const schema = new mongoose.Schema({
  _id: Number,
  somefield: Number
});
schema.plugin(AutoIncrementID, {});
const model = mongoose.model('SomeModel', schema);

const doc = await model.create({ somefield: 10 }); // _id will be 1

(typegoose)

@plugin(AutoIncrementID, {})
class SomeClass {
  @prop()
  public _id: number;

  @prop() // does not need to be empty
  public someIncrementedField: number;
}

const SomeModel = getModelForClass(SomeClass);

const doc = await SomeModel.create({ someIncrementedField: 10 }); // _id will be 1

Motivation

I started @typegoose/auto-increment because mongoose-auto-increment and mongoose-auto-increment-reworked are archived and didnt get any update for at least 2 years, and there were many issues about them in typegoose so i thought it will be easy to make an up-to-date automatic incrementing plugin.

Requirements

  • Node 14.17.0+
  • TypeScript 4.9+ (older versions could work, but are not tested)
  • mongoose 6.10.0+

Install

npm i -s @typegoose/auto-increment

You also need to install mongoose, because this plugin is made for mongoose.

Testing

npm run test / npm run test:watch

Versioning

Major.Minor.Fix (or how npm expresses it Major.Minor.Patch)
(This Project should comply with Semver)

Join Our Discord Server

To ask questions or just talk with us join our Discord Server

Documentation

AutoIncrementSimple - Options

The options can either be an object or an array of objects (single field / multiple fields).

Note: This function will only increment if the document is not new, this is to apply the default value.

field

string

This option is always required to get which field to increment.

incrementBy

number default 1

This option is optional, default is to increment by 1.

AutoIncrementID - Options

The options can only be one single object.

This plugin variant uses a model and a collection to store tracking(/counter) infomation to keep track of the ID in case the latest one gets deleted.

Note: the model used to keep track of the counter, will use the connection that the assigned schema uses.
Note: when the model is completly new, the first document will be "1", see here as on why.

if the hook should be skipped, use AutoIncrementIDSkipSymbol:

const doc = new Model();

doc[AutoIncrementIDSkipSymbol] = true;
await doc.save();

Note: AutoIncrementIDSkipSymbol can also be set inside hooks, but hooks might be called before others.

incrementBy

number default 1

This option is optional, default is to increment by 1.

field

string

This option is optional, defaults to _id.

trackerCollection

string

Set the Collection the tracker-model should use to store tracking infomation (/ to store the tracking documents).

This option is optional, defaults to identitycounters.

trackerModelName

string

Set the tracker-model's name (sets the model-name of the tracker model like mongoose.model(NameHere, ...)).

This option is optional, defaults to identitycounter.

startAt

number default 0

Set the starting number of the counter. (the first document will be this number)

overwriteModelName

string default's to the documents's model-name
(modelName: string, model: Model) => string

Overwrite the used value for the modelName property on the tracker-document, this can be used when wanting to use the same tracker document across different models.
If the value is falsy, then it will default to the modelName.
Can also be used as a function to generate a name to use based on modelName or the model itself, needs to return a non-empty string, when wanting to default back to default behavior just return the parameter modelName.

Notes

  • Please dont add comments with +1 or something like that, use the Reactions
  • npm run doc generates all documentation for all files that can be used as modules (is used for github-pages)
  • npm run doc:all generates documentation even for internal modules