Nautical Pirate Moustache
Join us to discuss the challenges, solutions and best practices for in-house JavaScript code sharing. Tuesday, 12/17 at 10am PT/1pm ET.Sign up here »


0.0.26 • Public • Published

Datastore Driver

Standalone Config:

Import config.service.ts, instantiate the class. The constructor takes in one argument the location of a .env file like:

PROJECT_ID = <cloud datastore project id>
DATASTOREKEY = <path to cloud datastore credential json file>
DATASTORE_NAMESPACE = <datastore namespace>

Alternatively, pass in a JSON object with the above keys and values into the second argument of the config class like:

export interface jsonConfig {
  [key: string]: string;
  PROJECT_ID: string;

Once the config is instantiated, the datastore class needs to be instantiated, which takes in the config object as it's single constructor paramater.

The datastore should be configured and ready for use.

NestJS Config:


Using the Datastore:

Exposed functions:

createEntity - Creates a new object - Simply pass in a JSON object, and a 'kind' name (see datastore documentation) and that entity will be created. Optionally specify a keyName

readEntity - Read an entity - Read an array of JSON objects from the datastore. Requires a kind, and also a Filter object. (See below for filter object structure). Can optionally also take a {property:string} argument. If included this argument will sort the results and return only the the greatest element in descending order by a simple >= comparison, Will FAIL if property is not sortable numerically (first) or alphabetically

replaceEntity - replace an Entity - *Do a complete 1-1 replacement of an entity with another completely different entity.

editEntity - Edit an entity - *Edit any entity. Requires you to pass in an id, a kind, and an array of new values. The new values array is an array of key-value pairs representing TOP level of keys and values:

eg. Say you pass in: [{age:4}, geolocation:{lat:35,lng:65}] into the third argument of the edit function.

 This is basically saying for a given entity, replace the age property and the geolocation property wit the included values. (Will create those keys if they don't exist)

  ie: {"geolocation":{"lat":3,"lng":3}, "age":3}

 to replace only lat, you will need to replace the whole geolocation object, and pass in a new geolocation object as the third parameter.

 To replace age, you simply need to pass in {"age":5} as the third parameter.

 The edit occurs on the TOP level of the JSON object.

deleteEntity - removes an entity - Simply takes in kind and keyname.

What is a filter?

A filter is a JSON object used by the readEntity function to quickly filter elements.

The type definition is as follows

export type datastoreFilterComparator = '=' | '>' | '>=' | '<' | '<=';
export interface filter {
  property: string;
  comparator: datastoreFilterComparator;
  searchValue: string | number | object | boolean;

By passing in a filter to the readEntity function you can read entities where the property is filtered by some sort of criteria.

The readEntity function takes in an ARRAY of filters, so multiple criteria can be used but BEWARE: The filters are applied sequentially so the ORDER of the filtering does matter.


  1. Fix unit tests to work in standalone mode. (not depend on nestJS infrastrucutre)
  2. Finish docstring-ing the datastore functions


npm i google-datastore-driver

DownloadsWeekly Downloads






Unpacked Size

42.4 kB

Total Files


Last publish


  • avatar