

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.
Read more about the client libraries for Cloud APIs, including the older
Google APIs Client Libraries, in Client Libraries Explained.
Table of contents:
Quickstart
Before you begin
Installing the client library
npm install @google-cloud/spanner
Using the client library
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner({projectId});
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
const query = {
sql: 'SELECT 1',
};
const [rows] = await database.run(query);
console.log(`Query: ${rows.length} found.`);
rows.forEach(row => console.log(row));
Metrics
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.create IAM 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.
Traces
Refer to the Observability README to know more about tracing support in the Cloud Spanner client.
Multiplexed Sessions
Spanner's Multiplexed Sessions can now be used as an efficient alternative to the default session pool. This feature helps reduce
session management overhead and minimize session-related errors. Multiplexed sessions can be enabled for all transaction types via environment variables.
Configuration
To enable this feature, set the following environment variables to true:
- For Read-Only Transactions:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS
- For Partitioned Operations:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS
- For Read-Write Transactions:
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS
GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW
For a detailed explanation of this feature, please refer to the official documentation.
Samples
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.
Supported Node.js Versions
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.
Versioning
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
Contributing
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.
License
Apache Version 2.0
See LICENSE