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

How to declare a query in the Collection options? #11

Closed
0x80 opened this issue Apr 2, 2018 · 10 comments
Closed

How to declare a query in the Collection options? #11

0x80 opened this issue Apr 2, 2018 · 10 comments
Labels

Comments

@0x80
Copy link

0x80 commented Apr 2, 2018

The docs claim that query is part of the options argument of the Collection constructor, but don't mention how to declare it. The only examples I found set the query on the already created collection reference like so:

// Create a collection and set a query on it
const col3 = new Collection('artists');
col3.query = col3.ref.orderBy('name', 'asc');

How would you pass this query as part of the options?

@IjzerenHein
Copy link
Owner

Hi Thijs. Currently you can pass in a Firestore Query object to the constructor. However, this is a somewhat cumbersome and verbose API and I have therefore not promoted it too much. It works like this:

const query = firebase().firestore().collection('albums').where('name', '>=', 'D');
const col = new Collection('albums', {
  query: query
});

I am however currently working on a new Query API which will solve this (amongst other things).
For now, I recommend however to follow the examples in the docs, and initialize the query after the constructor (by using the ref of the Collection):

const col = new Collection('albums');
col.query = col.ref.limit(10);

@0x80
Copy link
Author

0x80 commented Apr 3, 2018

Yes, that seems really too verbose. And since you're referencing the album collection twice, it even feels wrong to me.

Wouldn't it be convenient to make the options.query a function which is passed the collection reference? Since that is what the query is built on. Something like this could work I think:

const col = new Collection('albums', {
  query: ref => ref.where('name', '>=', 'D')
});

@IjzerenHein
Copy link
Owner

Hey that's actually a really elegant solution, I hadn't considered that one, nice! I'll take it into consideration when I continue work on the Query API.

@IjzerenHein
Copy link
Owner

Hey, I've finished work on the new query API & docs. I've taken your suggestion as I found it very elegant. This is how it will work:
https://github.com/IjzerenHein/firestorter/blob/feature-query-persistency-pagination/docs/Queries.md

Let me know in case you have any feedback on this.

@0x80
Copy link
Author

0x80 commented Apr 3, 2018

Awesome! That was quick 😄

I don't have any more feedback. It's looking good 👍

@IjzerenHein
Copy link
Owner

Thanks!

@0x80
Copy link
Author

0x80 commented Jun 1, 2018

Hi @IjzerenHein I was trying out this API today but ran into a problem:

public pendingUserRegistrations = new Collection("userRegistrations", {
    query: ref =>
      ref
        .where("status", "==", UserRegistrationStatus.PendingApproval)
        .orderBy("updatedAt", "asc")
        .limit(100),
  });

Here I am getting an error:

Cannot read property 'where' of undefined

To solve this I am now doing it the old-school way:

public pendingUserRegistrations = new FsCollection("userRegistrations");

constructor() {
      const col = this.pendingUserRegistrations;
      col.query = col.ref
        .where("status", "==", UserRegistrationStatus.PendingApproval)
        .orderBy("updatedAt", "asc")
        .limit(100);
  }

Could it be that I am missing something?

@IjzerenHein
Copy link
Owner

Hey Thijs. Hmm that's strange, that should have worked. Just so be sure, are you using v0.11 of firestorter?

@0x80
Copy link
Author

0x80 commented Jun 5, 2018

Somehow my version was still stuck on 0.10. I thought 0.11 would show up in yarn upgrade-interactive automatically but it doesn't.

It seems to work now! 👍

@IjzerenHein
Copy link
Owner

Sweet!

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

No branches or pull requests

2 participants