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

Function readDataWithIndex

spanner/index-read-data.js:42–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected