Gathering detailed insights and metrics for zod-to-drizzle
Gathering detailed insights and metrics for zod-to-drizzle
npm install zod-to-drizzle
Typescript
Module System
Min. Node Version
Node Version
NPM Version
29
Supply Chain
92.5
Quality
77.6
Maintenance
100
Vulnerability
96.1
License
TypeScript (100%)
Total Downloads
1,165
Last Day
6
Last Week
9
Last Month
66
Last Year
1,165
1 Stars
60 Commits
1 Watching
1 Branches
1 Contributors
Latest Version
0.3.1
Package Id
zod-to-drizzle@0.3.1
Unpacked Size
36.95 kB
Size
8.37 kB
File Count
7
NPM Version
10.9.1
Node Version
22.11.0
Publised On
23 Nov 2024
Cumulative downloads
Total Downloads
Last day
0%
6
Compared to previous day
Last week
-40%
9
Compared to previous week
Last month
-81.3%
66
Compared to previous month
Last year
0%
1,165
Compared to previous year
3
5
Convert Zod schemas to Drizzle ORM tables with TypeScript support.
1bun add zod-to-drizzle 2# or 3npm install zod-to-drizzle 4# or 5pnpm add zod-to-drizzle 6# or 7yarn add zod-to-drizzle
1import { createTableFromZod } from "zod-to-drizzle"; 2import { z } from "zod"; 3import { SQLiteTable } from "drizzle-orm/sqlite-core"; 4 5// Define your schema 6const UserSchema = z.object({ 7 id: z.number(), 8 name: z.string(), 9 email: z.string().email().optional(), 10 createdAt: z.number().default(Date.now), 11}); 12 13// Create a table 14const users = createTableFromZod("users", UserSchema, { 15 dialect: "sqlite", 16 primaryKey: "id", 17}) as SQLiteTable;
1// Define schemas 2const UserSchema = z.object({ 3 id: z.number(), 4 name: z.string(), 5}); 6 7const PostSchema = z.object({ 8 id: z.number(), 9 title: z.string(), 10 userId: z.number(), 11}); 12 13// Create tables with references 14const users = createTableFromZod("users", UserSchema, { 15 dialect: "sqlite", 16 primaryKey: "id", 17}); 18 19const posts = createTableFromZod("posts", PostSchema, { 20 dialect: "sqlite", 21 primaryKey: "id", 22 references: [ 23 { 24 table: users, 25 columns: [["userId", "id"]], 26 }, 27 ], 28});
Zod Type | SQLite | PostgreSQL | MySQL |
---|---|---|---|
z.string() | ✅ | 🔜 | 🔜 |
z.number() | ✅ | 🔜 | 🔜 |
z.boolean() | ✅ | 🔜 | 🔜 |
z.date() | ✅ | 🔜 | 🔜 |
z.enum() | ✅ | 🔜 | 🔜 |
z.object() (JSON) | ✅ | 🔜 | 🔜 |
z.array() (JSON) | ✅ | 🔜 | 🔜 |
z.optional() | ✅ | 🔜 | 🔜 |
z.nullable() | ✅ | 🔜 | 🔜 |
z.default() | ✅ | 🔜 | 🔜 |
1function createTableFromZod<T extends z.ZodObject<any>>( 2 tableName: string, 3 schema: T, 4 options: { 5 dialect?: "sqlite" | "postgres" | "mysql"; 6 primaryKey?: keyof z.infer<T>; 7 references?: Array<{ 8 table: SQLiteTable; 9 columns: [keyof z.infer<T>, string][]; 10 }>; 11 }, 12);
tableName
: The name of the tableschema
: Zod object schemaoptions
:
dialect
: Database dialect (default: "sqlite")primaryKey
: Column to use as primary keyreferences
: Array of foreign key references1const CommonSchema = z.object({ 2 id: z.number(), 3 createdAt: z.number().default(Date.now()), 4 updatedAt: z.number().nullish(), 5 deletedAt: z.number().nullish(), 6 createdBy: z.number(), 7 updatedBy: z.number().nullish(), 8 deletedBy: z.number().nullish(), 9 deleted: z.boolean().default(false), 10}); 11 12const UserSchema = CommonSchema.extend({ 13 name: z.string(), 14 email: z.string().email(), 15 role: z.enum(["admin", "user"]), 16}); 17 18const users = createTableFromZod("users", UserSchema, { 19 dialect: "sqlite", 20 primaryKey: "id", 21});
1const Schema = z.object({ 2 id: z.number(), 3 metadata: z.object({ key: z.string() }), // Stored as JSON (TEXT in SQLite) 4 tags: z.array(z.string()), // Stored as JSON (TEXT in SQLite) 5 settings: z.record(z.string()), // Stored as JSON (TEXT in SQLite) 6 role: z.enum(["admin", "user"]), // Stored as text 7});
See CONTRIBUTING.md for details on how to contribute to this project.
Apache-2.0 - see LICENSE for details.
No vulnerabilities found.
No security vulnerabilities found.