dappsnation / akita-ng-fire

Akita ❤️ Angular 🔥 Firebase
MIT License
131 stars 27 forks source link

Option to provide default value on syncActive #170

Closed luke-rogers closed 3 years ago

luke-rogers commented 3 years ago

I am in the process of adding a new sub collection and am using route guards to keep this in sync.

There is one route where I need to sync the parent and this new sub collection - basically the document in sub collection will have same id as one in parent, I'm just using it to reduce size of document in parent query as we can't selectively load data using firestore.

When loading the documents in a service I am combining the data from the two documents using something similar to below.

load$(id: string) {
    combineLatest([
           this.parentQuery.selectActiveId().pipe(filter(activeId => activeId === id), switchMap(() => this.selectActive()), filterNil),
           this.childQuery.selectActiveId().pipe(filter(activeId => activeId === id), switchMap(() => this.selectActive()), filterNil),
        ]).pipe(
            map(([parent, child]) =>
                ...
            )
    );
}

It's possible that the document in this.childQuery won't exist so the above code won't emit as I am using filterNil at the end. I would just remove that but the consuming component is using take(1) so I would only want it to take when all of the data is available.

I am sure I could probably work around this in other ways but it would be nice if I could provide a default value when the document doesn't exist in firestore.

Seems like that just need a change here although it might be useful to also add it to other methods such as syncDoc.

Happy to have a go at raising a PR for this if you think its a worth while change.

fritzschoff commented 3 years ago

Hey, thanks for the issue. Will have a look asap.

fritzschoff commented 3 years ago

Why don't you check before you are syncing if the sub collection exists? Or you use the startWith operator from rxjs to see if the second parameter is undefined and if so, you know that there is no sub collection. But I'm happy to review your PR if you already got a nice solution for your problem.

luke-rogers commented 3 years ago

I ended up adding a tap onto the syncActive to update the store with the default value and active id if the sync returned null.

fritzschoff commented 3 years ago

Alright! Closing the issue for now.