Open rvdende opened 1 year ago
it would be nice and will save lots of effort.
I just happened to find this repo: https://github.com/maktouch/kysely-zod-codegen
I just happened to find this repo: https://github.com/maktouch/kysely-zod-codegen
@elitan Oh wow this looks great thanks for the link. Would make sense to merge this upstream, perhaps with an option to toggle it on/off.
Is @maktouch interested in opening a PR? Great work!
I'd like that! Wasn't sure if it would be accepted so I forked it cause I needed it right away.
There's a few downsides to it, it seems like I need to also generate using non-zod types for it to work correctly with kysely. It seems to work well for selects, but craps out for updates and inserts and I'm not too sure why.
I was just looking for this exact thing and came across @maktouch 's fork before this thread. I'd love to see something similar land here (or to bring that solution upstream)
@maktouch do you have any examples or reproductions of those issues?
edit: @maktouch it looks like issues are closed on ur repo? but wanted to report that it looks like the code skips generating enums rn (is supported by kysely-codegen)
@jjdonov hey, thanks for letting me know. I've enabled issues on the repo. I'll try to add reproduction for the non working inserts and updates.
Your solution should generate three shapes for each entity, insertable/updatable/selectable
Yeah I think that's the issue --- I needed it pretty quickly so right now I generate both from kysely-codegen and kysely-zod-codegen. Shitty hack, and I hope to find time to actually generate the three shapes for each entity.
Can I help with it? I'm building a CRUD API and a zod generated schema would be very helpful for validation.
I'd like that! Wasn't sure if it would be accepted so I forked it cause I needed it right away.
There's a few downsides to it, it seems like I need to also generate using non-zod types for it to work correctly with kysely. It seems to work well for selects, but craps out for updates and inserts and I'm not too sure why.
FYI I have been writing zod types for Kysely and I think with smt like this in the @maktouch generator PR it could be solved
import { Insertable, Selectable, Updateable, Kysely } from 'kysely';
import { z } from 'zod';
// have not test @maktouch gen but assume this is done from DB Schema
export const fooSchema = z.object({
foo: z.string(),
});
// Then add this to generator
export type FooTable = z.infer<typeof fooSchema>;
export type Foo = Selectable<FooTable>;
export type FooCreate = Insertable<FooTable>;
export type FooUpdate = Updateable<FooTable>;
// use it here
export type DB = {
foo: Foo;
};
export const db = new Kysely<DB>({
//...
});
Happy to help, what needs to be done.
@maktouch @RobinBlomberg great work.
I've been using my fork for almost a year now for founderpath.com
Works great. It generates a giant file though. It generates both the typescript one and the Zod ones, and they have the same name.
I'm shy to promote it because it's quite different, and I killed all the tests 😅
Please do feel free to take some logic from my fork to put it back here.
@RobinBlomberg and others.
I have a working(some test failing) version of @maktouch fork.
What would be an acceptable PR to land here.
Gen zod types based on a config flag OR always zod and TS types(no flag) ?
lmk 👍🏼
imho, zod types should be behind a flag.
To be honest, I'm reluctant to put Zod generation code directly into this codebase, adding more code to maintain and favoring it over Joi, Yup, Ajv, Valibot, JSON Schema and all other schema libraries people might use.
What I would like, however, would be one of these alternatives (probably both):
I'll keep this issue up for a while for the sake of discussion, but I want to be clear that I won't add Zod directly into this repository, as I think other solutions would be better all around.
that would be really dope and a way better solution than hardcoding. In my fork, I also needed another dialect (bigquery). It would be nice if the plugin can also handle that... I really would like to kill my fork lol
As the title says, who would be interested in generating zod schemas instead of typescript? What I see is often we have to manually create zod schemas for input validation before it gets passed to kysely, so if we could automatically keep our zod schemas in sync with the database that would be ideal!
Just curious if anyone else is looking into this?
Upvote & Fund