|
| 1 | +--- |
| 2 | +name: firebase-data-connect |
| 3 | +description: Build and deploy Firebase Data Connect backends with PostgreSQL. Use for schema design, GraphQL queries/mutations, authorization, and SDK generation for web, Android, iOS, and Flutter apps. |
| 4 | +--- |
| 5 | + |
| 6 | +# Firebase Data Connect |
| 7 | + |
| 8 | +Firebase Data Connect is a relational database service using Cloud SQL for PostgreSQL with GraphQL schema, auto-generated queries/mutations, and type-safe SDKs. |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +```graphql |
| 13 | +# schema.gql - Define your data model |
| 14 | +type Movie @table { |
| 15 | + id: UUID! @default(expr: "uuidV4()") |
| 16 | + title: String! |
| 17 | + releaseYear: Int |
| 18 | + genre: String |
| 19 | +} |
| 20 | + |
| 21 | +# queries.gql - Define operations |
| 22 | +query ListMovies @auth(level: PUBLIC) { |
| 23 | + movies { id title genre } |
| 24 | +} |
| 25 | + |
| 26 | +mutation CreateMovie($title: String!, $genre: String) @auth(level: USER) { |
| 27 | + movie_insert(data: { title: $title, genre: $genre }) |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Project Structure |
| 32 | + |
| 33 | +``` |
| 34 | +dataconnect/ |
| 35 | +├── dataconnect.yaml # Service configuration |
| 36 | +├── schema/ |
| 37 | +│ └── schema.gql # Data model (types with @table) |
| 38 | +└── connector/ |
| 39 | + ├── connector.yaml # Connector config + SDK generation |
| 40 | + ├── queries.gql # Queries |
| 41 | + └── mutations.gql # Mutations |
| 42 | +``` |
| 43 | + |
| 44 | +## Core Concepts |
| 45 | + |
| 46 | +| Concept | Description | |
| 47 | +|---------|-------------| |
| 48 | +| **Schema** | GraphQL types with `@table` → PostgreSQL tables | |
| 49 | +| **Connector** | Collection of queries/mutations as API endpoints | |
| 50 | +| **Generated Fields** | Auto-generated `movie`, `movies`, `movie_insert`, `movie_update`, `movie_delete` | |
| 51 | +| **Key Scalars** | `Movie_Key` type for record identification | |
| 52 | +| **@auth** | Authorization directive: `PUBLIC`, `USER`, `USER_EMAIL_VERIFIED`, `NO_ACCESS` | |
| 53 | + |
| 54 | +## Detailed References |
| 55 | + |
| 56 | +**Design your data model** → See [schema.md](reference/schema.md) |
| 57 | +- Types, @table, @col, @default directives |
| 58 | +- Relationships with @ref (one-to-one, one-to-many, many-to-many) |
| 59 | +- Data types: UUID, String, Int, Int64, Float, Boolean, Date, Timestamp, Vector |
| 60 | + |
| 61 | +**Build queries and mutations** → See [operations.md](reference/operations.md) |
| 62 | +- Generated fields and key scalars |
| 63 | +- Filtering with `where`, `orderBy`, `limit` |
| 64 | +- Relational queries with `_on_` and `_via_` syntax |
| 65 | +- Multi-step mutations with `@transaction` |
| 66 | + |
| 67 | +**Secure your operations** → See [security.md](reference/security.md) |
| 68 | +- @auth directive and access levels |
| 69 | +- CEL expressions for custom authorization |
| 70 | +- @check and @redact for data lookup authorization |
| 71 | +- Common authorization patterns and anti-patterns |
| 72 | + |
| 73 | +**Integrate with client apps** → See [sdks.md](reference/sdks.md) |
| 74 | +- Web, Android, iOS, Flutter SDK usage |
| 75 | +- SDK generation with Firebase CLI |
| 76 | +- Calling queries/mutations from client code |
| 77 | + |
| 78 | +**Configure and deploy** → See [config.md](reference/config.md) |
| 79 | +- dataconnect.yaml and connector.yaml structure |
| 80 | +- Firebase CLI commands |
| 81 | +- Local emulator setup |
| 82 | +- Deployment workflow |
| 83 | + |
| 84 | +**Advanced features** → See [advanced.md](reference/advanced.md) |
| 85 | +- Vector similarity search with Vertex AI embeddings |
| 86 | +- Full-text search with @searchable directive |
| 87 | +- Cloud Functions integration (mutation triggers) |
| 88 | +- Data seeding and bulk operations |
| 89 | + |
| 90 | +## Common Patterns |
| 91 | + |
| 92 | +### User-Owned Resources |
| 93 | + |
| 94 | +```graphql |
| 95 | +type Post @table { |
| 96 | + id: UUID! @default(expr: "uuidV4()") |
| 97 | + authorUid: String! @default(expr: "auth.uid") |
| 98 | + content: String! |
| 99 | +} |
| 100 | + |
| 101 | +mutation CreatePost($content: String!) @auth(level: USER) { |
| 102 | + post_insert(data: { authorUid_expr: "auth.uid", content: $content }) |
| 103 | +} |
| 104 | + |
| 105 | +query MyPosts @auth(level: USER) { |
| 106 | + posts(where: { authorUid: { eq_expr: "auth.uid" }}) { id content } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Many-to-Many Relationship |
| 111 | + |
| 112 | +```graphql |
| 113 | +type Movie @table { |
| 114 | + id: UUID! @default(expr: "uuidV4()") |
| 115 | + title: String! |
| 116 | +} |
| 117 | + |
| 118 | +type Actor @table { |
| 119 | + id: UUID! @default(expr: "uuidV4()") |
| 120 | + name: String! |
| 121 | +} |
| 122 | + |
| 123 | +type MovieActor @table(key: ["movie", "actor"]) { |
| 124 | + movie: Movie! |
| 125 | + actor: Actor! |
| 126 | + role: String! |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Filtered Queries |
| 131 | + |
| 132 | +```graphql |
| 133 | +query MoviesByGenre($genre: String!, $minRating: Int) @auth(level: PUBLIC) { |
| 134 | + movies( |
| 135 | + where: { genre: { eq: $genre }, rating: { ge: $minRating }}, |
| 136 | + orderBy: [{ rating: DESC }], |
| 137 | + limit: 10 |
| 138 | + ) { id title rating } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## Examples & Templates |
| 143 | + |
| 144 | +**Complete working examples** → See [examples.md](examples.md) |
| 145 | +**Ready-to-use templates** → See [templates.md](templates.md) |
| 146 | + |
| 147 | +## MCP Tools Available |
| 148 | + |
| 149 | +- `firebase_init` - Initialize Data Connect with `dataconnect` feature |
| 150 | +- `firebase_get_sdk_config` - Get Firebase configuration for client apps |
| 151 | +- `firebase_get_project` - Get current project information |
| 152 | +- `firebase_update_environment` - Set project directory and active project |
| 153 | + |
| 154 | +## CLI Commands |
| 155 | + |
| 156 | +```bash |
| 157 | +# Initialize Data Connect |
| 158 | +firebase init dataconnect |
| 159 | + |
| 160 | +# Start emulator for local development |
| 161 | +firebase emulators:start --only dataconnect |
| 162 | + |
| 163 | +# Generate SDKs |
| 164 | +firebase dataconnect:sdk:generate |
| 165 | + |
| 166 | +# Deploy to Firebase |
| 167 | +firebase deploy --only dataconnect |
| 168 | +``` |
| 169 | + |
| 170 | +## Key Directives Quick Reference |
| 171 | + |
| 172 | +| Directive | Purpose | Example | |
| 173 | +|-----------|---------|---------| |
| 174 | +| `@table` | Define PostgreSQL table | `type Movie @table { ... }` | |
| 175 | +| `@col` | Customize column name/type | `@col(name: "movie_id", dataType: "serial")` | |
| 176 | +| `@default` | Set default value | `@default(expr: "uuidV4()")` | |
| 177 | +| `@ref` | Foreign key reference | `author: User!` (implicit) or `@ref(fields: "authorId")` | |
| 178 | +| `@unique` | Unique constraint | `email: String! @unique` | |
| 179 | +| `@index` | Database index | `title: String! @index` | |
| 180 | +| `@searchable` | Enable full-text search | `title: String! @searchable` | |
| 181 | +| `@auth` | Authorization level | `@auth(level: USER)` or `@auth(expr: "auth.uid != nil")` | |
| 182 | +| `@check` | Validate field in mutation | `@check(expr: "this != null", message: "Not found")` | |
| 183 | +| `@redact` | Hide field from response | Used with @check for auth lookups | |
| 184 | +| `@transaction` | Atomic multi-step mutation | `mutation Multi @transaction { ... }` | |
0 commit comments