zod-sqlite 1.0.2 → 1.0.4

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/dist/index.mjs ADDED
@@ -0,0 +1,283 @@
1
+ import { z } from "zod";
2
+ //#region src/utils/format-check-constraint.ts
3
+ /**
4
+ * Generates formatted CHECK constraints for validatable columns.
5
+ *
6
+ * Supports:
7
+ * - `z.enum()`: CHECK(col IN ('a', 'b'))
8
+ * - `z.literal()`: CHECK(col = 'val')
9
+ * - `z.number().min/max()`: CHECK(col >= min AND col <= max)
10
+ * - `z.string().min/max()`: CHECK(length(col) >= min)
11
+ * @param name Column name
12
+ * @param schema Zod schema
13
+ * @returns SQL CHECK constraint string or null
14
+ */
15
+ function formatCheckConstraint(name, schema) {
16
+ let currentSchema = schema;
17
+ while (true) {
18
+ const def = currentSchema._zod.def;
19
+ if (def.type === "nullable" || def.type === "optional" || def.type === "default") currentSchema = def.innerType;
20
+ else break;
21
+ }
22
+ const definition = currentSchema._zod.def;
23
+ switch (definition.type) {
24
+ case "enum": {
25
+ const entries = definition.entries;
26
+ return `CHECK(${name} IN (${Object.values(entries).map((value) => `'${String(value).replaceAll("'", "''")}'`).join(", ")}))`;
27
+ }
28
+ case "literal": {
29
+ const values = definition.values;
30
+ if (values.length === 1) {
31
+ const value = values[0];
32
+ if (typeof value === "string") return `CHECK(${name} = '${value.replaceAll("'", "''")}')`;
33
+ return `CHECK(${name} = ${value})`;
34
+ }
35
+ return `CHECK(${name} IN (${values.map((value) => {
36
+ if (typeof value === "string") return `'${value.replaceAll("'", "''")}'`;
37
+ return String(value);
38
+ }).join(", ")}))`;
39
+ }
40
+ case "number": {
41
+ const checks = definition.checks;
42
+ const constraints = [];
43
+ if (!checks) return;
44
+ for (const check of checks) {
45
+ const checkParameters = check._zod.def.check;
46
+ const checkValue = check._zod.def.value;
47
+ switch (checkParameters) {
48
+ case "greater_than":
49
+ constraints.push(`${name} >= ${checkValue}`);
50
+ break;
51
+ case "less_than": constraints.push(`${name} <= ${checkValue}`);
52
+ }
53
+ }
54
+ return constraints.length > 0 ? `CHECK(${constraints.join(" AND ")})` : void 0;
55
+ }
56
+ case "string": {
57
+ const checks = definition.checks;
58
+ const constraints = [];
59
+ if (!checks) return;
60
+ for (const check of checks) {
61
+ const checkParameters = check._zod.def.check;
62
+ const checkDefinition = check._zod.def;
63
+ switch (checkParameters) {
64
+ case "length_equals":
65
+ constraints.push(`length(${name}) = ${checkDefinition.length}`);
66
+ break;
67
+ case "max_length":
68
+ constraints.push(`length(${name}) <= ${checkDefinition.maximum}`);
69
+ break;
70
+ case "min_length": constraints.push(`length(${name}) >= ${checkDefinition.minimum}`);
71
+ }
72
+ }
73
+ return constraints.length > 0 ? `CHECK(${constraints.join(" AND ")})` : void 0;
74
+ }
75
+ case "union": {
76
+ const options = definition.options;
77
+ if (!options.every((option) => option._zod.def.type === "literal")) return;
78
+ const values = [];
79
+ for (const option of options) values.push(...option._zod.def.values);
80
+ return `CHECK(${name} IN (${values.map((value) => {
81
+ if (typeof value === "string") return `'${value.replaceAll("'", "''")}'`;
82
+ return String(value);
83
+ }).join(", ")}))`;
84
+ }
85
+ }
86
+ }
87
+ //#endregion
88
+ //#region src/utils/format-default-value.ts
89
+ /**
90
+ * Formats a default value for SQL.
91
+ *
92
+ * Handles string quoting and type conversions for DEFAULT clauses.
93
+ * @param value The default value
94
+ * @param SQLiteType Target column type
95
+ * @returns Formatted SQL default string (e.g., "DEFAULT 'val'", "DEFAULT 42")
96
+ */
97
+ function formatDefaultValue(value, SQLiteType) {
98
+ switch (SQLiteType) {
99
+ case "TEXT": return `DEFAULT '${String(value).replaceAll("'", "''")}'`;
100
+ case "INTEGER":
101
+ case "REAL": return `DEFAULT ${Number(value)}`;
102
+ case "BOOLEAN": return `DEFAULT ${value ? 1 : 0}`;
103
+ case "NULL": return "DEFAULT NULL";
104
+ default: return `DEFAULT '${value}'`;
105
+ }
106
+ }
107
+ //#endregion
108
+ //#region src/utils/map-zod-type-to-sqlite.ts
109
+ /**
110
+ * Maps a Zod type to an SQLite column type.
111
+ *
112
+ * analyzes the Zod schema type and optionally its format (e.g., z.email())
113
+ * to determine the most appropriate SQLite storage class or supported type.
114
+ * @param zodType Zod type string (e.g., 'string', 'number')
115
+ * @param schema The actual Zod schema instance
116
+ * @returns SQLite column type
117
+ */
118
+ function mapZodTypeToSQLite(zodType, schema) {
119
+ switch (zodType) {
120
+ case "string": switch (schema._zod.def.format) {
121
+ case "date": return "DATE";
122
+ case "datetime": return "DATETIME";
123
+ case "duration": return "INTEGER";
124
+ default: return "TEXT";
125
+ }
126
+ case "enum":
127
+ case "literal":
128
+ case "array":
129
+ case "template_literal":
130
+ case "object": return "TEXT";
131
+ case "date": return "DATE";
132
+ case "number": switch (schema._zod.def.format) {
133
+ case "safeint":
134
+ case "uint32":
135
+ case "int32": return "INTEGER";
136
+ case "float32":
137
+ case "float64": return "FLOAT";
138
+ default: return "REAL";
139
+ }
140
+ case "boolean": return "BOOLEAN";
141
+ case "bigint": return "BIGINT";
142
+ case "null":
143
+ case "undefined": return "NULL";
144
+ case "file": return "BLOB";
145
+ default: return "TEXT";
146
+ }
147
+ }
148
+ //#endregion
149
+ //#region src/utils/zod-to-sqlite.ts
150
+ /**
151
+ * Analyzes a Zod schema to extract SQLite metadata.
152
+ *
153
+ * Unwraps optional/nullable/default layers to find the underlying core type
154
+ * and extracts metadata like nullability and default values.
155
+ * @param schema Zod schema to analyze
156
+ * @returns Object containing SQLite type, nullability, default value, and inner schema
157
+ */
158
+ function zodToSQLite(schema) {
159
+ let nullable = false;
160
+ let defaultValue;
161
+ let currentSchema = schema;
162
+ while (true) {
163
+ const definition = currentSchema._zod.def;
164
+ const zodType = definition.type;
165
+ if (zodType === "nullable" || zodType === "optional") {
166
+ nullable = true;
167
+ currentSchema = definition.innerType;
168
+ } else if (zodType === "default") {
169
+ defaultValue = typeof definition.defaultValue === "function" ? definition.defaultValue() : definition.defaultValue;
170
+ currentSchema = definition.innerType;
171
+ } else break;
172
+ }
173
+ const mainType = currentSchema._zod.def.type;
174
+ const SQLiteType = mapZodTypeToSQLite(mainType, currentSchema);
175
+ return {
176
+ defaultValue,
177
+ nullable,
178
+ schema,
179
+ SQLiteType
180
+ };
181
+ }
182
+ //#endregion
183
+ //#region src/column-definitions.ts
184
+ /**
185
+ * Builds the SQL definition for a single column.
186
+ *
187
+ * Combines name, type, and constraints into a valid SQL column string.
188
+ * @param column Column configuration object
189
+ * @returns Complete column definition string (e.g., "id INTEGER NOT NULL")
190
+ */
191
+ function buildColumnDefinition(column) {
192
+ const { name, references, schema, unique } = column;
193
+ const { defaultValue, nullable, SQLiteType } = zodToSQLite(schema);
194
+ const parts = [name, SQLiteType];
195
+ if (!nullable) parts.push("NOT NULL");
196
+ if (defaultValue !== void 0) parts.push(formatDefaultValue(defaultValue, SQLiteType));
197
+ if (unique) parts.push("UNIQUE");
198
+ const checkConstraint = formatCheckConstraint(name, schema);
199
+ if (checkConstraint) parts.push(checkConstraint);
200
+ if (references) {
201
+ const { column: refColumn, onDelete, onUpdate, table } = references;
202
+ parts.push(`REFERENCES ${table}(${refColumn})`);
203
+ if (onDelete) parts.push(`ON DELETE ${onDelete}`);
204
+ if (onUpdate) parts.push(`ON UPDATE ${onUpdate}`);
205
+ }
206
+ return parts.join(" ");
207
+ }
208
+ //#endregion
209
+ //#region src/index-statements.ts
210
+ /**
211
+ * Generates SQL statements for table indexes.
212
+ *
213
+ * Creates standard, unique, and partial indexes based on configuration.
214
+ * @param tableName Name of the table
215
+ * @param indexes Array of index configurations
216
+ * @returns Array of CREATE INDEX SQL statements
217
+ */
218
+ function buildIndexStatements(tableName, indexes) {
219
+ if (!indexes || indexes.length === 0) return [];
220
+ return indexes.map((index) => {
221
+ const uniqueClause = index.unique ? "UNIQUE " : "";
222
+ const whereClause = index.where ? ` WHERE ${index.where}` : "";
223
+ return `CREATE ${uniqueClause}INDEX ${index.name} ON ${tableName} (${index.columns.join(", ")})${whereClause};`;
224
+ });
225
+ }
226
+ //#endregion
227
+ //#region src/primary-key-constraint.ts
228
+ /**
229
+ * Builds the PRIMARY KEY constraint string.
230
+ *
231
+ * Handles both single-column and composite primary keys.
232
+ * @param primaryKeys Array of column names forming the primary key
233
+ * @returns SQL constraint string (e.g., "PRIMARY KEY (id, email)")
234
+ */
235
+ function buildPrimaryKeyConstraint(primaryKeys) {
236
+ if (primaryKeys.length === 0) return "";
237
+ return `PRIMARY KEY (${primaryKeys.join(", ")})`;
238
+ }
239
+ //#endregion
240
+ //#region src/zod-schema.ts
241
+ /**
242
+ * Builds a Zod schema from column definitions.
243
+ *
244
+ * Creates a Zod object schema where keys are column names and values are
245
+ * the corresponding Zod schemas from the column configuration.
246
+ * @template TColumns - Array of column configurations
247
+ * @param columns Array of column configurations
248
+ * @returns Zod object schema
249
+ */
250
+ function buildZodSchema(columns) {
251
+ const schemaShape = {};
252
+ for (const column of columns) schemaShape[column.name] = column.schema;
253
+ return z.object(schemaShape);
254
+ }
255
+ //#endregion
256
+ //#region src/table.ts
257
+ /**
258
+ * Creates a table definition and Zod schema.
259
+ *
260
+ * This function is the main entry point for defining a table. It takes a configuration object
261
+ * and returns:
262
+ * 1. `table`: SQL CREATE TABLE statement
263
+ * 2. `indexes`: Array of SQL CREATE INDEX statements
264
+ * 3. `schema`: Zod object schema matching the table structure
265
+ * @template TColumns - Array of column configurations
266
+ * @param config Table configuration object
267
+ * @returns Object containing SQL statements and Zod schema
268
+ */
269
+ function createTable(config) {
270
+ const { columns, indexes, name, primaryKeys } = config;
271
+ const columnDefs = columns.map((column) => buildColumnDefinition(column));
272
+ const primaryKeyConstraint = buildPrimaryKeyConstraint(primaryKeys);
273
+ const createTableSQL = `CREATE TABLE ${name} (
274
+ ${[...columnDefs, primaryKeyConstraint].filter(Boolean).join(",\n ")}
275
+ );`;
276
+ return {
277
+ indexes: buildIndexStatements(name, indexes),
278
+ schema: buildZodSchema(columns),
279
+ table: createTableSQL
280
+ };
281
+ }
282
+ //#endregion
283
+ export { createTable };
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "zod-sqlite",
3
- "version": "1.0.2",
4
- "description": "Generate type-safe SQLite table schemas from Zod validation schemas.",
3
+ "type": "module",
4
+ "version": "1.0.4",
5
5
  "private": false,
6
- "author": {
7
- "name": "Favour Emeka",
8
- "email": "favor.emeka@minibase.com",
9
- "url": "https://github.com/favorodera"
10
- },
6
+ "description": "Generate type-safe SQLite table schemas from Zod validation schemas.",
7
+ "author": "Favour Emeka <favorodera@gmail.com>",
11
8
  "license": "MIT",
9
+ "funding": "https://github.com/sponsors/favorodera",
10
+ "homepage": "https://github.com/favorodera/zod-sqlite#readme",
12
11
  "repository": {
13
12
  "type": "git",
14
13
  "url": "git+https://github.com/favorodera/zod-sqlite.git"
@@ -16,67 +15,51 @@
16
15
  "bugs": {
17
16
  "url": "https://github.com/favorodera/zod-sqlite/issues"
18
17
  },
19
- "homepage": "https://github.com/favorodera/zod-sqlite#readme",
20
- "type": "module",
21
- "main": "./dist/index.cjs",
22
- "module": "./dist/index.js",
23
- "types": "./dist/index.d.ts",
18
+ "keywords": [
19
+ "zod",
20
+ "sqlite",
21
+ "database",
22
+ "schema",
23
+ "validation",
24
+ "zod-to-sqlite"
25
+ ],
26
+ "sideEffects": false,
24
27
  "exports": {
25
- ".": {
26
- "import": {
27
- "types": "./dist/index.d.ts",
28
- "default": "./dist/index.js"
29
- },
30
- "require": {
31
- "types": "./dist/index.d.cts",
32
- "default": "./dist/index.cjs"
33
- }
34
- },
28
+ ".": "./dist/index.mjs",
35
29
  "./package.json": "./package.json"
36
30
  },
37
31
  "files": [
38
- "dist",
39
- "README.md",
40
- "LICENSE"
32
+ "dist"
41
33
  ],
42
- "sideEffects": false,
43
- "devDependencies": {
44
- "@changesets/cli": "^2.29.8",
45
- "@stylistic/eslint-plugin": "^5.6.1",
46
- "@typescript-eslint/eslint-plugin": "^8.51.0",
47
- "@typescript-eslint/parser": "^8.51.0",
48
- "@vitest/ui": "^4.0.16",
49
- "db0": "^0.3.4",
50
- "eslint": "^9.39.2",
51
- "tsup": "^8.5.1",
52
- "typescript": "^5.9.3",
53
- "vitest": "^4.0.16"
54
- },
55
34
  "publishConfig": {
56
35
  "access": "public"
57
36
  },
58
37
  "engines": {
59
- "node": ">=24.4.0"
38
+ "node": ">=22.0.0",
39
+ "pnpm": ">=11.0.0"
60
40
  },
61
41
  "dependencies": {
62
- "zod": "^4.3.5"
42
+ "zod": "^4.4.3"
43
+ },
44
+ "devDependencies": {
45
+ "@favorodera/eslint-config": "^1.0.4",
46
+ "@types/node": "^26.2.0",
47
+ "@typescript/native-preview": "7.0.0-dev.20260707.2",
48
+ "@vitest/ui": "4.1.10",
49
+ "db0": "^0.3.4",
50
+ "eslint": "^10.8.1",
51
+ "relizy": "^1.4.9",
52
+ "tsdown": "^0.22.14",
53
+ "typescript": "^6.0.3",
54
+ "vitest": "^4.1.10"
63
55
  },
64
- "keywords": [
65
- "zod",
66
- "sqlite",
67
- "database",
68
- "schema",
69
- "validation",
70
- "zod-to-sqlite"
71
- ],
72
56
  "scripts": {
73
- "build": "pnpm clean && tsup",
74
- "dev": "tsup --watch",
75
- "test": "vitest",
57
+ "build": "tsdown",
58
+ "dev": "tsdown --watch",
59
+ "test": "vitest run",
60
+ "test:watch": "vitest --watch --ui",
76
61
  "typecheck": "tsc --noEmit",
77
- "lint": "eslint src --ext .ts --fix",
78
- "clean": "rm -rf dist",
79
- "ci:release": "pnpm build && pnpm dlx @changesets/cli publish",
80
- "ci:version": "pnpm dlx @changesets/cli version && pnpm install --no-frozen-lockfile"
62
+ "lint": "eslint . --fix",
63
+ "ready": "pnpm install && pnpm lint && pnpm typecheck && pnpm test && pnpm build"
81
64
  }
82
65
  }
