|
| 1 | +`drizzle-arktype` is a plugin for [Drizzle ORM](https://github.com/drizzle-team/drizzle-orm) that allows you to generate [arktype](https://arktype.io/) schemas from Drizzle ORM schemas. |
| 2 | + |
| 3 | +**Features** |
| 4 | + |
| 5 | +- Create a select schema for tables, views and enums. |
| 6 | +- Create insert and update schemas for tables. |
| 7 | +- Supports all dialects: PostgreSQL, MySQL and SQLite. |
| 8 | + |
| 9 | +# Usage |
| 10 | + |
| 11 | +```ts |
| 12 | +import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core'; |
| 13 | +import { createInsertSchema, createSelectSchema } from 'drizzle-arktype'; |
| 14 | +import { type } from 'arktype'; |
| 15 | + |
| 16 | +const users = pgTable('users', { |
| 17 | + id: serial('id').primaryKey(), |
| 18 | + name: text('name').notNull(), |
| 19 | + email: text('email').notNull(), |
| 20 | + role: text('role', { enum: ['admin', 'user'] }).notNull(), |
| 21 | + createdAt: timestamp('created_at').notNull().defaultNow(), |
| 22 | +}); |
| 23 | + |
| 24 | +// Schema for inserting a user - can be used to validate API requests |
| 25 | +const insertUserSchema = createInsertSchema(users); |
| 26 | + |
| 27 | +// Schema for updating a user - can be used to validate API requests |
| 28 | +const updateUserSchema = createUpdateSchema(users); |
| 29 | + |
| 30 | +// Schema for selecting a user - can be used to validate API responses |
| 31 | +const selectUserSchema = createSelectSchema(users); |
| 32 | + |
| 33 | +// Overriding the fields |
| 34 | +const insertUserSchema = createInsertSchema(users, { |
| 35 | + role: type('string'), |
| 36 | +}); |
| 37 | + |
| 38 | +// Refining the fields - useful if you want to change the fields before they become nullable/optional in the final schema |
| 39 | +const insertUserSchema = createInsertSchema(users, { |
| 40 | + id: (schema) => schema.atLeast(1), |
| 41 | + role: type('string'), |
| 42 | +}); |
| 43 | + |
| 44 | +// Usage |
| 45 | + |
| 46 | +const isUserValid = parse(insertUserSchema, { |
| 47 | + name: 'John Doe', |
| 48 | + email: 'johndoe@test.com', |
| 49 | + role: 'admin', |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +thanks @L-Mario564 |
0 commit comments