| 270 | } |
| 271 | |
| 272 | async function queryNestedStructField(instanceId, databaseId, projectId) { |
| 273 | // [START spanner_field_access_on_nested_struct_parameters] |
| 274 | // Imports the Google Cloud client library |
| 275 | const {Spanner} = require('@google-cloud/spanner'); |
| 276 | |
| 277 | /** |
| 278 | * TODO(developer): Uncomment the following lines before running the sample. |
| 279 | */ |
| 280 | // const projectId = 'my-project-id'; |
| 281 | // const instanceId = 'my-instance'; |
| 282 | // const databaseId = 'my-database'; |
| 283 | |
| 284 | // Creates a client |
| 285 | const spanner = new Spanner({ |
| 286 | projectId: projectId, |
| 287 | }); |
| 288 | let database; |
| 289 | try { |
| 290 | // Gets a reference to a Cloud Spanner instance and database |
| 291 | const instance = spanner.instance(instanceId); |
| 292 | database = instance.database(databaseId); |
| 293 | |
| 294 | const nameType = { |
| 295 | type: 'struct', |
| 296 | fields: [ |
| 297 | { |
| 298 | name: 'FirstName', |
| 299 | type: 'string', |
| 300 | }, |
| 301 | { |
| 302 | name: 'LastName', |
| 303 | type: 'string', |
| 304 | }, |
| 305 | ], |
| 306 | }; |
| 307 | |
| 308 | // Creates Song info STRUCT with a nested ArtistNames array |
| 309 | const songInfoType = { |
| 310 | type: 'struct', |
| 311 | fields: [ |
| 312 | { |
| 313 | name: 'SongName', |
| 314 | type: 'string', |
| 315 | }, |
| 316 | { |
| 317 | name: 'ArtistNames', |
| 318 | type: 'array', |
| 319 | child: nameType, |
| 320 | }, |
| 321 | ], |
| 322 | }; |
| 323 | |
| 324 | const songInfoStruct = Spanner.struct({ |
| 325 | SongName: 'Imagination', |
| 326 | ArtistNames: [ |
| 327 | Spanner.struct({FirstName: 'Elena', LastName: 'Campbell'}), |
| 328 | Spanner.struct({FirstName: 'Hannah', LastName: 'Harris'}), |
| 329 | ], |