drizzle-zod
If you know SQL, you know Drizzle ORM
drizzle-zod
is a plugin for Drizzle ORM that allows you to generate Zod schemas from Drizzle ORM schemas.
Database | Insert schema | Select schema |
---|
PostgreSQL | ✅ | ✅ |
MySQL | ✅ | ✅ |
SQLite | ✅ | ✅ |
Usage
import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { z } from 'zod';
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const insertUserSchema = createInsertSchema(users);
const selectUserSchema = createSelectSchema(users);
const insertUserSchema = createInsertSchema(users, {
role: z.string(),
});
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
const user = insertUserSchema.parse({
name: 'John Doe',
email: 'johndoe@test.com',
role: 'admin',
});
const requestSchema = insertUserSchema.pick({ name: true, email: true });