-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Spanner + Cloud Functions sample #371
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45f38b5
Add spanner sample + test to GitHub
3f33b21
Switch to arrow syntax
678af97
Address comments
f74cd88
Address further comments (from jdobry + vikask)
a62010f
Fix nit
624de84
Add package.json + convert html to plaintext
afc6a6d
Address comments
e68f8b0
Merge branch 'master' into functions-sample
jmdobry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// [START spanner_functions_quickstart] | ||
// Imports the Google Cloud client library | ||
const Spanner = require('@google-cloud/spanner'); | ||
|
||
// Instantiates a client | ||
const spanner = Spanner(); | ||
|
||
// Your Cloud Spanner instance ID | ||
const instanceId = 'my-instance'; | ||
|
||
// Your Cloud Spanner database ID | ||
const databaseId = 'my-database'; | ||
|
||
/** | ||
* HTTP Cloud Function. | ||
* | ||
* @param {Object} req Cloud Function request context. | ||
* @param {Object} res Cloud Function response context. | ||
*/ | ||
exports.get = (req, res) => { | ||
// Gets a reference to a Cloud Spanner instance and database | ||
const instance = spanner.instance(instanceId); | ||
const database = instance.database(databaseId); | ||
|
||
// The query to execute | ||
const query = { | ||
sql: 'SELECT * FROM Albums' | ||
}; | ||
|
||
// Execute the query | ||
return database.run(query) | ||
.then((results) => { | ||
const rows = results[0].map((row) => row.toJSON()); | ||
rows.forEach((row) => { | ||
res.write(`SingerId: ${row.SingerId.value}, AlbumId: ${row.AlbumId.value}, AlbumTitle: ${row.AlbumTitle}\n`); | ||
}); | ||
res | ||
.status(200) | ||
.end(); | ||
}) | ||
.catch((err) => { | ||
res | ||
.status(500) | ||
.send(`Error querying Spanner: ${err}`) | ||
.end(); | ||
}); | ||
}; | ||
// [END spanner_functions_quickstart] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "nodejs-docs-samples-functions-spanner", | ||
"version": "0.0.1", | ||
"private": true, | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"engines": { | ||
"node": ">=4.3.2" | ||
}, | ||
"scripts": { | ||
"lint": "samples lint", | ||
"pretest": "npm run lint", | ||
"test": "ava -T 20s --verbose test/*.test.js" | ||
}, | ||
"dependencies": { | ||
"@google-cloud/spanner": "0.4.2" | ||
}, | ||
"devDependencies": { | ||
"@google-cloud/nodejs-repo-tools": "1.4.7", | ||
"ava": "0.19.1", | ||
"proxyquire": "1.7.11", | ||
"sinon": "2.1.0" | ||
}, | ||
"cloud-repo-tools": { | ||
"requiresKeyFile": true, | ||
"requiresProjectId": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright 2017, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const proxyquire = require(`proxyquire`).noCallThru(); | ||
const sinon = require(`sinon`); | ||
const test = require(`ava`); | ||
|
||
const entities = [ | ||
{ | ||
SingerId: { value: 1 }, | ||
AlbumId: { value: 1 }, | ||
AlbumTitle: 'Go, Go, Go' | ||
}, | ||
{ | ||
SingerId: { value: 1 }, | ||
AlbumId: { value: 2 }, | ||
AlbumTitle: 'Total Junk' | ||
} | ||
]; | ||
|
||
const query = { | ||
sql: 'SELECT * FROM Albums' | ||
}; | ||
|
||
function getSample () { | ||
const resultsMock = entities.map((row) => { | ||
return { toJSON: sinon.stub().returns(row) }; | ||
}); | ||
const databaseMock = { | ||
run: sinon.stub().returns(Promise.resolve([resultsMock])) | ||
}; | ||
const instanceMock = { | ||
database: sinon.stub().returns(databaseMock) | ||
}; | ||
const spannerMock = { | ||
instance: sinon.stub().returns(instanceMock) | ||
}; | ||
|
||
const SpannerMock = sinon.stub().returns(spannerMock); | ||
|
||
return { | ||
program: proxyquire(`../`, { | ||
'@google-cloud/spanner': SpannerMock | ||
}), | ||
mocks: { | ||
spanner: spannerMock, | ||
database: databaseMock, | ||
instance: instanceMock, | ||
results: resultsMock, | ||
res: { | ||
status: sinon.stub().returnsThis(), | ||
send: sinon.stub().returnsThis(), | ||
end: sinon.stub().returnsThis(), | ||
write: sinon.stub().returnsThis() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
test(`get: Gets albums`, async (t) => { | ||
const sample = getSample(); | ||
const mocks = sample.mocks; | ||
|
||
await sample.program.get(mocks.req, mocks.res); | ||
t.true(mocks.spanner.instance.called); | ||
t.true(mocks.instance.database.called); | ||
t.true(mocks.database.run.calledWith(query)); | ||
t.true(mocks.results[0].toJSON.called); | ||
t.true(mocks.res.write.calledWith(`SingerId: 1, AlbumId: 2, AlbumTitle: Total Junk\n`)); | ||
t.true(mocks.res.end.called); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
async