Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions spanner/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2016 Google LLC
//
// 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_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]

if (require.main === module) {
quickstart(...process.argv.slice(2));
}

module.exports = {quickstart};
7 changes: 5 additions & 2 deletions spanner/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,17 @@ describe('Autogenerated Admin Clients', () => {
});
});

describe.skip('quickstart', () => {
describe('quickstart', () => {
// Running the quickstart test in here since there's already a spanner
// instance and database set up at this point.
it('should query a table', async () => {
const output = execSync(
`node quickstart ${PROJECT_ID} ${INSTANCE_ID} ${DATABASE_ID}`
);
assert.match(output, /Query: \d+ found./);
assert.match(
output,
/Query successfully executed\. \d+ row\(s\) found\./
);
});
});

Expand Down
Loading