UNPKG

@datazod/zod-sql

Version:

Convert Zod schemas to SQL table definitions with support for SQLite, PostgreSQL, and MySQL

30 lines (29 loc) 831 B
import { z } from 'zod'; /** * Determines if a Zod type is nullable or optional */ export function isNullable(zodType) { return zodType.isOptional() || zodType.isNullable(); } /** * Helper function to detect if a Zod number type is an integer */ export function isInteger(zodType) { if (!(zodType instanceof z.ZodNumber)) return false; const checks = zodType._def.checks || []; return checks.some((c) => c.kind === 'int'); } /** * Gets the appropriate identifier quote character for the given SQL dialect */ export function getQuoteChar(dialect) { return dialect === 'mysql' ? '`' : '"'; } /** * Quotes an identifier (table, column) according to the SQL dialect */ export function quoteIdentifier(identifier, dialect) { const q = getQuoteChar(dialect); return `${q}${identifier}${q}`; }