fireorm
fireorm copied to clipboard
Add support for collection group queries
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 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
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).
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 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.
I'll have to check firebase documentation, been really busy lately
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 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.