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

mysequence

v1.0.4

Published

A nodejs sequence generator which generate unique sequential number as user-assigned id for your record/document.

Downloads

140

Readme

mysequence

A nodejs sequence generator which generate unique sequential numbers as user-assigned ids for your records/documents.

There are four reasons to use mysequence to generate sequences as ids/keys for your records/documents by application itself.

  • Independent: Our data model and persistence layer can be independent of any RDB & noSQL DB such as MySQL auto increment, MongoDB ObjectId, Oracle sequence, MS SQL Server identity, and so on. We can move our data to any store no matter what DB types or providers.
  • Fastest: It is the fastest id generator in all known solutions including DBMS proprietary features (like Oracle sequence, MySQL auto increment), UUID utilities, and O/R Mapping built-in features (Hibernate hilo, uuid.string and uuid.hex).
  • Unique: UUID utils and O/R Mapping libs CANNOT guarantee unique ids in cluster env.
  • Proactive: You know your new record id before you create it, so you can use it in frontend layer proactively.

You can deploy it as an id generating service in/with application code together.

Martin Fowler and peaa

This sequence generator inspired from the design pattern of key table in chapter Identity Field in peaa (Patterns of Enterprise Application Architecture) by Martin Fowler, the famous OOP master.

Martin contributes his great works to the community, and I pay my respect to martin and contribute this OSS lib to the community.

Installation

mysequence need redis as sequence store, by default, but you could also customize your own store.

npm install mysequence

Quick Start

    var SequenceGenerator = require('mysequence').SequenceGenerator;
    var SequenceStore = require('mysequence').SequenceStore;
    var redisClient = require('./redis');
    var store = new SequenceStore({
        keyPrefix: 'seq:id:',
        redis: redisClient,
        logger: console
    });

    var generator = new SequenceGenerator();
    generator.useLogger(console); //set logger here, or just use console as logger
    generator.useStore(store);    //set store here, here we use default redis store

    /*
     * set default sequence config in case of client don't config it.
     */
    generator.useDefaults({
        initialCursor: 0, //default start number for a new sequence
        segment: 20000,   //default segment width which a sequence apply once
        prebook: 18000    //default prebook point when a sequence start to apply a segment in advance
    });

    /**
     * put each of sequence config here
     */
    generator.putAll(
        [{
            key: 'Employee', //the sequence's name which is store in redis.
            initialCursor: 0,     //the sequence's initial value to start from
            segment: 100,         //a number width to increase before sequence touch the segment end.
            prebook: 60           //It means when to book segment. the value is normally
                                  //between half segment (50) and near segment end (90).
        },{
            key: 'User',
            //initialValue: 100,  //by default, use 0
            //segment: 1000,      //by default, use 20000
            //prebook: 500        //by default, use 18000
        }]
    );

    /*
     * Invoke #init method to initialize/sync all sequence to store(redis) until callback
     * is invoked with true parameter.
     */
    generator.init(function(result){
        if(!result){
            throw new Error('generator is not ready');
        }
        var so = generator.get('Employee'); //get a ready sequence object.
        console.log(so.next().val); //get and output a new sequence number
        console.log(so.next().val); //get and output a another sequence number
    });

Performance

Test in my macbook pro 13' (i5 8G), it take < one second to generate 1,000,000 unique numbers using 1000 segment width.

License

MIT License. A copy is included with the source.

Contact