( instanceId = 'my-instance', databaseId = 'my-database', projectId = 'my-project-id' )
| 19 | 'use strict'; |
| 20 | |
| 21 | function main( |
| 22 | instanceId = 'my-instance', |
| 23 | databaseId = 'my-database', |
| 24 | projectId = 'my-project-id' |
| 25 | ) { |
| 26 | // [START spanner_create_storing_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 createStoringIndex() { |
| 45 | const request = [ |
| 46 | 'CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) STORING (MarketingBudget)', |
| 47 | ]; |
| 48 | |
| 49 | // Creates a new index in the database |
| 50 | try { |
| 51 | const [operation] = await databaseAdminClient.updateDatabaseDdl({ |
| 52 | database: databaseAdminClient.databasePath( |
| 53 | projectId, |
| 54 | instanceId, |
| 55 | databaseId |
| 56 | ), |
| 57 | statements: request, |
| 58 | }); |
| 59 | |
| 60 | console.log('Waiting for operation to complete...'); |
| 61 | await operation.promise(); |
| 62 | |
| 63 | console.log('Added the AlbumsByAlbumTitle2 index.'); |
| 64 | } catch (err) { |
| 65 | console.error('Failed to create storing index:', err.message || err); |
| 66 | } finally { |
| 67 | // Close the spanner client when finished. |
| 68 | // The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient. |
| 69 | spanner.close(); |
| 70 | } |
| 71 | } |
| 72 | createStoringIndex(); |
| 73 | // [END spanner_create_storing_index] |
| 74 | } |
| 75 | |
| 76 | main(...process.argv.slice(2)); |
no test coverage detected