Tài liệu tham khảo về tệp cấu hình SQL Connect

Firebase CLI cho phép bạn quản lý các dự án Firebase trong một thư mục dự án cục bộ, có thể kiểm soát phiên bản. Điều này bao gồm SQL Connect các dịch vụ trong dự án, trình kết nối cho các dịch vụ đó và các tài nguyên như lược đồ, nguồn truy vấn và đột biến cho mỗi trình kết nối. CLI cũng cho phép bạn cài đặt và vận hành trình mô phỏng Firebase SQL Connect. CLI là một giải pháp thay thế hiệu quả cho việc làm việc trong bảng điều khiển Firebase.

Để biết hướng dẫn về cách cài đặt thử nghiệm CLI Firebase cho chương trình Bản dùng thử riêng tư và các lệnh CLI liên quan đến SQL Connect, hãy xem tài liệu tham khảo về CLI Firebase.

Tài liệu tham khảo này ghi lại:

  • Các mục dành riêng cho SQL Connect trong tệp cấu hình dự án firebase.json.
  • SQL Connect cấu hình trong dataconnect.yaml và connector.yaml.
  • SQL Connect biến môi trường của trình mô phỏng.

Tệp cấu hình dự án Firebase

Tài liệu tham khảo về cấu hình firebase.json

Sử dụng các khoá dataconnect và emulators.dataconnect trong tệp firebase.json để định cấu hình một hoặc nhiều dịch vụ SQL Connect và trình mô phỏng cục bộ trong dự án của bạn:

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

Tệp firebase.json hỗ trợ các khoá SQL Connect sau:

  • dataconnect: Bắt buộc đối với các dự án SQL Connect. Chấp nhận một đối tượng hoặc một mảng đối tượng khi dự án của bạn có nhiều dịch vụ.
    • source: Bắt buộc. Đường dẫn đến thư mục chứa tệp dịch vụ dataconnect.yaml.
    • predeploy và postdeploy: Không bắt buộc. Chuỗi lệnh hoặc mảng lệnh để chạy trước hoặc sau khi triển khai tài nguyên SQL Connect.
  • emulators.dataconnect: Cấu hình không bắt buộc cho trình mô phỏng SQL Connect.
    • host: Không bắt buộc. Tên máy chủ cho trình mô phỏng. Giá trị mặc định là "localhost".
    • port: Không bắt buộc. Cổng cho trình mô phỏng SQL Connect. Giá trị mặc định là 9399.
    • postgresPort: Không bắt buộc. Cổng cho máy chủ PGlite PostgreSQL tích hợp sẵn. Giá trị mặc định là 5432. Bị bỏ qua khi biến môi trường FIREBASE_DATACONNECT_POSTGRESQL_STRING được đặt.
    • dataDir: Không bắt buộc. Thư mục nơi trình mô phỏng tự động lưu giữ dữ liệu cơ sở dữ liệu PGlite cục bộ giữa các lần chạy. Bị bỏ qua khi biến môi trường FIREBASE_DATACONNECT_POSTGRESQL_STRING được đặt.

Tài liệu tham khảo về cấu hình dataconnect.yaml

Tệp dataconnect.yaml lưu trữ thông tin cấu hình về vị trí của các nguồn giản đồ ứng dụng, nguồn trình kết nối và thông tin kết nối nguồn dữ liệu. Tệp này cũng đóng vai trò là một ký hiệu thư mục dự án cho Firebase CLI.

Khoá schemaValidation kiểm soát mức độ xác thực giản đồ được thực hiện khi giản đồ được di chuyển trong quá trình triển khai. Nếu không có giá trị nào được đặt, thì ở chế độ tương tác, lệnh firebase dataconnect:sql:migrate sẽ nhắc bạn trước khi áp dụng các thay đổi bắt buộc về khả năng tương thích, sau đó nhắc bạn trước khi chạy các thay đổi nghiêm ngặt không bắt buộc. Để biết hành vi của --force và --non-interactive, hãy xem dataconnect:sql:migrate. Khi được đặt, hành vi sẽ như sau:

  • Chế độ STRICT. Giản đồ cơ sở dữ liệu phải khớp chính xác với giản đồ ứng dụng thì giản đồ ứng dụng mới có thể được triển khai. Mọi bảng hoặc cột không được dùng trong giản đồ SQL Connect của bạn sẽ bị xoá khỏi cơ sở dữ liệu.
  • Chế độ COMPATIBLE. Giản đồ cơ sở dữ liệu phải tương thích với giản đồ ứng dụng trước khi có thể triển khai giản đồ ứng dụng; mọi thay đổi bổ sung đều được coi là không bắt buộc. Tương thích có nghĩa là quá trình di chuyển giản đồ dựa trên giản đồ ứng dụng mà bạn viết. Các phần tử trong cơ sở dữ liệu không được giản đồ ứng dụng sử dụng sẽ không được sửa đổi. Do đó, sau khi triển khai, phần phụ trợ của bạn có thể chứa các giản đồ, bảng và cột không dùng đến.

Giá trị của các khoá khác trong tệp này được giải thích trong phần nhận xét bên dưới.

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

Tệp YAML giả định cấu trúc thư mục mặc định (nhưng có thể định cấu hình) như sau:

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

Tài liệu tham khảo về cấu hình connector.yaml

Sử dụng tệp connector.yaml để định cấu hình mã nhận dạng trình kết nối và các tuỳ chọn tạo 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

Biến môi trường của trình mô phỏng

Giao diện dòng lệnh (CLI) Firebase hỗ trợ các biến môi trường ghi đè chế độ cài đặt trình mô phỏng SQL Connect cục bộ được định cấu hình trong tệp firebase.json.

  • FIREBASE_DATACONNECT_POSTGRESQL_STRING: Không bắt buộc. URI kết nối PostgreSQL sử dụng lược đồ postgres:// hoặc postgresql://, chẳng hạn như postgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable.
    • Mức độ ưu tiên: Khi được đặt thành một giá trị không trống, trình mô phỏng sẽ kết nối với máy chủ PostgreSQL được chỉ định thay vì khởi động phiên bản PGlite tích hợp sẵn. Các khoá emulators.dataconnect.postgresPort và emulators.dataconnect.dataDir trong tệp firebase.json của bạn sẽ bị bỏ qua.
    • Lựa chọn cơ sở dữ liệu: Trình mô phỏng sử dụng trường database trong schema.datasource.postgresql trong tệp dataconnect.yaml, ghi đè mọi tên cơ sở dữ liệu trong URI kết nối. Nếu bạn bỏ qua trường database, trình mô phỏng sẽ sử dụng cơ sở dữ liệu postgres.
    • Ảnh chụp nhanh và hành vi khi không thành công: Đối với SQL Connect, cờ --import, cờ --export-on-exit và lệnh firebase emulators:export không có hiệu lực; các trình mô phỏng khác vẫn nhập và xuất như bình thường. Nếu URI không hợp lệ hoặc không thể kết nối với cơ sở dữ liệu, thì trình mô phỏng sẽ không khởi động được thay vì quay lại PGlite.

Để biết các bước thiết lập, điều kiện tiên quyết của tiện ích và những điểm đánh đổi của PGlite, hãy xem bài viết Kết nối với một phiên bản PostgreSQL tuỳ chỉnh.