@angular/fire 16.0.0-canary.596e208 → 16.0.0-canary.78407bc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +200 -0
- package/analytics/analytics.module.d.ts +2 -2
- package/analytics/firebase.d.ts +4 -1
- package/analytics/screen-tracking.service.d.ts +3 -3
- package/app-check/app-check.module.d.ts +3 -3
- package/app-check/firebase.d.ts +2 -1
- package/auth/auth.module.d.ts +2 -2
- package/auth/firebase.d.ts +3 -1
- package/auth-guard/auth-guard.d.ts +2 -2
- package/compat/analytics/analytics.d.ts +1 -3
- package/compat/analytics/analytics.module.d.ts +1 -1
- package/compat/analytics/screen-tracking.service.d.ts +1 -1
- package/compat/analytics/user-tracking.service.d.ts +1 -1
- package/compat/auth/auth.d.ts +3 -3
- package/compat/auth-guard/auth-guard.d.ts +2 -2
- package/compat/database/database.d.ts +4 -4
- package/compat/database/interfaces.d.ts +2 -2
- package/compat/database/list/audit-trail.d.ts +1 -1
- package/compat/database/list/create-reference.d.ts +1 -1
- package/compat/database/list/data-operation.d.ts +1 -1
- package/compat/database/list/state-changes.d.ts +1 -1
- package/compat/database/object/create-reference.d.ts +1 -1
- package/compat/database/observable/fromRef.d.ts +1 -1
- package/compat/database/utils.d.ts +1 -1
- package/compat/firebase.app.module.d.ts +1 -1
- package/compat/firestore/collection/collection.d.ts +2 -2
- package/compat/firestore/collection-group/collection-group.d.ts +2 -2
- package/compat/firestore/document/document.d.ts +4 -4
- package/compat/firestore/firestore.d.ts +7 -7
- package/compat/firestore/interfaces.d.ts +2 -2
- package/compat/functions/functions.d.ts +3 -3
- package/compat/messaging/messaging.d.ts +2 -2
- package/compat/performance/performance.d.ts +2 -2
- package/compat/remote-config/remote-config.d.ts +15 -49
- package/compat/storage/observable/fromTask.d.ts +1 -2
- package/compat/storage/ref.d.ts +1 -1
- package/compat/storage/storage.d.ts +3 -3
- package/compat/storage/task.d.ts +1 -1
- package/database/database.module.d.ts +2 -2
- package/docs/analytics.md +67 -0
- package/docs/app-check.md +53 -0
- package/docs/auth.md +165 -0
- package/docs/compat/analytics/getting-started.md +137 -0
- package/docs/compat/auth/getting-started.md +162 -0
- package/docs/compat/auth/router-guards.md +104 -0
- package/docs/compat/emulators/emulators.md +134 -0
- package/docs/compat/firestore/collections.md +326 -0
- package/docs/compat/firestore/documents.md +115 -0
- package/docs/compat/firestore/offline-data.md +39 -0
- package/docs/compat/firestore/querying-collections.md +204 -0
- package/docs/compat/functions/functions.md +166 -0
- package/docs/compat/messaging/messaging.md +232 -0
- package/docs/compat/performance/getting-started.md +132 -0
- package/docs/compat/remote-config/getting-started.md +134 -0
- package/docs/compat/rtdb/lists.md +257 -0
- package/docs/compat/rtdb/objects.md +182 -0
- package/docs/compat/rtdb/querying-lists.md +155 -0
- package/docs/compat/storage/storage.md +257 -0
- package/docs/compat.md +70 -0
- package/docs/database.md +175 -0
- package/docs/deploy/getting-started.md +204 -0
- package/docs/firebase.json +16 -0
- package/docs/firestore.md +148 -0
- package/docs/functions.md +52 -0
- package/docs/images/analytics-illo_1x.png +0 -0
- package/docs/images/auth-illo_1x.png +0 -0
- package/docs/images/cloud-messaging-illo_1x.png +0 -0
- package/docs/images/database-illo_1x.png +0 -0
- package/docs/images/firestore-illo_1x.png +0 -0
- package/docs/images/functions-illo_1x.png +0 -0
- package/docs/images/hosting-illo_1x.png +0 -0
- package/docs/images/performance-illo_1x.png +0 -0
- package/docs/images/reCAPTCHA-logo@1x.png +0 -0
- package/docs/images/remote-config-illo_1x.png +0 -0
- package/docs/images/storage-illo_1x.png +0 -0
- package/docs/install-and-setup.md +114 -0
- package/docs/install-angular-cli-windows10.md +82 -0
- package/docs/install-firebase-tools.md +78 -0
- package/docs/messaging.md +25 -0
- package/docs/performance.md +57 -0
- package/docs/remote-config.md +53 -0
- package/docs/storage.md +90 -0
- package/docs/universal/cloud-functions.md +96 -0
- package/docs/universal/getting-started.md +28 -0
- package/docs/universal/prerendering.md +72 -0
- package/docs/version-4-upgrade.md +119 -0
- package/docs/version-5-upgrade.md +82 -0
- package/docs/version-6-upgrade.md +16 -0
- package/docs/version-7-upgrade.md +298 -0
- package/esm2022/analytics/analytics.mjs +1 -1
- package/esm2022/analytics/analytics.module.mjs +10 -10
- package/esm2022/analytics/firebase.mjs +5 -2
- package/esm2022/analytics/screen-tracking.service.mjs +11 -10
- package/esm2022/analytics/user-tracking.service.mjs +5 -5
- package/esm2022/app/app.mjs +1 -1
- package/esm2022/app/app.module.mjs +9 -8
- package/esm2022/app-check/app-check.mjs +2 -2
- package/esm2022/app-check/app-check.module.mjs +10 -11
- package/esm2022/app-check/firebase.mjs +3 -2
- package/esm2022/auth/auth.mjs +1 -1
- package/esm2022/auth/auth.module.mjs +10 -10
- package/esm2022/auth/firebase.mjs +4 -2
- package/esm2022/auth-guard/auth-guard.mjs +8 -6
- package/esm2022/auth-guard/auth-guard.module.mjs +7 -7
- package/esm2022/compat/analytics/analytics.mjs +12 -13
- package/esm2022/compat/analytics/analytics.module.mjs +14 -11
- package/esm2022/compat/analytics/screen-tracking.service.mjs +9 -9
- package/esm2022/compat/analytics/user-tracking.service.mjs +7 -7
- package/esm2022/compat/auth/auth.mjs +13 -13
- package/esm2022/compat/auth/auth.module.mjs +7 -7
- package/esm2022/compat/auth-guard/auth-guard.mjs +8 -6
- package/esm2022/compat/auth-guard/auth-guard.module.mjs +7 -7
- package/esm2022/compat/cache.mjs +2 -1
- package/esm2022/compat/database/database.mjs +10 -10
- package/esm2022/compat/database/database.module.mjs +7 -7
- package/esm2022/compat/database/interfaces.mjs +1 -1
- package/esm2022/compat/database/list/audit-trail.mjs +3 -3
- package/esm2022/compat/database/list/changes.mjs +5 -5
- package/esm2022/compat/database/list/create-reference.mjs +5 -5
- package/esm2022/compat/database/list/data-operation.mjs +1 -1
- package/esm2022/compat/database/list/snapshot-changes.mjs +1 -1
- package/esm2022/compat/database/list/state-changes.mjs +2 -2
- package/esm2022/compat/database/object/create-reference.mjs +2 -2
- package/esm2022/compat/database/object/snapshot-changes.mjs +1 -1
- package/esm2022/compat/database/observable/fromRef.mjs +3 -2
- package/esm2022/compat/database/utils.mjs +1 -1
- package/esm2022/compat/firebase.app.mjs +1 -1
- package/esm2022/compat/firebase.app.module.mjs +11 -9
- package/esm2022/compat/firestore/collection/changes.mjs +2 -2
- package/esm2022/compat/firestore/collection/collection.mjs +4 -4
- package/esm2022/compat/firestore/collection-group/collection-group.mjs +4 -4
- package/esm2022/compat/firestore/document/document.mjs +4 -4
- package/esm2022/compat/firestore/firestore.mjs +11 -11
- package/esm2022/compat/firestore/firestore.module.mjs +7 -7
- package/esm2022/compat/firestore/interfaces.mjs +1 -1
- package/esm2022/compat/firestore/observable/fromRef.mjs +2 -2
- package/esm2022/compat/functions/functions.mjs +8 -8
- package/esm2022/compat/functions/functions.module.mjs +7 -7
- package/esm2022/compat/messaging/messaging.mjs +13 -12
- package/esm2022/compat/messaging/messaging.module.mjs +7 -7
- package/esm2022/compat/performance/performance.mjs +14 -20
- package/esm2022/compat/performance/performance.module.mjs +8 -9
- package/esm2022/compat/performance/performance.service.mjs +5 -4
- package/esm2022/compat/proxy.mjs +6 -7
- package/esm2022/compat/remote-config/remote-config.mjs +10 -11
- package/esm2022/compat/remote-config/remote-config.module.mjs +7 -7
- package/esm2022/compat/storage/observable/fromTask.mjs +3 -1
- package/esm2022/compat/storage/pipes/storageUrl.pipe.mjs +9 -9
- package/esm2022/compat/storage/ref.mjs +3 -3
- package/esm2022/compat/storage/storage.mjs +8 -8
- package/esm2022/compat/storage/storage.module.mjs +7 -7
- package/esm2022/compat/storage/task.mjs +2 -2
- package/esm2022/core.mjs +1 -3
- package/esm2022/database/database.mjs +1 -1
- package/esm2022/database/database.module.mjs +10 -10
- package/esm2022/firestore/firebase.mjs +22 -2
- package/esm2022/firestore/firestore.mjs +1 -1
- package/esm2022/firestore/firestore.module.mjs +10 -10
- package/esm2022/firestore/lite/firebase.mjs +11 -2
- package/esm2022/firestore/lite/lite.mjs +1 -1
- package/esm2022/firestore/lite/lite.module.mjs +9 -9
- package/esm2022/firestore/lite/rxfire.mjs +4 -2
- package/esm2022/firestore/rxfire.mjs +4 -4
- package/esm2022/functions/functions.mjs +1 -1
- package/esm2022/functions/functions.module.mjs +10 -10
- package/esm2022/messaging/messaging.mjs +1 -1
- package/esm2022/messaging/messaging.module.mjs +9 -9
- package/esm2022/performance/performance.mjs +1 -1
- package/esm2022/performance/performance.module.mjs +12 -12
- package/esm2022/remote-config/is-remote-config-supported-factory.mjs +2 -2
- package/esm2022/remote-config/remote-config.mjs +1 -1
- package/esm2022/remote-config/remote-config.module.mjs +9 -9
- package/esm2022/storage/storage.mjs +1 -1
- package/esm2022/storage/storage.module.mjs +10 -10
- package/esm2022/zones.mjs +9 -11
- package/fesm2022/angular-fire-analytics.mjs +18 -14
- package/fesm2022/angular-fire-analytics.mjs.map +1 -1
- package/fesm2022/angular-fire-app-check.mjs +8 -8
- package/fesm2022/angular-fire-app-check.mjs.map +1 -1
- package/fesm2022/angular-fire-app.mjs +6 -5
- package/fesm2022/angular-fire-app.mjs.map +1 -1
- package/fesm2022/angular-fire-auth-guard.mjs +13 -11
- package/fesm2022/angular-fire-auth-guard.mjs.map +1 -1
- package/fesm2022/angular-fire-auth.mjs +8 -6
- package/fesm2022/angular-fire-auth.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-analytics.mjs +29 -27
- package/fesm2022/angular-fire-compat-analytics.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-auth-guard.mjs +12 -10
- package/fesm2022/angular-fire-compat-auth-guard.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-auth.mjs +13 -13
- package/fesm2022/angular-fire-compat-auth.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-database.mjs +129 -128
- package/fesm2022/angular-fire-compat-database.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-firestore.mjs +96 -96
- package/fesm2022/angular-fire-compat-firestore.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-functions.mjs +10 -10
- package/fesm2022/angular-fire-compat-functions.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-messaging.mjs +13 -12
- package/fesm2022/angular-fire-compat-messaging.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-performance.mjs +23 -29
- package/fesm2022/angular-fire-compat-performance.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-remote-config.mjs +10 -11
- package/fesm2022/angular-fire-compat-remote-config.mjs.map +1 -1
- package/fesm2022/angular-fire-compat-storage.mjs +21 -19
- package/fesm2022/angular-fire-compat-storage.mjs.map +1 -1
- package/fesm2022/angular-fire-compat.mjs +15 -13
- package/fesm2022/angular-fire-compat.mjs.map +1 -1
- package/fesm2022/angular-fire-database.mjs +5 -5
- package/fesm2022/angular-fire-database.mjs.map +1 -1
- package/fesm2022/angular-fire-firestore-lite.mjs +19 -8
- package/fesm2022/angular-fire-firestore-lite.mjs.map +1 -1
- package/fesm2022/angular-fire-firestore.mjs +30 -10
- package/fesm2022/angular-fire-firestore.mjs.map +1 -1
- package/fesm2022/angular-fire-functions.mjs +6 -6
- package/fesm2022/angular-fire-functions.mjs.map +1 -1
- package/fesm2022/angular-fire-messaging.mjs +4 -4
- package/fesm2022/angular-fire-messaging.mjs.map +1 -1
- package/fesm2022/angular-fire-performance.mjs +7 -7
- package/fesm2022/angular-fire-performance.mjs.map +1 -1
- package/fesm2022/angular-fire-remote-config.mjs +4 -4
- package/fesm2022/angular-fire-remote-config.mjs.map +1 -1
- package/fesm2022/angular-fire-storage.mjs +4 -4
- package/fesm2022/angular-fire-storage.mjs.map +1 -1
- package/fesm2022/angular-fire.mjs +7 -11
- package/fesm2022/angular-fire.mjs.map +1 -1
- package/firestore/firebase.d.ts +21 -1
- package/firestore/firestore.module.d.ts +2 -2
- package/firestore/lite/firebase.d.ts +10 -1
- package/firestore/lite/lite.module.d.ts +2 -2
- package/firestore/lite/rxfire.d.ts +3 -1
- package/firestore/rxfire.d.ts +3 -3
- package/functions/functions.module.d.ts +2 -2
- package/messaging/messaging.module.d.ts +2 -2
- package/package.json +12 -12
- package/performance/performance.module.d.ts +2 -2
- package/remote-config/remote-config.module.d.ts +2 -2
- package/schematics/add/index.js +1 -1
- package/schematics/add/schema.json +16 -0
- package/schematics/builders.json +10 -0
- package/schematics/collection.json +15 -0
- package/schematics/common.js +1 -1
- package/schematics/deploy/actions.js +11 -11
- package/schematics/deploy/builder.js +2 -2
- package/schematics/deploy/schema.json +119 -0
- package/schematics/migration.json +15 -0
- package/schematics/setup/index.js +5 -6
- package/schematics/setup/prompts.js +5 -6
- package/schematics/setup/schema.json +16 -0
- package/schematics/update/index.js +1 -1
- package/schematics/update/v7/index.js +2 -2
- package/schematics/utils.js +4 -4
- package/storage/storage.module.d.ts +2 -2
- package/publish.sh +0 -1
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# 3. Collections in AngularFirestore
|
|
2
|
+
|
|
3
|
+
> Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in *documents*, which are organized into *collections*.
|
|
4
|
+
Each *document* contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents.
|
|
5
|
+
|
|
6
|
+
> **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).
|
|
7
|
+
|
|
8
|
+
## Using `AngularFirestoreCollection`
|
|
9
|
+
|
|
10
|
+
The `AngularFirestoreCollection` service is a wrapper around the native Firestore SDK's [`CollectionReference`](https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference) and [`Query`](https://firebase.google.com/docs/reference/js/firebase.firestore.Query) types. It is a generic service that provides you with a strongly typed set of methods for manipulating and streaming data. This service is designed for use as an `@Injectable()`.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { Component } from '@angular/core';
|
|
14
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
15
|
+
import { Observable } from 'rxjs';
|
|
16
|
+
|
|
17
|
+
export interface Item { name: string; }
|
|
18
|
+
|
|
19
|
+
@Component({
|
|
20
|
+
selector: 'app-root',
|
|
21
|
+
template: `
|
|
22
|
+
<ul>
|
|
23
|
+
<li *ngFor="let item of items | async">
|
|
24
|
+
{{ item.name }}
|
|
25
|
+
</li>
|
|
26
|
+
</ul>
|
|
27
|
+
`
|
|
28
|
+
})
|
|
29
|
+
export class AppComponent {
|
|
30
|
+
private itemsCollection: AngularFirestoreCollection<Item>;
|
|
31
|
+
items: Observable<Item[]>;
|
|
32
|
+
constructor(private afs: AngularFirestore) {
|
|
33
|
+
this.itemsCollection = afs.collection<Item>('items');
|
|
34
|
+
this.items = this.itemsCollection.valueChanges();
|
|
35
|
+
}
|
|
36
|
+
addItem(item: Item) {
|
|
37
|
+
this.itemsCollection.add(item);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The `AngularFirestoreCollection` is a service you use to create streams of the collection and perform data operations on the underyling collection.
|
|
43
|
+
|
|
44
|
+
### The `DocumentChangeAction` type
|
|
45
|
+
|
|
46
|
+
With the exception of the `valueChanges()`, each streaming method returns an Observable of `DocumentChangeAction[]`.
|
|
47
|
+
|
|
48
|
+
A `DocumentChangeAction` gives you the `type` and `payload` properties. The `type` tells when what `DocumentChangeType` operation occured (`added`, `modified`, `removed`). The `payload` property is a `DocumentChange` which provides you important metadata about the change and a `doc` property which is the `DocumentSnapshot`.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
interface DocumentChangeAction {
|
|
52
|
+
//'added' | 'modified' | 'removed';
|
|
53
|
+
type: DocumentChangeType;
|
|
54
|
+
payload: DocumentChange;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface DocumentChange {
|
|
58
|
+
type: DocumentChangeType;
|
|
59
|
+
doc: DocumentSnapshot;
|
|
60
|
+
oldIndex: number;
|
|
61
|
+
newIndex: number;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface DocumentSnapshot {
|
|
65
|
+
exists: boolean;
|
|
66
|
+
ref: DocumentReference;
|
|
67
|
+
id: string;
|
|
68
|
+
metadata: SnapshotMetadata;
|
|
69
|
+
data(): DocumentData;
|
|
70
|
+
get(fieldPath: string): any;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Streaming collection data
|
|
75
|
+
|
|
76
|
+
There are multiple ways of streaming collection data from Firestore.
|
|
77
|
+
|
|
78
|
+
### `valueChanges({ idField?: string })`
|
|
79
|
+
|
|
80
|
+
**What is it?** - The current state of your collection. Returns an Observable of data as a synchronized array of JSON objects. All Snapshot metadata is stripped and just the document data is included. Optionally, you can pass an options object with an `idField` key containing a string. If provided, the returned JSON objects will include their document ID mapped to a property with the name provided by `idField`.
|
|
81
|
+
|
|
82
|
+
**Why would you use it?** - When you just need a list of data. No document metadata is attached to the resulting array which makes it simple to render to a view.
|
|
83
|
+
|
|
84
|
+
**When would you not use it?** - When you need a more complex data structure than an array.
|
|
85
|
+
|
|
86
|
+
**Best practices** - Use this method to display data on a page. It's simple but effective. Use `.snapshotChanges()` once your needs become more complex.
|
|
87
|
+
|
|
88
|
+
#### Example of persisting a Document Id
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { Component } from '@angular/core';
|
|
92
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
93
|
+
import { Observable } from 'rxjs';
|
|
94
|
+
|
|
95
|
+
export interface Item { id: string; name: string; }
|
|
96
|
+
|
|
97
|
+
@Component({
|
|
98
|
+
selector: 'app-root',
|
|
99
|
+
template: `
|
|
100
|
+
<ul>
|
|
101
|
+
<li *ngFor="let item of items | async">
|
|
102
|
+
{{ item.name }}
|
|
103
|
+
</li>
|
|
104
|
+
</ul>
|
|
105
|
+
`
|
|
106
|
+
})
|
|
107
|
+
export class AppComponent {
|
|
108
|
+
private itemsCollection: AngularFirestoreCollection<Item>;
|
|
109
|
+
items: Observable<Item[]>;
|
|
110
|
+
constructor(private readonly afs: AngularFirestore) {
|
|
111
|
+
this.itemsCollection = afs.collection<Item>('items');
|
|
112
|
+
this.items = this.itemsCollection.valueChanges({ idField: 'customID' });
|
|
113
|
+
}
|
|
114
|
+
addItem(name: string) {
|
|
115
|
+
// Persist a document id
|
|
116
|
+
const id = this.afs.createId();
|
|
117
|
+
const item: Item = { id, name };
|
|
118
|
+
this.itemsCollection.doc(id).set(item);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### `snapshotChanges()`
|
|
124
|
+
|
|
125
|
+
**What is it?** - The current state of your collection. Returns an Observable of data as a synchronized array of `DocumentChangeAction[]`.
|
|
126
|
+
|
|
127
|
+
**Why would you use it?** - When you need a list of data but also want to keep around metadata. Metadata provides you the underyling `DocumentReference`, document id, and array index of the single document. Having the document's id around makes it easier to use data manipulation methods. This method gives you more horsepower with other Angular integrations such as ngrx, forms, and animations due to the `type` property. The `type` property on each `DocumentChangeAction` is useful for ngrx reducers, form states, and animation states.
|
|
128
|
+
|
|
129
|
+
**When would you not use it?** - When you need a more complex data structure than an array or if you need to process changes as they occur. This array is synchronized with the remote and local changes in Firestore.
|
|
130
|
+
|
|
131
|
+
**Best practices** - Use an observable operator to transform your data from `.snapshotChanges()`. Don't return the `DocumentChangeAction[]` to the template. See the example below.
|
|
132
|
+
|
|
133
|
+
#### Example
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { Component } from '@angular/core';
|
|
137
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
138
|
+
import { Observable } from 'rxjs';
|
|
139
|
+
import { map } from 'rxjs/operators';
|
|
140
|
+
|
|
141
|
+
export interface Shirt { name: string; price: number; }
|
|
142
|
+
export interface ShirtId extends Shirt { id: string; }
|
|
143
|
+
|
|
144
|
+
@Component({
|
|
145
|
+
selector: 'app-root',
|
|
146
|
+
template: `
|
|
147
|
+
<ul>
|
|
148
|
+
<li *ngFor="let shirt of shirts | async">
|
|
149
|
+
{{ shirt.name }} is {{ shirt.price }}
|
|
150
|
+
</li>
|
|
151
|
+
</ul>
|
|
152
|
+
`
|
|
153
|
+
})
|
|
154
|
+
export class AppComponent {
|
|
155
|
+
private shirtCollection: AngularFirestoreCollection<Shirt>;
|
|
156
|
+
shirts: Observable<ShirtId[]>;
|
|
157
|
+
constructor(private readonly afs: AngularFirestore) {
|
|
158
|
+
this.shirtCollection = afs.collection<Shirt>('shirts');
|
|
159
|
+
// .snapshotChanges() returns a DocumentChangeAction[], which contains
|
|
160
|
+
// a lot of information about "what happened" with each change. If you want to
|
|
161
|
+
// get the data and the id use the map operator.
|
|
162
|
+
this.shirts = this.shirtCollection.snapshotChanges().pipe(
|
|
163
|
+
map(actions => actions.map(a => {
|
|
164
|
+
const data = a.payload.doc.data() as Shirt;
|
|
165
|
+
const id = a.payload.doc.id;
|
|
166
|
+
return { id, ...data };
|
|
167
|
+
}))
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### `stateChanges()`
|
|
174
|
+
|
|
175
|
+
**What is it?** - Returns an Observable of the most recent changes as a `DocumentChangeAction[]`.
|
|
176
|
+
|
|
177
|
+
**Why would you use it?** - The above methods return a synchronized array sorted in query order. `stateChanges()` emits changes as they occur rather than syncing the query order. This works well for ngrx integrations as you can build your own data structure in your reducer methods.
|
|
178
|
+
|
|
179
|
+
**When would you not use it?** - When you just need a list of data. This is a more advanced usage of AngularFirestore.
|
|
180
|
+
|
|
181
|
+
#### Example
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { Component } from '@angular/core';
|
|
185
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
186
|
+
import { Observable } from 'rxjs';
|
|
187
|
+
import { map } from 'rxjs/operators';
|
|
188
|
+
|
|
189
|
+
export interface AccountDeposit { description: string; amount: number; }
|
|
190
|
+
export interface AccountDepositId extends AccountDeposit { id: string; }
|
|
191
|
+
|
|
192
|
+
@Component({
|
|
193
|
+
selector: 'app-root',
|
|
194
|
+
template: `
|
|
195
|
+
<ul>
|
|
196
|
+
<li *ngFor="let deposit of deposits | async">
|
|
197
|
+
{{ deposit.description }} for {{ deposit.amount }}
|
|
198
|
+
</li>
|
|
199
|
+
</ul>
|
|
200
|
+
`
|
|
201
|
+
})
|
|
202
|
+
export class AppComponent {
|
|
203
|
+
private depositCollection: AngularFirestoreCollection<AccountDeposit>;
|
|
204
|
+
deposits: Observable<AccountDepositId[]>;
|
|
205
|
+
constructor(private readonly afs: AngularFirestore) {
|
|
206
|
+
this.depositCollection = afs.collection<AccountDeposit>('deposits');
|
|
207
|
+
this.deposits = this.depositCollection.stateChanges(['added']).pipe(
|
|
208
|
+
map(actions => actions.map(a => {
|
|
209
|
+
const data = a.payload.doc.data() as AccountDeposit;
|
|
210
|
+
const id = a.payload.doc.id;
|
|
211
|
+
return { id, ...data };
|
|
212
|
+
}))
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### `auditTrail()`
|
|
219
|
+
|
|
220
|
+
**What is it?** - Returns an Observable of `DocumentChangeAction[]` as they occur. Similar to `stateChanges()`, but instead it keeps around the trail of events as an array.
|
|
221
|
+
|
|
222
|
+
**Why would you use it?** - This method is like `stateChanges()` except it is not ephemeral. It collects each change in an array as they occur. This is useful for ngrx integrations where you need to replay the entire state of an application. This also works as a great debugging tool for all applications. You can simply write `afs.collection('items').auditTrail().subscribe(console.log)` and check the events in the console as they occur.
|
|
223
|
+
|
|
224
|
+
**When would you not use it?** - When you just need a list of data. This is a more advanced usage of AngularFirestore.
|
|
225
|
+
|
|
226
|
+
#### Example
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import { Component } from '@angular/core';
|
|
230
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
231
|
+
import { Observable } from 'rxjs';
|
|
232
|
+
import { map } from 'rxjs/operators';
|
|
233
|
+
|
|
234
|
+
export interface AccountLogItem { description: string; amount: number; }
|
|
235
|
+
export interface AccountLogItemId extends AccountLogItem { id: string; }
|
|
236
|
+
|
|
237
|
+
@Component({
|
|
238
|
+
selector: 'app-root',
|
|
239
|
+
template: `
|
|
240
|
+
<ul>
|
|
241
|
+
<li *ngFor="let log of accountLogs | async">
|
|
242
|
+
{{ log.description }} for {{ log.amount }}
|
|
243
|
+
</li>
|
|
244
|
+
</ul>
|
|
245
|
+
`
|
|
246
|
+
})
|
|
247
|
+
export class AppComponent {
|
|
248
|
+
private accountLogCollection: AngularFirestoreCollection<AccountLogItem>;
|
|
249
|
+
accountLogs: Observable<AccountLogItemId[]>;
|
|
250
|
+
constructor(private readonly afs: AngularFirestore) {
|
|
251
|
+
this.accountLogCollection = afs.collection<AccountLogItem>('accountLog');
|
|
252
|
+
this.accountLogs = this.accountLogCollection.auditTrail().pipe(
|
|
253
|
+
map(actions => actions.map(a => {
|
|
254
|
+
const data = a.payload.doc.data() as AccountLogItem;
|
|
255
|
+
const id = a.payload.doc.id;
|
|
256
|
+
return { id, ...data };
|
|
257
|
+
}))
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Limiting events
|
|
264
|
+
|
|
265
|
+
There are three `DocumentChangeType`s in Firestore: `added`, `removed`, and `modified`. Each streaming method listens to all three by default. However, you may only be interested in one of these events. You can specify which events you'd like to use through the first parameter of each method:
|
|
266
|
+
|
|
267
|
+
#### Basic example
|
|
268
|
+
|
|
269
|
+
```ts
|
|
270
|
+
constructor(private afs: AngularFirestore): {
|
|
271
|
+
this.itemsCollection = afs.collection<Item>('items');
|
|
272
|
+
this.items = this.itemsCollection.snapshotChanges(['added', 'removed']);
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
#### Component example
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
import { Component } from '@angular/core';
|
|
280
|
+
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/compat/firestore';
|
|
281
|
+
import { Observable } from 'rxjs';
|
|
282
|
+
|
|
283
|
+
@Component({
|
|
284
|
+
selector: 'app-root',
|
|
285
|
+
template: `
|
|
286
|
+
<ul>
|
|
287
|
+
<li *ngFor="let item of items | async">
|
|
288
|
+
{{ item.name }}
|
|
289
|
+
</li>
|
|
290
|
+
</ul>
|
|
291
|
+
`
|
|
292
|
+
})
|
|
293
|
+
export class AppComponent {
|
|
294
|
+
private itemsCollection: AngularFirestoreCollection<Item>;
|
|
295
|
+
items: Observable<Item[]>;
|
|
296
|
+
constructor(private afs: AngularFirestore) {
|
|
297
|
+
this.itemsCollection = afs.collection<Item>('items');
|
|
298
|
+
this.items = this.itemsCollection.valueChanges(['added', 'removed']);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## State based vs. action based
|
|
304
|
+
|
|
305
|
+
Each one of these methods falls into two categories: state based and action based. State based methods return the state of your collection "as-is". Whereas action based methods return "what happened" in your collection.
|
|
306
|
+
|
|
307
|
+
For example, a user updates the third item in a list. In a state based method like `.valueChanges()` will update the third item in the collection and return an array of JSON data. This is how your state looks.
|
|
308
|
+
|
|
309
|
+
## Adding documents to a collection
|
|
310
|
+
|
|
311
|
+
To add a new document to a collection with a generated id use the `add()` method. This method uses the type provided by the generic class to validate it's type structure.
|
|
312
|
+
|
|
313
|
+
#### Basic example
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
constructor(private afs: AngularFirestore): {
|
|
317
|
+
const shirtsCollection = afs.collection<Item>('tshirts');
|
|
318
|
+
shirtsCollection.add({ name: 'item', price: 10 });
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Manipulating individual documents
|
|
323
|
+
|
|
324
|
+
To retrieve, update, or delete an individual document you can use the `doc()` method. This method returns an `AngularFirestoreDocument`, which provides methods for streaming, updating, and deleting. [See Using Documents with AngularFirestore for more information on how to use documents](documents.md).
|
|
325
|
+
|
|
326
|
+
### [Next Step: Querying Collections in AngularFirestore](querying-collections.md)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# 2. Documents in AngularFirestore
|
|
2
|
+
|
|
3
|
+
> Cloud Firestore is a NoSQL, document-oriented database. Unlike a SQL database, there are no tables or rows. Instead, you store data in *documents*, which are organized into *collections*.
|
|
4
|
+
Each *document* contains a set of key-value pairs. Cloud Firestore is optimized for storing large collections of small documents.
|
|
5
|
+
|
|
6
|
+
> **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).
|
|
7
|
+
|
|
8
|
+
## Using `AngularFirestoreDocument`
|
|
9
|
+
|
|
10
|
+
The `AngularFirestoreDocument` service is a wrapper around the native Firestore SDK's [`DocumentReference` type](https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference). It is a generic service that provides you with a strongly typed set of methods for manipulating and streaming data. This service is designed for use as an `@Injectable()`.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { Component } from '@angular/core';
|
|
14
|
+
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/compat/firestore';
|
|
15
|
+
import { Observable } from 'rxjs';
|
|
16
|
+
|
|
17
|
+
export interface Item { name: string; }
|
|
18
|
+
|
|
19
|
+
@Component({
|
|
20
|
+
selector: 'app-root',
|
|
21
|
+
template: `
|
|
22
|
+
<div>
|
|
23
|
+
{{ (item | async)?.name }}
|
|
24
|
+
</div>
|
|
25
|
+
`
|
|
26
|
+
})
|
|
27
|
+
export class AppComponent {
|
|
28
|
+
private itemDoc: AngularFirestoreDocument<Item>;
|
|
29
|
+
item: Observable<Item>;
|
|
30
|
+
constructor(private afs: AngularFirestore) {
|
|
31
|
+
this.itemDoc = afs.doc<Item>('items/1');
|
|
32
|
+
this.item = this.itemDoc.valueChanges();
|
|
33
|
+
}
|
|
34
|
+
update(item: Item) {
|
|
35
|
+
this.itemDoc.update(item);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### The `DocumentChangeAction` type
|
|
41
|
+
|
|
42
|
+
With the exception of the `valueChanges()`, each streaming method returns an Observable of `DocumentChangeAction[]`.
|
|
43
|
+
|
|
44
|
+
A `DocumentChangeAction` gives you the `type` and `payload` properties. The `type` tells when what `DocumentChangeType` operation occured (`added`, `modified`, `removed`). The `payload` property is a `DocumentChange` which provides you important metadata about the change and a `doc` property which is the `DocumentSnapshot`.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
interface DocumentChangeAction {
|
|
48
|
+
//'added' | 'modified' | 'removed';
|
|
49
|
+
type: DocumentChangeType;
|
|
50
|
+
payload: DocumentSnapshot;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface DocumentChange {
|
|
54
|
+
type: DocumentChangeType;
|
|
55
|
+
doc: DocumentSnapshot;
|
|
56
|
+
oldIndex: number;
|
|
57
|
+
newIndex: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface DocumentSnapshot {
|
|
61
|
+
exists: boolean;
|
|
62
|
+
ref: DocumentReference;
|
|
63
|
+
id: string;
|
|
64
|
+
metadata: SnapshotMetadata;
|
|
65
|
+
data(): DocumentData;
|
|
66
|
+
get(fieldPath: string): any;
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Streaming document data
|
|
71
|
+
|
|
72
|
+
There are multiple ways of streaming collection data from Firestore.
|
|
73
|
+
|
|
74
|
+
### `valueChanges({ idField?: string })`
|
|
75
|
+
|
|
76
|
+
**What is it?** - Returns an Observable of document data. All Snapshot metadata is stripped. This method provides only the data. Optionally, you can pass an options object with an `idField` key containing a string. If provided, the returned object will include its document ID mapped to a property with the name provided by `idField`.
|
|
77
|
+
|
|
78
|
+
**Why would you use it?** - When you just need the object data. No document metadata is attached which makes it simple to render to a view.
|
|
79
|
+
|
|
80
|
+
**When would you not use it?** - When you need document metadata.
|
|
81
|
+
|
|
82
|
+
### `snapshotChanges()`
|
|
83
|
+
|
|
84
|
+
**What is it?** - Returns an Observable of data as a `DocumentChangeAction`.
|
|
85
|
+
|
|
86
|
+
**Why would you use it?** - When you need the document data but also want to keep around metadata. This metadata provides you the underyling `DocumentReference` and document id. Having the document's id around makes it easier to use data manipulation methods. This method gives you more horsepower with other Angular integrations such as ngrx, forms, and animations due to the `type` property. The `type` property on each `DocumentChangeAction` is useful for ngrx reducers, form states, and animation states.
|
|
87
|
+
|
|
88
|
+
**When would you not use it?** - When you simply need to render data to a view and don't want to do any extra processing.
|
|
89
|
+
|
|
90
|
+
## Manipulating documents
|
|
91
|
+
|
|
92
|
+
AngularFirestore provides methods for setting, updating, and deleting document data.
|
|
93
|
+
|
|
94
|
+
- `set(data: T)` - Destructively updates a document's data.
|
|
95
|
+
- `update(data: T)` - Non-destructively updates a document's data.
|
|
96
|
+
- `delete()` - Deletes an entire document. Does not delete any nested collections.
|
|
97
|
+
|
|
98
|
+
## Querying?
|
|
99
|
+
|
|
100
|
+
Querying has no effect on documents. Documents are a single object and querying effects a range of multiple documents. If you are looking for querying then you want to use a collection.
|
|
101
|
+
|
|
102
|
+
## Retrieving nested collections
|
|
103
|
+
|
|
104
|
+
Nesting collections is a great way to structure your data. This allows you to group related data structures together. If you are creating a "Task List" site, you can group "tasks" under a user: `user/<uid>/tasks`.
|
|
105
|
+
|
|
106
|
+
To retrieve a nested collection use the `collection(path: string)` method.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
constructor(private afs: AngularFirestore) {
|
|
110
|
+
this.userDoc = afs.doc<Item>('user/david');
|
|
111
|
+
this.tasks = this.userDoc.collection<Task>('tasks').valueChanges();
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### [Next Step: Collections in AngularFirestore](collections.md)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Offline Data in Firestore
|
|
2
|
+
|
|
3
|
+
> Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the data stored remotely in Cloud Firestore.
|
|
4
|
+
|
|
5
|
+
> **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).
|
|
6
|
+
|
|
7
|
+
**Offline persistence is an experimental feature that is supported only by the Chrome, Safari, and Firefox web browsers.** If a user opens multiple browser tabs that point to the same Cloud Firestore database, and offline persistence is enabled, Cloud Firestore will work correctly only in the first tab.
|
|
8
|
+
|
|
9
|
+
## Enable Offline Data in AngularFirestore
|
|
10
|
+
|
|
11
|
+
To enable offline persistence in your AngularFire application, call `enablePersistence()` when you are importing `AngularFirestoreModule` into your `@NgModule`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
15
|
+
import { NgModule } from '@angular/core';
|
|
16
|
+
import { AppComponent } from './app.component';
|
|
17
|
+
import { AngularFireModule } from '@angular/fire/compat';
|
|
18
|
+
import { AngularFirestoreModule } from '@angular/fire/compat/firestore';
|
|
19
|
+
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
|
|
20
|
+
import { environment } from '../environments/environment';
|
|
21
|
+
|
|
22
|
+
@NgModule({
|
|
23
|
+
imports: [
|
|
24
|
+
BrowserModule,
|
|
25
|
+
AngularFireModule.initializeApp(environment.firebase),
|
|
26
|
+
AngularFirestoreModule.enablePersistence(),
|
|
27
|
+
],
|
|
28
|
+
declarations: [ AppComponent ],
|
|
29
|
+
bootstrap: [ AppComponent ]
|
|
30
|
+
})
|
|
31
|
+
export class AppModule {}
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
While offline your listeners will receive listen events when the locally cached data changes. You can use to Documents and Collections normally.
|
|
36
|
+
|
|
37
|
+
To check whether you're receiving data from the server or the cache, use the `fromCache` property on the `SnapshotMetadata` in your snapshot event. If `fromCache` is true, the data came from the cache and might be stale or incomplete. If `fromCache` is false, the data is complete and current with the latest updates on the server.
|
|
38
|
+
|
|
39
|
+
[To learn more about Offline Persistence in Firestore, check out the Firebase documentation](https://firebase.google.com/docs/firestore/enable-offline).
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# 4. Querying Collections in AngularFirestore
|
|
2
|
+
|
|
3
|
+
> Firestore has [powerful querying syntax](https://firebase.google.com/docs/firestore/query-data/queries) and the `AngularFirestoreCollection` provides a thin wrapper around it. This keeps you from having to learn two query syntax systems.
|
|
4
|
+
If you know the [Firestore query API](https://firebase.google.com/docs/reference/js/firebase.firestore.Query) then you know how to query in AngularFirestore.
|
|
5
|
+
|
|
6
|
+
> **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).
|
|
7
|
+
|
|
8
|
+
## Creating a query with primitive/scalar values
|
|
9
|
+
|
|
10
|
+
Queries are created by building on the [`firebase.firestore.CollectionReference`](https://firebase.google.com/docs/reference/js/firebase.firestore.CollectionReference).
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
afs.collection('items', ref => ref.where('size', '==', 'large'))
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Query options
|
|
17
|
+
|
|
18
|
+
| method | purpose |
|
|
19
|
+
| ---------|--------------------|
|
|
20
|
+
| `where` | Create a new query. *Can be chained to form complex queries.* |
|
|
21
|
+
| `orderBy` | Sort by the specified field, in descending or ascending order. |
|
|
22
|
+
| `limit` | Sets the maximum number of items to return. |
|
|
23
|
+
| `startAt` | Results start at the provided document (inclusive). |
|
|
24
|
+
| `startAfter` | Results start after the provided document (exclusive). |
|
|
25
|
+
| `endAt` | Results end at the provided document (inclusive). |
|
|
26
|
+
| `endBefore` | Results end before the provided document (exclusive). |
|
|
27
|
+
|
|
28
|
+
[To learn more about querying and sorting in Firestore, check out the Firebase documentation](https://firebase.google.com/docs/firestore/query-data/queries).
|
|
29
|
+
|
|
30
|
+
## Invalid query combinations
|
|
31
|
+
|
|
32
|
+
Range filters can only be specified on a single field:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// WARNING: Do not copy and paste. This will not work!
|
|
36
|
+
ref.where('state', '>=', 'CA')
|
|
37
|
+
.where('population', '>', 100000)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Range filter and orderBy cannot be on different fields:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// WARNING: Do not copy and paste. This will not work!
|
|
44
|
+
ref.where('population', '>', 100000).orderBy('country')
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Range filters / orderBy cannot be used in conjuction with user-defined data, they require composite indexes be defined on the specific fields.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// WARNING: Do not copy and paste. This will not work!
|
|
51
|
+
ref.where('tags.awesome', '==', true).orderBy('population')
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Dynamic querying
|
|
55
|
+
|
|
56
|
+
To enable dynamic queries one should lean on RxJS Operators like `switchMap`.
|
|
57
|
+
|
|
58
|
+
An RxJS Subject is imported below. A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. See, [What is a Subject](http://reactivex.io/rxjs/manual/overview.html#subject) for more information.
|
|
59
|
+
|
|
60
|
+
When we call [`switchMap` on the Subject](https://www.learnrxjs.io/operators/transformation/switchmap.html), we can map each value to a new Observable; in this case a database query.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
const size$ = new Subject<string>();
|
|
64
|
+
const queryObservable = size$.pipe(
|
|
65
|
+
switchMap(size =>
|
|
66
|
+
afs.collection('items', ref => ref.where('size', '==', size)).valueChanges()
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// subscribe to changes
|
|
71
|
+
queryObservable.subscribe(queriedItems => {
|
|
72
|
+
console.log(queriedItems);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// trigger the query
|
|
76
|
+
size$.next('large');
|
|
77
|
+
|
|
78
|
+
// re-trigger the query!!!
|
|
79
|
+
size$.next('small');
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Example app
|
|
83
|
+
|
|
84
|
+
[See this example in action on StackBlitz](https://stackblitz.com/edit/angularfire-db-api-fbad9p).
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { Component } from '@angular/core';
|
|
88
|
+
import { AngularFirestore } from '@angular/fire/compat/firestore';
|
|
89
|
+
import { Observable, BehaviorSubject, combineLatest } from 'rxjs';
|
|
90
|
+
import { switchMap } from 'rxjs/operators';
|
|
91
|
+
|
|
92
|
+
export interface Item {
|
|
93
|
+
text: string;
|
|
94
|
+
color: string;
|
|
95
|
+
size: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@Component({
|
|
99
|
+
selector: 'app-root',
|
|
100
|
+
template: `
|
|
101
|
+
<div *ngIf="items$ | async; let items; else loading">
|
|
102
|
+
<ul>
|
|
103
|
+
<li *ngFor="let item of items">
|
|
104
|
+
{{ item.text }}
|
|
105
|
+
</li>
|
|
106
|
+
</ul>
|
|
107
|
+
<div *ngIf="items.length === 0">No results, try clearing filters</div>
|
|
108
|
+
</div>
|
|
109
|
+
<ng-template #loading>Loading…</ng-template>
|
|
110
|
+
<div>
|
|
111
|
+
<h4>Filter by size</h4>
|
|
112
|
+
<button (click)="filterBySize('small')">Small</button>
|
|
113
|
+
<button (click)="filterBySize('medium')">Medium</button>
|
|
114
|
+
<button (click)="filterBySize('large')">Large</button>
|
|
115
|
+
<button (click)="filterBySize(null)" *ngIf="this.sizeFilter$.getValue()">
|
|
116
|
+
<em>clear filter</em>
|
|
117
|
+
</button>
|
|
118
|
+
</div>
|
|
119
|
+
<div>
|
|
120
|
+
<h4>Filter by color</h4>
|
|
121
|
+
<button (click)="filterByColor('red')">Red</button>
|
|
122
|
+
<button (click)="filterByColor('green')">Blue</button>
|
|
123
|
+
<button (click)="filterByColor('blue')">Green</button>
|
|
124
|
+
<button (click)="filterByColor(null)" *ngIf="this.colorFilter$.getValue()">
|
|
125
|
+
<em>clear filter</em>
|
|
126
|
+
</button>
|
|
127
|
+
</div>
|
|
128
|
+
`,
|
|
129
|
+
})
|
|
130
|
+
export class AppComponent {
|
|
131
|
+
items$: Observable<Item[]>;
|
|
132
|
+
sizeFilter$: BehaviorSubject<string|null>;
|
|
133
|
+
colorFilter$: BehaviorSubject<string|null>;
|
|
134
|
+
|
|
135
|
+
constructor(afs: AngularFirestore) {
|
|
136
|
+
this.sizeFilter$ = new BehaviorSubject(null);
|
|
137
|
+
this.colorFilter$ = new BehaviorSubject(null);
|
|
138
|
+
this.items$ = combineLatest(
|
|
139
|
+
this.sizeFilter$,
|
|
140
|
+
this.colorFilter$
|
|
141
|
+
).pipe(
|
|
142
|
+
switchMap(([size, color]) =>
|
|
143
|
+
afs.collection('items', ref => {
|
|
144
|
+
let query : firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
|
|
145
|
+
if (size) { query = query.where('size', '==', size) };
|
|
146
|
+
if (color) { query = query.where('color', '==', color) };
|
|
147
|
+
return query;
|
|
148
|
+
}).valueChanges()
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
filterBySize(size: string|null) {
|
|
153
|
+
this.sizeFilter$.next(size);
|
|
154
|
+
}
|
|
155
|
+
filterByColor(color: string|null) {
|
|
156
|
+
this.colorFilter$.next(color);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**To run the above example as is, you need to have sample data in your Firebase database with the following structure:**
|
|
162
|
+
|
|
163
|
+
```json
|
|
164
|
+
{
|
|
165
|
+
"items": {
|
|
166
|
+
"a" : {
|
|
167
|
+
"size" : "small",
|
|
168
|
+
"text" : "small thing",
|
|
169
|
+
"color" : "red"
|
|
170
|
+
},
|
|
171
|
+
"b" : {
|
|
172
|
+
"size" : "medium",
|
|
173
|
+
"text" : "medium sample",
|
|
174
|
+
"color" : "red"
|
|
175
|
+
},
|
|
176
|
+
"c" : {
|
|
177
|
+
"size" : "large",
|
|
178
|
+
"text" : "large widget",
|
|
179
|
+
"color" : "green"
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Collection Group Queries
|
|
186
|
+
|
|
187
|
+
To query across collections and sub-collections with the same name anywhere in Firestore, you can use collection group queries.
|
|
188
|
+
|
|
189
|
+
Collection Group Queries allow you to have a more nested data-structure without sacrificing performance. For example, we could easily query all comments a user posted; even if the comments were stored as a sub-collection under `Articles/**` or even nested deeply (`Articles/**/Comments/**/Comments/**/...`):
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
constructor(private afs: AngularFirestore) { }
|
|
193
|
+
|
|
194
|
+
ngOnInit() {
|
|
195
|
+
...
|
|
196
|
+
// Get all the user's comments, no matter how deeply nested
|
|
197
|
+
this.comments$ = afs.collectionGroup('Comments', ref => ref.where('user', '==', userId))
|
|
198
|
+
.valueChanges({ idField: 'docId' });
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
`collectionGroup` returns an `AngularFirestoreCollectionGroup` which is similar to `AngularFirestoreCollection`. The main difference is that `AngularFirestoreCollectionGroup` has no data operation methods such as `add` because it doesn't have a concrete reference.
|
|
203
|
+
|
|
204
|
+
### [Next Step: Getting started with Firebase Authentication](../auth/getting-started.md)
|