datastore-sequences
v1.0.8
Published
Generate sequential counters for Google Clound Datastore.
Downloads
17
Maintainers
Readme
datastore-sequences
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')];