Extends
Members
ended
Whether or not the transaction has ended. If true, make no further requests, and discard the transaction.
- Overrides:
id
The transaction ID.
- Overrides:
metadata
The raw transaction response object. It is populated after Snapshot#begin is called.
- Overrides:
readTimestamp
Snapshot only The timestamp at which all reads are performed.
- Overrides:
readTimestampProto
Snapshot only The protobuf version of Snapshot#readTimestamp. This is useful if you require microsecond precision.
- Overrides:
Methods
createReadStream(table, query) → {ReadableStream}
Create a readable object stream to receive rows from the database using key lookups and scans.
Wrapper around v1.SpannerClient#streamingRead.
Parameters:
Name | Type | Description |
---|---|---|
table |
string |
The table to read from. |
query |
ReadRequest |
Configuration object. See official
|
Returns:
Type | Description |
---|---|
ReadableStream |
A readable stream that emits rows. |
- Overrides:
- See:
Fires:
- PartialResultStream#event:response
- PartialResultStream#event:stats
Examples
```
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()`.
```
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 perform automatically.
```
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.
```
transaction.createReadStream('Singers', {
keys: ['1'],
columns: ['SingerId', 'name']
})
.on('data', function(row) {
this.end();
});
```
end()
Let the client know you're done with a particular transaction. This should mainly be called for Snapshot objects, however in certain cases you may want to call them for Transaction objects as well.
- Overrides:
Examples
Calling `end` on a read only snapshot
```
database.getSnapshot((err, transaction) => {
if (err) {
// Error handling omitted.
}
transaction.run('SELECT * FROM Singers', (err, rows) => {
if (err) {
// Error handling omitted.
}
// End the snapshot.
transaction.end();
});
});
```
Calling `end` on a read/write transaction
```
database.runTransaction((err, transaction) => {
if (err) {
// Error handling omitted.
}
const query = 'UPDATE Account SET Balance = 1000 WHERE Key = 1';
transaction.runUpdate(query, err => {
if (err) {
// In the event of an error, there would be nothing to rollback,
so
// instead of continuing, discard the
transaction. transaction.end(); return;
}
transaction.commit(err => {});
});
});
```
excludeTxnFromChangeStreams()
Use option excludeTxnFromChangeStreams to exclude partitionedDml queries from being tracked in change streams.
Enabling this options to true will effectively disable change stream tracking for a specified partitionedDml query, allowing write queries to operate without being included in change streams.
runStream(query) → {ReadableStream}
Create a readable object stream to receive resulting rows from a SQL statement.
Wrapper around v1.SpannerClient#executeStreamingSql.
Parameters:
Name | Type | Description |
---|---|---|
query |
string | ExecuteSqlRequest |
A SQL query or ExecuteSqlRequest object. |
Returns:
Type | Description |
---|---|
ReadableStream |
- Overrides:
- See:
Fires:
- PartialResultStream#event:response
- PartialResultStream#event:stats
Examples
```
const query = 'SELECT * FROM Singers';
transaction.runStream(query)
.on('error', function(err) {})
.on('data', function(row) {
// row = {
// SingerId: '1',
// Name: 'Eddie Wilson'
// }
})
.on('end', function() {
// All results retrieved.
});
```
The SQL query string can contain parameter placeholders.
A parameter placeholder consists of '@' followed by the parameter name.
```
const query = {
sql: 'SELECT * FROM Singers WHERE name = @name',
params: {
name: 'Eddie Wilson'
}
};
transaction.runStream(query)
.on('error', function(err) {})
.on('data', function(row) {})
.on('end', function() {});
```
If you anticipate many results, you can end a stream
early to prevent unnecessary processing and API requests.
```
transaction.runStream(query)
.on('data', function(row) {
this.end();
});
```