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

Function updateData

spanner/crud.js:20–59  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

18// title: CRUD
19
20async function updateData(instanceId, databaseId, projectId) {
21 // [START spanner_update_data]
22 // Imports the Google Cloud client library
23 const {Spanner} = require('@google-cloud/spanner');
24
25 /**
26 * TODO(developer): Uncomment the following lines before running the sample.
27 */
28 // const projectId = 'my-project-id';
29 // const instanceId = 'my-instance';
30 // const databaseId = 'my-database';
31
32 // Creates a client
33 const spanner = new Spanner({
34 projectId: projectId,
35 });
36
37 // Gets a reference to a Cloud Spanner instance and database
38 const instance = spanner.instance(instanceId);
39 const database = instance.database(databaseId);
40
41 // Update a row in the Albums table
42 // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they
43 // must be converted to strings before being inserted as INT64s
44 const albumsTable = database.table('Albums');
45
46 try {
47 await albumsTable.update([
48 {SingerId: '1', AlbumId: '1', MarketingBudget: '100000'},
49 {SingerId: '2', AlbumId: '2', MarketingBudget: '500000'},
50 ]);
51 console.log('Updated data.');
52 } catch (err) {
53 console.error('ERROR:', err);
54 } finally {
55 // Close the database when finished.
56 await database.close();
57 }
58 // [END spanner_update_data]
59}
60
61async function insertData(instanceId, databaseId, projectId) {
62 // [START spanner_insert_data]

Callers 1

crud.jsFile · 0.85

Calls 1

updateMethod · 0.45

Tested by

no test coverage detected