| 534 | } |
| 535 | |
| 536 | async function updateUsingBatchDml(instanceId, databaseId, projectId) { |
| 537 | // [START spanner_dml_batch_update] |
| 538 | // Imports the Google Cloud client library |
| 539 | const {Spanner} = require('@google-cloud/spanner'); |
| 540 | |
| 541 | /** |
| 542 | * TODO(developer): Uncomment the following lines before running the sample. |
| 543 | */ |
| 544 | // const projectId = 'my-project-id'; |
| 545 | // const instanceId = 'my-instance'; |
| 546 | // const databaseId = 'my-database'; |
| 547 | |
| 548 | // Creates a client |
| 549 | const spanner = new Spanner({ |
| 550 | projectId: projectId, |
| 551 | }); |
| 552 | |
| 553 | // Gets a reference to a Cloud Spanner instance and database |
| 554 | const instance = spanner.instance(instanceId); |
| 555 | const database = instance.database(databaseId); |
| 556 | |
| 557 | const insert = { |
| 558 | sql: `INSERT INTO Albums (SingerId, AlbumId, AlbumTitle, MarketingBudget) |
| 559 | VALUES (1, 3, "Test Album Title", 10000)`, |
| 560 | }; |
| 561 | |
| 562 | const update = { |
| 563 | sql: `UPDATE Albums SET MarketingBudget = MarketingBudget * 2 |
| 564 | WHERE SingerId = 1 and AlbumId = 3`, |
| 565 | }; |
| 566 | |
| 567 | const dmlStatements = [insert, update]; |
| 568 | |
| 569 | try { |
| 570 | await database.runTransactionAsync(async transaction => { |
| 571 | const [rowCounts] = await transaction.batchUpdate(dmlStatements); |
| 572 | await transaction.commit(); |
| 573 | console.log( |
| 574 | `Successfully executed ${rowCounts.length} SQL statements using Batch DML.` |
| 575 | ); |
| 576 | }); |
| 577 | } catch (err) { |
| 578 | console.error('ERROR:', err); |
| 579 | throw err; |
| 580 | } finally { |
| 581 | // Close the database when finished. |
| 582 | await database.close(); |
| 583 | } |
| 584 | // [END spanner_dml_batch_update] |
| 585 | } |
| 586 | |
| 587 | async function insertWithCustomTimeoutAndRetrySettings( |
| 588 | instanceId, |