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

There is a way to auto query subcollections? #299

Open
iamgbayer opened this issue Apr 16, 2022 · 2 comments
Open

There is a way to auto query subcollections? #299

iamgbayer opened this issue Apr 16, 2022 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@iamgbayer
Copy link

As the title said when I'm querying a document that contains a subcollection, can this subcollection be automatically queried?

In this example, wishes is a subcollection of the document wishlist, but since I receive wishes not queried, I need to query it manually

image

I've recently started using this amazing orm, sorry if I'm doing something wrong

@wovalle
Copy link
Owner

wovalle commented Jun 17, 2022

Hi!

I'm sorry for the late response but sadly I there's no way to query subcollections directly before fetching the collection. That's because the path must be constructed before querying a subcollection and so far we have no way of doing this in userland.

I'm open to suggestions though

@wovalle wovalle added enhancement New feature or request help wanted Extra attention is needed labels Jun 17, 2022
@gelouko
Copy link

gelouko commented Dec 13, 2022

A suggestion would be to set eager loading properties when querying the data:
(This is my first time seeing this repo, I haven't used it yet haha! So sorry if the following code makes no sense at all)

// Entity
import { Collection, SubCollection, ISubCollection } from 'fireorm';

class Album {
  id: string;
  name: string;
  year: number;
}

@Collection()
export class Band {
  id: string;
  name: string;
  formationYear: number;
  genres: Array<string>;

  @SubCollection(Album)
  albums?: ISubCollection<Album>;
}
const band = await bandRepository
  .whereGreaterThan(band => band.formationYear, 1985)
  .whereArrayCointain(band => band.genres, 'progressive-rock')
  .eagerLoad(band => band.albums) // <- when querying, state which properties are subcollections that should be eager loaded.
  .find();

console.log(band.albums.map(album => album.name) // correctly prints the albums' names
// QueryBuilder

function eagerLoad(...props: IWherePropParam<T>[]) {
  for (const prop of props) {
    if (isSubcollection) { // check if the property has the subCollection decorator
      this.eagerLoadProps.push(prop)
    }
  }
}
// BaseFirestoreRepository

async execute(
    queries: Array<IFireOrmQueryLine>,
    limitVal?: number,
    orderByObj?: IOrderByParams,
    single?: boolean,
    customQuery?: ICustomQuery<T>
    eagerLoadedProps?: IWherePropParam<T>[]
  ): Promise<T[]> {
    let query = queries.reduce<Query>((acc, cur) => {
      const op = cur.operator as WhereFilterOp;
      return acc.where(cur.prop, op, cur.val);
    }, this.firestoreColRef);
    ...
    
    const executedQuery = query.get().then(this.extractTFromColSnap);

    // The following code definitely does not work, but I think it will explain the idea
    if (executedQuery.eagerLoadedProps) {
       return await Promise.all(eagerLoadedProps.map((prop) => executedQuery.propName = firestore.getAll(prop())))
    }

    return executedQuery
  }

I know this is not the correct code, but I hope it could give some light on a solution for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants