Schema to type

Hey guys, how do you make a schema item as a static type ? for example, this thing:
export const orderItems = pgTable("orderItem", {
product_id: serial("product_id").notNull(),
order_id: serial("order_id").notNull(),
quantity: integer("quantity")

},
(orderItems) => ({
compoundKey: primaryKey(orderItems.product_id, orderItems.order_id),
}
))
export const orderItems = pgTable("orderItem", {
product_id: serial("product_id").notNull(),
order_id: serial("order_id").notNull(),
quantity: integer("quantity")

},
(orderItems) => ({
compoundKey: primaryKey(orderItems.product_id, orderItems.order_id),
}
))
as
{
productId: number,
order_id: number,
quantity: number
}
{
productId: number,
order_id: number,
quantity: number
}
14 Replies
Angelelz
Angelelz15mo ago
wailroth
wailrothOP15mo ago
bless you
wailroth
wailrothOP15mo ago
so I made it, but have few questions about it: for example in this code
No description
wailroth
wailrothOP15mo ago
No description
Angelelz
Angelelz15mo ago
You can't use the ifering helpers like that You have to use the table itself There are plenty of examples in the docs
wailroth
wailrothOP15mo ago
oh, okay lol i'll rtfm and stop annoying you
Angelelz
Angelelz15mo ago
I can help, no problem, but so can the docs
wailroth
wailrothOP15mo ago
yeah; but I didnt see something in the doc: can you set custom messages if something is missing... Like I did in my custom schema?
Angelelz
Angelelz15mo ago
I don't understand the question, can you write an example?
wailroth
wailrothOP15mo ago
and, like for example, with my useForm, values are string then I check the type with a transform
No description
wailroth
wailrothOP15mo ago
but in this case, with and insertProductSchema, it doesnt work and like that:
productId: z.number({
required_error: "Product id is required",
invalid_type_error: "Product id is a number"
}).optional(),
name: z.string({
required_error: "A name is required",
invalid_type_error: "Name should be a string"
}),
productId: z.number({
required_error: "Product id is required",
invalid_type_error: "Product id is a number"
}).optional(),
name: z.string({
required_error: "A name is required",
invalid_type_error: "Name should be a string"
}),
Angelelz
Angelelz15mo ago
If you're using zod, you might want to take a look at drizzle-zod It's an extension to create zod schemas for validations, seems like it's exactly what you're looking for
wailroth
wailrothOP15mo ago
In my case, I think i'll be forced to create a new schema because, for example, my product take in attributes "price" which is a number, but with the 'useForm' hook, you'r forced to take a string, then transform it to a number, then submit it like that for example:
price: z.string({
required_error: "A price is required",
invalid_type_error: "Price should be a number"
}).transform((val) => parseInt(val)),
price: z.string({
required_error: "A price is required",
invalid_type_error: "Price should be a number"
}).transform((val) => parseInt(val)),
Angelelz
Angelelz15mo ago
That's up to you how you want to handle your business logic. You can detach your front end logic from whatever you have in your backend by writing your own schema validators But you could pull the schema from the db using drizzle-zod, end then transform it however you want for your application zod has helpers for that
Want results from more Discord servers?
Add your server