Last active
November 25, 2020 16:37
-
-
Save hwindo/e54ccc38f6b312294d9188e43fa64efa to your computer and use it in GitHub Desktop.
dynamically create firebase firestore ref
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
function createRef(collectionName) { | |
let result = firestore; | |
function addResult(_result, collDocObj) { | |
let collDocKey = Object.keys(collDocObj)[0]; | |
let collDocVal = Object.values(collDocObj)[0]; | |
return _result.collection(collDocKey).doc(collDocVal); | |
} | |
if (arguments[1]) { | |
for (let i = 0, y = arguments[1].length; i < y; i++) { | |
console.log("parent " + i + ", collection: " + Object.keys(arguments[1][i]) + ", doc: " + Object.values(arguments[1][i]) + ".") | |
result = addResult(result, arguments[1][i]); | |
} | |
} | |
return result.collection(collectionName); | |
} | |
// usage | |
// createRef('collectionName', [{rootParentCollectionName: 'rootParentDocName'}, {secondParentCollectionName: 'secondParentDocName'}]) | |
// result: | |
// firestore.collection('rootParentCollectionName').doc('rootParentDocName') | |
// .collection('secondParentCollectionName').doc('secondParentDocName') | |
// .collection('collectionName') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
future improvement might be, to create 'docName' at the end