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

Function insertUsingDml

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

Source from the content-addressed store, hash-verified

18// title: DML
19
20async function insertUsingDml(instanceId, databaseId, projectId) {
21 // [START spanner_dml_standard_insert]
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 try {
42 await database.runTransactionAsync(async transaction => {
43 const [rowCount] = await transaction.runUpdate({
44 sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (10, @firstName, @lastName)',
45 params: {
46 firstName: 'Virginia',
47 lastName: 'Watson',
48 },
49 });
50
51 console.log(
52 `Successfully inserted ${rowCount} record into the Singers table.`
53 );
54
55 await transaction.commit();
56 });
57 } catch (err) {
58 console.error('ERROR:', err);
59 } finally {
60 // Close the database when finished.
61 await database.close();
62 }
63 // [END spanner_dml_standard_insert]
64}
65
66async function updateUsingDml(instanceId, databaseId, projectId) {
67 // [START spanner_dml_standard_update]

Callers 1

dml.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected