-
-
Save loiane/7f2a5c0cc00ee8dad21b57a2e7f8fff5 to your computer and use it in GitHub Desktop.
Firestore Basics, Modular Firebase 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Firestore, collectionData, collection, QueryConstraint } from '@angular/fire/firestore'; | |
import { addDoc, CollectionReference, deleteDoc, doc, getDoc, query, setDoc, updateDoc } from '@firebase/firestore'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class FirestoreService { | |
constructor(private firestore: Firestore) { } | |
createId() { | |
return doc(collection(this.firestore, 'id')).id; | |
} | |
list<T>(path: string, idField: string | undefined, ...q: QueryConstraint[]) { | |
return collectionData<T>( | |
query<T>( | |
collection(this.firestore, path) as CollectionReference<T>, | |
...q | |
), { idField } | |
); | |
} | |
add(path: string, data: any) { | |
const ref = collection(this.firestore, path); | |
return addDoc(ref, data); | |
} | |
set(path: string, data: any) { | |
const ref = doc(this.firestore, path); | |
return setDoc(ref, data); | |
} | |
get(path: string) { | |
const ref = doc(this.firestore, path); | |
return getDoc(ref); | |
} | |
update(path: string, data: any) { | |
const ref = doc(this.firestore, path); | |
return updateDoc(ref, data); | |
} | |
delete(path: string) { | |
const ref = doc(this.firestore, path); | |
return deleteDoc(ref); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment