Instance

Instance

The Instance class represents a Cloud Spanner instance.

Create an Instance object to interact with a Cloud Spanner instance.

Constructor

new Instance(spanner, name)

Parameters:
Name Type Description
spanner Spanner

Spanner instance.

name string

Name of the instance.

Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
```

Members

id

Methods

backup(backupId) → {Backup}

Get a reference to a Backup object.

Parameters:
Name Type Description
backupId string

The name of the backup.

Returns:
Type Description
Backup

A Backup object.

Throws:

If any parameter is not provided.

Type
GoogleError
Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const backup = instance.backup('my-backup');
```

create(config, callbackopt) → {Promise.<CreateInstanceResponse>}

Create an instance.

Wrapper around v1.InstanceAdminClient#createInstance.

Parameters:
Name Type Attributes Description
config CreateInstanceRequest

Configuration object.

callback CreateInstanceCallback <optional>

Callback function.

Returns:
Type Description
Promise.<CreateInstanceResponse>
See:
Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();

const instance = spanner.instance('my-instance');

instance.create(function(err, instance, operation, apiResponse) {
  if (err) {
    // Error handling omitted.
  }

  operation
    .on('error', function(err) {})
    .on('complete', function() {
      // Instance created successfully.
    });
});

//-
// If the callback is omitted, we'll return a Promise.
//-
instance.create()
  .then(function(data) {
    const operation = data[0];
    const apiResponse = data[1];

    return operation.promise();
  })
  .then(function() {
    // Instance created successfully.
  });
```

database(name, poolOptionsopt, queryOptionsopt) → {Database}

Get a reference to a Database object.

Parameters:
Name Type Attributes Description
name string

The name of the instance.

poolOptions SessionPoolOptions | SessionPoolCtor <optional>

Session pool configuration options.

queryOptions spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions <optional>

Default query options to use with the database. These options will be overridden by any query options set in environment variables or that are specified on a per-query basis.

Returns:
Type Description
Database

A Database object.

Throws:

If a name is not provided.

Type
GoogleError
Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();

const instance = spanner.instance('my-instance');
const database = instance.database('my-database');
```

getBackupOperations(optionsopt) → {Promise.<GetBackupOperationsResponse>}

List pending and completed backup operations for all databases in the instance.

Parameters:
Name Type Attributes Description
options GetBackupOperationsOptions <optional>

The query object for listing backup operations.

Properties
Name Type Attributes Description
gaxOptions gax.CallOptions <optional>

The request configuration options, See CallOptions for more details.

Returns:
Type Description
Promise.<GetBackupOperationsResponse>

When resolved, contains a paged list of backup operations.

See:
  • #listOperations
Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const [operations] = await instance.getBackupOperations();

//-
// To manually handle pagination, set autoPaginate:false in gaxOptions.
//-
let pageToken = undefined;
do {
  const [operations, , response] = await instance.getBackupOperations({
    pageSize: 3,
    pageToken,
    gaxOptions: {autoPaginate: false},
  });
  operations.forEach(operation => {
    // Do something with operation
  });
  pageToken = response.nextPageToken;
} while (pageToken);
```

getDatabaseOperations(optionsopt) → {Promise.<GetDatabaseOperationsResponse>}

List pending and completed operations for all databases in the instance.

Parameters:
Name Type Attributes Description
options GetDatabaseOperationsOptions <optional>

The query object for listing database operations.

Properties
Name Type Attributes Description
gaxOptions gax.CallOptions <optional>

The request configuration options, See CallOptions for more details.

Returns:
Type Description
Promise.<GetDatabaseOperationsResponse>

When resolved, contains a paged list of database operations.

See:
  • Database.getDatabaseOperations
Example
```
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner();
const instance = spanner.instance('my-instance');
const [operations] = await instance.getDatabaseOperations();
// ... then do something with the operations

//-
// To manually handle pagination, set autoPaginate:false in gaxOptions.
//-
let pageToken = undefined;
do {
  const [operations, , response] = await instance.getDatabaseOperations({
    pageSize: 3,
    pageToken,
    gaxOptions: {autoPaginate: false},
  });
  operations.forEach(operation => {
    // Do something with operation
  });
  pageToken = response.nextPageToken;
} while (pageToken);
```