Constructor

Session

new Session(database, name)

Parameter

database

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

See Database#createSession.

callback

Optional

CreateSessionCallback

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

SessionExistsCallback

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 options have the following properties:

Parameter

autoCreate

Optional

boolean

Automatically create the object if it does not exist.

callback

Optional

CreateSessionCallback

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

BasicCallback

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

PartitionedDml 

Example

const transaction = session.partitionedDml();

snapshot

snapshot(options) returns Snapshot

Create a Snapshot transaction.

Parameter

options

Optional

TimestampBounds

The timestamp bounds.

Returns

Snapshot 

Example

const snapshot = session.snapshot({strong: false});

transaction

transaction() returns Transaction

Create a read write Transaction.

Returns

Transaction 

Example

const transaction = session.transaction();