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

Function queryDataWithStruct

spanner/struct.js:79–131  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

77}
78
79async function queryDataWithStruct(instanceId, databaseId, projectId) {
80 // [START spanner_create_struct_with_data]
81 // Imports the Google Cloud client library
82 const {Spanner} = require('@google-cloud/spanner');
83
84 const nameStruct = Spanner.struct({
85 FirstName: 'Elena',
86 LastName: 'Campbell',
87 });
88 // [END spanner_create_struct_with_data]
89
90 // [START spanner_query_data_with_struct]
91 /**
92 * TODO(developer): Uncomment the following lines before running the sample.
93 */
94 // const projectId = 'my-project-id';
95 // const instanceId = 'my-instance';
96 // const databaseId = 'my-database';
97
98 // Creates a client
99 const spanner = new Spanner({
100 projectId: projectId,
101 });
102 let database;
103 try {
104 // Gets a reference to a Cloud Spanner instance and database
105 const instance = spanner.instance(instanceId);
106 database = instance.database(databaseId);
107
108 const query = {
109 sql:
110 'SELECT SingerId FROM Singers WHERE ' +
111 'STRUCT<FirstName STRING, LastName STRING>(FirstName, LastName) = @name',
112 params: {
113 name: nameStruct,
114 },
115 };
116
117 // Queries rows from the Singers table
118 const [rows] = await database.run(query);
119
120 rows.forEach(row => {
121 const json = row.toJSON();
122 console.log(`SingerId: ${json.SingerId}`);
123 });
124 } catch (err) {
125 console.error('ERROR:', err);
126 } finally {
127 // Close the database when finished.
128 await database.close();
129 }
130 // [END spanner_query_data_with_struct]
131}
132
133async function queryWithArrayofStruct(instanceId, databaseId, projectId) {
134 // Imports the Google Cloud client library

Callers 1

struct.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected