TransactionRequest
Constructor
TransactionRequest
new TransactionRequest(options)
Handle logic for Table/Transaction API operations.
Abstract class extended by Transaction and Table.
Parameter |
|
---|---|
options |
Optional object Timestamp options. |
Methods
createReadStream
createReadStream(table, query) returns ReadableStream
Create a readable object stream to receive rows from the database using key lookups and scans.
Wrapper around v1.SpannerClient#streamingRead.
Parameter |
|
---|---|
table |
string The table to read from. |
query |
Configuration object. See
|
- See also
- v1.SpannerClient#streamingRead
- Returns
-
ReadableStream
A readable stream that emits rows.
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.createReadStream('Singers', {
keys: ['1'],
columns: ['SingerId', 'name']
})
.on('error', function(err) {})
.on('data', function(row) {
// row = [
// {
// name: 'SingerId',
// value: '1'
// },
// {
// name: 'Name',
// value: 'Eddie Wilson'
// }
// ]
})
.on('end', function() {
// All results retrieved.
});
});
//-
// Provide an array for `query.keys` to read with a composite key.
//-
const query = {
keys: [
[
'Id1',
'Name1'
],
[
'Id2',
'Name2'
]
],
// ...
};
//-
// Rows are returned as an array of object arrays. Each object has a `name`
// and `value` property. To get a serialized object, call `toJSON()`.
//-
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.createReadStream('Singers', {
keys: ['1'],
columns: ['SingerId', 'name']
})
.on('error', function(err) {})
.on('data', function(row) {
// row.toJSON() = {
// SingerId: '1',
// Name: 'Eddie Wilson'
// }
})
.on('end', function() {
// All results retrieved.
});
});
//-
// Alternatively, set `query.json` to `true`, and this step will be performed
// automaticaly.
//-
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.createReadStream('Singers', {
keys: ['1'],
columns: ['SingerId', 'name'],
json: true,
})
.on('error', function(err) {})
.on('data', function(row) {
// row = {
// SingerId: '1',
// Name: 'Eddie Wilson'
// }
})
.on('end', function() {
// All results retrieved.
});
});
//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.createReadStream('Singers', {
keys: ['1'],
columns: ['SingerId', 'name']
})
.on('data', function(row) {
this.end();
});
});
deleteRows
deleteRows(table, keys)
Delete rows from a table.
Parameter |
|
---|---|
table |
string The name of the table. |
keys |
array The keys for the rows to delete. If using a composite key, provide an array within this array. See the example below. |
- See also
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const keys = ['Id1', 'Id2', 'Id3'];
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue this mutation until later calling `commit`.
// Note that a callback is not passed to `deleteRows`.
transaction.deleteRows('Singers', keys);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The rows were deleted successfully.
}
});
});
//-
// Provide an array for `keys` to delete rows with a composite key.
//-
const keys = [
[
'Id1',
'Name1'
],
[
'Id2',
'Name2'
]
];
insert
insert(table, keyVals)
Insert rows of data into this table.
Parameter |
|
---|---|
table |
string The name of the table. |
keyVals |
(object or Array of object) A map of names to values of data to insert into this table. |
- See also
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const row = {
SingerId: 'Id3',
Name: 'Eddie Wilson'
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue this mutation until later calling `commit`.
// Note that a callback is not passed to `insert`.
transaction.insert('Singers', row);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The row was inserted successfully.
}
});
});
//-
// Multiple rows can be inserted at once.
//-
const row2 = {
SingerId: 'Id3b',
Name: 'Joe West'
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue multiple mutations until later calling `commit`.
// Note that a callback is not passed to `insert`.
transaction.insert('Singers', [
row,
row2
]);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The rows were inserted successfully.
}
});
});
read
read(table, query, callback) returns Promise containing TransactionRequestReadResponse
Performs a read request against the specified Table.
Wrapper around v1.SpannerClient#read.
Parameter |
|
---|---|
table |
string The table to read from. |
query |
Configuration object, describing what to read from the table. |
callback |
Optional TransactionRequestReadCallback Callback function. |
- See also
- v1.SpannerClient#read
- Returns
-
Promise containing TransactionRequestReadResponse
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const query = {
keys: ['1'],
columns: ['SingerId', 'name']
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.read('Singers', query, function(err, rows) {
if (err) {
// Error handling omitted.
}
const firstRow = rows[0];
// firstRow = [
// {
// name: 'SingerId',
// value: '1'
// },
// {
// name: 'Name',
// value: 'Eddie Wilson'
// }
// ]
// End the transaction. Note that no callback is provided.
transaction.end();
});
});
//-
// Provide an array for `query.keys` to read with a composite key.
//-
const query = {
keys: [
[
'Id1',
'Name1'
],
[
'Id2',
'Name2'
]
],
// ...
};
//-
// Rows are returned as an array of object arrays. Each object has a `name`
// and `value` property. To get a serialized object, call `toJSON()`.
//-
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
transaction.read('Singers', query, function(err, rows) {
if (err) {
// Error handling omitted.
}
const firstRow = rows[0];
// firstRow.toJSON() = {
// SingerId: '1',
// Name: 'Eddie Wilson'
// }
// End the transaction. Note that no callback is provided.
transaction.end();
});
});
//-
// Alternatively, set `query.json` to `true`, and this step will be performed
// automaticaly.
//-
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
query.json = true;
transaction.read('Singers', query, function(err, rows) {
if (err) {
// Error handling omitted.
}
const firstRow = rows[0];
// firstRow = {
// SingerId: '1',
// Name: 'Eddie Wilson'
// }
// End the transaction. Note that no callback is provided.
transaction.end();
});
});
replace
replace(table, keyVals)
Replace rows of data within a table.
Parameter |
|
---|---|
table |
string The table to read from. |
keyVals |
(object or Array of object) A map of names to values of data to insert into this table. |
- See also
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const row = {
SingerId: 'Id3',
Name: 'Joe West'
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue this mutation until later calling `commit`.
// Note that a callback is not passed to `replace`.
transaction.replace('Singers', row);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The row was replaced successfully.
}
});
});
update
update(table, keyVals)
Update rows of data within a table.
Parameter |
|
---|---|
table |
string The table to read from. |
keyVals |
(object or Array of object) A map of names to values of data to insert into this table. |
- See also
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const row = {
SingerId: 'Id3',
Name: 'Joe West'
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue this mutation until later calling `commit`.
// Note that a callback is not passed to `update`.
transaction.update('Singers', row);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The row was updated successfully.
}
});
});
upsert
upsert(table, keyVals)
Insert or update rows of data within a table.
Parameter |
|
---|---|
table |
string The table to read from. |
keyVals |
(object or Array of object) A map of names to values of data to insert into this table. |
- See also
Example
const Spanner = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
const row = {
SingerId: 'Id3',
Name: 'Joe West'
};
database.runTransaction(function(err, transaction) {
if (err) {
// Error handling omitted.
}
// Queue this mutation until later calling `commit`.
// Note that a callback is not passed to `upsert`.
transaction.upsert('Singers', row);
// Commit the transaction.
transaction.commit(function(err) {
if (!err) {
// The row was updated or inserted successfully.
}
});
});