Back to Google Cloud Node

Cloud Spanner: Node.js Client

handwritten/spanner/README.md

0.57.053.1 KB
Original Source

Cloud Spanner: Node.js Client

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

  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Cloud Spanner API.
  4. Set up authentication so you can access the API from your local workstation.

Installing the client library

bash
npm install @google-cloud/spanner

Using the client library

javascript
// 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));

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:

javascript
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 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.

Regular Sessions

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_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 on session modes and env configurations, please refer to the official documentation.

Samples

Samples are in the samples/ directory. Each sample's README.md has instructions for running its sample.

SampleSource CodeTry it
Add and drop new database rolesource code
Backups-cancelsource code
Copies a source backupsource code
Copies a source backupsource code
Backups-create-with-encryption-keysource code
Backups-create-with-multiple-kms-keyssource code
Backups-createsource code
Backups-deletesource code
Backups-get-database-operationssource code
Backups-get-operationssource code
Backups-getsource code
Backups-restore-with-encryption-keysource code
Backups-restore-with-multiple-kms-keyssource code
Backups-restoresource code
Backups-updatesource code
Backupssource code
Batch Writesource code
Batchsource code
Creates a full backup schedulesource code
Creates an incremental backup schedulesource code
Create-instance-without-default-backup-schedulessource code
CRUDsource code
Adds split points to a database.source code
Creates a new database with a specific default leadersource code
Database-create-with-encryption-keysource code
Database-create-with-multiple-kms-keyssource code
Database-create-with-version-retention-periodsource code
Gets the schema definition of an existing databasesource code
Gets the default leader option of an existing databasesource code
Updates the default leader of an existing databasesource code
Updates a Cloud Spanner Database.source code
Datatypessource code
Deletes a backup schedulesource code
Runs an execute sql request with directed read optionssource code
Delete using DML returning.source code
Insert using DML returning.source code
Update using DML returning.source code
DMLsource code
Enable fine grained access controlsource code
Gets a backup schedulesource code
Get-commit-statssource code
List database rolessource code
Gets the instance config metadata for the configuration nam6source code
Creates a new value-storing indexsource code
Creates a new indexsource code
Executes a read-only SQL query using an existing index.source code
Reads data using an existing storing index.source code
Read data using an existing index.source code
Indexingsource code
Creates a user-managed instance configuration.source code
Deletes a user-managed instance configuration.source code
Lists the instance configuration operations.source code
Updates a user-managed instance configuration.source code
Creates a new instance partitionsource code
Updates an instance.source code
Creates a instance with asymmetric autoscaling config.source code
Creates a instance with autoscaling config.source code
Instance-with-processing-unitssource code
Instancesource code
Json-add-columnsource code
Json-query-parametersource code
Json-update-datasource code
Lists backup schedules of a databasesource code
Lists all databases on the selected instancesource code
Lists all the available instance configs for the selected project.source code
Executes request with max commit delaysource code
Numeric-add-columnsource code
Numeric-query-parametersource code
Numeric-update-datasource code
Observability (Tracing) with OpenTelemetry using OTLPsource code
Observability (Tracing) with OpenTelemetrysource code
Adds a column to an existing table in a Spanner PostgreSQL database.source code
Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database.source code
Creates a PostgreSQL Database.source code
Use cast operator to cast from one data type to another in a Spanner PostgreSQL database.source code
Execute a batch of DML statements on a Spanner PostgreSQL database.source code
Updates data in a table in a Spanner PostgreSQL database.source code
Execute a Partitioned DML on a Spanner PostgreSQL database.source code
Delete using DML returning on a Spanner PostgreSQL database.source code
Insert using DML returning on a Spanner PostgreSQL database.source code
Update using DML returning on a Spanner PostgreSQL database.source code
Execute a DML statement with parameters on a Spanner PostgreSQL database.source code
Calls a server side function on a Spanner PostgreSQL database.source code
Creates a new storing index in a Spanner PostgreSQL database.source code
Created interleaved table hierarchy using PostgreSQL dialect.source code
Showcase how add a jsonb column in a PostgreSQL table.source code
Showcase how query data to a jsonb column in a PostgreSQL table.source code
Showcase how update data to a jsonb column in a PostgreSQL table.source code
Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database.source code
Showcases how a Spanner PostgreSQL database orders null values in a query.source code
Execute a query with parameters on a Spanner PostgreSQL database.source code
Query the information schema metadata in a Spanner PostgreSQL database.source code
Alters a sequence in a PostgreSQL database.source code
Creates sequence in PostgreSQL database.source code
Drops a sequence in PostgreSQL database.source code
Proto-query-datasource code
Creates a new database with a proto column and enumsource code
Proto-update-data-dmlsource code
Proto-update-datasource code
Queryoptionssource code
Quickstartsource code
Read data with database rolesource code
Performs a read-write transaction with read lock mode optionsource code
Performs a read-write transaction with isolation level optionsource code
Sets a request tag for a single querysource code
Run Batch update with RPC prioritysource code
Run partitioned update with RPC prioritysource code
Create partitions with RPC prioritysource code
Read data with RPC Prioritysource code
Query data with RPC Prioritysource code
Run transaction with RPC prioritysource code
Schemasource code
Alters a sequence in a GoogleSQL database.source code
Creates sequence in GoogleSQL database.source code
Drops a sequence in GoogleSQL database.source code
Executes a read/write transaction with statement timeoutsource code
Structsource code
Alters a table with foreign key delete cascade actionsource code
Creates a table with foreign key delete cascade actionsource code
Drops a foreign key constraint with delete cascade actionsource code
Timestampsource code
Executes a read/write transaction with transaction and request tagssource code
Executes a read/write transaction with transaction timeoutsource code
Transactionsource code
Updates a backup schedulesource code
Updates an instance.source code

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