Skip to content

Instantly share code, notes, and snippets.

@jscalderon65
Last active March 18, 2022 03:52
Show Gist options
  • Save jscalderon65/5f6297393cd6164296847c87c9faba16 to your computer and use it in GitHub Desktop.
Save jscalderon65/5f6297393cd6164296847c87c9faba16 to your computer and use it in GitHub Desktop.
Cloud Firestore Operations
const isDbReferenceExist = async (dbRoute) => {
const dbValidation = await db.doc(dbRoute).get()
if (!dbValidation.exists) {
return false
} else {
debug('Document data: ', dbValidation.data())
return true
}
}
const getElementsFromDocument = async (collectionId, docId) => {
try {
const dbRef = await db.collection(collectionId).doc(docId).get()
if (!dbRef.exists) {
debugDbError('No such document!')
throw new Error('No such document!')
} else {
debug('Document data: ', dbRef.data())
return dbRef.data()
}
} catch (error) {
debugDbError(error)
throw new Error(error)
}
}
const setElementToDocument = async (
collectionId,
docId,
propertyId,
newElement,
) => {
try {
const dbRef = db.collection(collectionId).doc(docId)
const response = await dbRef.set(
{
[propertyId]: newElement,
},
{merge: true},
)
debug(
'Document successfully written, in route: ' + collectionId + '/' + docId,
)
return response
} catch (error) {
debugDbError(error)
throw new Error(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment