Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
datastore-sequences
Advanced tools
Readme
Generate sequential numbers (auto-increment IDs) in Google Cloud Datastore.
Common wisdom is that you can't or at least shouldn't sequentially number records/rows/entities on Google Cloud Datastore. For background see 1, 2, 3, 4 and 5.
While this is sound advice in most cases, there are situations where you need to have sequential numbered things - e.g. invoice numbers.
datastore-sequences
implements a battle tested approach for slow but save sequence numbering.
There is a strong guarantee that that no ID will be produced more than once.
There is no guarantee that there will never be gaps in the numbering sequence. But this should happen extremely seldom.
Thew usage of different Prefixes allows you to have multiple independent sequences.
To use it, you instantiate with a @google-cloud/datastore instance and call allocateId:
import { Datastore } from '@google-cloud/datastore';
import { SequenceNumbering } from 'datastore-sequences';
const numbering = new SequenceNumbering(new Datastore());
const newId1 = await numbering.allocateId();
const newId2 = await numbering.allocateId();
console.log(newId1, newId2)
'1' '2'
This automatically reties getting a new number on datastore congestion and tries to serialize number generation to avoid datastore congestion.
You can give a prefix to get different namespaces:
const newIds = await Promise.all([
numbering.allocateId('RG'),
numbering.allocateId('LS'),
numbering.allocateId('RG'),
numbering.allocateId('LS')])
console.log(newIds);
['RG1' 'LS1', 'RG2', 'LS2']
You are also encouraged to give a starting value. This is only needed on the first run for a given Prefix:
const newIds = await Promise.all([
numbering.allocateId('RG', 10000),
numbering.allocateId('RG'),
]);
console.log(newIds);
[('RG10001', 'RG10002')];
FAQs
Generate sequential counters for Google Clound Datastore.
The npm package datastore-sequences receives a total of 9 weekly downloads. As such, datastore-sequences popularity was classified as not popular.
We found that datastore-sequences demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.