There is two ways to configure your CollectionService
:
- With the
CollectionConfig
decorator. - By overriding default
getter
ofCollectionService
.
CollectionConfig
gives you an elegant way to define the configuration of your service :
@CollectionConfig({
path: 'movies',
idKey: 'name'
})
CollectionConfig
accept a Partial<CollectionOptions>
object as parameter that looks like that :
export interface CollectionOptions {
path: string; // The path of the collection in Firestore
idKey: string; // The key to use as an id for the document in Firestore. Default is store.idKey
}
Sometime the path is dynamic. If your service is targetting a subcollection, the path needs to know what is the id of the parent document.
In this case you'll need to override the path
getter inside the class. Let's see how to do that with a Stakeholder
of a specific movie :
@Injectable({ providedIn: 'root' })
export class StakeholderService extends CollectionService<StakeholderState> {
constructor(store: StakeholderStore, private movieQuery: MovieQuery) {
super(store);
}
get path() {
const movieId = this.movieQuery.getActiveId();
return `movies/${movieId}/stakeholders`;
}
}
- We do not need the
CollectionConfig
here. - We inject
MovieQuery
, the query of the parent collection. - We override the
path
getter by getting the active movie Id.
path
can also be an observable. That way, sync
is always going to be up-to-date with the state of your app.
To do that use selectActiveId
instead of getActiveId
:
export class StakeholderService extends CollectionService<StakeholderState> {
...
get path() {
return this.movieQuery.selectActiveId().pipe(
map(movieId => `movies/${movieId}/stakeholders`)
);
}
}