Constructor

Transaction

new Transaction(datastore)

A transaction is a set of Datastore operations on one or more entities. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied.

Parameter

datastore

Datastore

A Datastore instance.

Extends
Request
Mixes in
module:datastore/request
See also

Transactions Reference

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

Properties

datastore

Datastore

namespace

string

projectId

string

Methods

commit

commit(gaxOptions, callback)

Commit the remote transaction and finalize the current transaction instance.

If the commit request fails, we will automatically rollback the transaction.

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request. If the commit fails, we automatically try to rollback the transaction (see {module:datastore/transaction#rollback}).

Value may be null.

apiResponse

object

The full API response.

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

transaction.commit(function(err, apiResponse) {
  if (err) {
    // Transaction could not be committed.
  }
});

//-
// If the callback is omitted, we'll return a Promise.
//-
transaction.commit().then(function(data) {
  var apiResponse = data[0];
});

createQuery

createQuery(namespace, kind) returns Query

Create a query for the specified kind. See {module:datastore/query} for all of the available methods.

Parameter

namespace

Optional

string

Namespace.

kind

string

The kind to query.

See also

Datastore Queries

Query
Returns

Query 

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

// Run the query inside the transaction.
transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  var query = transaction.createQuery('Company');

  query.run(function(err, entities) {
    if (err) {
      // Error handling omitted.
    }

    transaction.commit(function(err) {
      if (!err) {
        // Transaction committed successfully.
      }
    });
  });
});

delete

delete(key)

Delete all entities identified with the specified key(s) in the current transaction.

Parameter

key

(Key or Array of Key)

Datastore key object(s).

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  // Delete a single entity.
  transaction.delete(datastore.key(['Company', 123]));

  // Delete multiple entities at once.
  transaction.delete([
    datastore.key(['Company', 123]),
    datastore.key(['Product', 'Computer'])
  ]);

  transaction.commit(function(err) {
    if (!err) {
      // Transaction committed successfully.
    }
  });
});

rollback

rollback(gaxOptions, callback)

Reverse a transaction remotely and finalize the current transaction instance.

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

callback

function()

The callback function.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

apiResponse

object

The full API response.

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  transaction.rollback(function(err) {
    if (!err) {
      // Transaction rolled back successfully.
    }
  });
});

//-
// If the callback is omitted, we'll return a Promise.
//-
transaction.rollback().then(function(data) {
  var apiResponse = data[0];
});

run

run(options, callback)

Begin a remote transaction. In the callback provided, run your transactional commands.

Parameter

options

Optional

object

Configuration object.

Values in options have the following properties:

Parameter

gaxOptions

Optional

object

Request configuration options, outlined here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.

readOnly

Optional

boolean

A read-only transaction cannot modify entities.

transactionId

Optional

string

The ID of a previous transaction.

callback

function()

The function to execute within the context of a transaction.

Values in callback have the following properties:

Parameter

err

error

An error returned while making this request.

Value may be null.

transaction

Transaction

This transaction instance.

apiResponse

object

The full API response.

Example

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

transaction.run(function(err, transaction) {
  // Perform Datastore transactional operations.
  var key = datastore.key(['Company', 123]);

  transaction.get(key, function(err, entity) {
    entity.name = 'Google';

    transaction.save({
      key: key,
      data: entity
    });

    transaction.commit(function(err) {
      if (!err) {
        // Data saved successfully.
      }
    });
  });
});

//-
// If the callback is omitted, we'll return a Promise.
//-
transaction.run().then(function(data) {
  var transaction = data[0];
  var apiResponse = data[1];
});

save

save(entities)

Insert or update the specified object(s) in the current transaction. If a key is incomplete, its associated object is inserted and the original Key object is updated to contain the generated ID.

This method will determine the correct Datastore method to execute (upsert, insert, or update) by using the key(s) provided. For example, if you provide an incomplete key (one without an ID), the request will create a new entity and have its ID automatically assigned. If you provide a complete key, the entity will be updated with the data specified.

By default, all properties are indexed. To prevent a property from being included in all indexes, you must supply an excludeFromIndexes array. See below for an example.

Parameter

entities

(object or Array of object)

Datastore key object(s).

Values in entities have the following properties:

Parameter

key

Key

Datastore key object.

excludeFromIndexes

Optional

Array of string

Exclude properties from indexing using a simple JSON path notation. See the example below to see how to target properties at different levels of nesting within your entity.

data

object

Data to save with the provided key.

Examples

Save a single entity.

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

// Notice that we are providing an incomplete key. After the transaction is
// committed, the Key object held by the `key` variable will be populated
// with a path containing its generated ID.
//-
const key = datastore.key('Company');

transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  transaction.save({
    key: key,
    data: {
      rating: '10'
    }
  });

  transaction.commit(function(err) {
    if (!err) {
      // Data saved successfully.
    }
  });
});
const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();

// Use an array, `excludeFromIndexes`, to exclude properties from indexing.
// This will allow storing string values larger than 1500 bytes.

transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  transaction.save({
    key: key,
    excludeFromIndexes: [
      'description',
      'embeddedEntity.description',
      'arrayValue[].description'
    ],
    data: {
      description: 'Long string (...)',
      embeddedEntity: {
        description: 'Long string (...)'
      },
      arrayValue: [
        {
          description: 'Long string (...)'
        }
      ]
    }
  });

  transaction.commit(function(err) {
    if (!err) {
      // Data saved successfully.
    }
  });
});

Save multiple entities at once.

const Datastore = require('@google-cloud/datastore');
const datastore = new Datastore();
const transaction = datastore.transaction();
const companyKey = datastore.key(['Company', 123]);
const productKey = datastore.key(['Product', 'Computer']);

transaction.run(function(err) {
  if (err) {
    // Error handling omitted.
  }

  transaction.save([
    {
      key: companyKey,
      data: {
        HQ: 'Dallas, TX'
      }
    },
    {
      key: productKey,
      data: {
        vendor: 'Dell'
      }
    }
  ]);

  transaction.commit(function(err) {
    if (!err) {
      // Data saved successfully.
    }
  });
});