package/dist/index.cjs DELETED
@@ -1,255 +0,0 @@
1
- 'use strict';
2
-
3
- require('zod/v4/core');
4
- var zod = require('zod');
5
-
6
- // src/utils/format-check-constraint.ts
7
- function formatCheckConstraint(name, schema) {
8
- let currentSchema = schema;
9
- while (true) {
10
- const def = currentSchema._zod.def;
11
- if (def.type === "nullable" || def.type === "optional" || def.type === "default") {
12
- currentSchema = def.innerType;
13
- } else {
14
- break;
15
- }
16
- }
17
- const definition = currentSchema._zod.def;
18
- const zodType = definition.type;
19
- switch (zodType) {
20
- case "enum": {
21
- const entries = definition.entries;
22
- const values = Object.values(entries);
23
- const quotedValues = values.map((value) => `'${String(value).replace(/'/g, "''")}'`).join(", ");
24
- return `CHECK(${name} IN (${quotedValues}))`;
25
- }
26
- case "literal": {
27
- const values = definition.values;
28
- if (values.length === 1) {
29
- const value = values[0];
30
- if (typeof value === "string") {
31
- return `CHECK(${name} = '${value.replace(/'/g, "''")}')`;
32
- }
33
- return `CHECK(${name} = ${value})`;
34
- }
35
- const quotedValues = values.map((value) => {
36
- if (typeof value === "string") {
37
- return `'${value.replace(/'/g, "''")}'`;
38
- }
39
- return String(value);
40
- }).join(", ");
41
- return `CHECK(${name} IN (${quotedValues}))`;
42
- }
43
- case "union": {
44
- const options = definition.options;
45
- const allAreLiterals = options.every((option) => option._zod.def.type === "literal");
46
- if (!allAreLiterals) return null;
47
- const values = [];
48
- for (const option of options) {
49
- values.push(...option._zod.def.values);
50
- }
51
- const quotedValues = values.map((value) => {
52
- if (typeof value === "string") {
53
- return `'${value.replace(/'/g, "''")}'`;
54
- }
55
- return String(value);
56
- }).join(", ");
57
- return `CHECK(${name} IN (${quotedValues}))`;
58
- }
59
- case "number": {
60
- const checks = definition.checks;
61
- const constraints = [];
62
- if (!checks) return null;
63
- for (const check of checks) {
64
- const checkParameters = check._zod.def.check;
65
- const checkValue = check._zod.def.value;
66
- switch (checkParameters) {
67
- case "greater_than":
68
- constraints.push(`${name} >= ${checkValue}`);
69
- break;
70
- case "less_than":
71
- constraints.push(`${name} <= ${checkValue}`);
72
- break;
73
- }
74
- }
75
- return constraints.length > 0 ? `CHECK(${constraints.join(" AND ")})` : null;
76
- }
77
- case "string": {
78
- const checks = definition.checks;
79
- const constraints = [];
80
- if (!checks) return null;
81
- for (const check of checks) {
82
- const checkParameters = check._zod.def.check;
83
- const checkDefinition = check._zod.def;
84
- switch (checkParameters) {
85
- case "min_length":
86
- constraints.push(`length(${name}) >= ${checkDefinition.minimum}`);
87
- break;
88
- case "max_length":
89
- constraints.push(`length(${name}) <= ${checkDefinition.maximum}`);
90
- break;
91
- case "length_equals":
92
- constraints.push(`length(${name}) = ${checkDefinition.length}`);
93
- break;
94
- }
95
- }
96
- return constraints.length > 0 ? `CHECK(${constraints.join(" AND ")})` : null;
97
- }
98
- default:
99
- return null;
100
- }
101
- }
102
-
103
- // src/utils/format-default-value.ts
104
- function formatDefaultValue(value, SQLiteType) {
105
- switch (SQLiteType) {
106
- case "TEXT":
107
- return `DEFAULT '${String(value).replace(/'/g, "''")}'`;
108
- case "INTEGER":
109
- case "REAL":
110
- return `DEFAULT ${Number(value)}`;
111
- case "BOOLEAN":
112
- return `DEFAULT ${Boolean(value) ? 1 : 0}`;
113
- case "NULL":
114
- return "DEFAULT NULL";
115
- default:
116
- return `DEFAULT '${value}'`;
117
- }
118
- }
119
- function mapZodTypeToSQLite(zodType, schema) {
120
- switch (zodType) {
121
- case "string": {
122
- const format = schema._zod.def.format;
123
- switch (format) {
124
- case "date":
125
- return "DATE";
126
- case "datetime":
127
- return "DATETIME";
128
- case "duration":
129
- return "INTEGER";
130
- default:
131
- return "TEXT";
132
- }
133
- }
134
- case "date":
135
- return "DATE";
136
- case "enum":
137
- case "literal":
138
- case "template_literal":
139
- case "array":
140
- case "object":
141
- return "TEXT";
142
- case "number": {
143
- const format = schema._zod.def.format;
144
- switch (format) {
145
- case "safeint":
146
- case "uint32":
147
- case "int32":
148
- return "INTEGER";
149
- case "float32":
150
- case "float64":
151
- return "FLOAT";
152
- default:
153
- return "REAL";
154
- }
155
- }
156
- case "boolean":
157
- return "BOOLEAN";
158
- case "bigint":
159
- return "BIGINT";
160
- case "null":
161
- case "undefined":
162
- return "NULL";
163
- case "file":
164
- return "BLOB";
165
- default:
166
- return "TEXT";
167
- }
168
- }
169
-
170
- // src/utils/zod-to-sqlite.ts
171
- function zodToSQLite(schema) {
172
- let nullable = false;
173
- let defaultValue = void 0;
174
- let currentSchema = schema;
175
- while (true) {
176
- const definition = currentSchema._zod.def;
177
- const zodType = definition.type;
178
- if (zodType === "nullable" || zodType === "optional") {
179
- nullable = true;
180
- currentSchema = definition.innerType;
181
- } else if (zodType === "default") {
182
- defaultValue = typeof definition.defaultValue === "function" ? definition.defaultValue() : definition.defaultValue;
183
- currentSchema = definition.innerType;
184
- } else {
185
- break;
186
- }
187
- }
188
- const mainType = currentSchema._zod.def.type;
189
- const SQLiteType = mapZodTypeToSQLite(mainType, currentSchema);
190
- return { SQLiteType, nullable, defaultValue, schema };
191
- }
192
-
193
- // src/core/column-definitions.ts
194
- function buildColumnDefinition(column) {
195
- const { name, schema, unique, references } = column;
196
- const { SQLiteType, nullable, defaultValue } = zodToSQLite(schema);
197
- const parts = [name, SQLiteType];
198
- if (!nullable) parts.push("NOT NULL");
199
- if (defaultValue !== void 0) parts.push(formatDefaultValue(defaultValue, SQLiteType));
200
- if (unique) parts.push("UNIQUE");
201
- const checkConstraint = formatCheckConstraint(name, schema);
202
- if (checkConstraint) parts.push(checkConstraint);
203
- if (references) {
204
- const { table, column: refColumn, onDelete, onUpdate } = references;
205
- parts.push(`REFERENCES ${table}(${refColumn})`);
206
- if (onDelete) parts.push(`ON DELETE ${onDelete}`);
207
- if (onUpdate) parts.push(`ON UPDATE ${onUpdate}`);
208
- }
209
- return parts.join(" ");
210
- }
211
-
212
- // src/core/index-statements.ts
213
- function buildIndexStatements(tableName, indexes) {
214
- if (!indexes || indexes.length === 0) return [];
215
- return indexes.map((index) => {
216
- const uniqueClause = index.unique ? "UNIQUE " : "";
217
- const whereClause = index.where ? ` WHERE ${index.where}` : "";
218
- return `CREATE ${uniqueClause}INDEX ${index.name} ON ${tableName} (${index.columns.join(", ")})${whereClause};`;
219
- });
220
- }
221
-
222
- // src/core/primary-key-constraint.ts
223
- function buildPrimaryKeyConstraint(primaryKeys) {
224
- if (primaryKeys.length === 0) return "";
225
- return `PRIMARY KEY (${primaryKeys.join(", ")})`;
226
- }
227
- function buildZodSchema(columns) {
228
- const schemaShape = {};
229
- for (const column of columns) {
230
- schemaShape[column.name] = column.schema;
231
- }
232
- return zod.z.object(schemaShape);
233
- }
234
-
235
- // src/core/table.ts
236
- function createTable(config) {
237
- const { name, columns, primaryKeys, indexes } = config;
238
- const columnDefs = columns.map(buildColumnDefinition);
239
- const primaryKeyConstraint = buildPrimaryKeyConstraint(primaryKeys);
240
- const tableElements = [...columnDefs, primaryKeyConstraint].filter(Boolean);
241
- const createTableSQL = `CREATE TABLE ${name} (
242
- ${tableElements.join(",\n ")}
243
- );`;
244
- const createIndexesSQL = buildIndexStatements(name, indexes);
245
- const zodSchema = buildZodSchema(columns);
246
- return {
247
- table: createTableSQL,
248
- indexes: createIndexesSQL,
249
- schema: zodSchema
250
- };
251
- }
252
-
253
- exports.createTable = createTable;
254
- //# sourceMappingURL=index.cjs.map
255
- //# sourceMappingURL=index.cjs.map