diff --git a/docs/ai.md b/docs/ai.md index c381c08a9..1d41650f1 100644 --- a/docs/ai.md +++ b/docs/ai.md @@ -17,6 +17,7 @@ As a prerequisite, ensure that `AngularFire` has been added to your project as d Provide an AI instance in the application's `app.config.ts`: ```ts +import { ApplicationConfig } from '@angular/core'; import { provideFirebaseApp, initializeApp } from '@angular/fire/app'; import { provideAI, getAI } from '@angular/fire/ai'; @@ -26,7 +27,7 @@ export const appConfig: ApplicationConfig = { provideAI(() => getAI()), ... ], - ..., + ... } ``` diff --git a/docs/analytics.md b/docs/analytics.md index d1da2d5a5..c8b33e404 100644 --- a/docs/analytics.md +++ b/docs/analytics.md @@ -17,6 +17,7 @@ As a prerequisite, ensure that `AngularFire` has been added to your project as d Provide an Analytics instance in the application's `app.config.ts`: ```ts +import { ApplicationConfig } from '@angular/core'; import { provideFirebaseApp, initializeApp } from '@angular/fire/app'; import { provideAnalytics, getAnalytics } from '@angular/fire/analytics'; @@ -26,7 +27,7 @@ export const appConfig: ApplicationConfig = { provideAnalytics(() => getAnalytics()), ... ], - ..., + ... } ``` @@ -43,6 +44,8 @@ export class UserProfileComponent { } ``` +`Analytics` is `null` during server rendering, although its type says `Analytics`, so check it before use. + ## Firebase API AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API. diff --git a/docs/compat/emulators/emulators.md b/docs/compat/emulators/emulators.md index c3d42539a..50789cd26 100644 --- a/docs/compat/emulators/emulators.md +++ b/docs/compat/emulators/emulators.md @@ -117,23 +117,6 @@ export class AppModule { } The environment `useEmulators` flag is used to control whenever the app should connect to the emulators, which is usually done in non-production environments. -Also you can opt-in the new way of setting the Cloud Functions [origin](https://firebase.google.com/docs/functions/locations) in Firebase v8 by using the `NEW_ORIGIN_BEHAVIOR` token in conjuction with the already present `ORIGIN` token. - -```ts -import { isDevMode, NgModule } from '@angular/core'; -import { ORIGIN as FUNCTIONS_ORIGIN, NEW_ORIGIN_BEHAVIOR } from '@angular/fire/compat/functions'; - -@NgModule({ - // ... Existing configuration - providers: [ - // ... Existing Providers - { provide: NEW_ORIGIN_BEHAVIOR, useValue: true }, - { provide: FUNCTIONS_ORIGIN, useFactory: () => isDevMode() ? undefined : location.origin }, - ] -}) -export class AppModule { } -``` - ### 6.0.0 Method With the exception of the Auth Emulator, the old way of setting the `host` and `port` for each emulator was done using a different set of tokens by passing the entire url path as string. diff --git a/docs/compat/functions/functions.md b/docs/compat/functions/functions.md index b43c9f70d..c16704b0b 100644 --- a/docs/compat/functions/functions.md +++ b/docs/compat/functions/functions.md @@ -2,11 +2,11 @@ > The Cloud Functions for Firebase client SDKs let you call functions directly from a Firebase app. To call a function from your app in this way, write and deploy an HTTPS Callable function in Cloud Functions, and then add client logic to call the function from your app. -> **NOTE**: [AngularFire has a new tree-shakable API](../../../README.md#developer-guide), you're looking at the documentation for the compatability version of the library. [See the v7 upgrade guide for more information on this change.](../../version-7-upgrade.md). +> **NOTE**: [AngularFire has a new tree-shakable API](../../../README.md#developer-guide), you're looking at the documentation for the compatibility version of the library. [See the v7 upgrade guide for more information on this change.](../../version-7-upgrade.md). ### Import the `NgModule` -Cloud Functions for AngularFire is contained in the `@angular/fire/functions` module namespace. Import the `AngularFireFunctionsModule` in your `NgModule`. This sets up the `AngularFireFunction` service for dependency injection. +Cloud Functions for AngularFire is contained in the `@angular/fire/compat/functions` module namespace. Import the `AngularFireFunctionsModule` in your `NgModule`. This sets up the `AngularFireFunctions` service for dependency injection. ```ts import { BrowserModule } from '@angular/platform-browser'; @@ -53,25 +53,29 @@ AngularFireFunctions is super easy. You create a function on the server side and | method | | | ---------|--------------------| -| `httpCallable(name: string): (data: T) ` | Creates a callable function based on a function name. Returns a function that can create the observable of the http call. | +| `httpsCallable(name: string, options?: HttpsCallableOptions): (data: T) => Observable` | Creates a callable function based on a function name. Returns a function that can create the observable of the http call. | ```ts - +import { AsyncPipe } from '@angular/common'; import { Component } from '@angular/core'; import { AngularFireFunctions } from '@angular/fire/compat/functions'; +import { Observable } from 'rxjs'; @Component({ selector: 'app-root', - template: `{ data$ | async }` + imports: [AsyncPipe], + template: `{{ data$ | async }}` }) export class AppComponent { - constructor(private fns: AngularFireFunctions) { + data$: Observable; + + constructor(private fns: AngularFireFunctions) { const callable = fns.httpsCallable('my-fn-name'); this.data$ = callable({ name: 'some-data' }); } } ``` -Notice that calling `httpsCallable()` does not initiate the request. It creates a function, which when called creates an Observable, subscribe or convert it to a Promise to initiate the request. +Notice that calling `httpsCallable()` does not initiate the request. It creates a function, which when called creates an Observable. Subscribe to the Observable or convert it to a Promise to initiate the request. ## Configuration via Dependency Injection @@ -145,11 +149,11 @@ To set this up, you first need to update your `hosting` section in `firebase.jso } ``` -Deploy your hosting project to the new settings go into effect, finally configure functions origin to point at your app domain: +Deploy your hosting project so the new settings go into effect, then configure the functions origin to point at your app domain: ```ts import { NgModule } from '@angular/core'; -import { AngularFireFunctionsModule, ORIGIN, NEW_ORIGIN_BEHAVIOR } from '@angular/fire/compat/functions'; +import { AngularFireFunctionsModule, ORIGIN } from '@angular/fire/compat/functions'; @NgModule({ imports: [ @@ -159,10 +163,11 @@ import { AngularFireFunctionsModule, ORIGIN, NEW_ORIGIN_BEHAVIOR } from '@angula ], ... providers: [ - { provide: NEW_ORIGIN_BEHAVIOR, useValue: true }, { provide: ORIGIN, useValue: 'https://project-name.web.app' } ] }) export class AppModule {} ``` + +Note: `REGION` and `ORIGIN` cannot be provided at the same time. diff --git a/docs/data-connect.md b/docs/data-connect.md index 062324057..50833538e 100644 --- a/docs/data-connect.md +++ b/docs/data-connect.md @@ -15,6 +15,7 @@ As a prerequisite, ensure that `AngularFire` has been added to your project as d Provide a Data Connect instance in the application's `app.config.ts`. `getDataConnect` takes a connector config that identifies your service, connector, and location; this is generated for you when you set up Data Connect and is also exported from your generated SDK: ```ts +import { ApplicationConfig } from '@angular/core'; import { provideFirebaseApp, initializeApp } from '@angular/fire/app'; import { provideDataConnect, getDataConnect } from '@angular/fire/data-connect'; @@ -30,7 +31,7 @@ export const appConfig: ApplicationConfig = { provideDataConnect(() => getDataConnect(connectorConfig)), ... ], - ..., + ... } ``` diff --git a/docs/firestore.md b/docs/firestore.md index 551e56337..4a7322ed7 100644 --- a/docs/firestore.md +++ b/docs/firestore.md @@ -73,12 +73,16 @@ import { execute, field, greaterThan } from '@angular/fire/firestore/lite/pipeli class BooksComponent { private firestore = inject(Firestore); - loadBooks() { + loadBooksChained() { // Method Chaining const pipeline = this.firestore.pipeline() .collection('books') .where(field('rating').greaterThan(4.5)); + return execute(pipeline); + } + + loadBooksFunctional() { // Functional Approach const pipeline = this.firestore.pipeline() .collection('books') @@ -106,6 +110,7 @@ In `user-profile.component.ts`: ```typescript import { Firestore, FirestoreDataConverter, collection, collectionData } from '@angular/fire/firestore'; +import { AsyncPipe } from '@angular/common'; import { Component, inject } from '@angular/core'; import { Observable } from 'rxjs'; @@ -126,9 +131,9 @@ export const userProfileConverter: FirestoreDataConverter = { }, }; -@Component ({ +@Component({ selector: 'app-user-profile', - standalone: true, + imports: [AsyncPipe], ... }) export class UserProfileComponent { @@ -145,15 +150,15 @@ export class UserProfileComponent { } ``` -`collectionData` returns an `observable` that can we can use to display the data in the template. In `user-profile.component.html`: +`collectionData` returns an `Observable` that we can use to display the data in the template. In `user-profile.component.html`: ```html

User Profiles

    -
  • - {{user.username}} -
  • + @for (user of users$ | async; track user) { +
  • {{ user.username }}
  • + }
``` @@ -177,35 +182,30 @@ To write to Cloud Firestore use the `addDoc` function. It will create a new docu ```typescript -import { Firestore, collection, collectionData, addDoc, CollectionReference, DocumentReference } from '@angular/fire/firestore'; +import { Firestore, collection, addDoc, CollectionReference, DocumentReference } from '@angular/fire/firestore'; import { Component, inject } from '@angular/core'; -import { Observable } from 'rxjs'; +// Uses UserProfile and userProfileConverter from the example above -@Component ({ +@Component({ selector: 'app-user-profile', - standalone: true, ... }) export class UserProfileComponent { private firestore: Firestore = inject(Firestore); // inject Cloud Firestore - users$: Observable; - usersCollection: CollectionReference; - - constructor() {...} + private usersCollection: CollectionReference = + collection(this.firestore, 'users').withConverter(userProfileConverter); + ... addUserProfile(username: string) { if (!username) return; - addDoc(this.usersCollection, { username }).then((documentReference: DocumentReference) => { + addDoc(this.usersCollection, { username }).then((documentReference: DocumentReference) => { // the documentReference provides access to the newly created document }); } } -export interface UserProfile { - username: string; -} ``` -In the `addUserProfile` method we use a reference to the `this.usersCollection` and provide `UserProfile` data to the the `addDoc` function. `addDoc` returns a promise that can be used to respond to the successful addition of the data. Errors can also be caught here. +In the `addUserProfile` method we use a reference to the `this.usersCollection` and provide `UserProfile` data to the `addDoc` function. `addDoc` returns a promise that can be used to respond to the successful addition of the data. Errors can also be caught here. ## Learn More -More information on API methods and other functions can be found on the [Firebase Official Docs](https://firebase.google.com/docs/reference/js/firestore_) +More information on API methods and other functions can be found on the [Firebase Official Docs](https://firebase.google.com/docs/reference/js/firestore) diff --git a/docs/performance.md b/docs/performance.md index 6dc53e797..d22cb4e85 100644 --- a/docs/performance.md +++ b/docs/performance.md @@ -34,7 +34,7 @@ export const appConfig: ApplicationConfig = { Next inject `Performance` into your component: ```ts -import { Component, inject} from '@angular/core'; +import { Component, inject } from '@angular/core'; import { Performance } from '@angular/fire/performance'; @Component({ ... }) @@ -44,6 +44,8 @@ export class PerformanceComponent { } ``` +`Performance` is `null` during server rendering, although its type says `Performance`, so check it before use. + ## Firebase API AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, while providing the same API. @@ -51,3 +53,29 @@ AngularFire wraps the Firebase JS SDK to ensure proper functionality in Angular, Update the imports from `import { ... } from 'firebase/performance'` to `import { ... } from '@angular/fire/performance'` and follow the official documentation. [Getting Started](https://firebase.google.com/docs/perf-mon/get-started-web) | [API Reference](https://firebase.google.com/docs/reference/js/performance) + +## RxJS operators + +`@angular/fire/performance` also exports RxJS operators (from `rxfire/performance`, wrapped for Angular) that create User Timing API traces picked up by Firebase Performance Monitoring (once `Performance` has been injected) and browser DevTools: + +```ts +import { Component, inject } from '@angular/core'; +import { Firestore, collection, collectionData } from '@angular/fire/firestore'; +import { Performance, traceUntilFirst } from '@angular/fire/performance'; + +@Component({ ... }) +export class ItemsComponent { + private firestore = inject(Firestore); + private performance = inject(Performance); + items$ = collectionData(collection(this.firestore, 'items')).pipe( + traceUntilFirst('items'), + ); +} +``` + +| Operator | Behavior | +| --- | --- | +| `traceUntilFirst(name: string)` | Traces from subscription until the first emission. | +| `traceUntilComplete(name: string)` | Traces from subscription until the observable completes. | +| `traceUntil(name: string, test: (value: T) => boolean, options?: { orComplete?: boolean })` | Traces from subscription until the first emission where `test(value)` returns `true` (or until completion if `options.orComplete` is `true`). | +| `traceWhile(name: string, test: (value: T) => boolean, options?: { orComplete?: boolean })` | Starts tracing when `test(value)` returns `true` and stops when an emission returns `false` (or on completion if `options.orComplete` is `true`). |