Session
Create a Session object to interact with a Cloud Spanner session.
It is unlikely you will need to interact with sessions directly. By default, sessions are created and utilized for maximum performance automatically.
Constructor
Session
new Session(database, name)
Parameter |
|
---|---|
database |
Parent Database instance. |
name |
Optional string The name of the session. If not provided, it is assumed you are going to create it. |
Example
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
//-
// To create a session manually, don't provide a name.
//-
const session = database.session();
session.create(function(err) {
if (err) {
// Error handling omitted.
}
// Session created successfully.
// `session.id` = The name of the session.
});
//-
// To access a previously-created session, provide a name.
//-
const session = database.session('session-name');
Property
id
string
Methods
create
create(options, callback) returns Promise containing CreateSessionResponse
Create a session.
Parameter |
|
---|---|
options |
Optional object |
callback |
Optional Callback function. |
- Returns
-
Promise containing CreateSessionResponse
Example
session.create(function(err, session, apiResponse) {
if (err) {
// Error handling omitted.
}
// Session created successfully.
});
//-
//Returns a Promise if the callback is omitted.
//-
session.create()
.then(function(data) {
const session = data[0];
const apiResponse = data[1];
// Session created successfully.
});
exists
exists(callback) returns Promise containing SessionExistsResponse
Check if a session exists.
Parameter |
|
---|---|
callback |
Optional Callback function. |
- Returns
-
Promise containing SessionExistsResponse
Example
session.exists(function(err, exists) {});
//-
//Returns a Promise if the callback is omitted.
//-
session.exists().then(function(data) {
const exists = data[0];
});
get
get(options, callback) returns Promise containing GetSessionResponse
Get a session if it exists.
You may optionally use this to "get or create" an object by providing an object with autoCreate
set to true
. Any extra configuration that is normally required for the create
method must be
contained within this object as well.
Parameter |
|||||
---|---|---|---|---|---|
options |
Optional options Configuration object. Values in
|
||||
callback |
Optional Callback function. |
- Returns
-
Promise containing GetSessionResponse
Example
session.get(function(err, session, apiResponse) {
// `session.metadata` has been populated.
});
//-
//Returns a Promise if the callback is omitted.
//-
session.get().then(function(data) {
const session = data[0];
const apiResponse = data[0];
});
keepAlive
keepAlive(callback) returns Promise containing BasicResponse
Ping the session with SELECT 1
to prevent it from expiring.
Parameter |
|
---|---|
callback |
Optional Callback function. |
- Returns
-
Promise containing BasicResponse
Example
session.keepAlive(function(err) {
if (err) {
// An error occurred while trying to keep this session alive.
}
});
partitionedDml
partitionedDml() returns PartitionedDml
Create a PartitionedDml transaction.
- Returns
Example
const transaction = session.partitionedDml();
snapshot
snapshot(options) returns Snapshot
Create a Snapshot transaction.
Parameter |
|
---|---|
options |
Optional The timestamp bounds. |
- Returns
Example
const snapshot = session.snapshot({strong: false});
transaction
transaction() returns Transaction
Create a read write Transaction.
- Returns
Example
const transaction = session.transaction();