MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / quickstart

Function quickstart

spanner/quickstart.js:31–56  ·  view source on GitHub ↗

* 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 I

(projectId, instanceId, databaseId)

Source from the content-addressed store, hash-verified

29 * @param {string} databaseId The ID of the Spanner database (for example 'example-database').
30 */
31async function quickstart(projectId, instanceId, databaseId) {
32 try {
33 // Gets a reference to a Cloud Spanner instance and database
34 const instance = spanner.instance(instanceId);
35 const database = instance.database(databaseId);
36
37 const query = {
38 sql: 'SELECT 1',
39 };
40
41 const [rows] = await database.run(query);
42
43 console.log(`Query successfully executed. ${rows.length} row(s) found.`);
44 rows.forEach(row => {
45 console.log(` Row details: ${JSON.stringify(row)}`);
46 });
47 } catch (err) {
48 if (err.code === status.NOT_FOUND) {
49 console.error(
50 `Resource not found. Please verify that instance '${instanceId}' and database '${databaseId}' exist under project '${projectId}'.`
51 );
52 } else {
53 console.error('An unexpected error occurred during execution:', err);
54 }
55 }
56}
57// [END spanner_quickstart]
58
59if (require.main === module) {

Callers 1

quickstart.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected