Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
zod-to-drizzle
Advanced tools
Convert Zod schemas to Drizzle ORM tables with TypeScript support.
bun add zod-to-drizzle
# or
npm install zod-to-drizzle
# or
pnpm add zod-to-drizzle
# or
yarn add zod-to-drizzle
import { createTableFromZod } from "zod-to-drizzle";
import { z } from "zod";
import { SQLiteTable } from "drizzle-orm/sqlite-core";
// Define your schema
const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email().optional(),
createdAt: z.number().default(Date.now),
});
// Create a table
const users = createTableFromZod("users", UserSchema, {
dialect: "sqlite",
primaryKey: "id",
}) as SQLiteTable;
// Define schemas
const UserSchema = z.object({
id: z.number(),
name: z.string(),
});
const PostSchema = z.object({
id: z.number(),
title: z.string(),
userId: z.number(),
});
// Create tables with references
const users = createTableFromZod("users", UserSchema, {
dialect: "sqlite",
primaryKey: "id",
});
const posts = createTableFromZod("posts", PostSchema, {
dialect: "sqlite",
primaryKey: "id",
references: [
{
table: users,
columns: [["userId", "id"]],
},
],
});
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() | ✅ | 🔜 | 🔜 |
function createTableFromZod<T extends z.ZodObject<any>>(
tableName: string,
schema: T,
options: {
dialect?: "sqlite" | "postgres" | "mysql";
primaryKey?: keyof z.infer<T>;
references?: Array<{
table: SQLiteTable;
columns: [keyof z.infer<T>, string][];
}>;
},
);
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 referencesconst CommonSchema = z.object({
id: z.number(),
createdAt: z.number().default(Date.now()),
updatedAt: z.number().nullish(),
deletedAt: z.number().nullish(),
createdBy: z.number(),
updatedBy: z.number().nullish(),
deletedBy: z.number().nullish(),
deleted: z.boolean().default(false),
});
const UserSchema = CommonSchema.extend({
name: z.string(),
email: z.string().email(),
role: z.enum(["admin", "user"]),
});
const users = createTableFromZod("users", UserSchema, {
dialect: "sqlite",
primaryKey: "id",
});
const Schema = z.object({
id: z.number(),
metadata: z.object({ key: z.string() }), // Stored as JSON (TEXT in SQLite)
tags: z.array(z.string()), // Stored as JSON (TEXT in SQLite)
settings: z.record(z.string()), // Stored as JSON (TEXT in SQLite)
role: z.enum(["admin", "user"]), // Stored as text
});
See CONTRIBUTING.md for details on how to contribute to this project.
Apache-2.0 - see LICENSE for details.
FAQs
Create Drizzle ORM tables from Zod schemas
The npm package zod-to-drizzle receives a total of 572 weekly downloads. As such, zod-to-drizzle popularity was classified as not popular.
We found that zod-to-drizzle demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.