drizzle-typebox
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

drizzle-typebox is a plugin for Drizzle ORM that allows you to generate @sinclair/typebox 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-typebox';
import { Type } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';

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(),
});

// Schema for inserting a user - can be used to validate API requests
const insertUserSchema = createInsertSchema(users);

// Schema for selecting a user - can be used to validate API responses
const selectUserSchema = createSelectSchema(users);

// Overriding the fields
const insertUserSchema = createInsertSchema(users, {
	role: Type.String(),
});

// Refining the fields - useful if you want to change the fields before they become nullable/optional in the final schema
const insertUserSchema = createInsertSchema(users, {
	id: (schema) => Type.Number({ minimum: 0 }),
	role: Type.String(),
});

// Usage

const isUserValid: boolean = Value.Check(insertUserSchema, {
	name: 'John Doe',
	email: 'johndoe@test.com',
	role: 'admin',
});

Package Sidebar

Install

npm i drizzle-typebox

Weekly Downloads

2,938

Version

0.1.1

License

Apache-2.0

Unpacked Size

26.5 kB

Total Files

9

Last publish

Collaborators

  • alexblokh
  • sheriman
  • dankochetov