JSPM

drizzle-arktype

0.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3766
  • Score
    100M100P100Q119191F
  • License Apache-2.0

Generate arktype schemas from Drizzle ORM schemas

Package Exports

  • drizzle-arktype

Readme

drizzle-arktype is a plugin for Drizzle ORM that allows you to generate arktype schemas from Drizzle ORM schemas.

Features

  • Create a select schema for tables, views and enums.
  • Create insert and update schemas for tables.
  • Supports all dialects: PostgreSQL, MySQL and SQLite.

Usage

import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-arktype';
import { type } from 'arktype';

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 updating a user - can be used to validate API requests
const updateUserSchema = createUpdateSchema(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) => schema.atLeast(1),
    role: type('string'),
});

// Usage

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