MutationSet

MutationSet

MutationSet represent a set of changes to be applied atomically to a Cloud Spanner database with a Transaction. Mutations are used to insert, update, upsert(insert or update), replace, or delete rows within tables.

Mutations are added to a Transaction and are not executed until the transaction is committed via Transaction#commit.

If the transaction is rolled back or encounters an error, the mutations are discarded.

Constructor

new MutationSet()

Creates a new Mutation object.

Example
```
const {Spanner, Mutation} = require('@google-cloud/spanner');
const spanner = new Spanner();

const instance = spanner.instance('my-instance');
const database = instance.database('my-database');

const mutations = new MutationSet();
mutations.insert('Singers', {SingerId: '123', FirstName: 'David'});
mutations.update('Singers', {SingerId: '123', FirstName: 'Marc'});

try {
 database.writeAtLeastOnce(mutations, (err, res) => {
   console.log("RESPONSE: ", res);
 });
} catch(err) {
 console.log("ERROR: ", err);
}
```

Methods

deleteRows()

Adds a deleteRows operation to the mutation set. This operation deletes rows from the specified table based on their primary keys.

Parameters:
Name Type Description
table. string

The name of the table to deleteRows from.

key. Array.<key>

An array of key objects, each represeting the primary key of a row to delete.

insert()

Adds an insert operation to the mutation set.

Parameters:
Name Type Description
table. string

The name of the table to insert into.

rows. object | Array.<object>

A single row object or an array of row objects to insert.

proto() → {Array.<spannerClient.spanner.v1.Mutation>}

Returns the internal representation of the queued mutations as a protobuf message.

Returns:
Type Description
Array.<spannerClient.spanner.v1.Mutation>

. The protobuf message representing the mutations.

replace()

Adds a replace operation to the mutation set. A replace operation deletes the existing row (if it exists) and inserts the new row.

Parameters:
Name Type Description
table. string

The name of the table to replace.

rows. object | Array.<object>

A single row object or an array of row objects to replace.

update()

Adds an update operation to the mutation set.

Parameters:
Name Type Description
table. string

The name of the table to update.

rows. object | Array.<object>

A single row object or an array of row objects to update. Each row object must contain the primary key values to indentify the row to update.

upsert()

Adds an upsert operation to the mutation set. An upsert will insert a new row if it does not exist or update an existing row if it does.

Parameters:
Name Type Description
table. string

The name of the table to upsert.

rows. object | Array.<object>

A single row object or an array of row objects to upsert.