Skip to content

Commit dfc3639

Browse files
author
Ace Nassri
authored
Spanner samples (GoogleCloudPlatform#313)
* Add spanner samples * Remove mac cruft * Address comments
1 parent 0cc1c1f commit dfc3639

12 files changed

+2736
-97
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@google-cloud/monitoring": "0.1.4",
8383
"@google-cloud/pubsub": "0.8.0",
8484
"@google-cloud/resource": "0.6.0",
85+
"@google-cloud/spanner": "0.1.0",
8586
"@google-cloud/speech": "0.6.0",
8687
"@google-cloud/storage": "0.7.0",
8788
"@google-cloud/translate": "0.7.0",

spanner/README.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<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"/>
2+
3+
# Cloud Spanner Node.js Samples
4+
5+
[Cloud Spanner][spanner_docs] is a managed, mission-critical, globally
6+
consistent and scalable relational database service. Cloud Spanner solves the
7+
need for a horizontally-scaling database with consistent global transaction and
8+
SQL semantics.
9+
10+
## Table of Contents
11+
12+
* [Setup](#setup)
13+
* [Samples](#samples)
14+
* [Getting started with Google Cloud Spanner API](#getting-started-with-google-cloud-spanner-api)
15+
16+
## Setup
17+
18+
1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
19+
1. Install dependencies:
20+
21+
npm install
22+
23+
[prereq]: ../README.md#prerequisities
24+
[run]: ../README.md#how-to-run-a-sample
25+
26+
## Samples
27+
28+
### Getting started with Google Cloud Spanner API
29+
30+
View the [Spanner documentation][spanner_docs] or the [samples][spanner_samples].
31+
32+
__Run the samples:__
33+
34+
```sh
35+
node schema.js --help
36+
```
37+
38+
```
39+
Commands:
40+
createDatabase <instanceName> <databaseName> Creates an example database with two tables in a Cloud Spanner instance.
41+
addColumn <instanceName> <databaseName> Adds an example MarketingBudget column to an example Cloud Spanner
42+
table.
43+
queryNewColumn <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table
44+
with an additional column (MarketingBudget) added by addColumn.
45+
46+
Options:
47+
--help Show help [boolean]
48+
49+
Examples:
50+
node schema.js createDatabase "my-instance" "my-database"
51+
node schema.js addColumn "my-instance" "my-database"
52+
node schema.js queryNewColumn "my-instance" "my-database"
53+
54+
For more information, see https://cloud.google.com/spanner/docs
55+
```
56+
57+
```sh
58+
node crud.js --help
59+
```
60+
61+
```
62+
Commands:
63+
update <instanceName> <databaseName> Modifies existing rows of data in an example Cloud Spanner table.
64+
query <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner table.
65+
insert <instanceName> <databaseName> Inserts new rows of data into an example Cloud Spanner table.
66+
read <instanceName> <databaseName> Reads data in an example Cloud Spanner table.
67+
68+
Options:
69+
--help Show help [boolean]
70+
71+
Examples:
72+
node crud.js update "my-instance" "my-database"
73+
node crud.js query "my-instance" "my-database"
74+
node crud.js insert "my-instance" "my-database"
75+
node crud.js read "my-instance" "my-database"
76+
77+
For more information, see https://cloud.google.com/spanner/docs
78+
```
79+
80+
```sh
81+
node indexing.js --help
82+
```
83+
84+
```
85+
Commands:
86+
createIndex <instanceName> <databaseName> Creates a new index in an example Cloud Spanner table.
87+
createStoringIndex <instanceName> <databaseName> Creates a new value-storing index in an example Cloud Spanner table.
88+
queryIndex <instanceName> <databaseName> Executes a read-only SQL query against an example Cloud Spanner
89+
table using an existing index.
90+
readIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
91+
index.
92+
readStoringIndex <instanceName> <databaseName> Reads data from an example Cloud Spanner table using an existing
93+
storing index.
94+
95+
Options:
96+
--help Show help [boolean]
97+
98+
Examples:
99+
node indexing.js createIndex "my-instance" "my-database"
100+
node indexing.js createStoringIndex "my-instance" "my-database"
101+
node indexing.js queryIndex "my-instance" "my-database"
102+
node indexing.js readIndex "my-instance" "my-database"
103+
node indexing.js readStoringIndex "my-instance" "my-database"
104+
105+
For more information, see https://cloud.google.com/spanner/docs
106+
```
107+
108+
```sh
109+
node transaction.js --help
110+
```
111+
112+
```
113+
Commands:
114+
readOnly <instanceName> <databaseName> Execute a read-only transaction on an example Cloud Spanner table.
115+
readWrite <instanceName> <databaseName> Execute a read-write transaction on an example Cloud Spanner table.
116+
117+
Options:
118+
--help Show help [boolean]
119+
120+
Examples:
121+
node transaction.js readOnly "my-instance" "my-database"
122+
node transaction.js readWrite "my-instance" "my-database"
123+
124+
For more information, see https://cloud.google.com/spanner/docs
125+
```
126+
127+
For more information, see [the docs][spanner_docs].
128+
129+
[spanner_samples]: ../
130+
[spanner_docs]: https://cloud.google.com/spanner/docs/

spanner/crud.js

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function updateData (instanceId, databaseId) {
19+
// [START update_data]
20+
// Imports the Google Cloud client library
21+
const Spanner = require('@google-cloud/spanner');
22+
23+
// Instantiates a client
24+
const spanner = Spanner();
25+
26+
// Uncomment these lines to specify the instance and database to use
27+
// const instanceId = 'my-instance';
28+
// const databaseId = 'my-database';
29+
30+
// Gets a reference to a Cloud Spanner instance and database
31+
const instance = spanner.instance(instanceId);
32+
const database = instance.database(databaseId);
33+
34+
// Update a row in the Albums table
35+
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they
36+
// must be converted to strings before being inserted as INT64s
37+
const albumsTable = database.table('Albums');
38+
39+
albumsTable.update([
40+
{ SingerId: '1', AlbumId: '1', MarketingBudget: '100000' },
41+
{ SingerId: '2', AlbumId: '2', MarketingBudget: '500000' }
42+
])
43+
.then(() => {
44+
console.log('Updated data.');
45+
});
46+
// [END update_data]
47+
}
48+
49+
function insertData (instanceId, databaseId) {
50+
// [START insert_data]
51+
// Imports the Google Cloud client library
52+
const Spanner = require('@google-cloud/spanner');
53+
54+
// Instantiates a client
55+
const spanner = Spanner();
56+
57+
// Uncomment these lines to specify the instance and database to use
58+
// const instanceId = 'my-instance';
59+
// const databaseId = 'my-database';
60+
61+
// Gets a reference to a Spanner instance and database
62+
const instance = spanner.instance(instanceId);
63+
const database = instance.database(databaseId);
64+
65+
// Instantiate Spanner table objects
66+
const singersTable = database.table('Singers');
67+
const albumsTable = database.table('Albums');
68+
69+
Promise.all([
70+
// Inserts rows into the Singers table
71+
// Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so
72+
// they must be converted to strings before being inserted as INT64s
73+
singersTable.insert([
74+
{ SingerId: '1', FirstName: 'Marc', LastName: 'Richards' },
75+
{ SingerId: '2', FirstName: 'Catalina', LastName: 'Smith' },
76+
{ SingerId: '3', FirstName: 'Alice', LastName: 'Trentor' },
77+
{ SingerId: '4', FirstName: 'Lea', LastName: 'Martin' },
78+
{ SingerId: '5', FirstName: 'David', LastName: 'Lomond' }
79+
]),
80+
81+
// Inserts rows into the Albums table
82+
albumsTable.insert([
83+
{ SingerId: '1', AlbumId: '1', AlbumTitle: 'Go, Go, Go' },
84+
{ SingerId: '1', AlbumId: '2', AlbumTitle: 'Total Junk' },
85+
{ SingerId: '2', AlbumId: '1', AlbumTitle: 'Green' },
86+
{ SingerId: '2', AlbumId: '2', AlbumTitle: 'Forever Hold your Peace' },
87+
{ SingerId: '2', AlbumId: '3', AlbumTitle: 'Terrified' }
88+
])
89+
])
90+
.then(() => {
91+
console.log('Inserted data.');
92+
});
93+
// [END insert_data]
94+
}
95+
96+
function queryData (instanceId, databaseId) {
97+
// [START query_data]
98+
// Imports the Google Cloud client library
99+
const Spanner = require('@google-cloud/spanner');
100+
101+
// Instantiates a client
102+
const spanner = Spanner();
103+
104+
// Uncomment these lines to specify the instance and database to use
105+
// const instanceId = 'my-instance';
106+
// const databaseId = 'my-database';
107+
108+
// Gets a reference to a Cloud Spanner instance and database
109+
const instance = spanner.instance(instanceId);
110+
const database = instance.database(databaseId);
111+
112+
const query = {
113+
sql: 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums'
114+
};
115+
116+
// Queries rows from the Albums table
117+
database.run(query)
118+
.then((results) => {
119+
const rows = results[0];
120+
121+
rows.forEach((row) => {
122+
const json = row.toJSON();
123+
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`);
124+
});
125+
});
126+
// [END query_data]
127+
}
128+
129+
function readData (instanceId, databaseId) {
130+
// [START read_data]
131+
// Imports the Google Cloud client library
132+
const Spanner = require('@google-cloud/spanner');
133+
134+
// Instantiates a client
135+
const spanner = Spanner();
136+
137+
// Uncomment these lines to specify the instance and database to use
138+
// const instanceId = 'my-instance';
139+
// const databaseId = 'my-database';
140+
141+
// Gets a reference to a Cloud Spanner instance and database
142+
const instance = spanner.instance(instanceId);
143+
const database = instance.database(databaseId);
144+
145+
// Read rows from the Albums table
146+
const albumsTable = database.table('Albums');
147+
148+
const query = {
149+
columns: ['SingerId', 'AlbumId', 'AlbumTitle'],
150+
keySet: {
151+
all: true
152+
}
153+
};
154+
155+
albumsTable.read(query)
156+
.then((results) => {
157+
const rows = results[0];
158+
159+
rows.forEach((row) => {
160+
const json = row.toJSON();
161+
console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`);
162+
});
163+
});
164+
// [END read_data]
165+
}
166+
167+
const cli = require(`yargs`)
168+
.demand(1)
169+
.command(
170+
`update <instanceName> <databaseName>`,
171+
`Modifies existing rows of data in an example Cloud Spanner table.`,
172+
{},
173+
(opts) => updateData(opts.instanceName, opts.databaseName)
174+
)
175+
.command(
176+
`query <instanceName> <databaseName>`,
177+
`Executes a read-only SQL query against an example Cloud Spanner table.`,
178+
{},
179+
(opts) => queryData(opts.instanceName, opts.databaseName)
180+
)
181+
.command(
182+
`insert <instanceName> <databaseName>`,
183+
`Inserts new rows of data into an example Cloud Spanner table.`,
184+
{},
185+
(opts) => insertData(opts.instanceName, opts.databaseName)
186+
)
187+
.command(
188+
`read <instanceName> <databaseName>`,
189+
`Reads data in an example Cloud Spanner table.`,
190+
{},
191+
(opts) => readData(opts.instanceName, opts.databaseName)
192+
)
193+
.example(`node $0 update "my-instance" "my-database"`)
194+
.example(`node $0 query "my-instance" "my-database"`)
195+
.example(`node $0 insert "my-instance" "my-database"`)
196+
.example(`node $0 read "my-instance" "my-database"`)
197+
.wrap(120)
198+
.recommendCommands()
199+
.epilogue(`For more information, see https://cloud.google.com/spanner/docs`);
200+
201+
if (module === require.main) {
202+
cli.help().strict().argv;
203+
}

0 commit comments

Comments
 (0)