Skip to content

Commit

Permalink
Added very rough support for collectionGroup
Browse files Browse the repository at this point in the history
Only supports finding 1 subcollection at the moment
  • Loading branch information
joelpoloney committed Nov 14, 2019
1 parent eac85a7 commit cfd816c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions browser/firebasemock.js
Original file line number Diff line number Diff line change
Expand Up @@ -51778,6 +51778,28 @@ MockFirestore.prototype.batch = function () {
return batch;
};

MockFirestore.prototype.collectionGroup = function (subcollection) {
// need to find all subcollections with the name `subcollection`
const children = []
this._findAllSubcollections (subcollection, this, children)
if (children.length) {
// TODO: support more than just 1 result
return children[0]
} else {
return this.collection(subcollection)
}
}

MockFirestore.prototype._findAllSubcollections = function (name, obj, foundObjs) {
_.forEach(obj.children, (value, key) => {
if (key === name) {
foundObjs.push(value)
} else {
return this._findAllSubcollections (name, value, foundObjs)
}
})
}

MockFirestore.prototype.collection = function (path) {
return this._child(path, false);
};
Expand Down
22 changes: 22 additions & 0 deletions src/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ MockFirestore.prototype.batch = function () {
return batch;
};

MockFirestore.prototype.collectionGroup = function (subcollection) {
// need to find all subcollections with the name `subcollection`
const children = []
this._findAllSubcollections (subcollection, this, children)
if (children.length) {
// TODO: support more than just 1 result
return children[0]
} else {
return this.collection(subcollection)
}
}

MockFirestore.prototype._findAllSubcollections = function (name, obj, foundObjs) {
_.forEach(obj.children, (value, key) => {
if (key === name) {
foundObjs.push(value)
} else {
return this._findAllSubcollections (name, value, foundObjs)
}
})
}

MockFirestore.prototype.collection = function (path) {
return this._child(path, false);
};
Expand Down

0 comments on commit cfd816c

Please sign in to comment.