| 15 | 'use strict'; |
| 16 | |
| 17 | async function getCommitStats(instanceId, databaseId, projectId) { |
| 18 | // [START spanner_get_commit_stats] |
| 19 | // Imports the Google Cloud client library. |
| 20 | const {Spanner} = require('@google-cloud/spanner'); |
| 21 | |
| 22 | /** |
| 23 | * TODO(developer): Uncomment 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 | |
| 34 | // Gets a reference to a Cloud Spanner instance and database. |
| 35 | const instance = spanner.instance(instanceId); |
| 36 | const database = instance.database(databaseId); |
| 37 | |
| 38 | // Instantiate Spanner table objects. |
| 39 | const albumsTable = database.table('Albums'); |
| 40 | |
| 41 | // Updates rows in the Venues table. |
| 42 | try { |
| 43 | const [response] = await albumsTable.upsert( |
| 44 | [ |
| 45 | {SingerId: '1', AlbumId: '1', MarketingBudget: '200000'}, |
| 46 | {SingerId: '2', AlbumId: '2', MarketingBudget: '400000'}, |
| 47 | ], |
| 48 | {returnCommitStats: true} |
| 49 | ); |
| 50 | console.log( |
| 51 | `Updated data with ${response.commitStats.mutationCount} mutations.` |
| 52 | ); |
| 53 | } catch (error) { |
| 54 | console.error('Failed to get commit stats:', error.message || error); |
| 55 | } finally { |
| 56 | // Close the database when finished. |
| 57 | await database.close(); |
| 58 | } |
| 59 | // [END spanner_get_commit_stats] |
| 60 | } |
| 61 | |
| 62 | module.exports.getCommitStats = getCommitStats; |