pg2zod 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +131 -0
- package/README.md +404 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +180 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +216 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/src-18pV45Fu.js +835 -0
- package/dist/src-18pV45Fu.js.map +1 -0
- package/package.json +73 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { n as generateZodSchemasString } from "./src-18pV45Fu.js";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
//#region src/cli.ts
|
|
7
|
+
const DEFAULT_OUTPUT = "schema.ts";
|
|
8
|
+
/**
|
|
9
|
+
* Parse command line arguments
|
|
10
|
+
*/
|
|
11
|
+
function parseArgs() {
|
|
12
|
+
const args = process.argv.slice(2);
|
|
13
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
14
|
+
printHelp();
|
|
15
|
+
process.exit(0);
|
|
16
|
+
}
|
|
17
|
+
if (args.includes("--version") || args.includes("-v")) {
|
|
18
|
+
console.log("pg-to-zod 1.0.0");
|
|
19
|
+
process.exit(0);
|
|
20
|
+
}
|
|
21
|
+
const connectionUrl = getArg(args, "--url");
|
|
22
|
+
let config;
|
|
23
|
+
if (connectionUrl) config = parseConnectionUrl(connectionUrl);
|
|
24
|
+
else config = {
|
|
25
|
+
host: getArg(args, "--host") || process.env.PGHOST || "localhost",
|
|
26
|
+
port: parseInt(getArg(args, "--port") || process.env.PGPORT || "5432"),
|
|
27
|
+
database: getArg(args, "--database") || process.env.PGDATABASE || "postgres",
|
|
28
|
+
user: getArg(args, "--user") || process.env.PGUSER || "postgres",
|
|
29
|
+
password: getArg(args, "--password") || process.env.PGPASSWORD || "",
|
|
30
|
+
ssl: args.includes("--ssl")
|
|
31
|
+
};
|
|
32
|
+
const options = {
|
|
33
|
+
schemas: getArgArray(args, "--schemas") || ["public"],
|
|
34
|
+
tables: getArgArray(args, "--tables"),
|
|
35
|
+
excludeTables: getArgArray(args, "--exclude-tables"),
|
|
36
|
+
generateInputSchemas: !args.includes("--no-input-schemas"),
|
|
37
|
+
includeCompositeTypes: args.includes("--composite-types"),
|
|
38
|
+
useBrandedTypes: args.includes("--branded-types"),
|
|
39
|
+
strictMode: args.includes("--strict"),
|
|
40
|
+
includeComments: !args.includes("--no-comments"),
|
|
41
|
+
useCamelCase: args.includes("--camel-case")
|
|
42
|
+
};
|
|
43
|
+
const output = getArg(args, "--output") || getArg(args, "-o") || DEFAULT_OUTPUT;
|
|
44
|
+
return {
|
|
45
|
+
config,
|
|
46
|
+
options,
|
|
47
|
+
output
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get argument value
|
|
52
|
+
*/
|
|
53
|
+
function getArg(args, flag) {
|
|
54
|
+
const index = args.indexOf(flag);
|
|
55
|
+
if (index !== -1 && index + 1 < args.length) return args[index + 1];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get argument array value (comma-separated)
|
|
59
|
+
*/
|
|
60
|
+
function getArgArray(args, flag) {
|
|
61
|
+
const value = getArg(args, flag);
|
|
62
|
+
if (value) return value.split(",").map((s) => s.trim());
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Parse PostgreSQL connection URL
|
|
66
|
+
*/
|
|
67
|
+
function parseConnectionUrl(url) {
|
|
68
|
+
const match = url.match(/postgresql:\/\/([^:]+):([^@]+)@([^:\/]+):(\d+)\/(.+)/);
|
|
69
|
+
if (!match) throw new Error("Invalid connection URL format");
|
|
70
|
+
const [, user, password, host, port, database] = match;
|
|
71
|
+
return {
|
|
72
|
+
host,
|
|
73
|
+
port: parseInt(port),
|
|
74
|
+
database,
|
|
75
|
+
user,
|
|
76
|
+
password,
|
|
77
|
+
ssl: url.includes("sslmode=require")
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Print help message
|
|
82
|
+
*/
|
|
83
|
+
function printHelp() {
|
|
84
|
+
console.log(`
|
|
85
|
+
pg-to-zod - Generate strict Zod v4 schemas from PostgreSQL database
|
|
86
|
+
|
|
87
|
+
USAGE:
|
|
88
|
+
pg-to-zod [OPTIONS]
|
|
89
|
+
|
|
90
|
+
CONNECTION OPTIONS:
|
|
91
|
+
--url <url> PostgreSQL connection URL
|
|
92
|
+
(postgresql://user:pass@host:port/db)
|
|
93
|
+
--host <host> Database host (default: localhost)
|
|
94
|
+
--port <port> Database port (default: 5432)
|
|
95
|
+
--database <database> Database name (default: postgres)
|
|
96
|
+
--user <user> Database user (default: postgres)
|
|
97
|
+
--password <password> Database password
|
|
98
|
+
--ssl Use SSL connection
|
|
99
|
+
|
|
100
|
+
GENERATION OPTIONS:
|
|
101
|
+
--schemas <schemas> Comma-separated list of schemas (default: public)
|
|
102
|
+
--tables <tables> Comma-separated list of tables to include
|
|
103
|
+
--exclude-tables <tables> Comma-separated list of tables to exclude
|
|
104
|
+
--no-input-schemas Skip generating input schemas (generated by default)
|
|
105
|
+
--composite-types Include composite types (skipped by default)
|
|
106
|
+
--branded-types Use branded types for IDs
|
|
107
|
+
--strict Fail on unmapped types instead of using z.unknown()
|
|
108
|
+
--no-comments Don't include comments in generated code
|
|
109
|
+
--camel-case Use camelCase for field names
|
|
110
|
+
|
|
111
|
+
OUTPUT OPTIONS:
|
|
112
|
+
--output <file> Output file path (default: schema.ts)
|
|
113
|
+
-o <file> Short form of --output
|
|
114
|
+
|
|
115
|
+
OTHER OPTIONS:
|
|
116
|
+
--help, -h Show this help message
|
|
117
|
+
--version, -v Show version
|
|
118
|
+
|
|
119
|
+
ENVIRONMENT VARIABLES:
|
|
120
|
+
PGHOST Database host
|
|
121
|
+
PGPORT Database port
|
|
122
|
+
PGDATABASE Database name
|
|
123
|
+
PGUSER Database user
|
|
124
|
+
PGPASSWORD Database password
|
|
125
|
+
|
|
126
|
+
EXAMPLES:
|
|
127
|
+
# Generate schemas from local database
|
|
128
|
+
pg-to-zod --database mydb --output src/schemas.ts
|
|
129
|
+
|
|
130
|
+
# Use connection URL
|
|
131
|
+
pg-to-zod --url postgresql://user:pass@localhost:5432/mydb
|
|
132
|
+
|
|
133
|
+
# Generate with input schemas and camelCase
|
|
134
|
+
pg-to-zod --database mydb --input-schemas --camel-case
|
|
135
|
+
|
|
136
|
+
# Include specific tables only
|
|
137
|
+
pg-to-zod --database mydb --tables users,posts,comments
|
|
138
|
+
|
|
139
|
+
# Exclude specific tables
|
|
140
|
+
pg-to-zod --database mydb --exclude-tables migrations,internal
|
|
141
|
+
|
|
142
|
+
# Multiple schemas
|
|
143
|
+
pg-to-zod --database mydb --schemas public,auth,api
|
|
144
|
+
`);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Main CLI function
|
|
148
|
+
*/
|
|
149
|
+
async function main() {
|
|
150
|
+
try {
|
|
151
|
+
const { config, options, output } = parseArgs();
|
|
152
|
+
console.log("🔍 Introspecting database...");
|
|
153
|
+
console.log(` Database: ${config.database}`);
|
|
154
|
+
console.log(` Schemas: ${options.schemas?.join(", ") || "public"}`);
|
|
155
|
+
const schema = await generateZodSchemasString(config, options);
|
|
156
|
+
const outputPath = path.resolve(process.cwd(), output);
|
|
157
|
+
await fs.writeFile(outputPath, schema, "utf-8");
|
|
158
|
+
console.log("✅ Schema generated successfully!");
|
|
159
|
+
console.log(` Output: ${outputPath}`);
|
|
160
|
+
const enumCount = (schema.match(/export const \w+Schema = z\.enum\(/g) || []).length;
|
|
161
|
+
const tableCount = (schema.match(/export const \w+Schema = z\.object\({/g) || []).length - enumCount;
|
|
162
|
+
console.log(` Tables: ${tableCount}`);
|
|
163
|
+
console.log(` Enums: ${enumCount}`);
|
|
164
|
+
if (schema.includes("// Warnings")) {
|
|
165
|
+
const warningLines = schema.split("\n").filter((line) => line.startsWith("// - ")).map((line) => line.slice(5));
|
|
166
|
+
if (warningLines.length > 0) {
|
|
167
|
+
console.log("\n⚠️ Warnings:");
|
|
168
|
+
for (const warning of warningLines) console.log(` ${warning}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.error("❌ Error:", error instanceof Error ? error.message : error);
|
|
173
|
+
process.exit(1);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
main();
|
|
177
|
+
|
|
178
|
+
//#endregion
|
|
179
|
+
export { };
|
|
180
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport * as fs from 'node:fs/promises';\nimport * as path from 'node:path';\nimport {generateZodSchemasString} from './index.js';\nimport type {DatabaseConfig, SchemaGenerationOptions} from './types.js';\n\nconst DEFAULT_OUTPUT = 'schema.ts';\n\n/**\n * Parse command line arguments\n */\nfunction parseArgs(): {\n config: DatabaseConfig;\n options: SchemaGenerationOptions;\n output: string;\n} {\n const args = process.argv.slice(2);\n\n if (args.includes('--help') || args.includes('-h')) {\n printHelp();\n process.exit(0);\n }\n\n if (args.includes('--version') || args.includes('-v')) {\n console.log('pg-to-zod 1.0.0');\n process.exit(0);\n }\n\n // Parse connection string or individual params\n const connectionUrl = getArg(args, '--url');\n let config: DatabaseConfig;\n\n if (connectionUrl) {\n config = parseConnectionUrl(connectionUrl);\n } else {\n config = {\n host: getArg(args, '--host') || process.env.PGHOST || 'localhost',\n port: parseInt(getArg(args, '--port') || process.env.PGPORT || '5432'),\n database: getArg(args, '--database') || process.env.PGDATABASE || 'postgres',\n user: getArg(args, '--user') || process.env.PGUSER || 'postgres',\n password: getArg(args, '--password') || process.env.PGPASSWORD || '',\n ssl: args.includes('--ssl'),\n };\n }\n\n const options: SchemaGenerationOptions = {\n schemas: getArgArray(args, '--schemas') || ['public'],\n tables: getArgArray(args, '--tables'),\n excludeTables: getArgArray(args, '--exclude-tables'),\n generateInputSchemas: !args.includes('--no-input-schemas'), // Default: true\n includeCompositeTypes: args.includes('--composite-types'), // Default: false\n useBrandedTypes: args.includes('--branded-types'),\n strictMode: args.includes('--strict'),\n includeComments: !args.includes('--no-comments'),\n useCamelCase: args.includes('--camel-case'),\n };\n\n const output = getArg(args, '--output') || getArg(args, '-o') || DEFAULT_OUTPUT;\n\n return {config, options, output};\n}\n\n/**\n * Get argument value\n */\nfunction getArg(args: string[], flag: string): string | undefined {\n const index = args.indexOf(flag);\n if (index !== -1 && index + 1 < args.length) {\n return args[index + 1];\n }\n return undefined;\n}\n\n/**\n * Get argument array value (comma-separated)\n */\nfunction getArgArray(args: string[], flag: string): string[] | undefined {\n const value = getArg(args, flag);\n if (value) {\n return value.split(',').map((s) => s.trim());\n }\n return undefined;\n}\n\n/**\n * Parse PostgreSQL connection URL\n */\nfunction parseConnectionUrl(url: string): DatabaseConfig {\n const match = url.match(\n /postgresql:\\/\\/([^:]+):([^@]+)@([^:\\/]+):(\\d+)\\/(.+)/\n );\n\n if (!match) {\n throw new Error('Invalid connection URL format');\n }\n\n const [, user, password, host, port, database] = match;\n\n return {\n host,\n port: parseInt(port),\n database,\n user,\n password,\n ssl: url.includes('sslmode=require'),\n };\n}\n\n/**\n * Print help message\n */\nfunction printHelp(): void {\n console.log(`\npg-to-zod - Generate strict Zod v4 schemas from PostgreSQL database\n\nUSAGE:\n pg-to-zod [OPTIONS]\n\nCONNECTION OPTIONS:\n --url <url> PostgreSQL connection URL\n (postgresql://user:pass@host:port/db)\n --host <host> Database host (default: localhost)\n --port <port> Database port (default: 5432)\n --database <database> Database name (default: postgres)\n --user <user> Database user (default: postgres)\n --password <password> Database password\n --ssl Use SSL connection\n\nGENERATION OPTIONS:\n --schemas <schemas> Comma-separated list of schemas (default: public)\n --tables <tables> Comma-separated list of tables to include\n --exclude-tables <tables> Comma-separated list of tables to exclude\n --no-input-schemas Skip generating input schemas (generated by default)\n --composite-types Include composite types (skipped by default)\n --branded-types Use branded types for IDs\n --strict Fail on unmapped types instead of using z.unknown()\n --no-comments Don't include comments in generated code\n --camel-case Use camelCase for field names\n\nOUTPUT OPTIONS:\n --output <file> Output file path (default: schema.ts)\n -o <file> Short form of --output\n\nOTHER OPTIONS:\n --help, -h Show this help message\n --version, -v Show version\n\nENVIRONMENT VARIABLES:\n PGHOST Database host\n PGPORT Database port\n PGDATABASE Database name\n PGUSER Database user\n PGPASSWORD Database password\n\nEXAMPLES:\n # Generate schemas from local database\n pg-to-zod --database mydb --output src/schemas.ts\n\n # Use connection URL\n pg-to-zod --url postgresql://user:pass@localhost:5432/mydb\n\n # Generate with input schemas and camelCase\n pg-to-zod --database mydb --input-schemas --camel-case\n\n # Include specific tables only\n pg-to-zod --database mydb --tables users,posts,comments\n\n # Exclude specific tables\n pg-to-zod --database mydb --exclude-tables migrations,internal\n\n # Multiple schemas\n pg-to-zod --database mydb --schemas public,auth,api\n`);\n}\n\n/**\n * Main CLI function\n */\nasync function main(): Promise<void> {\n try {\n const {config, options, output} = parseArgs();\n\n console.log('🔍 Introspecting database...');\n console.log(` Database: ${config.database}`);\n console.log(` Schemas: ${options.schemas?.join(', ') || 'public'}`);\n\n const schema = await generateZodSchemasString(config, options);\n\n // Write to file\n const outputPath = path.resolve(process.cwd(), output);\n await fs.writeFile(outputPath, schema, 'utf-8');\n\n console.log('✅ Schema generated successfully!');\n console.log(` Output: ${outputPath}`);\n\n // Count generated items\n const enumCount = (schema.match(/export const \\w+Schema = z\\.enum\\(/g) || []).length;\n const tableCount = (schema.match(/export const \\w+Schema = z\\.object\\({/g) || []).length - enumCount;\n\n console.log(` Tables: ${tableCount}`);\n console.log(` Enums: ${enumCount}`);\n\n // Show warnings if any\n if (schema.includes('// Warnings')) {\n const warningLines = schema\n .split('\\n')\n .filter((line) => line.startsWith('// - '))\n .map((line) => line.slice(5));\n\n if (warningLines.length > 0) {\n console.log('\\n⚠️ Warnings:');\n for (const warning of warningLines) {\n console.log(` ${warning}`);\n }\n }\n }\n\n } catch (error) {\n console.error('❌ Error:', error instanceof Error ? error.message : error);\n process.exit(1);\n }\n}\n\nmain();\n"],"mappings":";;;;;;AAOA,MAAM,iBAAiB;;;;AAKvB,SAAS,YAIP;CACE,MAAM,OAAO,QAAQ,KAAK,MAAM,EAAE;AAElC,KAAI,KAAK,SAAS,SAAS,IAAI,KAAK,SAAS,KAAK,EAAE;AAChD,aAAW;AACX,UAAQ,KAAK,EAAE;;AAGnB,KAAI,KAAK,SAAS,YAAY,IAAI,KAAK,SAAS,KAAK,EAAE;AACnD,UAAQ,IAAI,kBAAkB;AAC9B,UAAQ,KAAK,EAAE;;CAInB,MAAM,gBAAgB,OAAO,MAAM,QAAQ;CAC3C,IAAI;AAEJ,KAAI,cACA,UAAS,mBAAmB,cAAc;KAE1C,UAAS;EACL,MAAM,OAAO,MAAM,SAAS,IAAI,QAAQ,IAAI,UAAU;EACtD,MAAM,SAAS,OAAO,MAAM,SAAS,IAAI,QAAQ,IAAI,UAAU,OAAO;EACtE,UAAU,OAAO,MAAM,aAAa,IAAI,QAAQ,IAAI,cAAc;EAClE,MAAM,OAAO,MAAM,SAAS,IAAI,QAAQ,IAAI,UAAU;EACtD,UAAU,OAAO,MAAM,aAAa,IAAI,QAAQ,IAAI,cAAc;EAClE,KAAK,KAAK,SAAS,QAAQ;EAC9B;CAGL,MAAM,UAAmC;EACrC,SAAS,YAAY,MAAM,YAAY,IAAI,CAAC,SAAS;EACrD,QAAQ,YAAY,MAAM,WAAW;EACrC,eAAe,YAAY,MAAM,mBAAmB;EACpD,sBAAsB,CAAC,KAAK,SAAS,qBAAqB;EAC1D,uBAAuB,KAAK,SAAS,oBAAoB;EACzD,iBAAiB,KAAK,SAAS,kBAAkB;EACjD,YAAY,KAAK,SAAS,WAAW;EACrC,iBAAiB,CAAC,KAAK,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/C,UAAQ,IAAI,cAAc,aAAa;EAGvC,MAAM,aAAa,OAAO,MAAM,sCAAsC,IAAI,EAAE,EAAE;EAC9E,MAAM,cAAc,OAAO,MAAM,yCAAyC,IAAI,EAAE,EAAE,SAAS;AAE3F,UAAQ,IAAI,cAAc,aAAa;AACvC,UAAQ,IAAI,aAAa,YAAY;AAGrC,MAAI,OAAO,SAAS,cAAc,EAAE;GAChC,MAAM,eAAe,OAChB,MAAM,KAAK,CACX,QAAQ,SAAS,KAAK,WAAW,QAAQ,CAAC,CAC1C,KAAK,SAAS,KAAK,MAAM,EAAE,CAAC;AAEjC,OAAI,aAAa,SAAS,GAAG;AACzB,YAAQ,IAAI,kBAAkB;AAC9B,SAAK,MAAM,WAAW,aAClB,SAAQ,IAAI,MAAM,UAAU;;;UAKnC,OAAO;AACZ,UAAQ,MAAM,YAAY,iBAAiB,QAAQ,MAAM,UAAU,MAAM;AACzE,UAAQ,KAAK,EAAE;;;AAIvB,MAAM"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Database connection configuration
|
|
4
|
+
*/
|
|
5
|
+
interface DatabaseConfig {
|
|
6
|
+
host: string;
|
|
7
|
+
port: number;
|
|
8
|
+
database: string;
|
|
9
|
+
user: string;
|
|
10
|
+
password: string;
|
|
11
|
+
ssl?: boolean | {
|
|
12
|
+
rejectUnauthorized: boolean;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Column metadata from introspection
|
|
17
|
+
*/
|
|
18
|
+
interface ColumnMetadata {
|
|
19
|
+
columnName: string;
|
|
20
|
+
dataType: string;
|
|
21
|
+
isNullable: boolean;
|
|
22
|
+
columnDefault: string | null;
|
|
23
|
+
characterMaximumLength: number | null;
|
|
24
|
+
numericPrecision: number | null;
|
|
25
|
+
numericScale: number | null;
|
|
26
|
+
datetimePrecision: number | null;
|
|
27
|
+
udtName: string;
|
|
28
|
+
domainName: string | null;
|
|
29
|
+
arrayDimensions: number;
|
|
30
|
+
isArray: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Check constraint metadata
|
|
34
|
+
*/
|
|
35
|
+
interface CheckConstraintMetadata {
|
|
36
|
+
constraintName: string;
|
|
37
|
+
checkClause: string;
|
|
38
|
+
columnName: string | null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Enum type metadata
|
|
42
|
+
*/
|
|
43
|
+
interface EnumMetadata {
|
|
44
|
+
enumName: string;
|
|
45
|
+
enumValues: string[];
|
|
46
|
+
schemaName: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Composite type metadata
|
|
50
|
+
*/
|
|
51
|
+
interface CompositeTypeMetadata {
|
|
52
|
+
typeName: string;
|
|
53
|
+
schemaName: string;
|
|
54
|
+
attributes: Array<{
|
|
55
|
+
attributeName: string;
|
|
56
|
+
dataType: string;
|
|
57
|
+
attributeNumber: number;
|
|
58
|
+
}>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Range type metadata
|
|
62
|
+
*/
|
|
63
|
+
interface RangeTypeMetadata {
|
|
64
|
+
rangeName: string;
|
|
65
|
+
subtype: string;
|
|
66
|
+
schemaName: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Domain type metadata
|
|
70
|
+
*/
|
|
71
|
+
interface DomainMetadata {
|
|
72
|
+
domainName: string;
|
|
73
|
+
dataType: string;
|
|
74
|
+
schemaName: string;
|
|
75
|
+
characterMaximumLength: number | null;
|
|
76
|
+
numericPrecision: number | null;
|
|
77
|
+
numericScale: number | null;
|
|
78
|
+
isNullable: boolean;
|
|
79
|
+
domainDefault: string | null;
|
|
80
|
+
checkConstraints: CheckConstraintMetadata[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Table metadata
|
|
84
|
+
*/
|
|
85
|
+
interface TableMetadata {
|
|
86
|
+
tableName: string;
|
|
87
|
+
schemaName: string;
|
|
88
|
+
columns: ColumnMetadata[];
|
|
89
|
+
checkConstraints: CheckConstraintMetadata[];
|
|
90
|
+
primaryKeys: string[];
|
|
91
|
+
uniqueConstraints: Array<{
|
|
92
|
+
constraintName: string;
|
|
93
|
+
columns: string[];
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Complete database schema metadata
|
|
98
|
+
*/
|
|
99
|
+
interface DatabaseMetadata {
|
|
100
|
+
tables: TableMetadata[];
|
|
101
|
+
enums: EnumMetadata[];
|
|
102
|
+
compositeTypes: CompositeTypeMetadata[];
|
|
103
|
+
rangeTypes: RangeTypeMetadata[];
|
|
104
|
+
domains: DomainMetadata[];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Schema generation options
|
|
108
|
+
*/
|
|
109
|
+
interface SchemaGenerationOptions {
|
|
110
|
+
/** Include only specific schemas (default: ['public']) */
|
|
111
|
+
schemas?: string[];
|
|
112
|
+
/** Include only specific tables (default: all) */
|
|
113
|
+
tables?: string[];
|
|
114
|
+
/** Exclude specific tables */
|
|
115
|
+
excludeTables?: string[];
|
|
116
|
+
/** Generate input schemas (for create/update operations) (default: true) */
|
|
117
|
+
generateInputSchemas?: boolean;
|
|
118
|
+
/** Include composite types in generation (default: false) */
|
|
119
|
+
includeCompositeTypes?: boolean;
|
|
120
|
+
/** Use branded types for IDs and specific fields */
|
|
121
|
+
useBrandedTypes?: boolean;
|
|
122
|
+
/** Strict mode: fail on unmapped types instead of using z.unknown() */
|
|
123
|
+
strictMode?: boolean;
|
|
124
|
+
/** Include comments in generated code */
|
|
125
|
+
includeComments?: boolean;
|
|
126
|
+
/** Use camelCase for field names (default: keep original) */
|
|
127
|
+
useCamelCase?: boolean;
|
|
128
|
+
/** Custom type mappings */
|
|
129
|
+
customTypeMappings?: Record<string, string>;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Generated schema result
|
|
133
|
+
*/
|
|
134
|
+
interface GeneratedSchema {
|
|
135
|
+
tableName: string;
|
|
136
|
+
schemaName: string;
|
|
137
|
+
readSchema: string;
|
|
138
|
+
inputSchema?: string;
|
|
139
|
+
typeDefinitions: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Complete generation result
|
|
143
|
+
*/
|
|
144
|
+
interface GenerationResult {
|
|
145
|
+
schemas: GeneratedSchema[];
|
|
146
|
+
enums: Array<{
|
|
147
|
+
name: string;
|
|
148
|
+
code: string;
|
|
149
|
+
}>;
|
|
150
|
+
compositeTypes: Array<{
|
|
151
|
+
name: string;
|
|
152
|
+
code: string;
|
|
153
|
+
}>;
|
|
154
|
+
domains: Array<{
|
|
155
|
+
name: string;
|
|
156
|
+
code: string;
|
|
157
|
+
}>;
|
|
158
|
+
ranges: Array<{
|
|
159
|
+
name: string;
|
|
160
|
+
code: string;
|
|
161
|
+
}>;
|
|
162
|
+
warnings: string[];
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/introspect.d.ts
|
|
166
|
+
/**
|
|
167
|
+
* Introspect a PostgreSQL database and return complete metadata
|
|
168
|
+
*/
|
|
169
|
+
declare function introspectDatabase(config: DatabaseConfig, options?: SchemaGenerationOptions): Promise<DatabaseMetadata>;
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/generator.d.ts
|
|
172
|
+
/**
|
|
173
|
+
* Generate Zod schemas from database metadata
|
|
174
|
+
*/
|
|
175
|
+
declare function generateSchemas(metadata: DatabaseMetadata, options?: SchemaGenerationOptions): GenerationResult;
|
|
176
|
+
/**
|
|
177
|
+
* Format the complete output file
|
|
178
|
+
*/
|
|
179
|
+
declare function formatOutput(result: GenerationResult): string;
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/type-mapper.d.ts
|
|
182
|
+
/**
|
|
183
|
+
* Map PostgreSQL column to Zod schema string with strict validation
|
|
184
|
+
*/
|
|
185
|
+
declare function mapColumnToZod(column: ColumnMetadata, metadata: DatabaseMetadata, options: SchemaGenerationOptions, warnings: string[]): string;
|
|
186
|
+
/**
|
|
187
|
+
* Convert snake_case to PascalCase
|
|
188
|
+
*/
|
|
189
|
+
declare function toPascalCase(str: string): string;
|
|
190
|
+
/**
|
|
191
|
+
* Convert snake_case to camelCase
|
|
192
|
+
*/
|
|
193
|
+
declare function toCamelCase(str: string): string;
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/index.d.ts
|
|
196
|
+
/**
|
|
197
|
+
* Main function: introspect database and generate Zod schemas
|
|
198
|
+
*/
|
|
199
|
+
declare function generateZodSchemas(config: DatabaseConfig, options?: SchemaGenerationOptions): Promise<GenerationResult>;
|
|
200
|
+
/**
|
|
201
|
+
* Convenience function: generate schemas and return formatted output string
|
|
202
|
+
*/
|
|
203
|
+
declare function generateZodSchemasString(config: DatabaseConfig, options?: SchemaGenerationOptions): Promise<string>;
|
|
204
|
+
/**
|
|
205
|
+
* Default export
|
|
206
|
+
*/
|
|
207
|
+
declare const _default: {
|
|
208
|
+
generateZodSchemas: typeof generateZodSchemas;
|
|
209
|
+
generateZodSchemasString: typeof generateZodSchemasString;
|
|
210
|
+
introspectDatabase: typeof introspectDatabase;
|
|
211
|
+
generateSchemas: typeof generateSchemas;
|
|
212
|
+
formatOutput: typeof formatOutput;
|
|
213
|
+
};
|
|
214
|
+
//#endregion
|
|
215
|
+
export { type CheckConstraintMetadata, type ColumnMetadata, type CompositeTypeMetadata, type DatabaseConfig, type DatabaseMetadata, type DomainMetadata, type EnumMetadata, type GeneratedSchema, type GenerationResult, type RangeTypeMetadata, type SchemaGenerationOptions, type TableMetadata, _default as default, formatOutput, generateSchemas, generateZodSchemas, generateZodSchemasString, introspectDatabase, mapColumnToZod, toCamelCase, toPascalCase };
|
|
216
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/introspect.ts","../src/generator.ts","../src/type-map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H,CAAA,EIhEP,OJgEO,CAAA,MAAA,CAAA;;;;cI7DT,QJiEU,EAAA;EAAc,kBAAA,EAAA,yBAAA;EAMR,wBAAA,EAAuB,+BA6BX;EAMZ,kBAAe,EAAA,yBAAA;EAWf,eAAA,EAAA,sBAAgB;EACtB,YAAA,EAAA,mBAAA;CACF"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as generateSchemas, c as toPascalCase, i as formatOutput, l as introspectDatabase, n as generateZodSchemasString, o as mapColumnToZod, r as src_default, s as toCamelCase, t as generateZodSchemas } from "./src-18pV45Fu.js";
|
|
2
|
+
|
|
3
|
+
export { src_default as default, formatOutput, generateSchemas, generateZodSchemas, generateZodSchemasString, introspectDatabase, mapColumnToZod, toCamelCase, toPascalCase };
|