patmood / pocketbase-typegen

Typescript generation for pocketbase records
https://www.npmjs.com/package/pocketbase-typegen
542 stars 20 forks source link

Would it be possible to achieve automatic typing for expanded fields? #89

Closed einarpersson closed 8 months ago

einarpersson commented 1 year ago

With https://github.com/patmood/pocketbase-typegen/pull/82 a lot became simpler.

But, currently I still have to do

const orgs = await pb.collection('organizations').getFullList<
        OrganizationsResponse<{
            contacts?: ContactsResponse[]
        }>
    >(undefined, {
        sort: 'name',
        expand: 'contacts',
    })

since I am expanding the record in this case.

But it would be nice if it was just

const orgs = await pb.collection('organizations').getFullList(undefined, {
        sort: 'name',
        expand: 'contacts',
    })

and that

  1. the expanded type was inferred by the value contacts
  2. that the expand property itself was not of type string but rather "contacts" | "...", i.e. that only valid

How much work is it to get this to work? Can it be done without changes in the pocketbase js-sdk? If not, what changes would make it feasible?

(or are there limitations that will prevent this from ever happening?)

patmood commented 1 year ago

I've looked at this before and I think it would be possible for the basic case, but expansions are very flexible and can multiple expansions, multiple levels deep. See https://pocketbase.io/docs/working-with-relations/#expanding-relations

I'm open to PRs that can handle all these cases, but unfortunately it's not something I'm motivated to add.

calebfaruki commented 8 months ago

Is it possible to keep this issue open? Would be a great feature and I'm sure someone would pick this up eventually.

patmood commented 8 months ago

I don't think this will ever be possible with the current sdk. If something changes I'm happy to revisit, but im trying to keep on top of the issues and don't want to keep them open forever.