firebase-data-connect
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFirebase SQL Connect
Firebase SQL Connect
Firebase SQL Connect is a relational database service using Cloud SQL for PostgreSQL with GraphQL schema, auto-generated queries/mutations, and type-safe SDKs.
[!NOTE] Product Rename: Firebase Data Connect was renamed to Firebase SQL Connect. All instructions, references, and examples in this skill repository referring to "Data Connect" or "Firebase Data Connect" apply to "SQL Connect" and "Firebase SQL Connect" as well.
Firebase SQL Connect是一项基于Cloud SQL for PostgreSQL的关系型数据库服务,支持GraphQL Schema、自动生成的查询/变更操作以及类型安全的SDK。
[!NOTE] 产品更名: Firebase Data Connect已更名为Firebase SQL Connect。本技能仓库中所有提及“Data Connect”或“Firebase Data Connect”的说明、参考资料和示例,同样适用于“SQL Connect”和“Firebase SQL Connect”。
Project Structure
项目结构
text
dataconnect/
├── dataconnect.yaml # Service configuration
├── schema/
│ └── schema.gql # Data model (types with @table)
└── connector/
├── connector.yaml # Connector config + SDK generation
├── queries.gql # Queries
└── mutations.gql # Mutationstext
dataconnect/
├── dataconnect.yaml # Service configuration
├── schema/
│ └── schema.gql # Data model (types with @table)
└── connector/
├── connector.yaml # Connector config + SDK generation
├── queries.gql # Queries
└── mutations.gql # MutationsKey Tools for Validation
验证核心工具
Rely on these two mechanisms to ensure project correctness:
- Review GraphQL Schema: Both user-defined and generated extensions (in ).
.dataconnect/schema/main/ - Validate Operations: Run against the schema.
npx -y firebase-tools@latest dataconnect:compile
依靠以下两种机制确保项目正确性:
- 审查GraphQL Schema:包括用户自定义的Schema和自动生成的扩展文件(位于目录下)。
.dataconnect/schema/main/ - 验证操作:针对Schema运行命令 。
npx -y firebase-tools@latest dataconnect:compile
Operation Strategies: GraphQL vs. Native SQL
操作策略:GraphQL vs 原生SQL
Always default to Native GraphQL. Native SQL lacks type safety and bypasses schema-enforced structures. Only use Native SQL when the user explicitly requests it or when the task requires advanced database features.
| Strategy | When to use | Implementation |
|---|---|---|
| Native GraphQL (Default) | Almost all use cases. Standard CRUD, basic filtering/sorting, simple relational joins. Requires full type safety. | Auto-generated fields ( |
| Native SQL (Advanced) | PostgreSQL extensions (e.g., PostGIS), window functions ( | Raw SQL string literals via |
默认始终使用原生GraphQL。原生SQL缺乏类型安全性,且会绕过Schema强制的结构约束。仅当用户明确要求,或任务需要高级数据库特性时,才使用原生SQL。
| 策略 | 使用场景 | 实现方式 |
|---|---|---|
| 原生GraphQL(默认) | 几乎所有使用场景。标准CRUD操作、基础过滤/排序、简单关联查询。需要完整类型安全性。 | 自动生成字段(如 |
| 原生SQL(高级) | PostgreSQL扩展(如PostGIS)、窗口函数( | 通过 |
Development Workflow
开发工作流
Follow this strict workflow to build your application. You must read the linked reference files for each step to understand the syntax and available features.
遵循以下严格工作流构建应用。您必须阅读每个步骤对应的参考文件,以了解语法和可用特性。
1. Define Data Model (schema/schema.gql
)
schema/schema.gql1. 定义数据模型(schema/schema.gql
)
schema/schema.gqlDefine your GraphQL types, tables, and relationships (which map to a Postgres schema).
Read reference/schema.md for:
,@table,@col@default- Relationships (
, one-to-many, many-to-many)@ref- Data types (UUID, Vector, JSON, etc.)
定义您的GraphQL类型、表和关系(映射到Postgres Schema)。
**阅读reference/schema.md**了解:
、@table、@col指令@default- 关系(
、一对多、多对多)@ref- 数据类型(UUID、Vector、JSON等)
2. Define Authorized Operations (connector/queries.gql
, connector/mutations.gql
)
connector/queries.gqlconnector/mutations.gql2. 定义授权操作(connector/queries.gql
、connector/mutations.gql
)
connector/queries.gqlconnector/mutations.gqlWrite the queries and mutations your client will use, including authorization logic. SQL Connect is secure by default.
Read reference/operations.md for:
- Queries: Filtering (
), Ordering (where), Pagination (orderBy/limit).offset- Mutations: Create (
), Update (_insert), Delete (_update)._delete- Upserts: Use
to "insert or update" records (CRITICAL for user profiles)._upsert- Transactions: Use
for multi-step atomic operations. Use@transactionto pass data between steps._expr: "response.<prevStep>"Read reference/security.md for authorization:
for PUBLIC, USER, or NO_ACCESS.@auth(level: ...) and@checkfor row-level security and validation.@redactRead reference/realtime.md for real-time subscriptions:
directive for time-based polling and event-driven updates.@refresh- CEL conditions to scope refresh triggers precisely.
Read reference/native_sql.md for Native SQL operations:
- Embedding raw SQL with
,_select,_selectFirst_execute- Strict rules for positional parameters (
,$1), quoting, and CTEs$2- Advanced PostgreSQL features (PostGIS, Window Functions)
编写客户端将使用的查询和变更操作,包括授权逻辑。SQL Connect默认是安全的。
**阅读reference/operations.md**了解:
- 查询:过滤(
)、排序(where)、分页(orderBy/limit)。offset- 变更:创建(
)、更新(_insert)、删除(_update)。_delete- Upsert操作:使用
实现“插入或更新”记录(对用户资料至关重要)。_upsert- 事务:使用
实现多步骤原子操作。使用@transaction在步骤间传递数据。_expr: "response.<prevStep>"**阅读reference/security.md**了解授权机制:
用于设置PUBLIC、USER或NO_ACCESS权限。@auth(level: ...) 和@check用于行级安全和验证。@redact**阅读reference/realtime.md**了解实时订阅:
指令用于基于时间的轮询和事件驱动更新。@refresh- CEL条件用于精确限定刷新触发范围。
**阅读reference/native_sql.md**了解原生SQL操作:
- 通过
、_select、_selectFirst嵌入原始SQL_execute- 位置参数(
、$1)、引号和CTE的严格规则$2- PostgreSQL高级特性(PostGIS、窗口函数)
3. Use type-safe SDK in your apps
3. 在应用中使用类型安全的SDK
Generate type-safe code for your client platform.
Configure SDK generation in :
connector.yamlyaml
connectorId: my-connector
generate:
javascriptSdk:
outputDir: "../web-app/src/lib/dataconnect"
package: "@movie-app/dataconnect"
kotlinSdk:
outputDir: "../android-app/app/src/main/kotlin/com/example/dataconnect"
package: "com.example.dataconnect"
swiftSdk:
outputDir: "../ios-app/DataConnect"Generate SDKs:
bash
npx -y firebase-tools@latest dataconnect:sdk:generateFor platform-specific instructions on how to use the generated SDKs, read:
- Web (TypeScript): reference/sdk_web.md
- Android (Kotlin): reference/sdk_android.md
- iOS (Swift): reference/sdk_ios.md
- Admin (Node.js): reference/sdk_admin_node.md
- Flutter (Dart): reference/sdk_flutter.md
为您的客户端平台生成类型安全的代码。
在中配置SDK生成:
connector.yamlyaml
connectorId: my-connector
generate:
javascriptSdk:
outputDir: "../web-app/src/lib/dataconnect"
package: "@movie-app/dataconnect"
kotlinSdk:
outputDir: "../android-app/app/src/main/kotlin/com/example/dataconnect"
package: "com.example.dataconnect"
swiftSdk:
outputDir: "../ios-app/DataConnect"生成SDK:
bash
npx -y firebase-tools@latest dataconnect:sdk:generate关于如何使用生成的SDK的平台特定说明,请阅读:
- Web(TypeScript): reference/sdk_web.md
- Android(Kotlin): reference/sdk_android.md
- iOS(Swift): reference/sdk_ios.md
- Admin(Node.js): reference/sdk_admin_node.md
- Flutter(Dart): reference/sdk_flutter.md
Feature Capability Map
功能能力映射
If you need to implement a specific feature, consult the mapped reference file:
| Feature | Reference File | Key Concepts |
|---|---|---|
| Data Modeling | reference/schema.md | |
| Vector Search | reference/advanced.md | |
| Full-Text Search | reference/advanced.md | |
| Upserting Data | reference/operations.md | |
| Complex Filters | reference/operations.md | |
| Transactions | reference/operations.md | |
| Environment Config | reference/config.md | |
| Realtime Subscriptions | reference/realtime.md | |
| Starter Templates | templates.md | CRUD, user-owned resources, many-to-many, SDK init |
如果您需要实现特定功能,请查阅对应的参考文件:
| 功能 | 参考文件 | 核心概念 |
|---|---|---|
| 数据建模 | reference/schema.md | |
| 向量搜索 | reference/advanced.md | |
| 全文搜索 | reference/advanced.md | |
| 数据Upsert | reference/operations.md | |
| 复杂过滤 | reference/operations.md | |
| 事务 | reference/operations.md | |
| 环境配置 | reference/config.md | |
| 实时订阅 | reference/realtime.md | |
| 启动模板 | templates.md | CRUD、用户自有资源、多对多关系、SDK初始化 |
Deployment & CLI
部署与CLI工具
Read reference/config.md for deep dive on configuration.
Follow these patterns based on your current task:
**阅读reference/config.md**深入了解配置详情。
根据当前任务遵循以下模式:
How to initialize SQL Connect in a Firebase project
如何在Firebase项目中初始化SQL Connect
- Understand the app idea. Ask clarification questions if unclear.
- Run .
npx -y firebase-tools@latest init dataconnect - Validate that the app template and generated SDK are setup.
- 理解应用构想。如有不清楚的地方,询问澄清问题。
- 运行命令 。
npx -y firebase-tools@latest init dataconnect - 验证应用模板和生成的SDK已正确设置。
How to build apps using SQL Connect locally
如何使用SQL Connect本地构建应用
- Start the emulator: .
npx -y firebase-tools@latest emulators:start --only dataconnect - Write schema and operations.
- Run or
npx -y firebase-tools@latest dataconnect:compileto validate them.npx -y firebase-tools@latest dataconnect:sdk:generate - Use the operations in your app and build it.
- 启动模拟器:。
npx -y firebase-tools@latest emulators:start --only dataconnect - 编写Schema和操作。
- 运行命令 或
npx -y firebase-tools@latest dataconnect:compile验证它们。npx -y firebase-tools@latest dataconnect:sdk:generate - 在应用中使用这些操作并构建应用。
How to deploy SQL Connect to Cloud SQL
如何将SQL Connect部署到Cloud SQL
- Run .
npx -y firebase-tools@latest deploy --only dataconnect
- 运行命令 。
npx -y firebase-tools@latest deploy --only dataconnect
Examples
示例
For complete, working code examples of schemas and operations, see
examples.md.
For ready-to-use starter templates (CRUD, user-owned resources, many-to-many, YAML configs, SDK init), see templates.md.
如需完整可运行的Schema和操作代码示例,请查看**examples.md**。
如需即用型启动模板(CRUD、用户自有资源、多对多关系、YAML配置、SDK初始化),请查看**templates.md**。