Last active
May 30, 2021 13:11
-
-
Save muhammadawaisshaikh/c3249c175f5183e9627b0e153f58d069 to your computer and use it in GitHub Desktop.
firebase CloudFirestore centralized api-service to consume CRUD methods of firebase from a single source of truth
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
// POST | |
createCategory() { | |
let data = { | |
name: this.programForm.value.categoryName | |
} | |
let request = this.api.post(this.config.collections.categories_table, data); | |
request.then(() => { | |
this.programForm.reset(); | |
}) | |
.catch((error) => { | |
alert(error); | |
}); | |
} | |
// GET ALL | |
getCategories() { | |
let data = this.api.getAll(this.config.collections.categories_table); | |
} | |
// GET Single | |
getCategory(id) { | |
this.api.getSingle(this.config.collections.categories_table, id).subscribe(res => { | |
console.log(res.data()); | |
}, | |
error => { | |
alert(error); | |
}) | |
} | |
// update | |
updateCategory(id) { | |
let data = { | |
name: "My Place/Community" | |
} | |
let request = this.api.put(this.config.collections.categories_table, id, data); | |
request.then(() => { | |
console.log('updated successfully') | |
}) | |
.catch((error) => { | |
alert(error); | |
}); | |
} | |
// delete | |
deleteCategory(id) { | |
let request = this.api.delete(this.config.collections.categories_table, id); | |
request.then(() => { | |
console.log('deleted successfully') | |
}) | |
.catch((error) => { | |
alert(error); | |
}); | |
} |
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 { ConfigService } from '../../../core/http/config/config.service' | |
import { AngularFirestore } from '@angular/fire/firestore'; | |
import { Observable } from 'rxjs'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class ApiService { | |
constructor( | |
private config: ConfigService, | |
private firestore: AngularFirestore, | |
) { } | |
post(collection, data) { | |
return this.firestore.collection(collection).doc().set(data); | |
} | |
getAll(collection) { | |
let data = []; | |
this.firestore.collection(collection).get().subscribe((res) => { | |
res.docs.forEach((doc) => { | |
data.push({ | |
id: doc.id, | |
...doc.data() as {} | |
}); | |
}); | |
}); | |
return new Observable((observer) => { | |
observer.next(data); | |
}); | |
} | |
getSingle(collection, id) { | |
let data = {}; | |
return new Observable((observer) => { | |
this.firestore.collection(collection).doc(id).get().subscribe(res => { | |
data = { | |
id: res.id, | |
...res.data() as {} | |
} | |
observer.next(data); | |
}) | |
}); | |
} | |
put(collection, id, data) { | |
return this.firestore.collection(collection).doc(id).update(data); | |
} | |
delete(collection, id) { | |
return this.firestore.collection(collection).doc(id).delete(); | |
} | |
getWithQuery(collection, key, operator, matchingValue) { | |
let data = []; | |
return new Observable((observer) => { | |
this.firestore.collection(collection, ref => ref.where(key, operator, matchingValue)).get().subscribe(res => { | |
res.forEach(i => { | |
data.push({ | |
id: i.id, | |
...i.data() as {} | |
}) | |
}); | |
observer.next(data); | |
}) | |
}); | |
} | |
} |
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
// -------------------------- save - Add new record -------------------------- | |
let data = { | |
name: this.programForm.value.categoryName | |
} | |
this.firestore.collection("categories").doc().set(data) | |
.then(() => { | |
this.programForm.reset(); | |
}) | |
.catch((error) => { | |
alert("Error adding document: " + error); | |
}); | |
// -------------------------- GET ALL -------------------------- | |
this.firestore.collection(this.config.collections.categories_table).get().subscribe((res) => { | |
res.docs.forEach((doc) => { | |
this.data.push(doc.data()); | |
}); | |
}); | |
// -------------------------- GET ALL - mapped with doc Id -------------------------- | |
this.firestore.collection(this.config.collections.categories_table).get().subscribe((res) => { | |
res.docs.forEach((doc) => { | |
this.data.push({ | |
id: doc.id, | |
...doc.data() as {} | |
}); | |
}); | |
}); | |
// -------------------------- GET Single -------------------------- | |
this.firestore.collection(collection).doc(id).get().subscribe(res => { | |
console.log(res.data()); | |
}, | |
error => { | |
alert(error); | |
}) | |
// -------------------------- PUT -------------------------- | |
this.firestore.collection(collection).doc(id).update(data) | |
.then(() => { | |
console.log('updated successfully') | |
}) | |
.catch((error) => { | |
alert(error); | |
}); | |
// -------------------------- DELETE -------------------------- | |
this.api.delete(this.config.collections.categories_table, id) | |
.then(() => { | |
console.log('deleted successfully') | |
}) | |
.catch((error) => { | |
alert(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment