kossnocorp / typesaurus

🦕 Type-safe TypeScript-first ODM for Firestore
https://typesaurus.com
415 stars 36 forks source link

accessing sub collections from a Typesaurus Doc #134

Open drewdearing opened 4 months ago

drewdearing commented 4 months ago

I have a nested collection db.

export const db = schema(($) => ({
  items: $.collection<Item>().sub({
    sub_items: $.collection<SubItem>(),
  }),
}));

When I access an Item Doc, I expect to be able to query the sub items directly from the item using some instance method.

const [items] = useRead(db.items.all().on);

items.map((item) => item.sub_items.all()...)

It looks like this is not currently supported, unless I'm missing something. I can do something like this, but I can see this getting very annoying to deal with in situations where I have a triple nested collections.

//get sub_item's items
db.items(subItem.data.item_ref.id).sub_items(subItem.ref.id).items.all();

It basically means I need to store the ref of every ancestor doc in my sub_item doc in order to access the sub_item's subcollection.

Please tell me I'm missing something.

kossnocorp commented 4 months ago

It looks like this is not currently supported, unless I'm missing something. I can do something like this, but I can see this getting very annoying to deal with in situations where I have a triple nested collections.

You're right. I planned to allow accessing doc/ref subcollections through sub property, but I never got to implement it.

It would look something like this:

item.sub.sub_items.all()
drewdearing commented 4 months ago

item.sub.sub_items.all()

All I can say is that is exactly what I am looking for. Would be happy to look into it as well if I get the time. I can predict having to write out the db collection chain on every query will become quite ruthless.