Last active
December 5, 2021 13:03
-
-
Save ZulianTiger/2e6540c704127f7d97742b154e61f9df to your computer and use it in GitHub Desktop.
Firebase firestore services
This file contains 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
/* eslint-disable */ | |
import firebaseApp from "../config/firebase"; | |
import firebase from "firebase"; | |
//READ WHOLE COLLECTION | |
export async function getAllCollection(collection) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
const data = await db.collection(collection).get(); | |
const dataResult = data.docs.map((doc) => doc.data()); | |
return new Promise((resolve) => { | |
if (data) { | |
resolve({ | |
data: dataResult, | |
}); | |
} | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
//QUERY COLLECTION WITH DESIRED CRITERIA | |
export async function queryCollection( | |
collection, | |
queryField, | |
comparission, | |
queryCriteria | |
) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
const data = await db | |
.collection(collection) | |
.where(queryField, comparission, queryCriteria) | |
.get(); | |
const dataResult = data.docs.map((doc) => doc.data()); | |
return new Promise((resolve) => { | |
if (data) { | |
resolve({ | |
data: dataResult, | |
}); | |
} | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
//ADD DOCUMENT TO COLLECTION | |
export async function addToCollection(collection, doc, data) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
db.collection(collection).doc(doc).set(data); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
//DELETE DOCUMENT FROM COLLECTION | |
export async function deleteFromCollection(collection, doc) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
db.collection(collection).doc(doc.toString()).delete(); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
//EDIT DOCUMENT | |
export async function editDocument(collection, doc, field, value) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
db.collection(collection) | |
.doc(doc.toString()) | |
.update({ [field]: value }); | |
} catch (error) { | |
console.log(error); | |
} | |
} | |
//EDIT PROFILE | |
export async function editProfile( | |
collection, | |
doc, | |
fullName, | |
phone, | |
email, | |
address, | |
zip, | |
city, | |
country, | |
company | |
) { | |
try { | |
const db = firebase.firestore(firebaseApp); | |
db.collection(collection).doc(doc.toString()).update({ | |
fullName, | |
phone, | |
email, | |
address, | |
zip, | |
city, | |
country, | |
company, | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment