SQL Connect 配置文件参考文档

借助 Firebase CLI,您可以在本地、可控制版本的项目目录中管理 Firebase 项目。这包括项目中的 SQL Connect 服务、这些服务的连接器,以及每个连接器的架构、查询和变更来源等资源。您还可以通过 CLI 安装和操作 Firebase SQL Connect 模拟器。CLI 是在 Firebase 控制台中工作的有效替代方案。

如需了解如何安装适用于非公开预览版计划的 Firebase CLI 实验以及与 SQL Connect 相关的 CLI 命令,请参阅 Firebase CLI 参考文档。

本参考指南介绍了:

  • firebase.json 项目配置文件中特定于 SQL Connect 的条目。
  • dataconnect.yaml 和 connector.yaml 中的 SQL Connect 配置。
  • SQL Connect 模拟器环境变量。

Firebase 项目配置文件

firebase.json 配置参考

使用 firebase.json 文件中的 dataconnect 和 emulators.dataconnect 键,在项目中配置一个或多个 SQL Connect 服务和本地模拟器:

{
  "dataconnect": {
    "source": "dataconnect"
  },
  "emulators": {
    "dataconnect": {
      "host": "localhost",
      "port": 9399,
      "postgresPort": 5432,
      "dataDir": "dataconnect/.dataconnect/pgliteData"
    }
  }
}

firebase.json 文件支持以下 SQL Connect 键:

  • dataconnect:SQL Connect 项目必需。如果您的项目有多个服务,则接受单个对象或对象数组。
    • source:必填。包含 dataconnect.yaml 服务文件的目录的路径。
    • predeploy 和 postdeploy:可选。 要在部署 SQL Connect 资源之前或之后运行的命令字符串或命令数组。
  • emulators.dataconnect:SQL Connect 模拟器的可选配置。
    • host:可选。模拟器的主机名。默认值为 "localhost"。
    • port:可选。SQL Connect 模拟器的端口。默认值为 9399。
    • postgresPort:可选。内置 PGlite PostgreSQL 服务器的端口。默认值为 5432。设置 FIREBASE_DATACONNECT_POSTGRESQL_STRING 环境变量后,系统会忽略此变量。
    • dataDir:可选。模拟器在运行之间自动保留本地 PGlite 数据库数据的目录。设置 FIREBASE_DATACONNECT_POSTGRESQL_STRING 环境变量后,系统会忽略此变量。

dataconnect.yaml 配置参考

dataconnect.yaml 文件存储有关应用架构来源、连接器来源和数据源连接信息的位置的配置信息。该文件还用作 Firebase CLI 的项目目录标识符。

schemaValidation 键用于控制在部署期间迁移架构时执行的架构验证级别。如果未设置任何值,在交互模式下,firebase dataconnect:sql:migrate 命令会在应用所需的兼容更改之前提示您,然后在运行可选的严格更改之前提示您。如需了解 --force 和 --non-interactive 行为,请参阅 dataconnect:sql:migrate。设置后,行为如下:

  • STRICT 模式。数据库架构必须与应用架构完全匹配,然后才能部署应用架构。SQL Connect 架构中未使用的任何表或列都将从数据库中删除。
  • COMPATIBLE 模式。数据库架构必须与应用架构兼容,然后才能部署应用架构;任何其他更改都被视为可选。“兼容”意味着架构迁移基于您编写的应用架构。数据库中未被应用架构使用的元素将保持不变。因此,部署后,您的后端可能包含未使用的架构、表和列。

此文件中其他键的值在以下注释中进行了说明。

# The top-level Firebase SQL Connect YAML file.

# The Firebase SQL Connect API version to target.
# Optional. Defaults to the latest version.
specVersion: string

# The ID of the Firebase SQL Connect service resource.
# Required.
serviceId: string

# The location of the Firebase SQL Connect service.
# Required.
location: string

# Required.
schema:
  # Relative path to the directory for schema definitions.
  # Recursively loads all .gql files in this directory.
  # Required. Typically "./schema" (set by firebase init).
  source: string
  # Datasource connection information.
  # Required.
  datasource:
    # Required.
    postgresql:
      # The name of the PostgreSQL database.
      # Required.
      database: string
      cloudSql:
        # The ID of the Cloud SQL instance resource.
        # Required. For supported PostgreSQL versions, see:
        # https://firebase.google.com/docs/sql-connect/manage-services-and-databases#supported-postgres-versions
        instanceId: string
      # Schema validation mode for schema migrations.
      # Optional. If unspecified, migrations prompt for required compatible
      # changes and then optional strict changes.
      # Set to "STRICT" or "COMPATIBLE".
      schemaValidation: string

# Required.
# Relative paths to directories for connector definitions.
# Recursively loads all .gql files in the listed directories.
# All directories specified MUST contain a connector.yaml file.
connectorDirs: [string]

YAML 文件采用默认(但可配置)目录结构:

./(project root)
   /dataconnect
      dataconnect.yaml
      /schema
        *.gql
      /example
        connector.yaml
        *.gql

connector.yaml 配置参考文档

使用 connector.yaml 文件配置连接器 ID 和 SDK 生成选项。

# The connector-level YAML file.

# Required. The connector name of the Firebase SQL Connect connector resource.
connectorId: string

# Optional. If not specified, no generated libraries (i.e. type-safe SDKs) will
# be generated.
generate:
    # Optional.
    javascriptSdk:
        # Path to the directory that will be updated with the latest generated
        # web TypeScript SDK.
        # Required.
        outputDir: string
        # Name of the Javascript package to be created.
        # Required. Example: @dataconnect/generated
        package: string
        # Path to your package.json directory. If specified, the new generated
        # SDK will be installed in this path.
        # Optional. If not provided, the package will not be auto-installed for
        # you.
        packageJsonDir: string
        # Enable React framework bindings.
        # Optional. Default to false.
        react: Boolean
        # Enable Angular framework bindings.
        # Optional. Default to false.
        angular: Boolean
        # Enable and configure client-side caching for the generated SDK.
        # Optional. If the `clientCache` key is not present, caching is disabled.
        clientCache:
            # The maximum age allowed for cached results before fetching fresh
            # values. A value of "0" means that responses are cached, but the
            # client SDK will always fetch fresh values; the cached values are
            # only used when CACHE_ONLY is specified to executeQuery() and as the
            # initial result returned from subscribe().
            # Examples: "0", "30s", "1h30m"
            # Optional. Default: "0"
            maxAge: string
            # Configures how results are to be cached.
            # Optional. Only "memory" is allowed for JavaScript SDKs.
            storage: string
    # Optional.
    dartSdk:
        # Path to the directory that will be updated with the latest generated
        # Flutter Dart SDK.
        # Required.
        outputDir: string
        # Name of the Dart package to be created.
        # Required. Example: "dataconnect_generated"
        package: string
        # Enable and configure client-side caching for the generated SDK.
        # Optional. If the `clientCache` key is not present, caching is disabled.
        clientCache:
            # The maximum age allowed for cached results before fetching fresh
            # values. A value of "0" means that responses are cached, but the
            # client SDK will always fetch fresh values; the cached values are
            # only used when CACHE_ONLY is specified to execute() and as the
            # initial result returned from subscribe().
            # Examples: "0", "30s", "1h30m"
            # Optional. Default: "0"
            maxAge: string
            # Configures whether results are to be cached in persistent storage
            # or in memory ("persistent" or "memory").
            # Optional.
            # Default: "persistent" for Android and iOS targets, "memory" for web.
            storage: string
    # Optional.
    kotlinSdk:
        # Path to the directory that will be updated with the latest generated
        # Android SDK.
        # Required.
        outputDir: string
        # Name of the package to be created.
        # Required. Example: com.google.firebase.dataconnect.generated
        package: string
        # Enable and configure client-side caching for the generated SDK.
        # Optional. If the `clientCache` key is not present, caching is disabled.
        clientCache:
            # The maximum age allowed for cached results before fetching fresh
            # values. A value of "0" means that responses are cached, but the
            # client SDK will always fetch fresh values; the cached values are
            # only used when CACHE_ONLY is specified to execute().
            # Examples: "0", "30s", "1h30m"
            # Optional. Default: "0"
            maxAge: string
            # Configures whether results are to be cached in persistent storage
            # or in memory ("persistent" or "memory").
            # Optional. Default: persistent
            storage: string
    # Optional.
    adminNodeSdk:
        # Path to the directory that will be updated with the latest generated
        # Node Admin SDK.
        # Required.
        outputDir: string
        # Path to your package.json directory. If specified, the new generated
        # SDK will be installed in this path.
        # Optional. If not provided, the package will not be auto-installed for
        # you.
        packageJsonDir: string
        # Name of the package to be created (for example: @dataconnect/admin-generated).
        # Required.
        package: string
    # Optional.
    swiftSdk:
        # Path to the directory that will be updated with the latest generated
        # iOS Swift SDK.
        # Required.
        outputDir: string
        # Name of the Swift package to be created.
        # Required. Example: "FirebaseDataConnectGenerated"
        package: string
        # Enable and configure client-side caching for the generated SDK.
        # Optional. If the `clientCache` key is not present, caching is disabled.
        clientCache:
            # The maximum age allowed for cached results before fetching fresh
            # values. A value of "0" means that responses are cached, but the
            # client SDK will always fetch fresh values; the cached values are
            # only used when CACHE_ONLY is specified to execute().
            # Examples: "0", "30s", "1h30m"
            # Optional. Default: "0"
            maxAge: string
            # Configures whether results are to be cached in persistent storage
            # or in memory ("persistent" or "memory").
            # Optional. Default: persistent
            storage: string

模拟器环境变量

Firebase CLI 支持环境变量,这些变量会替换在 firebase.json 文件中配置的本地 SQL Connect 模拟器设置。

  • FIREBASE_DATACONNECT_POSTGRESQL_STRING:可选。使用 postgres:// 或 postgresql:// 架构的 PostgreSQL 连接 URI,例如 postgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable。
    • 优先级:设置为非空值时,模拟器会连接到指定的 PostgreSQL 服务器,而不是启动内置 PGlite 实例。firebase.json 文件中的 emulators.dataconnect.postgresPort 和 emulators.dataconnect.dataDir 键会被忽略。
    • 数据库选择:模拟器会使用 dataconnect.yaml 文件中 schema.datasource.postgresql 下的 database 字段,覆盖连接 URI 中的任何数据库名称。如果省略 database 字段,模拟器会使用 postgres 数据库。
    • 快照和故障行为:对于 SQL Connect,--import 标志、--export-on-exit 标志和 firebase emulators:export 命令无效;其他模拟器仍会照常导入和导出。如果 URI 无效或数据库无法访问,模拟器启动会失败,而不会回退到 PGlite。

如需了解设置步骤、扩展程序前提条件和 PGlite 权衡,请参阅连接到自定义 PostgreSQL 实例。