Skip to content

Instantly share code, notes, and snippets.

@dev-sankhadip
Created November 28, 2021 10:03
Show Gist options
  • Save dev-sankhadip/180ef4c2119c56280d3ca40fa7db8551 to your computer and use it in GitHub Desktop.
Save dev-sankhadip/180ef4c2119c56280d3ca40fa7db8551 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { initializeApp } from 'firebase/app';
import { Firestore, getFirestore, collection, addDoc, getDocs, deleteDoc, doc, updateDoc, DocumentData, CollectionReference, onSnapshot, QuerySnapshot } from 'firebase/firestore'
import { Subject } from 'rxjs';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class FirebaseService {
db: Firestore;
studentCol: CollectionReference<DocumentData>;
private updatedSnapshot = new Subject<QuerySnapshot<DocumentData>>();
obsr_UpdatedSnapshot = this.updatedSnapshot.asObservable();
constructor() {
initializeApp(environment.firebaseConfig);
this.db = getFirestore();
this.studentCol = collection(this.db, 'students');
// Get Realtime Data
onSnapshot(this.studentCol, (snapshot) => {
this.updatedSnapshot.next(snapshot);
}, (err) => {
console.log(err);
})
}
async getStudents() {
const snapshot = await getDocs(this.studentCol);
return snapshot;
}
async addStudent(name: string, age: string) {
await addDoc(this.studentCol, {
name,
age
})
return;
}
async deleteStudent(docId: string) {
const docRef = doc(this.db, 'students', docId)
await deleteDoc(docRef);
return;
}
async updateStudent(docId: string, name: string, age: string) {
const docRef = doc(this.db, 'students', docId);
await updateDoc(docRef, { name, age })
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment