Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions spanner/database-create-with-default-leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2024 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// sample-metadata:
// title: Creates a new database with a specific default leader
// usage: node database-create-with-default-leader.js <INSTANCE_ID> <DATABASE_ID> <DEFAULT_LEADER> <PROJECT_ID>

'use strict';

function main(instanceId, databaseId, defaultLeader, projectId) {
// [START spanner_create_database_with_default_leader]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const defaultLeader = 'my-default-leader'; example: 'asia-northeast1'

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// creates a client
const spanner = new Spanner({
projectId: projectId,
});

// Gets a reference to a Cloud Spanner Database Admin Client object
const databaseAdminClient = spanner.getDatabaseAdminClient();

async function createDatabaseWithDefaultLeader() {
// Create a new database with an extra statement which will alter the
// database after creation to set the default leader.
console.log(
`Creating database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
)}.`
);
const createSingersTableStatement = `
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX)
) PRIMARY KEY (SingerId)`;
const createAlbumsStatement = `
CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX)
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE`;

// Default leader is one of the possible values in the leaderOptions field of the
// instance config of the instance where the database is created.
const setDefaultLeaderStatement = `
ALTER DATABASE \`${databaseId}\`
SET OPTIONS (default_leader = '${defaultLeader}')`;

const [operation] = await databaseAdminClient.createDatabase({
createStatement: 'CREATE DATABASE `' + databaseId + '`',
extraStatements: [
createSingersTableStatement,
createAlbumsStatement,
setDefaultLeaderStatement,
],
parent: databaseAdminClient.instancePath(projectId, instanceId),
});

console.log(`Waiting for creation of ${databaseId} to complete...`);
await operation.promise();
console.log(
`Created database ${databaseId} with default leader ${defaultLeader}.`
);
}
createDatabaseWithDefaultLeader();
// [END spanner_create_database_with_default_leader]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions spanner/database-get-ddl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2024 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// sample-metadata:
// title: Gets the schema definition of an existing database
// usage: node database-get-ddl.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

function main(instanceId, databaseId, projectId) {
// [START spanner_get_database_ddl]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// creates a client
const spanner = new Spanner({
projectId: projectId,
});

const databaseAdminClient = spanner.getDatabaseAdminClient();

async function getDatabaseDdl() {
// Get the schema definition of the database.
const [ddlStatements] = await databaseAdminClient.getDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
});

console.log(
`Retrieved database DDL for ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
)}:`
);
ddlStatements.statements.forEach(element => {
console.log(element);
});
}
getDatabaseDdl();
// [END spanner_get_database_ddl]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions spanner/database-get-default-leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2021 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// sample-metadata:
// title: Gets the default leader option of an existing database
// usage: node database-get-default-leader.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

function main(instanceId, databaseId, projectId) {
// [START spanner_query_information_schema_database_options]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});
// Gets a reference to a Cloud Spanner instance and a database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

async function getDatabaseDdl() {
// Get the default leader option for the database.
const [rows] = await database.run({
sql: `
SELECT s.OPTION_NAME, s.OPTION_VALUE
FROM INFORMATION_SCHEMA.DATABASE_OPTIONS s
WHERE s.OPTION_NAME = 'default_leader'`,
json: true,
});
if (rows.length > 0) {
const option = rows[0];
console.log(
`The ${option.OPTION_NAME} for ${databaseId} is ${option.OPTION_VALUE}`
);
} else {
console.log(
`Database ${databaseId} does not have a value for option 'default_leader'`
);
}
}
getDatabaseDdl();
// [END spanner_query_information_schema_database_options]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
75 changes: 75 additions & 0 deletions spanner/database-update-default-leader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2024 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// sample-metadata:
// title: Updates the default leader of an existing database
// usage: node database-update-default-leader.js <INSTANCE_ID> <DATABASE_ID> <DEFAULT_LEADER> <PROJECT_ID>

'use strict';

function main(instanceId, databaseId, defaultLeader, projectId) {
// [START spanner_update_database_with_default_leader]
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance-id';
// const databaseId = 'my-database-id';
// const defaultLeader = 'my-default-leader';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// creates a client
const spanner = new Spanner({
projectId: projectId,
});

const databaseAdminClient = spanner.getDatabaseAdminClient();

async function updateDatabaseWithDefaultLeader() {
console.log(
`Updating database ${databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
)}.`
);
const setDefaultLeaderStatement = `
ALTER DATABASE \`${databaseId}\`
SET OPTIONS (default_leader = '${defaultLeader}')`;
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: [setDefaultLeaderStatement],
});

console.log(`Waiting for updating of ${databaseId} to complete...`);
await operation.promise();
console.log(
`Updated database ${databaseId} with default leader ${defaultLeader}.`
);
}
updateDatabaseWithDefaultLeader();
// [END spanner_update_database_with_default_leader]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
61 changes: 61 additions & 0 deletions spanner/get-instance-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2024 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// sample-metadata:
// title: Gets the instance config metadata for the configuration nam6
// usage: node get-instance-config.js <PROJECT_ID>

'use strict';

function main(projectId) {
// [START spanner_get_instance_config]

/**
* TODO(developer): Uncomment the following line before running the sample.
*/
// const projectId = 'my-project-id';

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

// Creates a client
const spanner = new Spanner({
projectId: projectId,
});

const instanceAdminClient = spanner.getInstanceAdminClient();

async function getInstanceConfig() {
// Get the instance config for the multi-region North America 6 (NAM6).
// See https://cloud.google.com/spanner/docs/instance-configurations#configuration for a list of all available
// configurations.
const [instanceConfig] = await instanceAdminClient.getInstanceConfig({
name: instanceAdminClient.instanceConfigPath(projectId, 'nam6'),
});
console.log(
`Available leader options for instance config ${instanceConfig.name} ('${
instanceConfig.displayName
}'):
${instanceConfig.leaderOptions.join()}`
);
}
getInstanceConfig();
// [END spanner_get_instance_config]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading
Loading