You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are multiple ways of streaming collection data from Firestore.
74
+
There are multiple ways of streaming collection data from Firestore.
73
75
74
76
### `valueChanges()`
75
-
**What is it?** - Returns an Observable of data as a synchronized array of JSON objects. All Snapshot metadata is stripped and just the method provides only the data.
77
+
**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 method provides only the data.
76
78
77
79
**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.
78
80
79
81
**When would you not use it?** - When you need a more complex data structure than an array or you need the `id` of each document to use data manipulation metods. This method assumes you either are saving the `id` to the document data or using a "readonly" approach.
80
82
83
+
**Best practices** - Use this method to display data on a page. It's simple but effective. Use `.snapshotChanges()` once your needs become more complex.
// doc.id() in the value you must persist it your self
111
+
// or use .snapshotChanges() instead. See the addItem()
112
+
// method below for how to persist the id with
113
+
// valueChanges()
114
+
this.items=this.itemsCollection.valueChanges();
115
+
}
116
+
addItem(name:string) {
117
+
// Persist a document id
118
+
const id =this.afs.createId();
119
+
const item:Item= { id, item };
120
+
this.itemsCollection.add(item);
121
+
}
122
+
}
123
+
```
124
+
81
125
### `snapshotChanges()`
82
-
**What is it?** - Returns an Observable of data as a synchronized array of `DocumentChangeAction[]`.
126
+
**What is it?** - The current state of your collection. Returns an Observable of data as a synchronized array of `DocumentChangeAction[]`.
83
127
84
128
**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.
85
129
86
130
**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.
87
131
132
+
**Best practices** - Use an observable operator to transform your data from `.snapshotChanges()`. Don't return the `DocumentChangeAction[]` to the template. See the example below.
**What is it?** - Returns an Observable of the most recent changes as a `DocumentChangeAction[]`.
90
170
91
171
**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.
92
172
93
-
**When would you not use it?** - When you just need a list of data. This is a more advanced usage of AngularFirestore.
173
+
**When would you not use it?** - When you just need a list of data. This is a more advanced usage of AngularFirestore.
**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.
@@ -99,9 +210,42 @@ There are multiple ways of streaming collection data from Firestore.
99
210
100
211
**When would you not use it?** - When you just need a list of data. This is a more advanced usage of AngularFirestore.
There are three `DocumentChangeType`s in Firestore: `added`, `removed`, and `moved`. Each streaming method listens to all three by default. However, your site may only be intrested in one of these events. You can specify which events you'd like to use through the first parameter of each method:
248
+
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 intrested in one of these events. You can specify which events you'd like to use through the first parameter of each method:
105
249
106
250
#### Basic smaple
107
251
```ts
@@ -111,6 +255,8 @@ There are three `DocumentChangeType`s in Firestore: `added`, `removed`, and `mov
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.
289
+
290
+
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.
291
+
140
292
## Adding documents to a collection
141
293
142
294
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.
0 commit comments