Skip to content

Instantly share code, notes, and snippets.

@hwindo
Last active November 25, 2020 16:37
Show Gist options
  • Save hwindo/e54ccc38f6b312294d9188e43fa64efa to your computer and use it in GitHub Desktop.
Save hwindo/e54ccc38f6b312294d9188e43fa64efa to your computer and use it in GitHub Desktop.
dynamically create firebase firestore ref
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')
@hwindo
Copy link
Author

hwindo commented Oct 16, 2017

future improvement might be, to create 'docName' at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment