* Executes a query against a Cloud Spanner database. * * @param {string} projectId The project ID containing the Spanner instance (for example, 'example-project-id'). * @param {string} instanceId The ID of the Spanner instance (for example, 'example-instance'). * @param {string} databaseId The I
(projectId, instanceId, databaseId)
| 29 | * @param {string} databaseId The ID of the Spanner database (for example 'example-database'). |
| 30 | */ |
| 31 | async function quickstart(projectId, instanceId, databaseId) { |
| 32 | try { |
| 33 | // Gets a reference to a Cloud Spanner instance and database |
| 34 | const instance = spanner.instance(instanceId); |
| 35 | const database = instance.database(databaseId); |
| 36 | |
| 37 | const query = { |
| 38 | sql: 'SELECT 1', |
| 39 | }; |
| 40 | |
| 41 | const [rows] = await database.run(query); |
| 42 | |
| 43 | console.log(`Query successfully executed. ${rows.length} row(s) found.`); |
| 44 | rows.forEach(row => { |
| 45 | console.log(` Row details: ${JSON.stringify(row)}`); |
| 46 | }); |
| 47 | } catch (err) { |
| 48 | if (err.code === status.NOT_FOUND) { |
| 49 | console.error( |
| 50 | `Resource not found. Please verify that instance '${instanceId}' and database '${databaseId}' exist under project '${projectId}'.` |
| 51 | ); |
| 52 | } else { |
| 53 | console.error('An unexpected error occurred during execution:', err); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | // [END spanner_quickstart] |
| 58 | |
| 59 | if (require.main === module) { |