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

Function main

spanner/rpc-priority-read.js:21–80  ·  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_read]
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 readDataWithRpcPriority(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 albumsTable = database.table('Albums');
47
48 const query = {
49 columns: ['SingerId', 'AlbumId', 'AlbumTitle'],
50 keySet: {
51 all: true,
52 },
53 requestOptions: {
54 priority: Priority.PRIORITY_LOW,
55 },
56 };
57
58 // Reads the Albums table using an index
59 const [rows] = await albumsTable.read(query);
60
61 console.log(
62 `Successfully fetched ${rows.length} rows using low RPC priority.`
63 );
64
65 rows.forEach(row => {
66 const json = row.toJSON();
67 console.log(
68 `SingerId: ${json.SingerId}, AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}`
69 );
70 });
71 } catch (err) {
72 console.error('ERROR:', err);
73 } finally {
74 // Close the database when finished.
75 await database.close();
76 }
77 }
78 await readDataWithRpcPriority(instanceId, databaseId);

Callers 1

Calls 1

readDataWithRpcPriorityFunction · 0.85

Tested by

no test coverage detected