UNPKG

2.88 kBMarkdownView Raw
1# Upgrading to AngularFire 5.0
2
3AngularFire 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
7Rather than `.list()` returning a `FirebaseListObservable`, it now returns an `AngularFireList<T>`. This service contains methods that allow you to manipulate and stream data.
8
9In the case of streaming back data, you now call one of the observable methods on `AngularFireList`.
10
11### 4.0
12
13```ts
14constructor(afDb: AngularFireDatabase) {
15 afDb.list('items').subscribe(console.log);
16}
17```
18
19### 5.0
20
21```ts
22constructor(afDb: AngularFireDatabase) {
23 afDb.list<Item>('items').valueChanges().subscribe(console.log);
24}
25```
26
27The same concepts apply to `FirebaseObjectObservable` to `AngularFireObject`.
28
29## Moving away from `$key` and `$value`
30
31In 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
33Calling `.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
38constructor(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
48constructor(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
61AngularFire 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
66constructor(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
76constructor(afDb: AngularFireDatabase) {
77 const afList = afDb.list('items');
78 afList.push({ name: 'item' });
79 const listObservable = afList.snapshotChanges();
80 listObservable.subscribe();
81}
82```