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

Function main

spanner/index-read-data.js:21–74  ·  view source on GitHub ↗
(
  instanceId = 'my-instance',
  databaseId = 'my-database',
  projectId = 'my-project-id'
)

Source from the content-addressed store, hash-verified

19'use strict';
20
21function main(
22 instanceId = 'my-instance',
23 databaseId = 'my-database',
24 projectId = 'my-project-id'
25) {
26 // [START spanner_read_data_with_index]
27 /**
28 * TODO(developer): Uncomment these variables before running the sample.
29 */
30 // const instanceId = 'my-instance';
31 // const databaseId = 'my-database';
32 // const projectId = 'my-project-id';
33
34 // Imports the Google Cloud Spanner client library
35 const {Spanner} = require('@google-cloud/spanner');
36
37 // Instantiates a client
38 const spanner = new Spanner({
39 projectId: projectId,
40 });
41
42 async function readDataWithIndex() {
43 // Gets a reference to a Cloud Spanner instance and database
44 const instance = spanner.instance(instanceId);
45 const database = instance.database(databaseId);
46
47 const albumsTable = database.table('Albums');
48
49 const query = {
50 columns: ['AlbumId', 'AlbumTitle'],
51 keySet: {
52 all: true,
53 },
54 index: 'AlbumsByAlbumTitle',
55 };
56
57 // Reads the Albums table using an index
58 try {
59 const [rows] = await albumsTable.read(query);
60
61 rows.forEach(row => {
62 const json = row.toJSON();
63 console.log(`AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}`);
64 });
65 } catch (err) {
66 console.error('ERROR:', err);
67 } finally {
68 // Close the database when finished.
69 await database.close();
70 }
71 }
72 readDataWithIndex();
73 // [END spanner_read_data_with_index]
74}
75
76main(...process.argv.slice(2));

Callers 1

index-read-data.jsFile · 0.70

Calls 1

readDataWithIndexFunction · 0.85

Tested by

no test coverage detected