datastore-api
TypeScript icon, indicating that this package has built-in type declarations

6.0.2 • Public • Published

version license downloads

datastore-api

Simplified, more consistent API for Google Cloud Datastore.

Dstore implements a slightly more accessible version of the Google Cloud Datastore: Node.js Client

@google-cloud/datastore is a strange beast: The documentation is auto generated missing some core methods and completely shy of documenting any advanced concepts.

Also the typings are strange and overly broad.

Dstore tries to abstract away most surprises the datastore provides to you but als tries to stay as API compatible as possible to @google-cloud/datastore.

Main differences:

  • Everything asynchronous is Promise-based - no callbacks.
  • get always returns a single DstoreEntry.
  • getMulti always returns an Array.
  • set is called with (key, value) and always returns the complete Key of the entity being written.
  • allocateOneId returns a single numeric string encoded unique datastore id without the need of fancy unpacking.
  • runInTransaction allows you to provide a function to be executed inside an transaction without the need of passing around the transaction object. This is modelled after Python 2.7 ndb's @ndb.transactional feature. This is implemented via node's AsyncLocalStorage.
  • keySerialize is synchronous. 🦄
  • Starting your code with the environment variable DEBUG='ds:api' allows you to trace API calls.

Find the full documentation here. In there also some of the idiosyncrasies of using the Datastore are explained.

See the API documentation for Details, Github for source.

Major issues

  • The Javascript-Datastore Bindings use nanosecond-Timestamp Information stored in the Datasore and rounds it to milliseconds. Python at least retains microseconds.
  • the old get_entity_group_version() / getEntityGroupVersion() API has been retired. You can still for key query { path: [key.path[0], {'kind': '__entity_group__', 'id': 1}]} to get a __version__ property. The reliability of this data on FireStore is unknown.
  • Googles Javascript API decided to use [Symbol(KEY)] to represent the Key in an entity. This results in all kinds of confusion when serializing to JSON, e.g. for caching. This library adds the property _keyStr which will be transparently used to regenerate [Symbol(KEY)] when needed.
  • Many functions are somewhat polymorphic where the shape of the return value depends on the function parameters, e.g. if the API was called with a key or a list of keys. You are encouraged to alvais provide a list of parameters instead a single parameter, e.g. get([key]) instead of get(key).
  • insert() and save() sometimes return the key being written and sometimes not. So you might or might not get some data in insertResponse?.[0].mutationResults?.[0]?.key?.path - urgs.
  • Google avoids BigInt. So key.id is (usually but not always) returned as a String but you have to provide a Number to the API.

Metrics

Datastore-API is instrumented with prom-client. Metrics are all prefixed with dstore_.

In an express based Application you can make them available like this:

import promClient from 'prom-client';

server.get('/metrics', async (req, res) => {
  try {
    res.set('Content-Type', promClient.register.contentType);
    res.end(await promClient.register.metrics());
  } catch (ex) {
    res.status(500).end(ex);
  }
});

See also

child_process.execSync(`rm -Rf ./dsData`)
const port = await getPort({ port: 8081 })
emulator = new Emulator({ debug: false, port, storeOnDisk: true , clean: false, dataDir: './dsData'})

Then inspect datastore output with something like this:

/opt/homebrew/share/google-cloud-sdk/platform/cloud-datastore-emulator/cloud_datastore_emulator start --host=localhost --port=8081 --store_on_disk=True ./dsData & npx dsadmin --project=huwawi2

Readme

Keywords

Package Sidebar

Install

npm i datastore-api

Weekly Downloads

117

Version

6.0.2

License

MIT

Unpacked Size

464 kB

Total Files

21

Last publish

Collaborators

  • mdornseif