fireorm icon indicating copy to clipboard operation
fireorm copied to clipboard

Add support for collection group queries

Open joemckie opened this issue 5 years ago • 7 comments

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.

joemckie avatar Nov 29 '19 15:11 joemckie

@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?

z-hirschtritt avatar Dec 02 '19 16:12 z-hirschtritt

@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).

joemckie avatar Dec 03 '19 09:12 joemckie

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?

wovalle avatar Dec 04 '19 09:12 wovalle

@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.

joemckie avatar Dec 04 '19 10:12 joemckie

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

wovalle avatar Dec 09 '19 13:12 wovalle

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

miroo93 avatar May 31 '20 05:05 miroo93

@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.

zhirschtritt avatar Jun 01 '20 13:06 zhirschtritt