How to work with sub-collections

Continuing the discussion from Unofficial Anvil Firebase Integration v0.1šŸ”„:

First of all, thank you @mark.breuss for making integration with firebase so simple!

I am finally getting around to educating myself with firebase and am trying to structure my data in the ā€œmakes most sense (to me)ā€ way.

I had a few questions on sub-collections:

  1. How do you pull all sub-collections from a parent:

(This is more a novice question; I am not so sure this is capable from my readings online)

This is what I did and worked to get a single sub-collection:

    customer_collection = firestore.collection(firestore.db,'customers')
    query = firestore.query(customer_collection,[])
    documents = firestore.get_docs(query)
    print(documents)
    sub_collections = firestore.collection(firestore.db,f'customers/{documents[0][0]}/tasks')
    query = firestore.query(sub_collections,[])
    tasks = firestore.get_docs(query)
    print(tasks)
  1. How do you perform a ā€œCollectionGroupā€ query?

  2. Is there a way to get the parent document id from a sub-collection document or that sub-collection documents path?

Hi @anthonys,

collection group queries are currently not implemented in the client side SDK - but Iā€™ll add it to our list.

Generally speaking I would be carefull when nesting collections to much.
In your case I would proboably have one collection for customers, and another collection for tasks where each task has an attribute customer_uid. With this structure you can simply get all tasks or only the tasks for a given customer.

This way you are much more flexible in your queries and dontā€™t need the mechanisms above.

Having said that - I do not know enough about your use case to give specific advice on data structure, so take this with a grain of salt.

Thank you for the advice!

Iā€™ll think about possibly structuring my data-model differently.

Follow up question:

Is there a way to ā€œstopā€ listening to a stream?

And what is the likely-hood of this becoming an open-source project? I would definitely put in some hours and love to be able to see the actual code.

Regarding open sourcing, we are definetly open - so far it has just not been worth the hassle to be honest. But if it is of interest to people we definetly will publish the code.

If you want to stop a listener simply call unsubscribe on the listener object. Both, listen to doc an docs returns a Listener object that can be closed.

my_listener =  firestore.listen_to_doc(doc(db_ref,'customers',customer_uid),callback_func)

#later 
my_listener.unsubscribe()

Ā“Ā“Ā“
1 Like

Thank you!

That would be great if it was open source.

For example, I wanted to know how to get the user id from the return of the authentication.get_user() and had to print the __dir__() to see that uid was a attribute. Maybe this should be intuitive, but it would definitely be clearer what the project layer does on top of the firebase sdk.

Agreed!

Du you perhaps know the best practice workflow for open source anvil dependencies?
Do people mainly develop in the IDE and then periodically push the code to a public github repo?
Or the other way around?

Because from my understanding the anvil internal git repo would be a separate repository from the public repo.

But Iā€™ll look into it when time allows.

1 Like

For Anvil Extras (and labs etc.), we have the public repo on github which is the ā€˜source of truthā€™

We each do dev work wherever we like - I have a dev copy on my anvil account and I have a copy locally. Sometimes I work on one, sometimes the other.

When weā€™re ready to share some work, we create a Pull Request on the main repo and that gets reviewed. If and when itā€™s merged, a github action automatically deploys that to the live app. Itā€™s up to each developer to ensure they then update whatever dev environments they use.

2 Likes

Hi @owen.campbell,

thanks for jumping in - wanted to ask you about that for some time.
Figured you would have some kind of GH action to update the anvil repo.

Iā€™m wondering however if Anvil is planning to enable this kind of collaboration purely in the anvil ecosystem. A link that allows peoploe to join as collaborator with restricted permissions would essentially do the same thing but keep things within the platform.
Although, on second thought it would require quite a few more features, PR reviews etc until this kind of collaboration might work.

They would have to do an awful lot of work to catch up with even the very basics of what github or gitlab offer.

As soon as youā€™re collaborating with other people, code review and the tools to enable it become important and those two have had years of effort in that arena.