Cloud Spanner is a fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability.
A comprehensive list of changes in each version may be found in the CHANGELOG.
- Cloud Spanner Node.js Client API Reference
- Cloud Spanner Documentation
- github.com/googleapis/nodejs-spanner
Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.
Table of contents:
- Select or create a Cloud Platform project.
- Enable billing for your project.
- Enable the Cloud Spanner API.
- Set up authentication so you can access the API from your local workstation.
npm install @google-cloud/spanner// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');
// Creates a client
const spanner = new Spanner({projectId});
// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
// The query to execute
const query = {
sql: 'SELECT 1',
};
// Execute a simple SQL statement
const [rows] = await database.run(query);
console.log(`Query: ${rows.length} found.`);
rows.forEach(row => console.log(row));Cloud Spanner client supports client-side metrics that you can use along with server-side metrics to optimize performance and troubleshoot performance issues if they occur.
Client-side metrics are measured from the time a request leaves your application to the time your application receives the response. In contrast, server-side metrics are measured from the time Spanner receives a request until the last byte of data is sent to the client.
These metrics are enabled by default. You can opt out of using client-side metrics with the following code:
const spanner = new Spanner({
disableBuiltInMetrics: true
});You can also disable these metrics by setting SPANNER_DISABLE_BUILTIN_METRICS to true.
Note: Client-side metrics needs
monitoring.timeSeries.createIAM permission to export metrics data. Ask your administrator to grant your service account the Monitoring Metric Writer (roles/monitoring.metricWriter) IAM role on the project.
Refer to the Observability README to know more about tracing support in the Cloud Spanner client.
Spanner's Multiplexed Sessions is now default enabled session mode in node client. This feature helps reduce session management overhead and minimize session-related errors.
For a detailed explanation on multiplexed sessions, please refer to the official documentation.
To use regular sessions, disable the multiplexed sessions and set the following environment variables to false:
- For Read-Only Transactions:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS
- For Partitioned Operations:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONSGOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS
- For Read-Write Transactions:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONSGOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
For a detailed explanation on session modes and env configurations, please refer to the official documentation.
Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.
The Cloud Spanner Node.js Client API Reference documentation also contains samples.
Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js. If you are using an end-of-life version of Node.js, we recommend that you update as soon as possible to an actively supported LTS version.
Google's client libraries support legacy versions of Node.js runtimes on a best-efforts basis with the following warnings:
- Legacy versions are not tested in continuous integration.
- Some security patches and features cannot be backported.
- Dependencies cannot be kept up-to-date.
Client libraries targeting some end-of-life versions of Node.js are available, and
can be installed through npm dist-tags.
The dist-tags follow the naming convention legacy-(version).
For example, npm install @google-cloud/spanner@legacy-8 installs client libraries
for versions compatible with Node.js 8.
This library follows Semantic Versioning.
This library is considered to be stable. The code surface will not change in backwards-incompatible ways unless absolutely necessary (e.g. because of critical security issues) or with an extensive deprecation period. Issues and requests against stable libraries are addressed with the highest priority.
More Information: Google Cloud Platform Launch Stages
Contributions welcome! See the Contributing Guide.
Please note that this README.md, the samples/README.md,
and a variety of configuration files in this repository (including .nycrc and tsconfig.json)
are generated from a central template. To edit one of these files, make an edit
to its templates in
directory.
Apache Version 2.0
See LICENSE

