| 20 | 'use strict'; |
| 21 | |
| 22 | async function main( |
| 23 | instanceId = 'my-instance', |
| 24 | databaseId = 'my-database', |
| 25 | projectId = 'my-project-id' |
| 26 | ) { |
| 27 | // [START spanner_batch_write_at_least_once] |
| 28 | |
| 29 | // Imports the Google Cloud client library |
| 30 | const {Spanner, MutationGroup} = require('@google-cloud/spanner'); |
| 31 | |
| 32 | /** |
| 33 | * TODO(developer): Uncomment the following lines before running the sample. |
| 34 | */ |
| 35 | // const instanceId = 'my-instance'; |
| 36 | // const databaseId = 'my-database'; |
| 37 | // const projectId = 'my-project-id'; |
| 38 | |
| 39 | // Creates a client |
| 40 | const spanner = new Spanner({ |
| 41 | projectId: projectId, |
| 42 | }); |
| 43 | |
| 44 | // Gets a reference to a Cloud Spanner instance and database |
| 45 | const instance = spanner.instance(instanceId); |
| 46 | const database = instance.database(databaseId); |
| 47 | |
| 48 | // Create Mutation Groups |
| 49 | /** |
| 50 | * Related mutations should be placed in a group, such as insert mutations for both a parent and a child row. |
| 51 | * A group must contain related mutations. |
| 52 | * Please see {@link https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.BatchWriteRequest.MutationGroup} |
| 53 | * for more details and examples. |
| 54 | */ |
| 55 | const mutationGroup1 = new MutationGroup(); |
| 56 | mutationGroup1.insert('Singers', { |
| 57 | SingerId: 1, |
| 58 | FirstName: 'Scarlet', |
| 59 | LastName: 'Terry', |
| 60 | }); |
| 61 | |
| 62 | const mutationGroup2 = new MutationGroup(); |
| 63 | mutationGroup2.insert('Singers', { |
| 64 | SingerId: 2, |
| 65 | FirstName: 'Marc', |
| 66 | }); |
| 67 | mutationGroup2.insert('Singers', { |
| 68 | SingerId: 3, |
| 69 | FirstName: 'Catalina', |
| 70 | LastName: 'Smith', |
| 71 | }); |
| 72 | mutationGroup2.insert('Albums', { |
| 73 | AlbumId: 1, |
| 74 | SingerId: 2, |
| 75 | AlbumTitle: 'Total Junk', |
| 76 | }); |
| 77 | mutationGroup2.insert('Albums', { |
| 78 | AlbumId: 2, |
| 79 | SingerId: 3, |