|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function quickstart( |
| 18 | + projectId = 'YOUR-PROJECT-ID', // Your Google Cloud Platform project ID |
| 19 | + instanceId = 'my-instance', // Your Cloud Spanner instance ID |
| 20 | + databaseId = 'my-database', // Your Cloud Spanner database ID |
| 21 | +) { |
| 22 | + // [START spanner_quickstart] |
| 23 | + // Imports the Google Cloud client library |
| 24 | + const {Spanner} = require('@google-cloud/spanner'); |
| 25 | + |
| 26 | + // Creates a client |
| 27 | + const spanner = new Spanner({projectId}); |
| 28 | + |
| 29 | + // Gets a reference to a Cloud Spanner instance and database |
| 30 | + const instance = spanner.instance(instanceId); |
| 31 | + const database = instance.database(databaseId); |
| 32 | + |
| 33 | + // The query to execute |
| 34 | + const query = { |
| 35 | + sql: 'SELECT 1', |
| 36 | + }; |
| 37 | + |
| 38 | + // Execute a simple SQL statement |
| 39 | + const [rows] = await database.run(query); |
| 40 | + console.log(`Query: ${rows.length} found.`); |
| 41 | + rows.forEach(row => console.log(row)); |
| 42 | + // [END spanner_quickstart] |
| 43 | +} |
| 44 | + |
| 45 | +const args = process.argv.slice(2); |
| 46 | +quickstart(...args).catch(console.error); |
0 commit comments