| 278 | } |
| 279 | |
| 280 | async function readStaleData(instanceId, databaseId, projectId) { |
| 281 | // [START spanner_read_stale_data] |
| 282 | // Imports the Google Cloud client library |
| 283 | const {Spanner} = require('@google-cloud/spanner'); |
| 284 | |
| 285 | /** |
| 286 | * TODO(developer): Uncomment the following lines before running the sample. |
| 287 | */ |
| 288 | // const projectId = 'my-project-id'; |
| 289 | // const instanceId = 'my-instance'; |
| 290 | // const databaseId = 'my-database'; |
| 291 | |
| 292 | // Creates a client |
| 293 | const spanner = new Spanner({ |
| 294 | projectId: projectId, |
| 295 | }); |
| 296 | |
| 297 | // Gets a reference to a Cloud Spanner instance and database |
| 298 | const instance = spanner.instance(instanceId); |
| 299 | const database = instance.database(databaseId); |
| 300 | |
| 301 | // Reads rows from the Albums table |
| 302 | const albumsTable = database.table('Albums'); |
| 303 | |
| 304 | const query = { |
| 305 | columns: ['SingerId', 'AlbumId', 'AlbumTitle', 'MarketingBudget'], |
| 306 | keySet: { |
| 307 | all: true, |
| 308 | }, |
| 309 | }; |
| 310 | |
| 311 | const options = { |
| 312 | // Guarantees that all writes committed more than 15000 milliseconds ago are visible |
| 313 | exactStaleness: 15000, |
| 314 | }; |
| 315 | |
| 316 | try { |
| 317 | const [rows] = await albumsTable.read(query, options); |
| 318 | |
| 319 | rows.forEach(row => { |
| 320 | const json = row.toJSON(); |
| 321 | const id = json.SingerId; |
| 322 | const album = json.AlbumId; |
| 323 | const title = json.AlbumTitle; |
| 324 | const budget = json.MarketingBudget ? json.MarketingBudget : ''; |
| 325 | console.log( |
| 326 | `SingerId: ${id}, AlbumId: ${album}, AlbumTitle: ${title}, MarketingBudget: ${budget}` |
| 327 | ); |
| 328 | }); |
| 329 | } catch (err) { |
| 330 | console.error('ERROR:', err); |
| 331 | } finally { |
| 332 | // Close the database when finished. |
| 333 | await database.close(); |
| 334 | } |
| 335 | // [END spanner_read_stale_data] |
| 336 | } |
| 337 | |