MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

spanner/rpc-priority-batch-dml.js:21–79  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

19'use strict';
20
21async function main(instanceId, databaseId, projectId) {
22 // [START spanner_rpc_priority_batch_dml]
23 // Imports the Google Cloud client library.
24 const {Spanner, protos} = require('@google-cloud/spanner');
25 const Priority = protos.google.spanner.v1.RequestOptions.Priority;
26
27 /**
28 * TODO(developer): Uncomment the following lines before running the sample.
29 */
30 // const projectId = 'my-project-id';
31 // const instanceId = 'my-instance';
32 // const databaseId = 'my-database';
33
34 // Creates a client
35 const spanner = new Spanner({
36 projectId: projectId,
37 });
38
39 async function updateUsingBatchDmlWithRpcPriority(instanceId, databaseId) {
40 let database;
41 try {
42 // Gets a reference to a Cloud Spanner instance and database
43 const instance = spanner.instance(instanceId);
44 database = instance.database(databaseId);
45
46 const insert = {
47 sql: `INSERT INTO Albums (SingerId, AlbumId, AlbumTitle, MarketingBudget)
48 VALUES (1, 4, "Test Album Title", 10000)`,
49 };
50
51 const update = {
52 sql: `UPDATE Albums SET MarketingBudget = MarketingBudget * 2
53 WHERE SingerId = 1 and AlbumId = 4`,
54 };
55
56 const dmlStatements = [insert, update];
57
58 await database.runTransactionAsync(async transaction => {
59 const [rowCounts] = await transaction.batchUpdate(dmlStatements, {
60 requestOptions: {
61 priority: Priority.PRIORITY_LOW,
62 },
63 });
64 await transaction.commit();
65 console.log(
66 `Successfully executed ${rowCounts.length} SQL statements using Batch DML using low RPC priority.`
67 );
68 });
69 } catch (err) {
70 console.error('ERROR:', err);
71 throw err;
72 } finally {
73 // Close the database when finished.
74 await database.close();
75 }
76 }
77 await updateUsingBatchDmlWithRpcPriority(instanceId, databaseId);
78 // [END spanner_rpc_priority_batch_dml]

Callers 1

Calls 1

Tested by

no test coverage detected