new DatastoreClient([options])

Construct an instance of DatastoreClient.

Parameters

Name Type Optional Description

options

 

Yes

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

Values in options have the following properties:

Name Type Optional Description

credentials

 

Yes

Credentials object.

credentials.client_email

 

Yes

credentials.private_key

 

Yes

email

 

Yes

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

keyFilename

 

Yes

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 below is not necessary. NOTE: .pem and .p12 require you to specify options.email as well.

port

 

Yes

The port on which to connect to the remote host.

projectId

 

Yes

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

 

Yes

Custom promise module to use instead of native Promises.

servicePath

 

Yes

The domain name of the API remote host.

Properties

static

apiEndpoint

The DNS address for this API service - same as servicePath(), exists for compatibility reasons.

static

port

The port for this API service.

static

scopes

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

static

servicePath

The DNS address for this API service.

Methods

allocateIds(request[, options][, callback]) → Promise

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

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

beginTransaction(request[, options][, callback]) → Promise

Begins a new transaction.

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

projectId

string

 

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

transactionOptions

Object

Yes

Options for a new transaction.

This object should have the same structure as TransactionOptions

options

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

commit(request[, options][, callback]) → Promise

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

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

string

Yes

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

options

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

getProjectId(callback)

Return the project ID used by this class.

Parameter

Name Type Optional Description

callback

function(Error, string)

 

the callback to be called with the current project Id.

lookup(request[, options][, callback]) → Promise

Looks up entities by key.

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

Object

Yes

The options for this lookup request.

This object should have the same structure as ReadOptions

options

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

reserveIds(request[, options][, callback]) → Promise

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

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

string

Yes

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

options

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

rollback(request[, options][, callback]) → Promise

Rolls back a transaction.

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.

runQuery(request[, options][, callback]) → Promise

Queries for entities.

Example

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

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

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

Parameters

Name Type Optional Description

request

Object

 

The request object that will be sent.

Values in request have the following properties:

Name Type Optional Description

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

Object

Yes

The options for this query.

This object should have the same structure as ReadOptions

query

Object

Yes

The query to run.

This object should have the same structure as Query

gqlQuery

Object

Yes

The GQL query to run.

This object should have the same structure as GqlQuery

options

Object

Yes

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

callback

function(nullable Error, nullable Object)

Yes

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.