ITADN
googleapis/nodejs-firestore
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

Google Cloud Platform logo

Cloud Firestore: Node.js Client

release level npm version

这是 Google Cloud Firestore 的 Node.js 服务器 SDK。Google Cloud Firestore 是一款专为自动扩展、高性能和简化应用开发而构建的 NoSQL 文档数据库。

此 Cloud Firestore 服务器 SDK 使用 Google 的 Cloud Identity and Access Management 进行身份验证,并且仅应在受信任的环境中使用。您的 Cloud Identity 凭据允许您绕过所有访问限制,并提供对您 Cloud Firestore 项目中所有数据的读写访问权限。

Cloud Firestore 服务器 SDK 旨在管理您 Cloud Firestore 项目中的完整数据集,并在可靠的网络连接下表现最佳。通过这些 SDK 执行的数据操作直接访问 Cloud Firestore 后端,并且所有文档的读写操作均针对高吞吐量进行了优化。

使用 Google's 服务器 SDK 的应用程序不应在最终用户环境中使用,例如在手机上或公开托管的网站上。如果您正在开发代表最终用户访问 Cloud Firestore 的 Web 或 Node.js 应用程序,请使用 firebase 客户端 SDK。

注意: 此 Cloud Firestore 服务器 SDK 不支持在 Datastore 模式 中创建的 Firestore 数据库。要访问这些数据库,请使用 Datastore SDK

每个版本的全面更改列表可在 CHANGELOG 中找到。

有关 Cloud API 的客户端库(包括旧版 Google API 客户端库)的更多信息,请参阅 客户端库详解

目录:

快速入门

开始之前

  1. 选择或创建一个 Cloud Platform 项目
  2. 启用 Cloud Firestore API
  3. 设置身份验证,以便您可以从本地工作站 访问 API。

安装客户端库

npm install @google-cloud/firestore

使用客户端库

const {Firestore} = require('@google-cloud/firestore');

// Create a new client
const firestore = new Firestore();

async function quickstart() {
  // Obtain a document reference.
  const document = firestore.doc('posts/intro-to-firestore');

  // Enter new data into the document.
  await document.set({
    title: 'Welcome to Firestore',
    body: 'Hello World',
  });
  console.log('Entered new data into the document');

  // Update an existing document.
  await document.update({
    body: 'My first Firestore app',
  });
  console.log('Updated an existing document');

  // Read the document.
  const doc = await document.get();
  console.log('Read the document');

  // Delete the document.
  await document.delete();
  console.log('Deleted the document');
}
quickstart();

在 Pipelines 中使用客户端库


const {Firestore} = require('@google-cloud/firestore');

// Require/import Pipelines from '@google-cloud/firestore/pipelines'
const {field} = require('@google-cloud/firestore/pipelines');

// Create a new client
const firestore = new Firestore({
  projectId: 'firestore-sdk-nightly',
  databaseId: 'enterprise'
});

async function pipelinesQuickstart() {
  // Obtain a collection reference.
  const collection = firestore.collection('books');

  // Enter new documents into the document.
  await collection.add({
    "title": "Whispers of the Cobalt Sea",
    "price": 12.99,
    "author": "Elara Vance",
    "yearPublished": 2023
  });
  await collection.add({
    "title": "The Antigravity Cat's Guide to Napping",
    "price": 24.50,
    "author": "Mittens the IV",
    "yearPublished": 2026
  });
  console.log('Entered new documents into the collection.');

  // Define a Pipeline query that selects books published this century,
  // orders them by price, and computes a discounted price (20% off).
  const pipeline = firestore.pipeline().collection('books')
      .where(field('yearPublished').greaterThanOrEqual(2000))
      .sort(field('price').ascending())
      .select('title', 'author', field('price').multiply(0.8).as('discountedPrice'));

  // Execute the pipeline
  const pipelineSnapshot = await pipeline.execute();
  console.log('Executed the Pipeline.');

  console.log('Results:');
  pipelineSnapshot.results.forEach(pipelineResult=> {
    console.log(pipelineResult.data());
  });
}
pipelinesQuickstart();

示例

示例位于 samples/ 目录中。每个示例的 README.md 都包含运行该示例的说明。

示例源代码试一试
Limit-to-last-query源代码Open in Cloud Shell
Pipelines-quickstart源代码Open in Cloud Shell
Quickstart源代码Open in Cloud Shell
Solution-counters源代码Open in Cloud Shell

Cloud Firestore Node.js Client API Reference 文档 也包含示例。

支持的 Node.js 版本

我们的客户端库遵循 Node.js 发布计划。 这些库与所有当前 activemaintenance 版本的 Node.js 兼容。 如果您使用的是已停止支持的 Node.js 版本,我们建议您尽快更新 到当前受支持的 LTS 版本。

Google 的客户端库以尽力而为的方式支持旧版 Node.js 运行时,并附带以下警告:

  • 旧版版本未在持续集成中进行测试。
  • 某些安全补丁和功能无法回溯移植。
  • 依赖项无法保持最新。

针对某些已停止支持的 Node.js 版本的客户端库是可用的, 并且可以通过 npm 安装 dist-tags. dist-tags 遵循命名约定 legacy-(version). 例如,npm install @google-cloud/firestore@legacy-8 安装与 Node.js 8 兼容的 客户端库。

版本控制

本库遵循 语义化版本控制.

本库被视为稳定版本。除非绝对必要(例如由于关键安全问题)或经过 广泛的弃用期,否则代码接口不会以向后不兼容的方式更改。针对稳定库的 问题和请求将以最高优先级处理。

更多信息:Google Cloud Platform 发布阶段

贡献

欢迎贡献!请参阅 贡献指南.

请注意,本 README.mdsamples/README.md 以及本仓库中的各种配置文件(包括 .nycrctsconfig.json) 是从中央模板生成的。要编辑其中一个文件,请对其 directory 中的模板进行编辑。

许可证

Apache Version 2.0

参见 LICENSE