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

Auto-generate record types #152

Open
septatrix opened this issue Feb 18, 2023 · 7 comments
Open

Auto-generate record types #152

septatrix opened this issue Feb 18, 2023 · 7 comments

Comments

@septatrix
Copy link

Currently many areas are still untyped and the user has to provide custom record types in many cases. Manually maintaining these types and keeping them in sync is cumbersome and error prone.
It would be fantastic if pocketbase could export/generate a type file which one can use to get more complete type coverage similar to many OpenAPI generators. For this SDK it might even be possible to just provide a .d.ts file exporting a single interface which can be set as the generic type during client creation.

@Grubba27
Copy link

Grubba27 commented Feb 28, 2023

I was able to make a PoC using sql-ts it is definitely doable, but I think it would require some work/effort, I would argue that as it could be an awesome thing to have in pocketbase (being typesafe would be another plus), but at the cost of having more API surface to take care and probably having the need to have in dev-mode more dependencies (sql-ts require to use a driver as well) so it would be sql-ts + sqlite-3 in the dev-bundle. But the end result is good for a PoC
some prints of the result:

Photos Generated types:

Screenshot 2023-02-28 at 19 07 48

Using the API:
Screenshot 2023-02-28 at 19 33 53

Screenshot 2023-02-28 at 19 34 36

If you pass a wrong collection name:
Screenshot 2023-02-28 at 19 36 47

Would love some input/feedback.

@ganigeorgiev
Copy link
Member

ganigeorgiev commented Feb 28, 2023

@Grubba27, @septatrix There is a non-official/community maintained library that do this to some extend - https://github.com/patmood/pocketbase-typegen.

Something like the above eventually will be integrated in the SDK, but for now remains a low priority.

@david-plugge
Copy link

I faced this issue aswell and created typed-pocketbase to apply the types generated by https://github.com/patmood/pocketbase-typegen to the js sdk

@johnnypea
Copy link

This is awesome @david-plugge, thanks!

@babakfp
Copy link

babakfp commented Jan 5, 2024

Hi

I'm interested to know how this more TypeScript support is going to be implemented (whenever it's added). Is it going to be something like Typed PocketBase, or Drizzle? Can we have something like Drizzle?

"Typed PocketBase" was great, however I had to deal with things like this:

import type { ListResult } from "pocketbase"
import type { EventsResponse, UsersResponse, TiersResponse } from "$utilities/pb-typegen"

export type CustomEventsResponse = EventsResponse & {
    expand: {
        user: UsersResponse & {
            expand: {
                retainedTiers: TiersResponse[]
            }
        }
        inviter: UsersResponse & {
            expand: {
                retainedTiers: TiersResponse[]
            }
        }
    }
}

export type ListResultEventsResponse = ListResult<CustomEventsResponse>

const events: ListResultEventsResponse = await locals.pb
    .collection("events")
    .getList(1, 50, {
        sort: "-created",
        expand: "user,user.retainedTiers,inviter,inviter.retainedTiers",
    })

I would have liked to not write any of the code before await locals.pb. I would also like to have type-safety for expand, and other properties (also a better syntax). (I'm writing my wishlist! 😅).

I tried to use new things like Drizzle and Turso, but they were difficult to work with. I spent a day to get them working but, I was facing issues, one after another.

I would love to use PocketBase, but when I think about adding types (same as the above code) and struggling to find free hosting, it starts to hurt.

I hate writing this comment, I don't want to bring negativity to such an awesome project with just an awesome author, and it bugs me that I can't support this project in any way (I have no skills and no money). I hope this doesn't bring in any negative feelings ❤️.

I have a question, is it possible to use Drizzle? I don't even know what I mean, I don't even fully understand Drizzle, but I like type safety and doing less work :)

Anyways, thanks.

@david-plugge
Copy link

A new version of typed-pocketbase is going to be released soon with support for nested selects. And you will never worry about the expand field again!

@pixiedevpraveen
Copy link

Can be type like this for all collections and methods

Screenshot from 2024-08-10 18-08-56
Screenshot from 2024-08-10 18-35-03

I manage to achieve this using patmood's typegen and some type tweaks. But i can't achieve the auto completion for the fields at the time of writing it. But when I provide the fields string it provide type according to the value.

file contains my type tweaks:
pb-types-advance.ts.txt

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

No branches or pull requests

7 participants