docs(spanner): migrate quickstart sample#4346
Conversation
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new quickstart.js script for Cloud Spanner and enables its corresponding integration test in spanner.test.js. The reviewer suggested using row.toJSON() instead of direct object logging when processing Spanner query results to ensure cleaner and more robust output representation.
| @@ -1,4 +1,4 @@ | |||
| // Copyright 2024 Google LLC | |||
| // Copyright 2026 Google LLC | |||
There was a problem hiding this comment.
please don't update the copyright year; it should always be the original date.
| @@ -0,0 +1,46 @@ | |||
| // Copyright 2026 Google LLC | |||
There was a problem hiding this comment.
if this is migrating, keep the date of the original file.
iennae
left a comment
There was a problem hiding this comment.
Going to offer up an alternative to match current sample styling for this example, please use whatever works from your point of view
'use strict';
// [START spanner_quickstart]
const {Spanner} = require('@google-cloud/spanner');
const {status} = require('@grpc/grpc-js');
// Creates a client using the ambient project ID or environment configuration
const spanner = new Spanner();
/**
* Executes a query against a Cloud Spanner database.
*
* @param {string} projectId The project ID containing the Spanner instance (for example, 'example-project-id').
* @param {string} instanceId The ID of the Spanner instance (for example, 'example-instance').
* @param {string} databaseId The ID of the Spanner database (for example 'example-database').
*/
async function quickstart(projectId, instanceId, databaseId) {
try {
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
const query = {
sql: 'SELECT 1',
};
const [rows] = await database.run(query);
console.log(`Query successfully executed. ${rows.length} row(s) found.`);
rows.forEach(row => {
console.log(` Row details: ${JSON.stringify(row)}`);
});
} catch (err) {
if (err.code === status.NOT_FOUND) {
console.error(
`Resource not found. Please verify that instance '${instanceId}' and database '${databaseId}' exist under project '${projectId}'.`
);
} else {
console.error('An unexpected error occurred during execution:', err);
}
}
}
// [END spanner_quickstart]
module.exports = {quickstart};
Migrates quickstart.js from googleapis/nodejs-spanner and enables its test suite in spanner.test.js.
|
Thanks for the review @iennae. I have addressed the comments. |
Migrates quickstart.js from googleapis/nodejs-spanner and enables its test suite in spanner.test.js.
Description
Fixes #
Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.
Checklist
npm test(see Testing)npm run lint(see Style)GoogleCloudPlatform/nodejs-docs-samples. Not a fork.