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

Function main

spanner/index-create.js:21–72  ·  view source on GitHub ↗
(
  instanceId = 'my-instance',
  databaseId = 'my-database',
  projectId = 'my-project-id'
)

Source from the content-addressed store, hash-verified

19'use strict';
20
21function main(
22 instanceId = 'my-instance',
23 databaseId = 'my-database',
24 projectId = 'my-project-id'
25) {
26 // [START spanner_create_index]
27 /**
28 * TODO(developer): Uncomment these variables before running the sample.
29 */
30 // const instanceId = 'my-instance';
31 // const databaseId = 'my-database';
32 // const projectId = 'my-project-id';
33
34 // Imports the Google Cloud client library
35 const {Spanner} = require('@google-cloud/spanner');
36
37 // creates a client
38 const spanner = new Spanner({
39 projectId: projectId,
40 });
41
42 const databaseAdminClient = spanner.getDatabaseAdminClient();
43
44 async function createIndex() {
45 const request = ['CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)'];
46
47 // Creates a new index in the database
48 try {
49 const [operation] = await databaseAdminClient.updateDatabaseDdl({
50 database: databaseAdminClient.databasePath(
51 projectId,
52 instanceId,
53 databaseId
54 ),
55 statements: request,
56 });
57
58 console.log('Waiting for operation to complete...');
59 await operation.promise();
60
61 console.log('Added the AlbumsByAlbumTitle index.');
62 } catch (err) {
63 console.error('Failed to create index:', err.message || err);
64 } finally {
65 // Close the spanner client when finished.
66 // The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
67 spanner.close();
68 }
69 }
70 createIndex();
71 // [END spanner_create_index]
72}
73
74main(...process.argv.slice(2));

Callers 1

index-create.jsFile · 0.70

Calls 1

createIndexFunction · 0.85

Tested by

no test coverage detected