Cloud Spanner: Node.js Samples
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.
Table of Contents
- Before you begin
- Samples
- Add and drop new database role
- Backups-cancel
- Copies a source backup
- Copies a source backup
- Backups-create-with-encryption-key
- Backups-create-with-multiple-kms-keys
- Backups-create
- Backups-delete
- Backups-get-database-operations
- Backups-get-operations
- Backups-get
- Backups-restore-with-encryption-key
- Backups-restore-with-multiple-kms-keys
- Backups-restore
- Backups-update
- Backups
- Batch Write
- Batch
- Creates a full backup schedule
- Creates an incremental backup schedule
- Create-instance-without-default-backup-schedules
- CRUD
- Adds split points to a database.
- Creates a new database with a specific default leader
- Database-create-with-encryption-key
- Database-create-with-multiple-kms-keys
- Database-create-with-version-retention-period
- Gets the schema definition of an existing database
- Gets the default leader option of an existing database
- Updates the default leader of an existing database
- Updates a Cloud Spanner Database.
- Datatypes
- Deletes a backup schedule
- Runs an execute sql request with directed read options
- Delete using DML returning.
- Insert using DML returning.
- Update using DML returning.
- DML
- Enable fine grained access control
- Gets a backup schedule
- Get-commit-stats
- List database roles
- Gets the instance config metadata for the configuration nam6
- Creates a new value-storing index
- Creates a new index
- Executes a read-only SQL query using an existing index.
- Reads data using an existing storing index.
- Read data using an existing index.
- Indexing
- Creates a user-managed instance configuration.
- Deletes a user-managed instance configuration.
- Lists the instance configuration operations.
- Updates a user-managed instance configuration.
- Creates a new instance partition
- Updates an instance.
- Creates a instance with asymmetric autoscaling config.
- Creates a instance with autoscaling config.
- Instance-with-processing-units
- Instance
- Json-add-column
- Json-query-parameter
- Json-update-data
- Lists backup schedules of a database
- Lists all databases on the selected instance
- Lists all the available instance configs for the selected project.
- Executes request with max commit delay
- Numeric-add-column
- Numeric-query-parameter
- Numeric-update-data
- Observability (Tracing) with OpenTelemetry using OTLP
- Observability (Tracing) with OpenTelemetry
- Adds a column to an existing table in a Spanner PostgreSQL database.
- Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database.
- Creates a PostgreSQL Database.
- Use cast operator to cast from one data type to another in a Spanner PostgreSQL database.
- Execute a batch of DML statements on a Spanner PostgreSQL database.
- Updates data in a table in a Spanner PostgreSQL database.
- Execute a Partitioned DML on a Spanner PostgreSQL database.
- Delete using DML returning on a Spanner PostgreSQL database.
- Insert using DML returning on a Spanner PostgreSQL database.
- Update using DML returning on a Spanner PostgreSQL database.
- Execute a DML statement with parameters on a Spanner PostgreSQL database.
- Calls a server side function on a Spanner PostgreSQL database.
- Creates a new storing index in a Spanner PostgreSQL database.
- Created interleaved table hierarchy using PostgreSQL dialect.
- Showcase how add a jsonb column in a PostgreSQL table.
- Showcase how query data to a jsonb column in a PostgreSQL table.
- Showcase how update data to a jsonb column in a PostgreSQL table.
- Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database.
- Showcases how a Spanner PostgreSQL database orders null values in a query.
- Execute a query with parameters on a Spanner PostgreSQL database.
- Query the information schema metadata in a Spanner PostgreSQL database.
- Alters a sequence in a PostgreSQL database.
- Creates sequence in PostgreSQL database.
- Drops a sequence in PostgreSQL database.
- Proto-query-data
- Creates a new database with a proto column and enum
- Proto-update-data-dml
- Proto-update-data
- Queryoptions
- Quickstart
- Read data with database role
- Performs a read-write transaction with read lock mode option
- Performs a read-write transaction with isolation level option
- Sets a request tag for a single query
- Run Batch update with RPC priority
- Run partitioned update with RPC priority
- Create partitions with RPC priority
- Read data with RPC Priority
- Query data with RPC Priority
- Run transaction with RPC priority
- Schema
- Alters a sequence in a GoogleSQL database.
- Creates sequence in GoogleSQL database.
- Drops a sequence in GoogleSQL database.
- Executes a read/write transaction with statement timeout
- Struct
- Alters a table with foreign key delete cascade action
- Creates a table with foreign key delete cascade action
- Drops a foreign key constraint with delete cascade action
- Timestamp
- Executes a read/write transaction with transaction and request tags
- Executes a read/write transaction with transaction timeout
- Transaction
- Updates a backup schedule
- Updates an instance.
Before you begin
Before running the samples, make sure you've followed the steps outlined in Using the client library.
cd samples
npm install
cd ..
Add and drop new database role
View the source code.
Usage:
node add-and-drop-new-database-role.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Backups-cancel
View the source code.
Usage:
node samples/backups-cancel.js
Copies a source backup
View the source code.
Usage:
node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>
Copies a source backup
View the source code.
Usage:
node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>
Backups-create-with-encryption-key
View the source code.
Usage:
node samples/backups-create-with-encryption-key.js
Backups-create-with-multiple-kms-keys
View the source code.
Usage:
node samples/backups-create-with-multiple-kms-keys.js
Backups-create
View the source code.
Usage:
node samples/backups-create.js
Backups-delete
View the source code.
Usage:
node samples/backups-delete.js
Backups-get-database-operations
View the source code.
Usage:
node samples/backups-get-database-operations.js
Backups-get-operations
View the source code.
Usage:
node samples/backups-get-operations.js
Backups-get
View the source code.
Usage:
node samples/backups-get.js
Backups-restore-with-encryption-key
View the source code.
Usage:
node samples/backups-restore-with-encryption-key.js
Backups-restore-with-multiple-kms-keys
View the source code.
Usage:
node samples/backups-restore-with-multiple-kms-keys.js
Backups-restore
View the source code.
Usage:
node samples/backups-restore.js
Backups-update
View the source code.
Usage:
node samples/backups-update.js
Backups
View the source code.
Usage:
node samples/backups.js
Batch Write
View the source code.
Usage:
node batch-write.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Batch
View the source code.
Usage:
node samples/batch.js
Creates a full backup schedule
View the source code.
Usage:
node create-full-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>
Creates an incremental backup schedule
View the source code.
Usage:
node create-incremental-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>
Create-instance-without-default-backup-schedules
View the source code.
Usage:
node samples/create-instance-without-default-backup-schedules.js
CRUD
View the source code.
Usage:
node samples/crud.js
Adds split points to a database.
View the source code.
Usage:
node database-add-split-points.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates a new database with a specific default leader
View the source code.
Usage:
node database-create-with-default-leader.js <INSTANCE_ID> <DATABASE_ID> <DEFAULT_LEADER> <PROJECT_ID>
Database-create-with-encryption-key
View the source code.
Usage:
node samples/database-create-with-encryption-key.js
Database-create-with-multiple-kms-keys
View the source code.
Usage:
node samples/database-create-with-multiple-kms-keys.js
Database-create-with-version-retention-period
View the source code.
Usage:
node samples/database-create-with-version-retention-period.js
Gets the schema definition of an existing database
View the source code.
Usage:
node database-get-ddl.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Gets the default leader option of an existing database
View the source code.
Usage:
node database-get-default-leader.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Updates the default leader of an existing database
View the source code.
Usage:
node database-update-default-leader.js <INSTANCE_ID> <DATABASE_ID> <DEFAULT_LEADER> <PROJECT_ID>
Updates a Cloud Spanner Database.
View the source code.
Usage:
node database-update.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Datatypes
View the source code.
Usage:
node samples/datatypes.js
Deletes a backup schedule
View the source code.
Usage:
node delete-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>
Runs an execute sql request with directed read options
View the source code.
Usage:
node directed-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Delete using DML returning.
View the source code.
Usage:
node dml-returning-delete.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Insert using DML returning.
View the source code.
Usage:
node dml-returning-insert.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Update using DML returning.
View the source code.
Usage:
node dml-returning-update.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
DML
View the source code.
Usage:
node samples/dml.js
Enable fine grained access control
View the source code.
Usage:
node enable-fine-grained-access.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Gets a backup schedule
View the source code.
Usage:
node get-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>
Get-commit-stats
View the source code.
Usage:
node samples/get-commit-stats.js
List database roles
View the source code.
Usage:
node get-database-roles.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Gets the instance config metadata for the configuration nam6
View the source code.
Usage:
node get-instance-config.js <PROJECT_ID>
Creates a new value-storing index
View the source code.
Usage:
node createStoringIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates a new index
View the source code.
Usage:
node createIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Executes a read-only SQL query using an existing index.
View the source code.
Usage:
node queryDataWithIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> <START_TITLE> <END_TITLE>
Reads data using an existing storing index.
View the source code.
Usage:
node readDataWithStoringIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Read data using an existing index.
View the source code.
Usage:
node readDataWithIndex <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Indexing
View the source code.
Usage:
node samples/indexing.js
Creates a user-managed instance configuration.
View the source code.
Usage:
node instance-config-create <INSTANCE_CONFIG_ID> <BASE_INSTANCE_CONFIG_ID> <PROJECT_ID>
Deletes a user-managed instance configuration.
View the source code.
Usage:
node instance-config-delete <INSTANCE_CONFIG_ID> <PROJECT_ID>
Lists the instance configuration operations.
View the source code.
Usage:
node instance-config-get-operations <PROJECT_ID>
Updates a user-managed instance configuration.
View the source code.
Usage:
node instance-config-update <INSTANCE_CONFIG_ID> <PROJECT_ID>
Creates a new instance partition
View the source code.
Usage:
node instance-partition-create.js <INSTANCE_ID> <INSTANCE_PARTITION_ID> <PROJECT_ID>
Updates an instance.
View the source code.
Usage:
node instance-update.js <INSTANCE_ID> <PROJECT_ID>
Creates a instance with asymmetric autoscaling config.
View the source code.
Usage:
node instance-with-asymmetric-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>
Creates a instance with autoscaling config.
View the source code.
Usage:
node instance-with-autoscaling-config.js <INSTANCE_ID> <PROJECT_ID>
Instance-with-processing-units
View the source code.
Usage:
node samples/instance-with-processing-units.js
Instance
View the source code.
Usage:
node samples/instance.js
Json-add-column
View the source code.
Usage:
node samples/json-add-column.js
Json-query-parameter
View the source code.
Usage:
node samples/json-query-parameter.js
Json-update-data
View the source code.
Usage:
node samples/json-update-data.js
Lists backup schedules of a database
View the source code.
Usage:
node list-backup-schedules.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID>
Lists all databases on the selected instance
View the source code.
Usage:
node list-databases.js <INSTANCE_ID> <PROJECT_ID>
Lists all the available instance configs for the selected project.
View the source code.
Usage:
node list-instance-configs.js <PROJECT_ID>
Executes request with max commit delay
View the source code.
Usage:
node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Numeric-add-column
View the source code.
Usage:
node samples/numeric-add-column.js
Numeric-query-parameter
View the source code.
Usage:
node samples/numeric-query-parameter.js
Numeric-update-data
View the source code.
Usage:
node samples/numeric-update-data.js
Observability (Tracing) with OpenTelemetry using OTLP
View the source code.
Usage:
node observability-traces-otlp.js <PROJECT-ID> <INSTANCE-ID> <DATABASE-ID>
Observability (Tracing) with OpenTelemetry
View the source code.
Usage:
node observability-traces.js <PROJECT-ID> <INSTANCE-ID> <DATABASE-ID>
Adds a column to an existing table in a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-case-sensitivity.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates a PostgreSQL Database.
View the source code.
Usage:
node pg-database-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Use cast operator to cast from one data type to another in a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-datatypes-casting.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Execute a batch of DML statements on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-batch.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Updates data in a table in a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-getting-started-update.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Execute a Partitioned DML on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-partitioned.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Delete using DML returning on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-returning-delete.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Insert using DML returning on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-returning-insert.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Update using DML returning on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-returning-update.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Execute a DML statement with parameters on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-dml-with-parameter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Calls a server side function on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-functions.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates a new storing index in a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-index-create-storing.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Created interleaved table hierarchy using PostgreSQL dialect.
View the source code.
Usage:
node pg-interleaving.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcase how add a jsonb column in a PostgreSQL table.
View the source code.
Usage:
node pg-jsonb-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcase how query data to a jsonb column in a PostgreSQL table.
View the source code.
Usage:
node pg-jsonb-query-parameter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcase how update data to a jsonb column in a PostgreSQL table.
View the source code.
Usage:
node pg-jsonb-update-data.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database.
View the source code.
Usage:
node ppg-numeric-data-type.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Showcases how a Spanner PostgreSQL database orders null values in a query.
View the source code.
Usage:
node pg-ordering-nulls.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Execute a query with parameters on a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-query-parameter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Query the information schema metadata in a Spanner PostgreSQL database.
View the source code.
Usage:
node pg-schema-information.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Alters a sequence in a PostgreSQL database.
View the source code.
Usage:
node pg-sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates sequence in PostgreSQL database.
View the source code.
Usage:
node pg-sequence-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Drops a sequence in PostgreSQL database.
View the source code.
Usage:
node pg-sequence-drop.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Proto-query-data
View the source code.
Usage:
node samples/proto-query-data.js
Creates a new database with a proto column and enum
View the source code.
Usage:
node proto-type-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Proto-update-data-dml
View the source code.
Usage:
node samples/proto-update-data-dml.js
Proto-update-data
View the source code.
Usage:
node samples/proto-update-data.js
Queryoptions
View the source code.
Usage:
node samples/queryoptions.js
Quickstart
View the source code.
Usage:
node samples/quickstart.js
Read data with database role
View the source code.
Usage:
node read-data-with-database-role.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Performs a read-write transaction with read lock mode option
View the source code.
Usage:
node read-lock-mode.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Performs a read-write transaction with isolation level option
View the source code.
Usage:
node repeatable-reads.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Sets a request tag for a single query
View the source code.
Usage:
node request-tag.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Run Batch update with RPC priority
View the source code.
Usage:
node rpc-priority-batch-dml.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Run partitioned update with RPC priority
View the source code.
Usage:
node rpc-priority-partitioned-dml.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Create partitions with RPC priority
View the source code.
Usage:
node rpc-priority-query-partitions.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Read data with RPC Priority
View the source code.
Usage:
node rpc-priority-read.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Query data with RPC Priority
View the source code.
Usage:
node rpc-priority-run.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Run transaction with RPC priority
View the source code.
Usage:
node rpc-priority-transaction.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Schema
View the source code.
Usage:
node samples/schema.js
Alters a sequence in a GoogleSQL database.
View the source code.
Usage:
node sequence-alter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates sequence in GoogleSQL database.
View the source code.
Usage:
node sequence-create.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Drops a sequence in GoogleSQL database.
View the source code.
Usage:
node sequence-drop.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Executes a read/write transaction with statement timeout
View the source code.
Usage:
node statement-timeout.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Struct
View the source code.
Usage:
node samples/struct.js
Alters a table with foreign key delete cascade action
View the source code.
Usage:
node table-alter-with-foreign-key-delete-cascade.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Creates a table with foreign key delete cascade action
View the source code.
Usage:
node table-create-with-foreign-key-delete-cascade.js.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Drops a foreign key constraint with delete cascade action
View the source code.
Usage:
node table-drop-foreign-key-constraint-delete-cascade.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Timestamp
View the source code.
Usage:
node samples/timestamp.js
Executes a read/write transaction with transaction and request tags
View the source code.
Usage:
node transaction-tag.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Executes a read/write transaction with transaction timeout
View the source code.
Usage:
node transaction-timeout.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
Transaction
View the source code.
Usage:
node samples/transaction.js
Updates a backup schedule
View the source code.
Usage:
node update-backup-schedule.js <PROJECT_ID> <INSTANCE_ID> <DATABASE_ID> <SCHEDULE_ID>
Updates an instance.
View the source code.
Usage:
node instance-update.js <INSTANCE_ID> <PROJECT_ID>
