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

Function queryWithTimestamp

spanner/timestamp.js:299–360  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

297}
298
299async function queryWithTimestamp(instanceId, databaseId, projectId) {
300 // [START spanner_query_data_with_timestamp_column]
301 // [START_EXCLUDE]
302 // This sample uses the `MarketingBudget` column. You can add the column
303 // by running the `schema.js addColumn` sample or by running this DDL statement against
304 // your database:
305 // ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
306 //
307 // In addition this query expects the `LastUpdateTime` column
308 // added by running the `timestamp.js addTimestampColumn` sample
309 // or applying the DDL statement:
310 // ALTER TABLE Albums ADD COLUMN
311 // LastUpdateTime TIMESTAMP OPTIONS (allow_commit_timestamp=true)
312 // [END_EXCLUDE]
313
314 // Imports the Google Cloud client library
315 const {Spanner} = require('@google-cloud/spanner');
316
317 /**
318 * TODO(developer): Uncomment the following lines before running the sample.
319 */
320 // const projectId = 'my-project-id';
321 // const instanceId = 'my-instance';
322 // const databaseId = 'my-database';
323
324 // Creates a client
325 const spanner = new Spanner({
326 projectId: projectId,
327 });
328 let database;
329 try {
330 // Gets a reference to a Cloud Spanner instance and database
331 const instance = spanner.instance(instanceId);
332 database = instance.database(databaseId);
333
334 const query = {
335 sql: `SELECT SingerId, AlbumId, MarketingBudget, LastUpdateTime
336 FROM Albums ORDER BY LastUpdateTime DESC`,
337 };
338
339 // Queries rows from the Albums table
340 const [rows] = await database.run(query);
341
342 rows.forEach(row => {
343 const json = row.toJSON();
344
345 console.log(
346 `SingerId: ${json.SingerId}, AlbumId: ${
347 json.AlbumId
348 }, MarketingBudget: ${
349 json.MarketingBudget ? json.MarketingBudget : null
350 }, LastUpdateTime: ${json.LastUpdateTime}`
351 );
352 });
353 } catch (err) {
354 console.error('ERROR:', err);
355 } finally {
356 // Close the database when finished

Callers 1

timestamp.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected