Skip to content

Commit

Permalink
Add Spanner stale read sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Sep 6, 2017
1 parent 6fca8cf commit 57fed2e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 32 deletions.
37 changes: 7 additions & 30 deletions spanner/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>

# Google Cloud Spanner Node.js Samples
# Cloud Spanner: Node.js Samples

[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-spanner.svg)]()
[![Build](https://storage.googleapis.com/.svg)]()

[Cloud Spanner](https://cloud.google.com/spanner/docs/) is a fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability.

Expand All @@ -18,19 +18,6 @@

## Setup

1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
1. Install dependencies:

With **npm**:

npm install

With **yarn**:

yarn install

[prereq]: ../README.md#prerequisites
[run]: ../README.md#how-to-run-a-sample

## Samples

Expand Down Expand Up @@ -70,10 +57,11 @@ __Usage:__ `node crud.js --help`

```
Commands:
update <instanceName> <databaseName> Modifies existing rows of data in an example Cloud Spanner table.
query <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table.
insert <instanceName> <databaseName> Inserts new rows of data into an example Cloud Spanner table.
read <instanceName> <databaseName> Reads data in an example Cloud Spanner table.
update <instanceName> <databaseName> Modifies existing rows of data in an example Cloud Spanner table.
query <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table.
insert <instanceName> <databaseName> Inserts new rows of data into an example Cloud Spanner table.
read <instanceName> <databaseName> Reads data in an example Cloud Spanner table.
read-stale <instanceName> <databaseName> Reads data in an example Cloud Spanner table.
Options:
--help Show help [boolean]
Expand Down Expand Up @@ -151,14 +139,3 @@ For more information, see https://cloud.google.com/spanner/docs

## Running the tests

1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables.

1. Run the tests:

With **npm**:

npm test

With **yarn**:

yarn test
50 changes: 50 additions & 0 deletions spanner/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,50 @@ function readData (instanceId, databaseId) {
// [END read_data]
}

function readStaleData (instanceId, databaseId) {
// [START read_stale_data]
// Imports the Google Cloud client library
const Spanner = require('@google-cloud/spanner');

// Instantiates a client
const spanner = Spanner();

// Uncomment these lines to specify the instance and database to use
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Read rows from the Albums table
const albumsTable = database.table('Albums');

const query = {
columns: ['SingerId', 'AlbumId', 'AlbumTitle'],
keySet: {
all: true
}
};

const options = {
// Guarantees that all writes that have committed more than 10 seconds ago
// are visible
exactStaleness: 10
};

albumsTable.read(query, options)
.then((results) => {
const rows = results[0];

rows.forEach((row) => {
const json = row.toJSON();
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}, MarketingBudget: ${json.MarketingBudget}`);
});
});
// [END read_stale_data]
}

const cli = require(`yargs`)
.demand(1)
.command(
Expand All @@ -189,6 +233,12 @@ const cli = require(`yargs`)
{},
(opts) => readData(opts.instanceName, opts.databaseName)
)
.command(
`read-stale <instanceName> <databaseName>`,
`Reads data in an example Cloud Spanner table.`,
{},
(opts) => readStaleData(opts.instanceName, opts.databaseName)
)
.example(`node $0 update "my-instance" "my-database"`)
.example(`node $0 query "my-instance" "my-database"`)
.example(`node $0 insert "my-instance" "my-database"`)
Expand Down
4 changes: 2 additions & 2 deletions spanner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.17",
"ava": "0.21.0",
"ava": "0.22.0",
"proxyquire": "1.8.0",
"sinon": "3.2.0"
"sinon": "3.2.1"
},
"cloud-repo-tools": {
"requiresKeyFile": true,
Expand Down
19 changes: 19 additions & 0 deletions spanner/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ test.serial(`should update existing rows in an example table`, async (t) => {
t.true(output.includes(`Updated data.`));
});

// read_stale_data
test.serial(`should read an example table`, (t) => {
t.plan(2);
// read-stale-data reads data that is exactly 10 seconds old. So, make sure
// 10 seconds have elapsed since the update_data test.
return new Promise((resolve, reject) => {
setTimeout(async () => {
const output = await tools.runAsync(`${crudCmd} read-stale ${INSTANCE_ID} ${DATABASE_ID}`, cwd);
try {
t.regex(output, new RegExp(`SingerId: 1, AlbumId: 1, AlbumTitle: Go, Go, Go, MarketingBudget: 100000`));
t.regex(output, new RegExp(`SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold your Peace, MarketingBudget: 500000`));
resolve();
} catch (err) {
reject(err);
}
}, 11000);
});
});

// query_data_with_new_column
test.serial(`should query an example table with an additional column and return matching rows`, async (t) => {
const output = await tools.runAsync(`${schemaCmd} queryNewColumn ${INSTANCE_ID} ${DATABASE_ID}`, cwd);
Expand Down

0 comments on commit 57fed2e

Please sign in to comment.