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 completeKey
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.
- 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 forkey
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 ofget(key)
. -
insert()
andsave()
sometimes return the key being written and sometimes not. So you might or might not get some data ininsertResponse?.[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.
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);
}
});
-
Google Documentation
- Official Google Node Datastore SDK Documentation
- Hidden Auto-Generated Datastore API Documentation
- Other Hidden Auto-Generated Datastore API Documentation with better navigation. Seems to contain more on the lower level access.
- SDK Source
- API reference helps to understand under-documented SDK.
- grpc-js environment variables - try GRPC_VERBOSITY=DEBUG GRPC_TRACE=all npm run test
-
API Simplification
-
ORM / Schema
-
Others
-
For debugging
-
dsadmin -
npx dsadmin --project=projectid
- google-datastore-emulator for node
Setup your Tests like this:
-
dsadmin -
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