Session
Constructor
Session
new Session(database, name)
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.
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.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
session.create()
.then(function(data) {
var session = data[0];
var apiResponse = data[1];
// Session created successfully.
});
delete
delete(callback) returns Promise containing BasicResponse
Delete a session.
Wrapper around v1.SpannerClient#deleteSession.
Parameter |
|
---|---|
callback |
Optional Callback function. |
- See also
- v1.SpannerClient#deleteSession
- Returns
-
Promise containing BasicResponse
Example
session.delete(function(err, apiResponse) {
if (err) {
// Error handling omitted.
}
// Session deleted successfully.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
session.delete().then(function(data) {
var apiResponse = data[0];
});
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) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
session.exists().then(function(data) {
var 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.
});
//-
// If the callback is omitted, we'll return a Promise.
//-
session.get().then(function(data) {
var session = data[0];
var apiResponse = data[0];
});
getMetadata
getMetadata(callback) returns Promise containing GetSessionMetadataResponse
Get the session's metadata.
Wrapper around v1.SpannerClient#getSession.
Parameter |
|
---|---|
callback |
Optional Callback function. |
- See also
- v1.SpannerClient#getSession
- Returns
-
Promise containing GetSessionMetadataResponse
Example
session.getMetadata(function(err, metadata, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
session.getMetadata().then(function(data) {
var metadata = data[0];
var apiResponse = data[1];
});
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.
}
});
transaction
transaction(id) returns Transaction
Create a Transaction object.
Parameter |
|
---|---|
id |
string The id of the transaction. |
- Throws
-
Error
If an ID is not provided.
- Returns
-
A Transaction object.
Example
var transaction = database.transaction('transaction-id');