|
| 1 | +--- |
| 2 | +title: Firebase Data Connect |
| 3 | +--- |
| 4 | + |
| 5 | +Firebase Data Connect is a relational database service for mobile and web apps that lets you build and scale using a fully-managed PostgreSQL database powered by Cloud SQL. It provides secure schema, query and mutation management using GraphQL technology that integrates well with Firebase Authentication. |
| 6 | + |
| 7 | +To get started, ensure you have setup your Firebase project and have the Data Connect setup in your project. To learn more, |
| 8 | +follow the [Firebase Data Connect documentation](https://firebase.google.com/docs/data-connect/quickstart). |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +Before using the Tanstack Query Firebase injectors for Data Connect, ensure you have configured your application using your chosen connector: |
| 13 | + |
| 14 | +```ts |
| 15 | +// app.config.ts |
| 16 | +export const appConfig: ApplicationConfig = { |
| 17 | + providers: [ |
| 18 | + ... |
| 19 | + provideFirebaseApp(() => |
| 20 | + initializeApp(/*Replace with your firebase config*/) |
| 21 | + ), |
| 22 | + provideDataConnect(() => getDataConnect(connectorConfig)), |
| 23 | + provideTanStackQuery(new QueryClient()), |
| 24 | + ], |
| 25 | +}; |
| 26 | +``` |
| 27 | + |
| 28 | +## Importing |
| 29 | + |
| 30 | +The package exports are available via the `@tanstack-query-firebase/angular` package under the `data-connect` namespace: |
| 31 | + |
| 32 | +```ts |
| 33 | +import { injectDataConnectQuery } from "@tanstack-query-firebase/angular/data-connect"; |
| 34 | +``` |
| 35 | + |
| 36 | +## Basic Usage |
| 37 | + |
| 38 | +To use the Tanstack Query Firebase injectors, you can either use the generated SDKs, or the `injectDataConnectQuery` injector to fetch data from the database: |
| 39 | + |
| 40 | +### Using the Generated SDK |
| 41 | + |
| 42 | +The generated SDK reduces the boilerplate required to use Tanstack Query Firebase's injectors. Instead of having to provide a ref to `injectDataConnectQuery`, you simply need to call the generated |
| 43 | +injector function like so: |
| 44 | + |
| 45 | +```ts |
| 46 | +import { injectListMyPosts } from '@firebasegen/posts/angular' |
| 47 | + |
| 48 | +@Component({ |
| 49 | + ... |
| 50 | + template: ` |
| 51 | + @if (posts.isPending()) { |
| 52 | + Loading... |
| 53 | + } |
| 54 | + @if (posts.error()) { |
| 55 | + An error has occurred: {{ posts.error() }} |
| 56 | + } |
| 57 | + @if (posts.data(); as data) { |
| 58 | + @for (post of data.posts; track post.id) { |
| 59 | + <mat-card appearance="outlined"> |
| 60 | + <mat-card-content>{{post.description}}</mat-card-content> |
| 61 | + </mat-card> |
| 62 | + } @empty { |
| 63 | + <h2>No items!</h2> |
| 64 | + } |
| 65 | + } |
| 66 | + `, |
| 67 | +}) |
| 68 | +export class PostListComponent { |
| 69 | + // Calls `injectDataConnectQuery` with the corresponding types. |
| 70 | + posts = injectListMyPosts(); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### Using `injectDataConnectQuery` |
| 75 | + |
| 76 | +Alternatively, you can use the `injectDataConnectQuery` injector. To use this, you need to pass the Response and Data generics: |
| 77 | + |
| 78 | +```ts |
| 79 | +import { injectDataConnectQuery } from "@tanstack-query-firebase/angular"; |
| 80 | +import { listMoviesRef } from "@firebasegen/posts"; |
| 81 | + |
| 82 | +@Component({ |
| 83 | + ... |
| 84 | + template: ` |
| 85 | + @if (posts.isPending()) { |
| 86 | + Loading... |
| 87 | + } |
| 88 | + @if (posts.error()) { |
| 89 | + An error has occurred: {{ posts.error() }} |
| 90 | + } |
| 91 | + @if (posts.data(); as data) { |
| 92 | + @for (post of data.posts; track post.id) { |
| 93 | + <mat-card appearance="outlined"> |
| 94 | + <mat-card-content>{{post.description}}</mat-card-content> |
| 95 | + </mat-card> |
| 96 | + } @empty { |
| 97 | + <h2>No items!</h2> |
| 98 | + } |
| 99 | + } |
| 100 | + `, |
| 101 | +}) |
| 102 | +export class PostListComponent { |
| 103 | + // Calls `injectDataConnectQuery` with the corresponding types. |
| 104 | + // Alternatively: |
| 105 | + // injectDataConnectQuery(queryRef<ListMoviesData, ListMoviesResponse>(dc, 'ListMovies')) |
| 106 | + posts = injectDataConnectQuery(listMoviesRef()); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The injectors will automatically infer the data type from the connector and the query and automtically create a [query key](https://tanstack.com/query/latest/docs/framework/angular/guides/query-keys) for the query. |
| 111 | + |
| 112 | +## Learning more |
| 113 | + |
| 114 | +To learn more about the Data Connect functions, check out the following pages: |
| 115 | + |
| 116 | +- [Querying](/angular/data-connect/querying) |
| 117 | +- [Mutations](/angular/data-connect/mutations) |
0 commit comments