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