| 104 | } |
| 105 | |
| 106 | async function deleteUsingDml(instanceId, databaseId, projectId) { |
| 107 | // [START spanner_dml_standard_delete] |
| 108 | // Imports the Google Cloud client library |
| 109 | const {Spanner} = require('@google-cloud/spanner'); |
| 110 | |
| 111 | /** |
| 112 | * TODO(developer): Uncomment the following lines before running the sample. |
| 113 | */ |
| 114 | // const projectId = 'my-project-id'; |
| 115 | // const instanceId = 'my-instance'; |
| 116 | // const databaseId = 'my-database'; |
| 117 | |
| 118 | // Creates a client |
| 119 | const spanner = new Spanner({ |
| 120 | projectId: projectId, |
| 121 | }); |
| 122 | |
| 123 | // Gets a reference to a Cloud Spanner instance and database |
| 124 | const instance = spanner.instance(instanceId); |
| 125 | const database = instance.database(databaseId); |
| 126 | |
| 127 | try { |
| 128 | await database.runTransactionAsync(async transaction => { |
| 129 | const [rowCount] = await transaction.runUpdate({ |
| 130 | sql: "DELETE FROM Singers WHERE FirstName = 'Alice'", |
| 131 | }); |
| 132 | |
| 133 | console.log(`Successfully deleted ${rowCount} record.`); |
| 134 | await transaction.commit(); |
| 135 | }); |
| 136 | } catch (err) { |
| 137 | console.error('ERROR:', err); |
| 138 | } finally { |
| 139 | // Close the database when finished. |
| 140 | await database.close(); |
| 141 | } |
| 142 | // [END spanner_dml_standard_delete] |
| 143 | } |
| 144 | |
| 145 | async function updateUsingDmlWithTimestamp(instanceId, databaseId, projectId) { |
| 146 | // [START spanner_dml_standard_update_with_timestamp] |