( instanceId = 'my-instance', databaseId = 'my-database', projectId = 'my-project-id' )
| 19 | 'use strict'; |
| 20 | |
| 21 | async function main( |
| 22 | instanceId = 'my-instance', |
| 23 | databaseId = 'my-database', |
| 24 | projectId = 'my-project-id' |
| 25 | ) { |
| 26 | // [START spanner_read_data_with_database_role] |
| 27 | /** |
| 28 | * TODO(developer): Uncomment these variables before running the sample. |
| 29 | */ |
| 30 | // const instanceId = 'my-instance'; |
| 31 | // const databaseId = 'my-database'; |
| 32 | // const projectId = 'my-project-id'; |
| 33 | // Imports the Google Cloud Spanner client library |
| 34 | const {Spanner} = require('@google-cloud/spanner'); |
| 35 | |
| 36 | // Instantiates a client |
| 37 | const spanner = new Spanner({ |
| 38 | projectId: projectId, |
| 39 | }); |
| 40 | |
| 41 | async function readDataWithDatabaseRole() { |
| 42 | // Gets a reference to a Cloud Spanner instance and database. |
| 43 | const instance = spanner.instance(instanceId); |
| 44 | // Connect to a database using the 'parent' database role. This means that the connection will only have the permissions that have explicitly been granted to the 'parent' role. |
| 45 | const options = { |
| 46 | databaseRole: 'parent', |
| 47 | }; |
| 48 | const database = instance.database(databaseId, options); |
| 49 | |
| 50 | try { |
| 51 | const query = { |
| 52 | sql: 'SELECT SingerId, FirstName, LastName FROM Singers', |
| 53 | }; |
| 54 | const [rows] = await database.run(query); |
| 55 | |
| 56 | for (const row of rows) { |
| 57 | const json = row.toJSON(); |
| 58 | |
| 59 | console.log( |
| 60 | `SingerId: ${json.SingerId}, FirstName: ${json.FirstName}, LastName: ${json.LastName}` |
| 61 | ); |
| 62 | } |
| 63 | } catch (err) { |
| 64 | console.error('Error reading data with database role:', err); |
| 65 | } finally { |
| 66 | // Close the database when finished. |
| 67 | await database.close(); |
| 68 | } |
| 69 | } |
| 70 | await readDataWithDatabaseRole(); |
| 71 | // [END spanner_read_data_with_database_role] |
| 72 | } |
| 73 | |
| 74 | main(...process.argv.slice(2)); |
no test coverage detected