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

Function writeUsingDml

spanner/dml.js:281–321  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

279}
280
281async function writeUsingDml(instanceId, databaseId, projectId) {
282 // [START spanner_dml_getting_started_insert]
283 // Imports the Google Cloud client library
284 const {Spanner} = require('@google-cloud/spanner');
285
286 /**
287 * TODO(developer): Uncomment the following lines before running the sample.
288 */
289 // const projectId = 'my-project-id';
290 // const instanceId = 'my-instance';
291 // const databaseId = 'my-database';
292
293 // Creates a client
294 const spanner = new Spanner({
295 projectId: projectId,
296 });
297
298 // Gets a reference to a Cloud Spanner instance and database
299 const instance = spanner.instance(instanceId);
300 const database = instance.database(databaseId);
301
302 try {
303 await database.runTransactionAsync(async transaction => {
304 const [rowCount] = await transaction.runUpdate({
305 sql: `INSERT Singers (SingerId, FirstName, LastName) VALUES
306 (12, 'Melissa', 'Garcia'),
307 (13, 'Russell', 'Morales'),
308 (14, 'Jacqueline', 'Long'),
309 (15, 'Dylan', 'Shaw')`,
310 });
311 console.log(`${rowCount} records inserted.`);
312 await transaction.commit();
313 });
314 } catch (err) {
315 console.error('ERROR:', err);
316 } finally {
317 // Close the database when finished.
318 await database.close();
319 }
320 // [END spanner_dml_getting_started_insert]
321}
322
323async function queryDataWithParameter(instanceId, databaseId, projectId) {
324 // [START spanner_query_with_parameter]

Callers 1

dml.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected