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 執行個體」。