| 59 | } |
| 60 | |
| 61 | async function insertData(instanceId, databaseId, projectId) { |
| 62 | // [START spanner_insert_data] |
| 63 | // Imports the Google Cloud client library |
| 64 | const {Spanner} = require('@google-cloud/spanner'); |
| 65 | |
| 66 | /** |
| 67 | * TODO(developer): Uncomment the following lines before running the sample. |
| 68 | */ |
| 69 | // const projectId = 'my-project-id'; |
| 70 | // const instanceId = 'my-instance'; |
| 71 | // const databaseId = 'my-database'; |
| 72 | |
| 73 | // Creates a client |
| 74 | const spanner = new Spanner({ |
| 75 | projectId: projectId, |
| 76 | }); |
| 77 | |
| 78 | // Gets a reference to a Cloud Spanner instance and database |
| 79 | const instance = spanner.instance(instanceId); |
| 80 | const database = instance.database(databaseId); |
| 81 | |
| 82 | // Instantiate Spanner table objects |
| 83 | const singersTable = database.table('Singers'); |
| 84 | const albumsTable = database.table('Albums'); |
| 85 | |
| 86 | // Inserts rows into the Singers table |
| 87 | // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so |
| 88 | // they must be converted to strings before being inserted as INT64s |
| 89 | try { |
| 90 | await singersTable.insert([ |
| 91 | {SingerId: '1', FirstName: 'Marc', LastName: 'Richards'}, |
| 92 | {SingerId: '2', FirstName: 'Catalina', LastName: 'Smith'}, |
| 93 | {SingerId: '3', FirstName: 'Alice', LastName: 'Trentor'}, |
| 94 | {SingerId: '4', FirstName: 'Lea', LastName: 'Martin'}, |
| 95 | {SingerId: '5', FirstName: 'David', LastName: 'Lomond'}, |
| 96 | ]); |
| 97 | |
| 98 | await albumsTable.insert([ |
| 99 | {SingerId: '1', AlbumId: '1', AlbumTitle: 'Total Junk'}, |
| 100 | {SingerId: '1', AlbumId: '2', AlbumTitle: 'Go, Go, Go'}, |
| 101 | {SingerId: '2', AlbumId: '1', AlbumTitle: 'Green'}, |
| 102 | {SingerId: '2', AlbumId: '2', AlbumTitle: 'Forever Hold your Peace'}, |
| 103 | {SingerId: '2', AlbumId: '3', AlbumTitle: 'Terrified'}, |
| 104 | ]); |
| 105 | |
| 106 | console.log('Inserted data.'); |
| 107 | } catch (err) { |
| 108 | console.error('ERROR:', err); |
| 109 | } finally { |
| 110 | await database.close(); |
| 111 | } |
| 112 | // [END spanner_insert_data] |
| 113 | } |
| 114 | |
| 115 | async function deleteData(instanceId, databaseId, projectId) { |
| 116 | // [START spanner_delete_data] |