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

Firebase Data Connect 中,使用更改执行批量数据操作。即使您的 Data Connect 项目将数据存储在 PostgreSQL,则您无法使用 SQL 语句或 SQL 工具批量加载数据: Data Connect 服务及其架构必须与您的 数据库,以及直接在 PostgreSQL 中操作会中断此同步。

因此,Data Connect 提供标准 _insertMany, 针对种子数据和批量数据的 _upsertMany_deleteMany 变更 管理。

在构建应用原型并运行 CI/CD 流程时,您可以在本地开发环境中使用 VS Code 扩展程序、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 表中

根据您当前的原型设计阶段,您可以使用“使用入门指南”中介绍的相同方法插入一个或两个记录:也就是说,您可以使用 VS Code 扩展程序中的 Code Lens 创建 _insert 更改、硬编码数据,并在 VS Code 中运行这些更改

最后,使用 _insertMany 运算。在电影评论应用示例中,这会在 MovieActor 中插入一组初始数据。

如需执行以下更改,请使用 VS Code Firebase 扩展程序,在相应的文件编辑器视图中,点击 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 时,将数据重置为零 对一组新数据集执行一系列新测试会很有帮助。

为此,如果您的原型代码没有将记录添加到表中,请使用 Data Connect 提供的 _upsertMany 变更。

在以下示例中,使用初始值调用 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",
    }
   …
}

后续步骤