All reads done by Firestore-Extended is using snapshotChanges().
This is done for the following reasons:
These properties are added as the property firestoreMetadata to the returned object when a document is read but will not be saved to firestore.
An object of type T
that contains the additional firestoreMetadata has the type FireItem<T>
.
FireItem is the type that is returned by all methods that return data from firestore.
listenForRestaurantById$(restaurantId: string): Observable <FireItem<RestaurantItem>> {
const docRef: DocumentReference <RestaurantItem> = getDocRefWithId(this.restaurantCollectionRef, restaurantId);
return this.firestoreExt.listenForDoc$<RestaurantItem>(docRef, restaurantSubCollectionQueries);
}
getRestaurantById$(restaurantId: string): Observable <FireItem<RestaurantItem>> {
const docRef: DocumentReference <RestaurantItem> = getDocRefWithId(this.restaurantCollectionRef, restaurantId);
return this.firestoreExt.listenForDoc$<RestaurantItem>(docRef, restaurantSubCollectionQueries).pipe(
take(1) // this makes sure that the observable stops after returning
);
}
listenForRestaurants$(): Observable <FireItem<Partial<RestaurantItem>> [] > {
return this.firestoreExt.listenForCollection$<RestaurantItem>(this.restaurantCollectionFs);
}
SubCollectionQuery documentation
const restaurantSubCollectionQueries: SubCollectionQuery[] = [
// add reviews sub Collection to restaurant object
{
name: 'reviews',
queryConstraints: [
orderBy('score')
]
},
{ // add dishes sub Collection to restaurant object
name: 'dishes',
subCollectionQueries: [
{name: 'images'} // add images sub Collection to dish object
]
},
];
export interface RestaurantItem {
name: string;
category: string;
averageReviewScore: number;
address: AddressItem;
dishes?: DishItem[]; // optional so that we can get just the base object to display in a list
reviews?: ReviewItem[]; // optional so that we can get just the base object to display in a list
}
export interface AddressItem {
zipCode: string;
city: string;
line1: string;
}
export interface DishItem {
name: string;
images: ImageItem[];
}
export interface ImageItem {
url: string;
}
export interface ReviewItem {
score: number;
text: string;
userName: string;
}