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

Function queryDataWithIndex

spanner/index-query-data.js:46–80  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44 });
45
46 async function queryDataWithIndex() {
47 // Gets a reference to a Cloud Spanner instance and database
48 const instance = spanner.instance(instanceId);
49 const database = instance.database(databaseId);
50
51 const query = {
52 sql: `SELECT AlbumId, AlbumTitle, MarketingBudget
53 FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}
54 WHERE AlbumTitle >= @startTitle AND AlbumTitle <= @endTitle`,
55 params: {
56 startTitle: startTitle,
57 endTitle: endTitle,
58 },
59 };
60
61 // Queries rows from the Albums table
62 try {
63 const [rows] = await database.run(query);
64
65 rows.forEach(row => {
66 const json = row.toJSON();
67 const marketingBudget = json.MarketingBudget
68 ? json.MarketingBudget
69 : null; // This value is nullable
70 console.log(
71 `AlbumId: ${json.AlbumId}, AlbumTitle: ${json.AlbumTitle}, MarketingBudget: ${marketingBudget}`
72 );
73 });
74 } catch (err) {
75 console.error('Failed to query data with index:', err.message || err);
76 } finally {
77 // Close the database when finished.
78 await database.close();
79 }
80 }
81 queryDataWithIndex();
82 // [END spanner_query_data_with_index]
83}

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected