Constructor

DatabaseAdminClient

new DatabaseAdminClient(options)

Construct an instance of DatabaseAdminClient.

Parameter

options

Optional

object

The configuration object. See the subsequent parameters for more details.

Values in options have the following properties:

Parameter

credentials

Optional

object

Credentials object.

credentials.client_email

Optional

string

credentials.private_key

Optional

string

email

Optional

string

Account email address. Required when using a .pem or .p12 keyFilename.

keyFilename

Optional

string

Full path to the a .json, .pem, or .p12 key downloaded from the Google Developers Console. If you provide a path to a JSON file, the projectId option below is not necessary. NOTE: .pem and .p12 require you to specify options.email as well.

port

Optional

number

The port on which to connect to the remote host.

projectId

Optional

string

The project ID from the Google Developer's Console, e.g. 'grape-spaceship-123'. We will also check the environment variable GCLOUD_PROJECT for your project ID. If your app is running in an environment which supports Application Default Credentials, your project ID will be detected automatically.

promise

Optional

function()

Custom promise module to use instead of native Promises.

servicePath

Optional

string

The domain name of the API remote host.

Properties

port

static

The port for this API service.

scopes

static

The scopes needed to make gRPC calls for every method defined in this service.

servicePath

static

The DNS address for this API service.

Methods

createDatabase

createDatabase(request, options, callback) returns Promise

Creates a new Cloud Spanner database and starts to prepare it for serving. The returned long-running operation will have a name of the format <database_name>/operations/<operation_id> and can be used to track preparation of the database. The metadata field type is CreateDatabaseMetadata. The response field type is Database, if successful.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

parent

string

Required. The name of the instance that will serve the new database. Values are of the form projects/<project>/instances/<instance>.

createStatement

string

Required. A CREATE DATABASE statement, which specifies the ID of the new database. The database ID must conform to the regular expression [a-z][a-z0-9_-]*[a-z0-9] and be between 2 and 30 characters in length. If the database ID is a reserved word or if it contains a hyphen, the database ID must be enclosed in backticks (`).

extraStatements

Optional

Array of string

An optional list of DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is a gax.Operation object.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is a gax.Operation object. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedParent = client.instancePath('[PROJECT]', '[INSTANCE]');
var createStatement = '';
var request = {
  parent: formattedParent,
  createStatement: createStatement,
};

// Handle the operation using the promise pattern.
client.createDatabase(request)
  .then(responses => {
    var operation = responses[0];
    var initialApiResponse = responses[1];

    // Operation#promise starts polling for the completion of the LRO.
    return operation.promise();
  })
  .then(responses => {
    // The final result of the operation.
    var result = responses[0];

    // The metadata value of the completed operation.
    var metadata = responses[1];

    // The response of the api call returning the complete operation.
    var finalApiResponse = responses[2];
  })
  .catch(err => {
    console.error(err);
  });

var formattedParent = client.instancePath('[PROJECT]', '[INSTANCE]');
var createStatement = '';
var request = {
  parent: formattedParent,
  createStatement: createStatement,
};

// Handle the operation using the event emitter pattern.
client.createDatabase(request)
  .then(responses => {
    var operation = responses[0];
    var initialApiResponse = responses[1];

    // Adding a listener for the "complete" event starts polling for the
    // completion of the operation.
    operation.on('complete', (result, metadata, finalApiResponse) => {
      // doSomethingWith(result);
    });

    // Adding a listener for the "progress" event causes the callback to be
    // called on any change in metadata when the operation is polled.
    operation.on('progress', (metadata, apiResponse) => {
      // doSomethingWith(metadata)
    });

    // Adding a listener for the "error" event handles any errors found during polling.
    operation.on('error', err => {
      // throw(err);
    });
  })
  .catch(err => {
    console.error(err);
  });

databasePath

databasePath(project, instance, database) returns String

Return a fully-qualified database resource name string.

Parameter

project

String

instance

String

database

String

Returns

String 

dropDatabase

dropDatabase(request, options, callback) returns Promise

Drops (aka deletes) a Cloud Spanner database.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

database

string

Required. The database to be dropped.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error)

The function which will be called with the result of the API call.

Returns

Promise 

  • The promise which resolves when API call finishes. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedDatabase = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
client.dropDatabase({database: formattedDatabase}).catch(err => {
  console.error(err);
});

getDatabase

getDatabase(request, options, callback) returns Promise

Gets the state of a Cloud Spanner database.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

name

string

Required. The name of the requested database. Values are of the form projects/<project>/instances/<instance>/databases/<database>.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing Database.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing Database. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedName = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
client.getDatabase({name: formattedName})
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

getDatabaseDdl

getDatabaseDdl(request, options, callback) returns Promise

Returns the schema of a Cloud Spanner database as a list of formatted DDL statements. This method does not show pending schema updates, those may be queried using the Operations API.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

database

string

Required. The database whose schema we wish to get.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing GetDatabaseDdlResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing GetDatabaseDdlResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedDatabase = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
client.getDatabaseDdl({database: formattedDatabase})
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

getIamPolicy

getIamPolicy(request, options, callback) returns Promise

Gets the access control policy for a database resource. Returns an empty policy if a database exists but does not have a policy set.

Authorization requires spanner.databases.getIamPolicy permission on resource.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

resource

string

REQUIRED: The resource for which the policy is being requested. resource is usually specified as a path. For example, a Project resource is specified as projects/{project}.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing Policy.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing Policy. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedResource = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
client.getIamPolicy({resource: formattedResource})
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

getProjectId

getProjectId(callback)

Return the project ID used by this class.

Parameter

callback

function(Error, string)

the callback to be called with the current project Id.

instancePath

instancePath(project, instance) returns String

Return a fully-qualified instance resource name string.

Parameter

project

String

instance

String

Returns

String 

listDatabases

listDatabases(request, options, callback) returns Promise

Lists Cloud Spanner databases.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

parent

string

Required. The instance whose databases should be listed. Values are of the form projects/<project>/instances/<instance>.

pageSize

Optional

number

The maximum number of resources contained in the underlying API response. If page streaming is performed per-resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Array, nullable Object, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is Array of Database.

When autoPaginate: false is specified through options, it contains the result in a single response. If the response indicates the next page exists, the third parameter is set to be used for the next request object. The fourth parameter keeps the raw response object of an object representing ListDatabasesResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is Array of Database.

    When autoPaginate: false is specified through options, the array has three elements. The first element is Array of Database in a single response. The second element is the next request object if the response indicates the next page exists, or null. The third element is an object representing ListDatabasesResponse.

    The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

// Iterate over all elements.
var formattedParent = client.instancePath('[PROJECT]', '[INSTANCE]');

client.listDatabases({parent: formattedParent})
  .then(responses => {
    var resources = responses[0];
    for (let i = 0; i < resources.length; i += 1) {
      // doThingsWith(resources[i])
    }
  })
  .catch(err => {
    console.error(err);
  });

// Or obtain the paged response.
var formattedParent = client.instancePath('[PROJECT]', '[INSTANCE]');


var options = {autoPaginate: false};
var callback = responses => {
  // The actual resources in a response.
  var resources = responses[0];
  // The next request if the response shows that there are more responses.
  var nextRequest = responses[1];
  // The actual response object, if necessary.
  // var rawResponse = responses[2];
  for (let i = 0; i < resources.length; i += 1) {
    // doThingsWith(resources[i]);
  }
  if (nextRequest) {
    // Fetch the next page.
    return client.listDatabases(nextRequest, options).then(callback);
  }
}
client.listDatabases({parent: formattedParent}, options)
  .then(callback)
  .catch(err => {
    console.error(err);
  });

listDatabasesStream

listDatabasesStream(request, options) returns Stream

Equivalent to listDatabases, but returns a NodeJS Stream object.

This fetches the paged responses for listDatabases continuously and invokes the callback registered for 'data' event for each element in the responses.

The returned object has 'end' method when no more elements are required.

autoPaginate option will be ignored.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

parent

string

Required. The instance whose databases should be listed. Values are of the form projects/<project>/instances/<instance>.

pageSize

Optional

number

The maximum number of resources contained in the underlying API response. If page streaming is performed per-resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

See also
https://nodejs.org/api/stream.html
Returns

Stream 

An object stream which emits an object representing Database on 'data' event.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedParent = client.instancePath('[PROJECT]', '[INSTANCE]');
client.listDatabasesStream({parent: formattedParent})
  .on('data', element => {
    // doThingsWith(element)
  }).on('error', err => {
    console.log(err);
  });

matchDatabaseFromDatabaseName

matchDatabaseFromDatabaseName(databaseName) returns String

Parse the databaseName from a database resource.

Parameter

databaseName

String

A fully-qualified path representing a database resources.

Returns

String 

  • A string representing the database.

matchInstanceFromDatabaseName

matchInstanceFromDatabaseName(databaseName) returns String

Parse the databaseName from a database resource.

Parameter

databaseName

String

A fully-qualified path representing a database resources.

Returns

String 

  • A string representing the instance.

matchInstanceFromInstanceName

matchInstanceFromInstanceName(instanceName) returns String

Parse the instanceName from a instance resource.

Parameter

instanceName

String

A fully-qualified path representing a instance resources.

Returns

String 

  • A string representing the instance.

matchProjectFromDatabaseName

matchProjectFromDatabaseName(databaseName) returns String

Parse the databaseName from a database resource.

Parameter

databaseName

String

A fully-qualified path representing a database resources.

Returns

String 

  • A string representing the project.

matchProjectFromInstanceName

matchProjectFromInstanceName(instanceName) returns String

Parse the instanceName from a instance resource.

Parameter

instanceName

String

A fully-qualified path representing a instance resources.

Returns

String 

  • A string representing the project.

setIamPolicy

setIamPolicy(request, options, callback) returns Promise

Sets the access control policy on a database resource. Replaces any existing policy.

Authorization requires spanner.databases.setIamPolicy permission on resource.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

resource

string

REQUIRED: The resource for which the policy is being specified. resource is usually specified as a path. For example, a Project resource is specified as projects/{project}.

policy

Object

REQUIRED: The complete policy to be applied to the resource. The size of the policy is limited to a few 10s of KB. An empty policy is a valid policy but certain Cloud Platform services (such as Projects) might reject them.

This object should have the same structure as Policy

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing Policy.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing Policy. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedResource = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
var policy = {};
var request = {
  resource: formattedResource,
  policy: policy,
};
client.setIamPolicy(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

testIamPermissions

testIamPermissions(request, options, callback) returns Promise

Returns permissions that the caller has on the specified database resource.

Attempting this RPC on a non-existent Cloud Spanner database will result in a NOT_FOUND error if the user has spanner.databases.list permission on the containing Cloud Spanner instance. Otherwise returns an empty set of permissions.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

resource

string

REQUIRED: The resource for which the policy detail is being requested. resource is usually specified as a path. For example, a Project resource is specified as projects/{project}.

permissions

Array of string

The set of permissions to check for the resource. Permissions with wildcards (such as '' or 'storage.') are not allowed. For more information see IAM Overview.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is an object representing TestIamPermissionsResponse.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is an object representing TestIamPermissionsResponse. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedResource = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
var permissions = [];
var request = {
  resource: formattedResource,
  permissions: permissions,
};
client.testIamPermissions(request)
  .then(responses => {
    var response = responses[0];
    // doThingsWith(response)
  })
  .catch(err => {
    console.error(err);
  });

updateDatabaseDdl

updateDatabaseDdl(request, options, callback) returns Promise

Updates the schema of a Cloud Spanner database by creating/altering/dropping tables, columns, indexes, etc. The returned long-running operation will have a name of the format <database_name>/operations/<operation_id> and can be used to track execution of the schema change(s). The metadata field type is UpdateDatabaseDdlMetadata. The operation has no response.

Parameter

request

Object

The request object that will be sent.

Values in request have the following properties:

Parameter

database

string

Required. The database to update.

statements

Array of string

DDL statements to be applied to the database.

operationId

Optional

string

If empty, the new update request is assigned an automatically-generated operation ID. Otherwise, operation_id is used to construct the name of the resulting Operation.

Specifying an explicit operation ID simplifies determining whether the statements were executed in the event that the UpdateDatabaseDdl call is replayed, or the return value is otherwise lost: the database and operation_id fields can be combined to form the name of the resulting longrunning.Operation: <database>/operations/<operation_id>.

operation_id should be unique within the database, and must be a valid identifier: [a-z][a-z0-9_]*. Note that automatically-generated operation IDs always begin with an underscore. If the named operation already exists, UpdateDatabaseDdl returns ALREADY_EXISTS.

options

Optional

Object

Optional parameters. You can override the default settings for this call, e.g, timeout, retries, paginations, etc. See gax.CallOptions for the details.

callback

Optional

function(nullable Error, nullable Object)

The function which will be called with the result of the API call.

The second parameter to the callback is a gax.Operation object.

Returns

Promise 

  • The promise which resolves to an array. The first element of the array is a gax.Operation object. The promise has a method named "cancel" which cancels the ongoing API call.

Example

const spanner = require('@google-cloud/spanner');

var client = new spanner.v1.DatabaseAdminClient({
  // optional auth parameters.
});

var formattedDatabase = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
var statements = [];
var request = {
  database: formattedDatabase,
  statements: statements,
};

// Handle the operation using the promise pattern.
client.updateDatabaseDdl(request)
  .then(responses => {
    var operation = responses[0];
    var initialApiResponse = responses[1];

    // Operation#promise starts polling for the completion of the LRO.
    return operation.promise();
  })
  .then(responses => {
    // The final result of the operation.
    var result = responses[0];

    // The metadata value of the completed operation.
    var metadata = responses[1];

    // The response of the api call returning the complete operation.
    var finalApiResponse = responses[2];
  })
  .catch(err => {
    console.error(err);
  });

var formattedDatabase = client.databasePath('[PROJECT]', '[INSTANCE]', '[DATABASE]');
var statements = [];
var request = {
  database: formattedDatabase,
  statements: statements,
};

// Handle the operation using the event emitter pattern.
client.updateDatabaseDdl(request)
  .then(responses => {
    var operation = responses[0];
    var initialApiResponse = responses[1];

    // Adding a listener for the "complete" event starts polling for the
    // completion of the operation.
    operation.on('complete', (result, metadata, finalApiResponse) => {
      // doSomethingWith(result);
    });

    // Adding a listener for the "progress" event causes the callback to be
    // called on any change in metadata when the operation is polled.
    operation.on('progress', (metadata, apiResponse) => {
      // doSomethingWith(metadata)
    });

    // Adding a listener for the "error" event handles any errors found during polling.
    operation.on('error', err => {
      // throw(err);
    });
  })
  .catch(err => {
    console.error(err);
  });