cogsbox-shape 0.5.87 → 0.5.88
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/schema.js +15 -9
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -292,30 +292,30 @@ export function schema(schema) {
|
|
|
292
292
|
}
|
|
293
293
|
function inferDefaultFromZod(zodType, sqlConfig) {
|
|
294
294
|
if (sqlConfig && typeof sqlConfig === "object" && "type" in sqlConfig) {
|
|
295
|
-
// ---
|
|
296
|
-
// If a `default` property exists directly on the SQL config, use it.
|
|
295
|
+
// --- PRIORITY 1: Check for an explicit `default` on the SQL config ---
|
|
297
296
|
if ("default" in sqlConfig && sqlConfig.default !== undefined) {
|
|
298
|
-
//
|
|
297
|
+
// FIX #1: If the default is CURRENT_TIMESTAMP, it's a DB responsibility.
|
|
298
|
+
// Return undefined so no client-side default is generated.
|
|
299
299
|
if (sqlConfig.default === "CURRENT_TIMESTAMP") {
|
|
300
|
-
return
|
|
300
|
+
return undefined;
|
|
301
301
|
}
|
|
302
|
+
// Otherwise, use the provided SQL default.
|
|
302
303
|
return sqlConfig.default;
|
|
303
304
|
}
|
|
305
|
+
// --- PRESERVED LOGIC: Handle relation types (NO CHANGES HERE) ---
|
|
304
306
|
if (typeof sqlConfig.type === "string" &&
|
|
305
307
|
["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
|
|
306
308
|
const relationConfig = sqlConfig;
|
|
307
309
|
if (relationConfig.type === "hasMany" ||
|
|
308
310
|
relationConfig.type === "manyToMany") {
|
|
309
|
-
// For hasMany/manyToMany, default to an array based on defaultCount.
|
|
310
311
|
return Array.from({ length: relationConfig.defaultCount || 0 }, () => ({}));
|
|
311
312
|
}
|
|
312
313
|
if (relationConfig.type === "hasOne" ||
|
|
313
314
|
relationConfig.type === "belongsTo") {
|
|
314
|
-
// For hasOne/belongsTo, default to a single empty object.
|
|
315
315
|
return {};
|
|
316
316
|
}
|
|
317
317
|
}
|
|
318
|
-
// Handle SQL
|
|
318
|
+
// --- PRESERVED LOGIC: Handle basic SQL types as a fallback (NO CHANGES HERE) ---
|
|
319
319
|
const sqlTypeConfig = sqlConfig;
|
|
320
320
|
if (sqlTypeConfig.type && !sqlTypeConfig.nullable) {
|
|
321
321
|
switch (sqlTypeConfig.type) {
|
|
@@ -325,7 +325,7 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
325
325
|
case "longtext":
|
|
326
326
|
return "";
|
|
327
327
|
case "int":
|
|
328
|
-
return 0;
|
|
328
|
+
return 0;
|
|
329
329
|
case "boolean":
|
|
330
330
|
return false;
|
|
331
331
|
case "date":
|
|
@@ -337,7 +337,7 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
337
337
|
return null;
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
|
-
// Fall back to Zod-based inference
|
|
340
|
+
// --- PRESERVED LOGIC: Fall back to Zod-based inference ---
|
|
341
341
|
if (zodType instanceof z.ZodOptional) {
|
|
342
342
|
return undefined;
|
|
343
343
|
}
|
|
@@ -346,6 +346,12 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
346
346
|
? zodType._def.defaultValue()
|
|
347
347
|
: zodType._def.defaultValue;
|
|
348
348
|
}
|
|
349
|
+
// --- FIX #2: Add intelligent fallback for unrecognized Zod types ---
|
|
350
|
+
// This handles z.email(), z.url(), etc., by checking the base type.
|
|
351
|
+
if (zodType instanceof z.ZodString) {
|
|
352
|
+
return "";
|
|
353
|
+
}
|
|
354
|
+
// Return undefined if no other default can be determined.
|
|
349
355
|
return undefined;
|
|
350
356
|
}
|
|
351
357
|
// export function reference<TField extends object>(config: TField) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.88",
|
|
4
4
|
"description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|