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

Function updateWithTimestamp

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

Source from the content-addressed store, hash-verified

231}
232
233async function updateWithTimestamp(instanceId, databaseId, projectId) {
234 // [START spanner_update_data_with_timestamp_column]
235 // [START_EXCLUDE]
236 // This sample uses the `MarketingBudget` column. You can add the column
237 // by running the `schema.js addColumn` sample or by running this DDL statement against
238 // your database:
239 // ALTER TABLE Albums ADD COLUMN MarketingBudget INT64
240 //
241 // In addition this update expects the `LastUpdateTime` column
242 // added by running the `timestamp.js addTimestampColumn` sample
243 // or applying the DDL statement:
244 // ALTER TABLE Albums ADD COLUMN
245 // LastUpdateTime TIMESTAMP OPTIONS (allow_commit_timestamp=true)
246 // [END_EXCLUDE]
247
248 // Imports the Google Cloud client library
249 const {Spanner} = require('@google-cloud/spanner');
250
251 /**
252 * TODO(developer): Uncomment the following lines before running the sample.
253 */
254 // const projectId = 'my-project-id';
255 // const instanceId = 'my-instance';
256 // const databaseId = 'my-database';
257
258 // Creates a client
259 const spanner = new Spanner({
260 projectId: projectId,
261 });
262 let database;
263 try {
264 // Gets a reference to a Cloud Spanner instance and database
265 const instance = spanner.instance(instanceId);
266 database = instance.database(databaseId);
267
268 // Update a row in the Albums table
269 // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they
270 // must be converted to strings before being inserted as INT64s
271 const albumsTable = database.table('Albums');
272
273 const data = [
274 {
275 SingerId: '1',
276 AlbumId: '1',
277 MarketingBudget: '1000000',
278 LastUpdateTime: 'spanner.commit_timestamp()',
279 },
280 {
281 SingerId: '2',
282 AlbumId: '2',
283 MarketingBudget: '750000',
284 LastUpdateTime: 'spanner.commit_timestamp()',
285 },
286 ];
287
288 await albumsTable.update(data);
289 console.log('Updated data.');
290 } catch (err) {

Callers 1

timestamp.jsFile · 0.85

Calls 1

updateMethod · 0.45

Tested by

no test coverage detected