pocketbase / js-sdk

PocketBase JavaScript SDK
https://www.npmjs.com/package/pocketbase
MIT License
2.17k stars 127 forks source link

Auto-generate record types #152

Open septatrix opened 1 year ago

septatrix commented 1 year ago

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 commented 1 year ago

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](https://user-images.githubusercontent.com/70247653/221996914-419ff1fd-091a-4bd8-9778-3bedd5b12d01.png) Using the API: ![Screenshot 2023-02-28 at 19 33 53](https://user-images.githubusercontent.com/70247653/221996997-78ee3215-7e9f-4b4f-a3be-6a9f7fb9e903.png) ![Screenshot 2023-02-28 at 19 34 36](https://user-images.githubusercontent.com/70247653/221997100-2ff3d938-fe24-4acf-896d-bd84251ee436.png) If you pass a wrong collection name: ![Screenshot 2023-02-28 at 19 36 47](https://user-images.githubusercontent.com/70247653/221997480-1c80fc5f-664b-4415-92b3-06b67f4e96d2.png)

Would love some input/feedback.

ganigeorgiev commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

This is awesome @david-plugge, thanks!

babakfp commented 10 months ago

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 commented 10 months ago

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 commented 3 months ago

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