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 = SequenceGenerator; var SequenceStore = SequenceStore; var redisClient = ; var store = keyPrefix: 'seq:id:' redis: redisClient logger: console ; var generator = ; generator; //set logger here, or just use console as logger generator; //set store here, here we use default redis store /* * set default sequence config in case of client don't config it. */ generator; /** * put each of sequence config here */ generator; /* * Invoke #init method to initialize/sync all sequence to store(redis) until callback * is invoked with true parameter. */ generator;
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
- GitHub (henryleu)
- QQ (1347653)
- Email (henryleu@126.com)