| 143 | } |
| 144 | |
| 145 | async function updateUsingDmlWithTimestamp(instanceId, databaseId, projectId) { |
| 146 | // [START spanner_dml_standard_update_with_timestamp] |
| 147 | // Imports the Google Cloud client library |
| 148 | const {Spanner} = require('@google-cloud/spanner'); |
| 149 | |
| 150 | /** |
| 151 | * TODO(developer): Uncomment the following lines before running the sample. |
| 152 | */ |
| 153 | // const projectId = 'my-project-id'; |
| 154 | // const instanceId = 'my-instance'; |
| 155 | // const databaseId = 'my-database'; |
| 156 | |
| 157 | // Creates a client |
| 158 | const spanner = new Spanner({ |
| 159 | projectId: projectId, |
| 160 | }); |
| 161 | |
| 162 | // Gets a reference to a Cloud Spanner instance and database |
| 163 | const instance = spanner.instance(instanceId); |
| 164 | const database = instance.database(databaseId); |
| 165 | |
| 166 | try { |
| 167 | await database.runTransactionAsync(async transaction => { |
| 168 | const [rowCount] = await transaction.runUpdate({ |
| 169 | sql: `UPDATE Albums |
| 170 | SET LastUpdateTime = PENDING_COMMIT_TIMESTAMP() |
| 171 | WHERE SingerId = 1`, |
| 172 | }); |
| 173 | |
| 174 | console.log(`Successfully updated ${rowCount} records.`); |
| 175 | await transaction.commit(); |
| 176 | }); |
| 177 | } catch (err) { |
| 178 | console.error('ERROR:', err); |
| 179 | } finally { |
| 180 | // Close the database when finished. |
| 181 | await database.close(); |
| 182 | } |
| 183 | // [END spanner_dml_standard_update_with_timestamp] |
| 184 | } |
| 185 | |
| 186 | async function writeAndReadUsingDml(instanceId, databaseId, projectId) { |
| 187 | // [START spanner_dml_write_then_read] |