RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
815 stars 73 forks source link

Postgres zod #206

Closed aristotelis96 closed 2 weeks ago

aristotelis96 commented 1 month ago

First attempt to generate Zod schemas from Postgres

This PR introduces a method for generating Zod schemas based on Postgres table definitions. It maps SQL data types to the corresponding Zod types, allowing us to easily validate our API payloads against database structure.

Usage

Just run the tool as usual, including the flag --generate-zod-schemas and the output file should contain zod schemas instead of TS types.

Limitations

Only postgres is supported in this PR.

Current Status

Request for Feedback

Closes #86.

aristotelis96 commented 3 weeks ago

Rebased, added tests, formatted and fixed small issues.

hazirmagron commented 3 weeks ago

Why not merge @RobinBlomberg ?

RobinBlomberg commented 2 weeks ago

Hey, thanks for the PR!

Per my comment here, I will not add specific support for Zod into kysely-codegen (nor favor it over Joi, Yup, Ajv, Valibot, JSON Schema or any other schema library people might use).

With that said, I am almost done with a new kysely-codegen version that will make it possible to add a custom codegen through a .kysely-codegenrc.(ts|js) file. It will likely be released within ~1 week. Then it should be possible to do something like:

import { zodSerializer } from 'some-custom-kysely-codegen-serializer';

export default {
  camelCase: true,
  serializer: zodSerializer,
  url: 'postgres://user:password@localhost:5433/database',
};

So I very much welcome alternative codegens as soon as that has been released, but I don't want to maintain a possibly infinite list of codegens in the kysely-codegen core.