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

Function queryDataWithNewColumn

spanner/schema.js:123–175  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

121}
122
123async function queryDataWithNewColumn(instanceId, databaseId, projectId) {
124 // [START spanner_query_data_with_new_column]
125 // This sample uses the `MarketingBudget` column. You can add the column
126 // by running the `add_column` sample or by running this DDL statement against
127 // your database:
128 // ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
129
130 // Imports the Google Cloud client library
131 const {Spanner} = require('@google-cloud/spanner');
132
133 /**
134 * TODO(developer): Uncomment the following lines before running the sample.
135 */
136 // const projectId = 'my-project-id';
137 // const instanceId = 'my-instance';
138 // const databaseId = 'my-database';
139
140 // Creates a client
141 const spanner = new Spanner({
142 projectId: projectId,
143 });
144
145 // Gets a reference to a Cloud Spanner instance and database
146 const instance = spanner.instance(instanceId);
147 const database = instance.database(databaseId);
148
149 const query = {
150 sql: 'SELECT SingerId, AlbumId, MarketingBudget FROM Albums',
151 };
152
153 // Queries rows from the Albums table
154 try {
155 const [rows] = await database.run(query);
156
157 rows.forEach(row => {
158 const json = row.toJSON();
159
160 console.log(
161 `SingerId: ${json.SingerId}, AlbumId: ${
162 json.AlbumId
163 }, MarketingBudget: ${
164 json.MarketingBudget ? json.MarketingBudget : null
165 }`
166 );
167 });
168 } catch (err) {
169 console.error('Failed to query data with new column:', err.message || err);
170 } finally {
171 // Close the database when finished.
172 await database.close();
173 }
174 // [END spanner_query_data_with_new_column]
175}
176
177const {
178 createDatabaseWithVersionRetentionPeriod,

Callers 1

schema.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected