播种数据并执行批量数据操作

Firebase Data Connect 中,您可以根据自己的工作流和环境,通过 不同的方式执行批量数据加载和更新:

  • 本地原型设计 中,当您尝试替代架构时,可以使用 Data Connect VS Code 扩展程序、Data Connect 模拟器和本地 数据库实例在本地开发环境中创建和调用数据 植入变更。

  • 生产环境开发中,当您使用稳定架构执行 较大的 CI/CD 流程并管理生产数据时,有两种选择:

    • 首选方法是使用 Firebase Admin SDK,这是一组在特权环境中运行的库 。

    • 您还可以将 SQL 工具与 Cloud SQL 实例搭配使用,以执行批量加载和更新,前提是您修改的是 数据,而不是 数据库架构。直接使用 SQL 工具修改数据库架构可能会 破坏您的 Data Connect 架构和连接器。

本地原型设计:在本地实例中植入数据

入门指南中,您设置了一个应用,用于 使用临时插入变更向单个表中添加单个记录。

为了能够使用,电影评论应用需要电影、评论和用户的数据,以便 原型设计查询和变更,这些查询和变更使用联接和其他操作来处理 具有真实数据的多个表。您可以扩展架构并植入数据库。

原型设计环境需要代码来执行数据植入。本指南提供了一些示例,说明了:

  • 在单个表上使用 _insertMany_upsertMany
  • 在相关表上使用 _insertMany

更新电影评论应用架构

您可以使用 _insertMany_upsertMany 变更一次更新一个数据库表,也可以更新通过联接关系关联的多个表。下面显示了一个扩展的电影评论应用架构,有助于说明这些用例和示例。它将 schema.gql 扩展到 初始 Movie 类型之外,以包含 ActorMovieActor 类型,以便我们可以 原型设计更复杂的查询。

# Actors
# Suppose an actor can participate in multiple movies and movies can have multiple actors
# Movie - Actors (or vice versa) is a many to many relationship
type Actor @table {
  id: UUID!
  imageUrl: String! 
  name: String! @col(name: "name", dataType: "varchar(30)")
}

# Join table for many-to-many relationship for movies and actors
# The 'key' param signifies the primary key(s) of this table
# In this case, the keys are [movieId, actorId], the generated fields of the reference types [movie, actor]
type MovieActor @table(key: ["movie", "actor"]) {
  # @ref creates a field in the current table (MovieActor) that holds the primary key of the referenced type
  # In this case, @ref(fields: "movieId", references: "id") is implied
  movie: Movie!
  # movieId: UUID! <- this is created by the implied @ref
  actor: Actor!
  # actorId: UUID! <- this is created by the implied @ref
  role: String! # "main" or "supporting"
}

编写变更以植入零状态数据

在原型设计期间,当您的查询和变更需要针对一系列离散值进行测试时,您可以使用多个记录填充数据。例如,您可能需要添加具有不同类型类型和评分的多个电影记录,以测试比较和过滤。

将数据植入 MovieActor 表中

根据原型设计的阶段,您可以使用入门指南中介绍的相同技术插入一两条记录:也就是说,您可以使用 Data Connect VS Code 扩展程序中的 CodeLens 创建 _insert 变更,对数据进行硬编码,然后在 VS Code 中 运行 这些变更

最终,使用 _insertMany 操作向表中添加许多记录更有意义。在电影评论应用示例中,这会在 MovieActor 中插入一组初始数据。

如需使用 Data Connect VS Code 扩展程序执行以下变更,请在 相应的文件编辑器视图中,点击 Run (Production)Run (Local) CodeLens 按钮,具体取决于您是使用 生产服务还是本地数据库进行原型设计。

# insertMany for Movie
# 2 records shown
mutation {
  movie_insertMany(data: [
    {
      id: "550e8400-e29b-41d4-a716-446655440000",
      title: "Inception",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
      genre: "sci-fi",
    },
    {
      id: "550e8400-e29b-41d4-a716-446655440001",
      title: "The Matrix",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
      genre: "action",
    }
  ])
}
# insertMany for Actor
# 2 records shown
mutation {
  actor_insertMany(data: [
    {
      id: "123e4567-e89b-12d3-a456-426614174000",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
      name: "Leonardo DiCaprio"
    },
    {
      id: "123e4567-e89b-12d3-a456-426614174001",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
      name: "Keanu Reeves"
    }
   ])
}

将数据植入 MovieActor 联接表中

如需使用联接和其他复杂操作测试查询和变更,您可以向 MovieActor 表中添加多个记录。

在这里,当您更新此类关系中的多个表时,可以添加 @transaction 指令,以确保更新正确完成。

mutation @transaction {
  movie_insertMany(data: [
    {
      id: "550e8400-e29b-41d4-a716-446655440000",
      title: "Inception",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
      genre: "sci-fi",
    },
    {
      id: "550e8400-e29b-41d4-a716-446655440001",
      title: "The Matrix",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
      genre: "action",
    }
  ])

  actor_insertMany(data: [
    {
      id: "123e4567-e89b-12d3-a456-426614174000",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fdicaprio.jpeg?alt=media&token=452e030a-efa5-4ef4-bb81-502b23241316",
      name: "Leonardo DiCaprio"
    },
    {
      id: "123e4567-e89b-12d3-a456-426614174001",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/actors%2Fkeanu.jpg?alt=media&token=6056520c-ef3e-4823-aad0-108aab163115",
      name: "Keanu Reeves"
    }
  ])
}

编写变更以重置植入数据

在原型设计和执行 CI/CD 时,将数据重置为零状态,以便针对新数据集执行一系列新测试,这可能会很有用。

为此,如果您的原型代码不会向表中添加记录,请使用 _upsertMany 变更,该变更由 Data Connect 提供。

在以下示例中,系统会使用初始值调用 movie_upsertMany,以将电影记录更新为原始状态。

mutation {
  # Execute an upsertMany operation to update the Movie table
  movie_upsertMany(data: [
    {
      id: "550e8400-e29b-41d4-a716-446655440000",
      title: "Inception",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
      genre: "sci-fi",
    },
    {
      id: "550e8400-e29b-41d4-a716-446655440001",
      title: "The Matrix",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
      genre: "action",
    }
   
}

生产环境开发:使用 Admin SDK 填充和更新

如果您想在特权 环境中工作,可以使用 Firebase Admin SDK。如果您想加载数千条记录,这是一个重要的用例,因为对生产数据执行批量数据操作至关重要。

安装 Firebase Admin SDK

即使您主要在本地工作,Firebase 也建议您设置 Admin SDK,以便您可以从特权环境(Firebase Data Connect包括本地环境)中使用。您需要为 Node.js 设置 Admin SDK

您可以详细了解如何在其他 Data Connect 用例中使用 Admin SDK。

执行生产数据的批量加载和更新

批量数据管理 API 会代表您构建 GraphQL 变更,而不是要求您使用前面介绍的 executeGraphQL API 构建 mutation {...} 字符串,以便在本地添加一些行。

管理 API 的一个主要优势是能够单独管理和重复使用 CI/CD 流程的数据数组,或为生产数据设置大型批量数据文件。

以下代码段演示了如何设置批量数据脚本。

import { initializeApp } from 'firebase-admin/app';
import { getDataConnect } from 'firebase-admin/data-connect';

const app = initializeApp();

const dc = getDataConnect({ location: "us-west2", serviceId: "my-service" });

const data = [
 {
      id: "550e8400-e29b-41d4-a716-446655440000",
      title: "Inception",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Finception.jpg?alt=media&token=07b09781-b302-4623-a5c3-1956d0143168",
      genre: "sci-fi",
  },
  {
      id: "550e8400-e29b-41d4-a716-446655440001",
      title: "The Matrix",
      imageUrl: "https://firebasestorage.googleapis.com/v0/b/fdc-quickstart-web.appspot.com/o/movies%2Fthe_matrix.jpg?alt=media&token=4975645d-fef8-409e-84a5-bcc1046e2059",
      genre: "action",
    }
];

// Methods of the bulk operations API
const resp = await dc.insert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.insertMany("movie" /*table name*/, data);

// Or
const resp = await dc.upsert("movie" /*table name*/, data[0]);
// Or
const resp = await dc.upsertMany("movie" /*table name*/, data);

生产环境开发:使用 SQL 进行批量数据更新

当您在生产环境中使用稳定架构且不修改架构时,可以在 Cloud SQL 实例中管理数据加载和更新。

如需导入数据,请参阅Cloud SQL for PostgreSQL 指南

接下来怎么做?