Documenting the difference between createSelectSchema & createInsertSchema #2398
johnrizzo4
started this conversation in
Show and tell
Replies: 1 comment
-
This ^ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It was not clear to me in the doc the difference between createSelectSchema & createInsertSchema.
I propose the enhance the readme.md with the following. I'm grateful for this framework, this is my humble contribution:
createSelectSchema vs createInsertSchema
By default, Drizzle fields are optional, unless specified .notNull().
createInsertSchema keep these nullable fields for the DB, optional for Zod.
On the contrary, the same code with createSelectSchema does not compile:
And if we remove the ": TestSelectDTO" from the code above, the compiler will not complain but at runtime the zod validation fails (.success == false).
primary keys
Drizzle makes fields nullable by default, except primary keys unless they are DB generated. In the following example, we have a regular primary key which is mandatory for Drizzle => mandatory for Zod with both createSelectSchema and createInsertSchema:
Changing the primary key to serial, will make the id optional for both the DB inserts and the Zod schemas generated by createInsertSchema:
Beta Was this translation helpful? Give feedback.
All reactions