MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / createDatabase

Function createDatabase

spanner/schema.js:19–75  ·  view source on GitHub ↗
(instanceID, databaseID, projectID)

Source from the content-addressed store, hash-verified

17
18// creates a database using Database Admin Client
19async function createDatabase(instanceID, databaseID, projectID) {
20 // [START spanner_create_database]
21
22 /**
23 * TODO(developer): Uncomment the following lines before running the sample.
24 */
25 // const projectId = 'my-project-id';
26 // const instanceId = 'my-instance';
27 // const databaseId = 'my-database';
28
29 // Imports the Google Cloud client library
30 const {Spanner} = require('@google-cloud/spanner');
31
32 // creates a client
33 const spanner = new Spanner({
34 projectId: projectID,
35 });
36
37 const databaseAdminClient = spanner.getDatabaseAdminClient();
38
39 const createSingersTableStatement = `
40 CREATE TABLE Singers (
41 SingerId INT64 NOT NULL,
42 FirstName STRING(1024),
43 LastName STRING(1024),
44 SingerInfo BYTES(MAX),
45 FullName STRING(2048) AS (ARRAY_TO_STRING([FirstName, LastName], " ")) STORED,
46 ) PRIMARY KEY (SingerId)`;
47 const createAlbumsTableStatement = `
48 CREATE TABLE Albums (
49 SingerId INT64 NOT NULL,
50 AlbumId INT64 NOT NULL,
51 AlbumTitle STRING(MAX)
52 ) PRIMARY KEY (SingerId, AlbumId),
53 INTERLEAVE IN PARENT Singers ON DELETE CASCADE`;
54
55 // Creates a new database
56 try {
57 const [operation] = await databaseAdminClient.createDatabase({
58 createStatement: 'CREATE DATABASE `' + databaseID + '`',
59 extraStatements: [
60 createSingersTableStatement,
61 createAlbumsTableStatement,
62 ],
63 parent: databaseAdminClient.instancePath(projectID, instanceID),
64 });
65
66 console.log(`Waiting for creation of ${databaseID} to complete...`);
67 await operation.promise();
68
69 console.log(`Created database ${databaseID} on instance ${instanceID}.`);
70 } catch (err) {
71 console.error('Failed to create database:', err.message || err);
72 }
73
74 // [END spanner_create_database]
75}
76

Callers 1

schema.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected