![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
@google-cloud/spanner
Advanced tools
@google-cloud/spanner is a Node.js client library for Google Cloud Spanner, a fully managed, scalable, globally distributed, and strongly consistent database service. This package allows developers to interact with Cloud Spanner databases, perform CRUD operations, execute SQL queries, and manage database schemas.
Connecting to a Spanner Instance
This code demonstrates how to connect to a Spanner instance using the @google-cloud/spanner package. You need to provide your Google Cloud project ID and the Spanner instance ID.
const {Spanner} = require('@google-cloud/spanner');
const spanner = new Spanner({
projectId: 'your-project-id',
});
const instance = spanner.instance('your-instance-id');
Creating a Database
This code shows how to create a new database within a Spanner instance. The `create` method returns an operation that you can wait for to ensure the database is created successfully.
const database = instance.database('your-database-id');
const [operation] = await database.create();
await operation.promise();
console.log(`Database ${database.id} created.`);
Executing a SQL Query
This code demonstrates how to execute a SQL query on a Spanner database. The `run` method executes the query and returns the result rows.
const query = {
sql: 'SELECT * FROM your-table',
};
const [rows] = await database.run(query);
rows.forEach(row => console.log(row.toJSON()));
Inserting Data
This code shows how to insert data into a table in a Spanner database. The `insert` method is used to add a new row to the specified table.
const table = database.table('your-table');
await table.insert({
column1: 'value1',
column2: 'value2',
});
console.log('Data inserted.');
Updating Data
This code demonstrates how to update existing data in a table. The `update` method is used to modify the values of specified columns.
await table.update({
column1: 'new-value1',
column2: 'new-value2',
});
console.log('Data updated.');
The `pg` package is a PostgreSQL client for Node.js. It allows you to interact with PostgreSQL databases, perform CRUD operations, and execute SQL queries. While `pg` is specific to PostgreSQL, @google-cloud/spanner is designed for Google Cloud Spanner, which offers additional features like global distribution and strong consistency.
The `mysql` package is a MySQL client for Node.js. It provides functionalities to connect to MySQL databases, execute queries, and manage database schemas. Similar to `pg`, `mysql` is specific to MySQL databases, whereas @google-cloud/spanner is tailored for Google Cloud Spanner.
Sequelize is a promise-based Node.js ORM for various SQL databases, including PostgreSQL, MySQL, SQLite, and MSSQL. It provides an abstraction layer for database operations, making it easier to work with different databases. While Sequelize supports multiple databases, @google-cloud/spanner is specifically optimized for Google Cloud Spanner.
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.
Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.
Table of contents:
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));
Samples are in the samples/
directory. The samples' README.md
has instructions for running the samples.
Sample | Source Code | Try it |
---|---|---|
Batch | source code | ![]() |
CRUD | source code | ![]() |
Datatypes | source code | ![]() |
DML | source code | ![]() |
Indexing | source code | ![]() |
Quickstart | source code | ![]() |
Schema | source code | ![]() |
Struct | source code | ![]() |
Timestamp | source code | ![]() |
Transaction | source code | ![]() |
The Cloud Spanner Node.js Client API Reference documentation also contains samples.
This library follows Semantic Versioning.
This library is considered to be General Availability (GA). This means it is 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 GA libraries are addressed with the highest priority.
More Information: Google Cloud Platform Launch Stages
Contributions welcome! See the Contributing Guide.
Apache Version 2.0
See LICENSE
FAQs
Cloud Spanner Client Library for Node.js
The npm package @google-cloud/spanner receives a total of 85,736 weekly downloads. As such, @google-cloud/spanner popularity was classified as popular.
We found that @google-cloud/spanner demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.