Created
January 27, 2024 21:10
-
-
Save nezahualcoyotl/355b539d223243172bdbb2fba17e6cea to your computer and use it in GitHub Desktop.
Functions to ease firestore instructions because I keep forgetting how to use them
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
const admin = require("firebase-admin"); | |
const db = admin.firestore(); | |
function getCollection(collectionName) { | |
const collection = db.collection(collectionName); | |
return collection; | |
} | |
async function getCollectionWithQuery( | |
collectionName, | |
column, | |
operator, | |
value, | |
limit = 0 | |
) { | |
const collection = getCollection(collectionName); | |
const filteredCollectionReference = collection | |
.where(column, operator, value) | |
.limit(limit); | |
const filteredCollection = await filteredCollectionReference.get(); | |
return filteredCollection; | |
} | |
async function getCollectionSnapshot(collectionName) { | |
const collection = getCollection(collectionName); | |
const snapshot = await collection.get(); | |
return snapshot; | |
} | |
async function getDoc(collectionName, docId) { | |
const collection = getCollection(collectionName); | |
const doc = await collection.doc(docId).get(); | |
return doc; | |
} | |
async function createDoc(collectionName, doc) { | |
const collection = getCollection(collectionName); | |
const newDoc = await collection.add(doc); | |
return newDoc; | |
} | |
async function updateDoc(collectionName, docId, doc) { | |
const collection = getCollection(collectionName); | |
const updatedDoc = await collection.doc(docId).update(doc); | |
return updatedDoc; | |
} | |
async function deleteDoc(collectionName, docId) { | |
const collection = getCollection(collectionName); | |
await collection.doc(docId).delete(); | |
return true; | |
} | |
async function createSubcollectionDoc( | |
collectionName, | |
docId, | |
subCollectionName, | |
subCollectionDoc | |
) { | |
const subCollection = await getSubCollectionReference( | |
collectionName, | |
docId, | |
subCollectionName | |
); | |
const newSubCollectionDoc = await subCollection.add(subCollectionDoc); | |
return newSubCollectionDoc; | |
} | |
async function getSubCollectionReference( | |
collectionName, | |
docId, | |
subCollectionName, | |
limit = 0 | |
) { | |
const doc = await getDoc(collectionName, docId); | |
const subCollection = doc.ref.collection(subCollectionName).limit(limit); | |
return subCollection; | |
} | |
async function getSubCollectionSnapshot( | |
collectionName, | |
docId, | |
subCollectionName, | |
limit | |
) { | |
const subCollection = await getSubCollectionReference( | |
collectionName, | |
docId, | |
subCollectionName, | |
limit | |
); | |
const snapshot = await subCollection.get(); | |
return snapshot; | |
} | |
async function getSubcollectionDoc( | |
collectionName, | |
docId, | |
subCollectionName, | |
subCollectionDocId | |
) { | |
const subCollection = await getSubCollectionSnapshot( | |
collectionName, | |
docId, | |
subCollectionName | |
); | |
const subCollectionDoc = subCollection.docs.find( | |
(doc) => doc.id === subCollectionDocId | |
); | |
return subCollectionDoc; | |
} | |
async function createUser(email, password, displayName) { | |
const userRecord = await admin.auth().createUser({ | |
email: email, | |
password: password, | |
displayName: displayName, | |
}); | |
return userRecord; | |
} | |
module.exports = { | |
getCollection, | |
getCollectionWithQuery, | |
getCollectionSnapshot, | |
getDoc, | |
createDoc, | |
updateDoc, | |
deleteDoc, | |
createSubcollectionDoc, | |
getSubCollectionReference, | |
getSubCollectionSnapshot, | |
getSubcollectionDoc, | |
createUser, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment