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

Function main

spanner/rpc-priority-transaction.js:21–75  ·  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_transaction]
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 transactionWithRpcPriority(instanceId, databaseId) {
40 let database;
41
42 try {
43 // Gets a reference to a Cloud Spanner instance and database.
44 const instance = spanner.instance(instanceId);
45 database = instance.database(databaseId);
46
47 await database.runTransactionAsync(async transaction => {
48 const [rowCount] = await transaction.runUpdate({
49 sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (17, @firstName, @lastName)',
50 params: {
51 firstName: 'Virginia',
52 lastName: 'Watson',
53 },
54 });
55
56 await transaction.commit({
57 priority: Priority.PRIORITY_LOW,
58 });
59
60 console.log(
61 `Successfully inserted ${rowCount} record into the Singers table using low RPC priority.`
62 );
63 });
64 } catch (err) {
65 console.error('ERROR:', err);
66 } finally {
67 // Close the database when finished.
68 if (database) {
69 await database.close();
70 }
71 }
72 }
73 transactionWithRpcPriority(instanceId, databaseId);
74 // [END spanner_rpc_priority_transaction]
75}
76
77process.on('unhandledRejection', err => {
78 console.error(err.message);

Callers 1

Calls 1

Tested by

no test coverage detected