Last active
March 18, 2022 03:52
-
-
Save jscalderon65/5f6297393cd6164296847c87c9faba16 to your computer and use it in GitHub Desktop.
Cloud Firestore Operations
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
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