Dokumentacja pliku konfiguracji SQL Connect

Interfejs wiersza poleceń Firebase umożliwia zarządzanie projektami w Firebase w lokalnym katalogu projektu z kontrolą wersji. Obejmuje to usługi SQL Connect w Twoich projektach, oprogramowanie sprzęgające dla tych usług oraz zasoby takie jak schemat, zapytanie i źródła mutacji dla każdego oprogramowania sprzęgającego. Interfejs wiersza poleceń umożliwia też instalowanie i obsługę emulatora Firebase SQL Connect. Interfejs wiersza poleceń to wydajna alternatywa dla pracy w konsoli Firebase.

Instrukcje instalowania eksperymentalnego interfejsu wiersza poleceń Firebase w ramach programu prywatnej wersji przedpremierowej oraz polecenia interfejsu wiersza poleceń związane z SQL Connect znajdziesz w dokumentacji interfejsu wiersza poleceń Firebase.

Ten przewodnik zawiera:

  • Wpisy dotyczące SQL Connect w pliku konfiguracji projektu firebase.json.
  • Konfiguracje SQL Connect w dataconnect.yaml i connector.yaml.
  • Zmienne środowiskowe emulatora SQL Connect.

Pliki konfiguracji projektu w Firebase

Dokumentacja konfiguracji firebase.json

Użyj kluczy dataconnect i emulators.dataconnect w pliku firebase.json, aby skonfigurować co najmniej 1 usługę SQL Connect i lokalny emulator w projekcie:

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

Plik firebase.json obsługuje te klucze SQL Connect:

  • dataconnect: wymagane w przypadku projektów SQL Connect. Akceptuje pojedynczy obiekt lub tablicę obiektów, gdy projekt zawiera wiele usług.
    • source: wymagane. Ścieżka do katalogu zawierającego plik usługi dataconnect.yaml.
    • predeploy i postdeploy: opcjonalne. Ciągi poleceń lub tablice poleceń do uruchomienia przed wdrożeniem zasobów SQL Connect lub po nim.
  • emulators.dataconnect: opcjonalna konfiguracja emulatora SQL Connect.
    • host: opcjonalny. Nazwa hosta emulatora. Domyślna wartość to "localhost".
    • port: opcjonalny. Port emulatora SQL Connect. Wartość domyślna to 9399.
    • postgresPort: opcjonalny. Port wbudowanego serwera PostgreSQL PGlite. Domyślna wartość to 5432. Ignorowana, gdy ustawiona jest zmienna środowiskowa FIREBASE_DATACONNECT_POSTGRESQL_STRING.
    • dataDir: opcjonalny. Katalog, w którym emulator automatycznie przechowuje dane lokalnej bazy danych PGlite między uruchomieniami. Ignorowana, gdy ustawiona jest zmienna środowiskowa FIREBASE_DATACONNECT_POSTGRESQL_STRING.

Dokumentacja konfiguracji dataconnect.yaml

Plik dataconnect.yaml przechowuje informacje o konfiguracji lokalizacji źródeł schematów aplikacji, źródeł oprogramowania sprzęgającego i informacji o połączeniu ze źródłem danych. Plik służy też jako oznaczenie katalogu projektu dla interfejsu wiersza poleceń Firebase.

Klucz schemaValidation kontroluje poziom weryfikacji schematu wykonywanej podczas migracji schematów w trakcie wdrażania. Jeśli nie ustawisz żadnej wartości, w trybie interaktywnym polecenie firebase dataconnect:sql:migrate wyświetli prośbę o potwierdzenie przed zastosowaniem wymaganych zgodnych zmian, a następnie przed wprowadzeniem opcjonalnych rygorystycznych zmian. Informacje o działaniu --force i --non-interactive znajdziesz w artykule dataconnect:sql:migrate. Po ustawieniu tej opcji zachowanie jest następujące:

  • Tryb STRICT. Zanim będzie można wdrożyć schemat aplikacji, schemat bazy danych musi być dokładnie zgodny ze schematem aplikacji. Wszystkie tabele i kolumny, które nie są używane w schemacie SQL Connect, zostaną usunięte z bazy danych.
  • Tryb COMPATIBLE. Schemat bazy danych musi być zgodny ze schematem aplikacji, zanim będzie można wdrożyć schemat aplikacji. Wszelkie dodatkowe zmiany są uważane za opcjonalne. Zgodność oznacza, że migracje schematu są oparte na schemacie aplikacji, który napiszesz. Elementy w bazie danych, które nie są używane przez schemat aplikacji, pozostają niezmienione. Dlatego po wdrożeniu backend może zawierać nieużywane schematy, tabele i kolumny.

Wartości innych kluczy w tym pliku są wyjaśnione w komentarzach poniżej.

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

Plik YAML zakłada domyślną (ale konfigurowalną) strukturę katalogów:

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

Dokumentacja konfiguracji pliku connector.yaml

Użyj pliku connector.yaml, aby skonfigurować identyfikator oprogramowania sprzęgającego i opcje generowania pakietu 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

Zmienne środowiskowe emulatora

Interfejs wiersza poleceń Firebase obsługuje zmienne środowiskowe, które zastępują lokalne ustawienia emulatora SQL Connect skonfigurowane w pliku firebase.json.

  • FIREBASE_DATACONNECT_POSTGRESQL_STRING: opcjonalny. Identyfikator URI połączenia PostgreSQL korzystający ze schematu postgres:// lub postgresql://, np. postgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable.
    • Precedence: gdy ustawiona jest wartość niepusta, emulator łączy się z określonym serwerem PostgreSQL zamiast uruchamiać wbudowaną instancję PGlite. Klucze emulators.dataconnect.postgresPort i emulators.dataconnect.dataDir w pliku firebase.json są ignorowane.
    • Wybór bazy danych: emulator używa pola database w sekcji schema.datasource.postgresql w pliku dataconnect.yaml, zastępując dowolną nazwę bazy danych w identyfikatorze URI połączenia. Jeśli pole database zostanie pominięte, emulator użyje bazy danych postgres.
    • Migawki i zachowanie w przypadku błędu: w przypadku SQL Connect flaga --import, flaga --export-on-exit i polecenie firebase emulators:export nie mają wpływu. Inne emulatory nadal importują i eksportują dane w zwykły sposób. Jeśli identyfikator URI jest nieprawidłowy lub baza danych jest nieosiągalna, uruchomienie emulatora nie powiedzie się, zamiast powrócić do PGlite.

Instrukcje konfiguracji, wymagania wstępne dotyczące rozszerzeń i kompromisy związane z PGlite znajdziesz w artykule Łączenie się z niestandardową instancją PostgreSQL.