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 キーを使用して、1 つ以上の 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 など)。
    • 優先順位: 空でない値に設定すると、エミュレータは組み込みの PGlite インスタンスを起動する代わりに、指定された PostgreSQL サーバーに接続します。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 インスタンスに接続するをご覧ください。