Referensi file konfigurasi SQL Connect

Dengan Firebase CLI, Anda dapat mengelola project Firebase di direktori project lokal yang dapat dikontrol versinya. Hal ini mencakup layanan SQL Connect dalam project Anda, konektor untuk layanan tersebut, dan resource seperti skema, kueri, dan sumber mutasi untuk setiap konektor. CLI juga memungkinkan Anda menginstal dan mengoperasikan emulator Firebase SQL Connect. CLI adalah alternatif yang efisien untuk bekerja di konsol Firebase.

Untuk mendapatkan petunjuk tentang cara menginstal eksperimen Firebase CLI untuk program Pratinjau Pribadi, dan perintah CLI terkait SQL Connect, lihat referensi Firebase CLI.

Panduan referensi ini mendokumentasikan:

  • Entri khusus SQL Connect dalam file konfigurasi project firebase.json Anda.
  • Konfigurasi SQL Connect di dataconnect.yaml dan connector.yaml.
  • Variabel lingkungan emulator SQL Connect.

File konfigurasi project Firebase

Referensi konfigurasi firebase.json

Gunakan kunci dataconnect dan emulators.dataconnect di file firebase.json untuk mengonfigurasi satu atau beberapa layanan SQL Connect dan emulator lokal di project Anda:

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

File firebase.json mendukung kunci SQL Connect berikut:

  • dataconnect: Diperlukan untuk project SQL Connect. Menerima satu objek atau array objek jika project Anda memiliki beberapa layanan.
    • source: Wajib diisi. Jalur ke direktori yang berisi file layanan dataconnect.yaml.
    • predeploy dan postdeploy: Opsional. String perintah atau array perintah yang akan dijalankan sebelum atau setelah men-deploy resource SQL Connect.
  • emulators.dataconnect: Konfigurasi opsional untuk emulator SQL Connect.
    • host: Opsional. Nama host untuk emulator. Nilai defaultnya adalah "localhost".
    • port: Opsional. Port untuk emulator SQL Connect. Setelan default-nya adalah 9399.
    • postgresPort: Opsional. Port untuk server PostgreSQL PGlite bawaan. Nilai defaultnya adalah 5432. Diabaikan jika variabel lingkungan FIREBASE_DATACONNECT_POSTGRESQL_STRING ditetapkan.
    • dataDir: Opsional. Direktori tempat emulator otomatis mempertahankan data database PGlite lokal di antara operasi. Diabaikan jika variabel lingkungan FIREBASE_DATACONNECT_POSTGRESQL_STRING ditetapkan.

Referensi konfigurasi dataconnect.yaml

File dataconnect.yaml menyimpan informasi konfigurasi tentang lokasi sumber skema aplikasi, sumber konektor, dan informasi koneksi sumber data. File ini juga berfungsi sebagai penanda direktori project untuk Firebase CLI.

Kunci schemaValidation mengontrol tingkat validasi skema yang dilakukan saat skema dimigrasikan selama deployment. Jika tidak ada nilai yang ditetapkan, dalam mode interaktif, perintah firebase dataconnect:sql:migrate akan meminta konfirmasi Anda sebelum menerapkan perubahan kompatibel yang diperlukan, lalu meminta konfirmasi Anda sebelum menjalankan perubahan ketat opsional. Untuk perilaku --force dan --non-interactive, lihat dataconnect:sql:migrate. Jika ditetapkan, perilakunya adalah sebagai berikut:

  • Mode STRICT. Skema database harus sama persis dengan skema aplikasi sebelum skema aplikasi dapat di-deploy. Setiap tabel atau kolom yang tidak digunakan dalam skema SQL Connect Anda akan dihapus dari database.
  • Mode COMPATIBLE. Skema database harus kompatibel dengan skema aplikasi sebelum skema aplikasi dapat di-deploy; setiap perubahan tambahan dianggap opsional. Kompatibel berarti migrasi skema didasarkan pada skema aplikasi yang Anda tulis. Elemen dalam database Anda yang tidak digunakan oleh skema aplikasi tidak akan diubah. Oleh karena itu, setelah deployment, backend Anda mungkin berisi skema, tabel, dan kolom yang tidak digunakan.

Nilai untuk kunci lain dalam file ini dijelaskan dalam komentar di bawah.

# 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]

File YAML mengasumsikan struktur direktori default (tetapi dapat dikonfigurasi) sebagai berikut:

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

Referensi konfigurasi connector.yaml

Gunakan file connector.yaml untuk mengonfigurasi ID konektor dan opsi pembuatan 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

Variabel lingkungan emulator

Firebase CLI mendukung variabel lingkungan yang mengganti setelan emulator SQL Connect lokal yang dikonfigurasi dalam file firebase.json Anda.

  • FIREBASE_DATACONNECT_POSTGRESQL_STRING: Opsional. URI koneksi PostgreSQL yang menggunakan skema postgres:// atau postgresql://, seperti postgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable.
    • Precedence: Jika disetel ke nilai yang tidak kosong, emulator akan terhubung ke server PostgreSQL yang ditentukan, bukan memulai instance PGlite bawaan. Kunci emulators.dataconnect.postgresPort dan emulators.dataconnect.dataDir dalam file firebase.json Anda akan diabaikan.
    • Pemilihan database: Emulator menggunakan kolom database di bagian schema.datasource.postgresql dalam file dataconnect.yaml, yang menggantikan nama database apa pun dalam URI koneksi. Jika kolom database dihilangkan, emulator akan menggunakan database postgres.
    • Snapshot dan perilaku kegagalan: Untuk SQL Connect, flag --import, flag --export-on-exit, dan perintah firebase emulators:export tidak berpengaruh; emulator lain masih mengimpor dan mengekspor seperti biasa. Jika URI tidak valid atau database tidak dapat dijangkau, startup emulator akan gagal, bukan beralih ke PGlite.

Untuk mengetahui langkah-langkah penyiapan, prasyarat ekstensi, dan konsekuensi PGlite, lihat Menghubungkan ke instance PostgreSQL kustom.