david-plugge / typed-pocketbase

Add types to the PocketBase JavaScript SDK
MIT License
94 stars 10 forks source link

how to use outsideof inline pocketbase query #23

Closed tigawanna closed 9 months ago

tigawanna commented 11 months ago

Thanks for the awesome package , Am here asking for some guidance on what generics to pass into the various helper methods. Am trying to compose the filters and sort and pass them into components dynamically, how do I type them? for example Am trying to pass this

const following_filter = or(
  and(["user_a.id", "=", profile_id], ["user_a_follow_user_b", "=", "yes"]),
  and(["user_b.id", "=", profile_id], ["user_b_follow_user_a", "=", "yes"]),
);

into this

        PB
          .collection("pocketbook_friendship")
          .getList(pageParam?.page,12,{
            filter: or(    
             and( ["user_a.id", "=",profile_id],),
              ["user_b.id", "=", profile_id]
              )
        })

what generic do I pass into it? I see it's supposed to be T extends BaseRecord, so should I pass in the Collection or CollectionResponse ? hovering the pocketbase client gave me this

TypedPocketBase<Schema>.collection<"pocketbook_friendship">(idOrName: "pocketbook_friendship"): TypedRecordService<PocketbookFriendshipCollection>

I had the same difficulty with sort too, is there a cheat sheet to what generic to pass into these helpers

david-plugge commented 11 months ago

Hi, glad you like it :)

Interesting use case, i havent thought about that. I think it makes sense to only provide this kind of type helpers for filter and sort, as it is impossible to resolve the return type for dynamic fields and expand options.

What do you think?

tigawanna commented 11 months ago

I some what got them working but I forgot how , the parts I remember was passing the type of the collection and type the expanded collection , sort was the tricky one.

My biggest stumbling block was that it was all guess work since , generic T,S and R are not very descriptive . It might be too much work t but if possible would be great to document what the generics required are for , in one of the ones I got working you needed to provide TypedRecord it took me a while to guess that one and I don't even remember how I used it and it's buried somewhere in my commits. The library is super awesome though and thanks for the great work

david-plugge commented 11 months ago

Good point. The type names should be refactored to be more descriptive, noted.

I played around a bit and got it working. My idea is to export two helper types that expect a collection as a generic:

import { Schema } from './schema'
import { eq, type Filter } from 'typed-pocketbase'

const following_filter: Filter<Schema['followers']> = or(
  and(["user_a.id", "=", profile_id], ["user_a_follow_user_b", "=", "yes"]),
  and(["user_b.id", "=", profile_id], ["user_b_follow_user_a", "=", "yes"]),
);
tigawanna commented 11 months ago

Helper types would be great too

david-plugge commented 9 months ago

This is available in 0.1.0! You can use

db.from('followers').createFilter(or(
  and(eq("user_a.id", profile_id]), eq("user_a_follow_user_b", "yes")),
  and(eq("user_b.id", "=", profile_id), eq("user_b_follow_user_a", "yes")),
))