Skip to content

docs(spanner): migrate quickstart sample#4346

Open
sakthivelmanii wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
sakthivelmanii:main
Open

docs(spanner): migrate quickstart sample#4346
sakthivelmanii wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
sakthivelmanii:main

Conversation

@sakthivelmanii

Copy link
Copy Markdown

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

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

Note: Any check with (dev), (experimental), or (legacy) can be ignored and should not block your PR from merging (see CI testing).

@sakthivelmanii sakthivelmanii requested review from a team as code owners June 17, 2026 14:59
@snippet-bot

snippet-bot Bot commented Jun 17, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Jun 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread spanner/quickstart.js Outdated
Comment thread spanner/system-test/spanner.test.js Outdated
@@ -1,4 +1,4 @@
// Copyright 2024 Google LLC
// Copyright 2026 Google LLC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't update the copyright year; it should always be the original date.

Comment thread spanner/quickstart.js Outdated
@@ -0,0 +1,46 @@
// Copyright 2026 Google LLC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is migrating, keep the date of the original file.

@iennae iennae left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@sakthivelmanii

Copy link
Copy Markdown
Author

Thanks for the review @iennae. I have addressed the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants