Registry / database / zod-sqlite

zod-sqlite

JSON →
library1.0.2jsnpmunverified

Generate type-safe SQLite table schemas from Zod validation schemas, v1.0.2. Requires Node >=24.4.0 and Zod v4 as a peer dependency. Provides a single createTable() function that produces SQL CREATE TABLE and INDEX statements plus a Zod object schema for runtime validation and TypeScript inference. Key differentiators: single source of truth for both database schema and validation, automatic CHECK constraints from Zod rules (min/max, enums, literals), foreign key support, and index management. Released as a new tool bridging Zod and SQLite, with active development.

npm install zod-sqlite
INSTALL
IMPORT
SIG · ZOD-SQLITE
Z
zod-sqlite
databasejavascriptv1.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createTable
✓ import { createTable } from 'zod-sqlite'
✗ const createTable = require('zod-sqlite')
ESM-only package; no CommonJS support.
z (from zod)
✓ import { z } from 'zod'
✗ import * as z from 'zod'
Common mistake is importing the entire module; z is a named export.
createTable result type
✓ import { createTable } from 'zod-sqlite'; const result = createTable({...}); type Schema = typeof result.schema
✗ import { CreateTableResult } from 'zod-sqlite'
No standalone type export; infer from return value.

Demonstrates creating a users table with primary key, unique index, and automatic CHECK constraints from Zod validators.

import { createTable } from 'zod-sqlite'; import { z } from 'zod'; const users = createTable({ name: 'users', columns: [ { name: 'id', schema: z.int() }, { name: 'email', schema: z.string().email() }, { name: 'username', schema: z.string().min(3).max(20) }, { name: 'created_at', schema: z.date().default(() => new Date()) } ], primaryKeys: ['id'], indexes: [ { name: 'idx_users_email', columns: ['email'], unique: true } ] }); console.log(users.table); // CREATE TABLE users (id INTEGER NOT NULL, email TEXT NOT NULL, username TEXT NOT NULL CHECK(length(username) >= 3 AND length(username) <= 20), created_at DATE NOT NULL DEFAULT '...', PRIMARY KEY (id)); const validData = users.schema.parse({ id: 1, email: 'test@example.com', username: 'john', created_at: new Date() }); type User = z.infer<typeof users.schema>; // { id: number; email: string; username: string; created_at: Date }
Debug
Known issues
breakingRequires Node >=24.4.0. Older versions will fail to import.
fix
Upgrade Node to >=24.4.0 or use an older major version of the package.
affects: <24.4.0
breakingRequires Zod v4 as a peer dependency. Zod v3 schemas are incompatible.
fix
Install zod@^4.0.0 alongside this package.
affects: all
deprecatedUsing z.default() without a function argument is deprecated in Zod v4. Use z.default(() => value) instead.
fix
Wrap default values in arrow functions: z.date().default(() => new Date())
affects: >=1.0
gotchaThe result.schema is a ZodObject, not a ZodEffects. Validation errors will be detailed per field.
fix
Use .safeParse() for error handling. No special treatment needed.
affects: all
gotchaColumn names must be valid SQL identifiers; spaces or special characters may cause SQL errors.
fix
Use snake_case or camelCase without spaces. Sanitize names before passing.
affects: all
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Using require() to import an ESM-only package.
fix
Change to import { createTable } from 'zod-sqlite' or use dynamic import().
Cannot find module 'zod'
Zod v4 not installed as a peer dependency.
fix
Run: npm install zod@^4.0.0
TypeError: z.date(...).default is not a function
Using .default() without parentheses or with invalid argument.
fix
Use .default(() => new Date()) in Zod v4.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
zodrequiredPeer dependency required for defining schemas; must be v4
Agent activity
34 hits · last 30 days
node
30
OpenAI (training)
1
Resources
zod-sqlite — npm install zod-sqlite · libregistry