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';
import { z } from 'zod';
import { createInsertSchema } from 'drizzle-zod';
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text<'admin' | 'user'>('role').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const newUserSchema = createInsertSchema(users);
const newUserSchema = createInsertSchema(users, 'snake');
const newUserSchema = createInsertSchema(users, 'camel');
const newUserSchema = createInsertSchema(users, {
role: z.enum(['admin', 'user']),
});
const newUserSchema = createInsertSchema(users, (schema) => ({
id: schema.id.positive(),
email: schema.email.email(),
role: z.enum(['admin', 'user']),
}));
const newUserSchema = createInsertSchema(users, 'snake', (schema) => ({
created_at: schema.createdAt.min(new Date()),
}));
const user = newUserSchema.parse({
name: 'John Doe',
email: 'johndoe@test.com',
role: 'admin',
});
const requestSchema = newUserSchema.pick({ name: true, email: true });