| 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 | } |