Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for collection group queries #125

Open
joemckie opened this issue Nov 29, 2019 · 7 comments
Open

Add support for collection group queries #125

joemckie opened this issue Nov 29, 2019 · 7 comments

Comments

@joemckie
Copy link
Contributor

https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query

I'm going to start working on this soon but thought I'd create an issue for transparency and to discuss syntax.

Perhaps a syntax along the lines of:

collectionGroup(Entity).where(...)

This should be fairly simple as the collection name can be derived from the entity.

@z-hirschtritt
Copy link
Contributor

@joemckie sounds good! does there might need to be an intermediate utility function to extract the intended collectionGroup name? I might be misunderstanding the concept, but can't an Entity have/belong-to multiple collectionGroups?

@joemckie
Copy link
Contributor Author

joemckie commented Dec 3, 2019

@z-hirschtritt

does there might need to be an intermediate utility function to extract the intended collectionGroup name?

The collection group name would be the name of the entity when it's saved into the database, so for Band, the collection group name would be 'Bands'. collectionGroup would grab this name when you pass in the entity

I might be misunderstanding the concept, but can't an Entity have/belong-to multiple collectionGroups?

Collection groups essentially allow you to search across multiple collections/subcollections with the same name (useful for searching inside subcollections without needing to know the parent record).

@wovalle
Copy link
Owner

wovalle commented Dec 4, 2019

I don't understand how the syntax would work. AFAIK Collection groups are a way to retrieve results from subcollections in all documents inside a collection.

In fireorm a "Collection" is represented by an entity class and you need to instantiate a repository to interact with the collection, how would create a collectionGroup query in the repository class?

@joemckie
Copy link
Contributor Author

joemckie commented Dec 4, 2019

@wovalle Collection groups are a way to get results from any collection with the same name, not just limited to subcollections. We would be able to determine the collection name (e.g. Bands) from the entity name, as we can use the same logic that is used to save the collection in the first place.

We don't need to interact with the collection at all, it just needs to use the collection name to form a query.

@wovalle
Copy link
Owner

wovalle commented Dec 9, 2019

I'll have to check firebase documentation, been really busy lately

@miroo93
Copy link

miroo93 commented May 31, 2020

Hi,

Was there any progress on this?

Been working with subcollection with fireorm lately, and now in need to query specific sub-collections across collections as described in the firestore doc here

As far as syntax, I would imagine we would be able to instantiate a repository for subcollection as such:

const albumRepository = getRepository(Album);
const album70s =  albumRepository.whereLessThan('releaseDate', new Date('1980-01-01')).find();

Here the intent would be to query albums across all Bands.

Thanks

@zhirschtritt
Copy link
Contributor

@miroo93 The first thing I see is that where the collection method returns a reference to a specific collection, the collectionGroup method returns a Query against the datastore as a whole. It would be awkward to try and model it as a repository, though it seems like that would be a reasonable abstraction on first glance.

However, you can still use fireorm's QueryBuilder to increase type safety of the query itself and light wrapper around collectionGroup + QueryBuilder might be the logical next step for this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants