Recipe for generated types + JSON?
Hey all! I'm currently migrating my application from objection to Kanon + Kysely, and I'm having trouble figuring out a good solution for the JSON Data Types. I have zod objects built for all of my JSON, but I'm unsure of how to integrate these types in a clean way.
As I see it, my best option is to import each Table's interface into a separate Database file. In that file, for each type that has a json object, omit the existing column type and union in the new column type. Of course, that results in unfortunately ugly type annotations.
The other option loses basically all of the benefits of Kanon outside of the first generation. In this method, I stop using the type auto-generation, and simply edit the json values into the schema and manually update the kysely schema whenever I need to update.
Lastly, I could do the first method, only instead of omitting and unioning, I could explicitly map out the keys and values referencing the old tables.
Are there any easier ways to do this? Has anyone else using Kanon figured out a clean way to manage these additional types?
6 Replies
Hey 👋
Kanel*. @thelinuxlich knows best about Kanel.
I can provide insights as a zod contributor.
@significantotter hey, I'm not at the PC right now but Kanel supports adding custom types through column COMMENTS, but it's undocumented, you will need to take a look at the source to figure out
Take a look at this issue https://github.com/kristiandupont/kanel/issues/429
GitHub
How to handle JSON/JSONB columns? · Issue #429 · kristiandupont/kan...
How do I enforce a specific type for a JSON column(like a sub-schema)?
That doesn’t exactly solve my issue. I don’t want a general custom type for JSONB. I want to assign each specific JSONB column the proper type for that object
I need to be able to later reference the type rather than supply it wholesale. The types are pretty large
Read the issue again because you are misunderstanding something
I must be, sorry! Will do
Ah! I see the import type comment now. I’ll play with that