| 185 | } |
| 186 | |
| 187 | async function queryData(instanceId, databaseId, projectId) { |
| 188 | // [START spanner_query_data] |
| 189 | // Imports the Google Cloud client library |
| 190 | const {Spanner} = require('@google-cloud/spanner'); |
| 191 | |
| 192 | /** |
| 193 | * TODO(developer): Uncomment the following lines before running the sample. |
| 194 | */ |
| 195 | // const projectId = 'my-project-id'; |
| 196 | // const instanceId = 'my-instance'; |
| 197 | // const databaseId = 'my-database'; |
| 198 | |
| 199 | // Creates a client |
| 200 | const spanner = new Spanner({ |
| 201 | projectId: projectId, |
| 202 | }); |
| 203 | |
| 204 | // Gets a reference to a Cloud Spanner instance and database |
| 205 | const instance = spanner.instance(instanceId); |
| 206 | const database = instance.database(databaseId); |
| 207 | |
| 208 | const query = { |
| 209 | sql: 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums', |
| 210 | }; |
| 211 | |
| 212 | // Queries rows from the Albums table |
| 213 | try { |
| 214 | const [rows] = await database.run(query); |
| 215 | |
| 216 | rows.forEach(row => { |
| 217 | const json = row.toJSON(); |
| 218 | console.log( |
| 219 | `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}` |
| 220 | ); |
| 221 | }); |
| 222 | } catch (err) { |
| 223 | console.error('ERROR:', err); |
| 224 | } finally { |
| 225 | // Close the database when finished. |
| 226 | await database.close(); |
| 227 | } |
| 228 | // [END spanner_query_data] |
| 229 | } |
| 230 | |
| 231 | async function readData(instanceId, databaseId, projectId) { |
| 232 | // [START spanner_read_data] |