1 | # Upgrading to AngularFire 5.0
|
2 |
|
3 | AngularFire 5.0 is a refactor of the `AngularFireDatabase` module. It removes the `FirebaseListObservable` and `FirebaseObjectObservable` in favor of a generic based service API.
|
4 |
|
5 | ## Updating `FirebaseListObservable` to `AngularFireList<T>`
|
6 |
|
7 | Rather than `.list()` returning a `FirebaseListObservable`, it now returns an `AngularFireList<T>`. This service contains methods that allow you to manipulate and stream data.
|
8 |
|
9 | In the case of streaming back data, you now call one of the observable methods on `AngularFireList`.
|
10 |
|
11 | ### 4.0
|
12 |
|
13 | ```ts
|
14 | constructor(afDb: AngularFireDatabase) {
|
15 | afDb.list('items').subscribe(console.log);
|
16 | }
|
17 | ```
|
18 |
|
19 | ### 5.0
|
20 |
|
21 | ```ts
|
22 | constructor(afDb: AngularFireDatabase) {
|
23 | afDb.list<Item>('items').valueChanges().subscribe(console.log);
|
24 | }
|
25 | ```
|
26 |
|
27 | The same concepts apply to `FirebaseObjectObservable` to `AngularFireObject`.
|
28 |
|
29 | ## Moving away from `$key` and `$value`
|
30 |
|
31 | In AngularFireDatabase 4.0 the snapshot was automatically unwrapped for you and metadata was placed in `$` property. The Firebase Database rejects any keys with `$` in them so this mechanism allowed us to provide you with important metadata alongside your actual data. However, persisting the object could be a pain in some cases as the SDK would reject any `$` based properties. In 5.0 we have moved away from `$` properties and we provide multiple observable methods for receiving the data.
|
32 |
|
33 | Calling `.valueChanges()` returns an Observable without any metadata. If you are already persisting the key as a property then you are fine. However, if you are relying on `$key`, then you need to use `.snapshotChanges()` and transform the data with an observable `.map()`.
|
34 |
|
35 | ### 4.0
|
36 |
|
37 | ```ts
|
38 | constructor(afDb: AngularFireDatabase) {
|
39 | afDb.list('items').subscribe(items => {
|
40 | const allKeys = items.map(item => item.$key);
|
41 | });
|
42 | }
|
43 | ```
|
44 |
|
45 | ### 5.0
|
46 |
|
47 | ```ts
|
48 | constructor(afDb: AngularFireDatabase) {
|
49 | afDb.list('items').snapshotChanges().pipe(
|
50 | map(actions =>
|
51 | actions.map(a => ({ key: a.key, ...a.payload.val() }))
|
52 | )
|
53 | ).subscribe(items => {
|
54 | return items.map(item => item.key);
|
55 | });
|
56 | }
|
57 | ```
|
58 |
|
59 | ## Data manipulation methods
|
60 |
|
61 | AngularFire 5.0 removes all custom observables which means their custom operators are gone as well. Instead of using custom operators on either a `FirebaseListObservable` or a `FirebaseObjectObservable`, use the methods on the service based APIs: `AngularFireList` and `AngularFireObject`. There is no resulting code change, but it worth pointing out.
|
62 |
|
63 | ### 4.0
|
64 |
|
65 | ```ts
|
66 | constructor(afDb: AngularFireDatabase) {
|
67 | const listObservable = afDb.list('items');
|
68 | listObservable.push({ name: 'item' });
|
69 | listObservable.subscribe();
|
70 | }
|
71 | ```
|
72 |
|
73 | ### 5.0
|
74 |
|
75 | ```ts
|
76 | constructor(afDb: AngularFireDatabase) {
|
77 | const afList = afDb.list('items');
|
78 | afList.push({ name: 'item' });
|
79 | const listObservable = afList.snapshotChanges();
|
80 | listObservable.subscribe();
|
81 | }
|
82 | ```
|