Registry / database / pg-tables-to-zod

pg-tables-to-zod

JSON →
library1.0.24jsnpmunverified

A CLI tool and Node module (v1.0.24) that introspects PostgreSQL database schemas and generates Zod v4 type schemas for each table and view. It connects directly to a Postgres instance using pg-structure, outputs TypeScript files, and supports customizable configuration via JSON files. Unlike manual schema writing or ORM-based generators, it provides a focused, zero-runtime-dependency pipeline for keeping Zod definitions in sync with database schemas. Release cadence is irregular; last update was in 2025.

npm install pg-tables-to-zod
INSTALL
IMPORT
SIG · PG-TABLES-TO-ZOD
P
pg-tables-to-zod
databasejavascriptv1.0.24
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.

convert
✓ import { convert } from 'pg-tables-to-zod'
✗ const convert = require('pg-tables-to-zod').convert
Package is ESM-only. CommonJS require will fail unless using Node's .mjs extension or dynamic import.
TConfiguration
✓ import type { TConfiguration } from 'pg-tables-to-zod'
✗ import { TConfiguration } from 'pg-tables-to-zod'
TConfiguration is a type, should be imported with 'type' for runtime efficiency. However, both work in TypeScript; the wrong pattern is only a linting concern, not a runtime error.
OutputSchema
✓ import type { OutputSchema } from 'pg-tables-to-zod'
✗ import { OutputSchema } from 'pg-tables-to-zod'
Same as TConfiguration – OutputSchema is a type. Use type import to avoid bundling issues.

Connects to a Postgres database and generates Zod schemas for all tables in the public schema, writing them to ./schemas.

import { convert } from 'pg-tables-to-zod'; const configuration = { connection: { host: process.env.PG_HOST ?? 'localhost', port: parseInt(process.env.PG_PORT ?? '5432'), user: process.env.PG_USER ?? 'postgres', password: process.env.PG_PASSWORD ?? '', database: process.env.PG_DATABASE ?? 'mydb', }, schema: ['public'], outputPath: './schemas', generateIndex: true, }; try { const outputSchemas = await convert(configuration); console.log(`Generated ${outputSchemas.length} schema files.`); } catch (error) { console.error('Conversion failed:', error); }
Debug
Known issues
breakingZod v4 is required; the generated schemas use Zod v4 syntax (e.g., z.string().minLength(1)) which is incompatible with Zod v3.
fix
Ensure your project uses zod@^4.0.0. If using zod v3, the generated schemas will cause runtime errors.
affects: >=1.0.0
gotchaThe generated schemas are for Zod v4, which changed keyword casing (e.g., .minLength() instead of .min()). If you are on Zod v3, you must either upgrade or manually rewrite the output.
fix
Upgrade to zod v4 or use a different tool (e.g., schemats) for Zod v3 compatibility.
affects: >=1.0.0
gotchaThe convert() function is async and returns an array of OutputSchema objects. Calling it without await will return a Promise, not the results.
fix
Always use await convert(configuration) or .then().
affects: >=1.0.0
Errors
Common errors & fixes
Could not find a declaration file for module 'pg-tables-to-zod'.
TypeScript cannot locate the module’s type definitions (missing ‘types’ field in package.json or not installed).
fix
Install the package with npm install pg-tables-to-zod (types are bundled). Ensure tsconfig.json has `moduleResolution: 'node'` (or 'node16' for ESM).
Cannot find module 'pg-tables-to-zod' or its corresponding type declarations.
The package is not installed or the import path is incorrect.
fix
Run npm install pg-tables-to-zod. Verify the package is in node_modules.
TypeError: zod.z is not a function
The generated schemas import zod as default while your project has zod v3 which exports differently.
fix
Upgrade to zod v4: npm install zod@latest
Upgrade
Version history
1.0.24latest on npm
Audit
Dependencies
typescriptoptionalCompile-time peer dependency for type generation
Agent activity
6 hits · last 30 days
node
6
Resources