// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // sample-metadata: // title: Updates data in a table in a Spanner PostgreSQL database. // usage: node pg-dml-getting-started-update.js 'use strict'; function main( instanceId = 'my-instance', databaseId = 'my-database', projectId = 'my-project-id', ) { // [START spanner_postgresql_dml_getting_started_update] /** * TODO(developer): Uncomment these variables before running the sample. */ // const instanceId = 'my-instance'; // const databaseId = 'my-database'; // const projectId = 'my-project-id'; // Imports the Google Cloud Spanner client library const {Spanner} = require('@google-cloud/spanner'); // Instantiates a client const spanner = new Spanner({ projectId: projectId, }); function updateUsingDml() { // Gets a reference to a Cloud Spanner instance and database const instance = spanner.instance(instanceId); const database = instance.database(databaseId); database.runTransaction(async (err, transaction) => { if (err) { console.error(err); return; } try { const [rowCount] = await transaction.runUpdate({ sql: 'UPDATE singers SET FirstName = $1 WHERE singerid = 1', params: { p1: 'Virginia', }, }); console.log( `Successfully updated ${rowCount} record in the Singers table.`, ); await transaction.commit(); } catch (err) { console.error('ERROR:', err); } finally { // Close the database when finished. await database.close(); } }); } updateUsingDml(); // [END spanner_postgresql_dml_getting_started_update] } process.on('unhandledRejection', err => { console.error(err.message); process.exitCode = 1; }); main(...process.argv.slice(2));