Socket
Socket
Sign inDemoInstall

datastore-sequences

Package Overview
Dependencies
112
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    datastore-sequences

Generate sequential counters for Google Clound Datastore.


Version published
Weekly downloads
9
decreased by-73.53%
Maintainers
1
Install size
22.4 MB
Created
Weekly downloads
 

Readme

Source

version NPM Types Last Commit license downloads

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')];

See also

Keywords

FAQs

Last updated on 16 Jan 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc