0.0.17 • Published 5 months ago

@handfish/drizzle-effect v0.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

DatabaseInsert schemaSelect schema
PostgreSQL
MySQL
SQLite

Usage

import { pgEnum, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-effect';
import { Schema } from '@effect/schema';

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: Schema.String,
});

const emailPattern = /[^ .@]+@[^ .@]+\.[^ .@]/;

// 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.id.pipe(Schema.positive()),
  email: (schema) => schema.email.pipe(Schema.pattern(emailPattern)),
  role: Schema.String,
});

// Usage

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

// Effect schema type is also inferred from the table schema, so you have full type safety.
// Additionally it is created using `Schema.Struct`, so you can do:
const requestSchema = insertUserSchema.pick('name', 'email');
0.0.17

5 months ago

0.0.16

5 months ago

0.0.15

6 months ago

0.0.14

6 months ago

0.0.13

6 months ago

0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.9

6 months ago

0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

7 months ago

0.0.1

11 months ago