|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +// sample-metadata: |
| 17 | +// title: Batch Write |
| 18 | +// usage: node batch-write.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> |
| 19 | + |
| 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, |
| 80 | + AlbumTitle: 'Go, Go, Go', |
| 81 | + }); |
| 82 | + |
| 83 | + const options = { |
| 84 | + transactionTag: 'batch-write-tag', |
| 85 | + }; |
| 86 | + |
| 87 | + try { |
| 88 | + database |
| 89 | + .batchWriteAtLeastOnce([mutationGroup1, mutationGroup2], options) |
| 90 | + .on('error', console.error) |
| 91 | + .on('data', response => { |
| 92 | + // Check the response code of each response to determine whether the mutation group(s) were applied successfully. |
| 93 | + if (response.status.code === 0) { |
| 94 | + console.log( |
| 95 | + `Mutation group indexes ${ |
| 96 | + response.indexes |
| 97 | + }, have been applied with commit timestamp ${Spanner.timestamp( |
| 98 | + response.commitTimestamp |
| 99 | + ).toJSON()}` |
| 100 | + ); |
| 101 | + } |
| 102 | + // Mutation groups that fail to commit trigger a response with a non-zero status code. |
| 103 | + else { |
| 104 | + console.log( |
| 105 | + `Mutation group indexes ${response.indexes}, could not be applied with error code ${response.status.code}, and error message ${response.status.message}` |
| 106 | + ); |
| 107 | + } |
| 108 | + }) |
| 109 | + .on('end', () => { |
| 110 | + console.log('Request completed successfully'); |
| 111 | + }); |
| 112 | + } catch (err) { |
| 113 | + console.log(err); |
| 114 | + } |
| 115 | + // [END spanner_batch_write_at_least_once] |
| 116 | +} |
| 117 | + |
| 118 | +process.on('unhandledRejection', err => { |
| 119 | + console.error(err.message); |
| 120 | + process.exitCode = 1; |
| 121 | +}); |
| 122 | + |
| 123 | +main(...process.argv.slice(2)); |
0 commit comments