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

Function writeDataForStructQueries

spanner/struct.js:17–77  ·  view source on GitHub ↗
(instanceId, databaseId, projectId)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function writeDataForStructQueries(instanceId, databaseId, projectId) {
18 // [START spanner_write_data_for_struct_queries]
19 // Imports the Google Cloud client library
20 const {Spanner} = require('@google-cloud/spanner');
21
22 /**
23 * TODO(developer): Uncomment and update the following lines before running the sample.
24 */
25 // const projectId = 'my-project-id';
26 // const instanceId = 'my-instance';
27 // const databaseId = 'my-database';
28
29 // Creates a client
30 const spanner = new Spanner({
31 projectId: projectId,
32 });
33 let database;
34 try {
35 // Gets a reference to a Cloud Spanner instance and database
36 const instance = spanner.instance(instanceId);
37 database = instance.database(databaseId);
38
39 // Instantiates Spanner table objects
40 const singersTable = database.table('Singers');
41
42 // Inserts rows into the Singers table
43 // Note: Cloud Spanner interprets Javascript numbers as FLOAT64s.
44 // Use strings as shown in this example if you need INT64s.
45 const data = [
46 {
47 SingerId: '6',
48 FirstName: 'Elena',
49 LastName: 'Campbell',
50 },
51 {
52 SingerId: '7',
53 FirstName: 'Gabriel',
54 LastName: 'Wright',
55 },
56 {
57 SingerId: '8',
58 FirstName: 'Benjamin',
59 LastName: 'Martinez',
60 },
61 {
62 SingerId: '9',
63 FirstName: 'Hannah',
64 LastName: 'Harris',
65 },
66 ];
67
68 await singersTable.insert(data);
69 console.log('Inserted data.');
70 } catch (err) {
71 console.error('ERROR:', err);
72 } finally {
73 // Close the database when finished.
74 await database.close();

Callers 1

struct.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected