Constructor

DatastoreClient

new DatastoreClient(options)

Construct an instance of DatastoreClient.

Parameter

options

Optional

object

The configuration object. See the subsequent parameters for more details.

Values in options have the following properties:

Parameter

credentials

Optional

object

Credentials object.

credentials.client_email

Optional

string

credentials.private_key

Optional

string

email

Optional

string

Account email address. Required when usaing a .pem or .p12 keyFilename.

keyFilename

Optional

string

Full path to the a .json, .pem, or .p12 key downloaded from the Google Developers Console. If you provide a path to a JSON file, the projectId option above is not necessary. NOTE: .pem and .p12 require you to specify options.email as well.

port

Optional

number

The port on which to connect to the remote host.

projectId

Optional

string

The project ID from the Google Developer's Console, e.g. 'grape-spaceship-123'. We will also check the environment variable GCLOUD_PROJECT for your project ID. If your app is running in an environment which supports Application Default Credentials, your project ID will be detected automatically.

promise

Optional

function()

Custom promise module to use instead of native Promises.

servicePath

Optional

string

The domain name of the API remote host.

Properties

port

static

The port for this API service.

scopes

static

The scopes needed to make gRPC calls for every method defined in this service.

servicePath

static

The DNS address for this API service.

Methods

allocateIds

allocateIds(request, options, callback) returns Promise

Allocates IDs for the given keys, which is useful for referencing an entity before it is inserted.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

keys

Array of Object

A list of keys with incomplete key paths for which to allocate IDs. No key may be reserved/read-only.

This object should have the same structure as Key

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing AllocateIdsResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing AllocateIdsResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var keys = [];
var request = {
  projectId: projectId,
  keys: keys,
};
client.allocateIds(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

beginTransaction

beginTransaction(request, options, callback) returns Promise

Begins a new transaction.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

transactionOptions

Optional

Object

Options for a new transaction.

This object should have the same structure as TransactionOptions

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing BeginTransactionResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing BeginTransactionResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
client.beginTransaction({projectId: projectId})
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

commit

commit(request, options, callback) returns Promise

Commits a transaction, optionally creating, deleting or modifying some entities.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

mode

number

The type of commit to perform. Defaults to TRANSACTIONAL.

The number should be among the values of Mode

mutations

Array of Object

The mutations to perform.

When mode is TRANSACTIONAL, mutations affecting a single entity are applied in order. The following sequences of mutations affecting a single entity are not permitted in a single Commit request:

  • insert followed by insert
  • update followed by insert
  • upsert followed by insert
  • delete followed by update

    When mode is NON_TRANSACTIONAL, no two mutations may affect a single entity.

    This object should have the same structure as Mutation

transaction

Optional

string

The identifier of the transaction associated with the commit. A transaction identifier is returned by a call to Datastore.BeginTransaction.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing CommitResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing CommitResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var mode = 'MODE_UNSPECIFIED';
var mutations = [];
var request = {
  projectId: projectId,
  mode: mode,
  mutations: mutations,
};
client.commit(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

getProjectId

getProjectId(callback)

Return the project ID used by this class.

Parameter

callback

function(Error, string)

the callback to be called with the current project Id.

lookup

lookup(request, options, callback) returns Promise

Looks up entities by key.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

keys

Array of Object

Keys of entities to look up.

This object should have the same structure as Key

readOptions

Optional

Object

The options for this lookup request.

This object should have the same structure as ReadOptions

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing LookupResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing LookupResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var keys = [];
var request = {
  projectId: projectId,
  keys: keys,
};
client.lookup(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

reserveIds

reserveIds(request, options, callback) returns Promise

Prevents the supplied keys' IDs from being auto-allocated by Cloud Datastore.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

keys

Array of Object

A list of keys with complete key paths whose numeric IDs should not be auto-allocated.

This object should have the same structure as Key

databaseId

Optional

string

If not empty, the ID of the database against which to make the request.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing ReserveIdsResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing ReserveIdsResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var keys = [];
var request = {
  projectId: projectId,
  keys: keys,
};
client.reserveIds(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

rollback

rollback(request, options, callback) returns Promise

Rolls back a transaction.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

transaction

string

The transaction identifier, returned by a call to Datastore.BeginTransaction.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing RollbackResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing RollbackResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var transaction = '';
var request = {
  projectId: projectId,
  transaction: transaction,
};
client.rollback(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

runQuery

runQuery(request, options, callback) returns Promise

Queries for entities.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

projectId

string

The ID of the project against which to make the request.

partitionId

Object

Entities are partitioned into subsets, identified by a partition ID. Queries are scoped to a single partition. This partition ID is normalized with the standard default context partition ID.

This object should have the same structure as PartitionId

readOptions

Optional

Object

The options for this query.

This object should have the same structure as ReadOptions

query

Optional

Object

The query to run.

This object should have the same structure as Query

gqlQuery

Optional

Object

The GQL query to run.

This object should have the same structure as GqlQuery

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing RunQueryResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing RunQueryResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const datastore = require('@google-cloud/datastore');

var client = new datastore.v1.DatastoreClient({
  // optional auth parameters.
});

var projectId = '';
var partitionId = {};
var request = {
  projectId: projectId,
  partitionId: partitionId,
};
client.runQuery(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });