cogsbox-shape 0.5.87 → 0.5.89

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.
Files changed (2) hide show
  1. package/dist/schema.js +21 -13
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -165,11 +165,13 @@ function createBuilder(config) {
165
165
  : config.sqlZod; // If only a primitive is passed, the "new" schema is still the SQL one.
166
166
  const finalDefaultValue = hasSchemaArg
167
167
  ? isFunction(defaultValue)
168
- ? defaultValue() // If it's a function, call it
169
- : defaultValue // If it's a direct value, use it as-is
168
+ ? defaultValue()
169
+ : defaultValue
170
170
  : isFunction(schemaOrDefault)
171
- ? schemaOrDefault()
172
- : schemaOrDefault;
171
+ ? schemaOrDefault({ sql: config.sqlZod })
172
+ : schemaOrDefault && schemaOrDefault._def
173
+ ? inferDefaultFromZod(schemaOrDefault, config.sqlConfig)
174
+ : schemaOrDefault;
173
175
  const newCompletedStages = new Set(completedStages);
174
176
  newCompletedStages.add("new");
175
177
  // ---- THIS IS THE RUNTIME FIX THAT MATCHES YOUR INTERFACE ----
@@ -292,30 +294,30 @@ export function schema(schema) {
292
294
  }
293
295
  function inferDefaultFromZod(zodType, sqlConfig) {
294
296
  if (sqlConfig && typeof sqlConfig === "object" && "type" in sqlConfig) {
295
- // --- THIS IS THE NEW, HIGHEST-PRIORITY CHECK ---
296
- // If a `default` property exists directly on the SQL config, use it.
297
+ // --- PRIORITY 1: Check for an explicit `default` on the SQL config ---
297
298
  if ("default" in sqlConfig && sqlConfig.default !== undefined) {
298
- // Exclude CURRENT_TIMESTAMP as it's a special keyword, not a value.
299
+ // FIX #1: If the default is CURRENT_TIMESTAMP, it's a DB responsibility.
300
+ // Return undefined so no client-side default is generated.
299
301
  if (sqlConfig.default === "CURRENT_TIMESTAMP") {
300
- return new Date();
302
+ return undefined;
301
303
  }
304
+ // Otherwise, use the provided SQL default.
302
305
  return sqlConfig.default;
303
306
  }
307
+ // --- PRESERVED LOGIC: Handle relation types (NO CHANGES HERE) ---
304
308
  if (typeof sqlConfig.type === "string" &&
305
309
  ["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
306
310
  const relationConfig = sqlConfig;
307
311
  if (relationConfig.type === "hasMany" ||
308
312
  relationConfig.type === "manyToMany") {
309
- // For hasMany/manyToMany, default to an array based on defaultCount.
310
313
  return Array.from({ length: relationConfig.defaultCount || 0 }, () => ({}));
311
314
  }
312
315
  if (relationConfig.type === "hasOne" ||
313
316
  relationConfig.type === "belongsTo") {
314
- // For hasOne/belongsTo, default to a single empty object.
315
317
  return {};
316
318
  }
317
319
  }
318
- // Handle SQL type-based generation (this is the fallback)
320
+ // --- PRESERVED LOGIC: Handle basic SQL types as a fallback (NO CHANGES HERE) ---
319
321
  const sqlTypeConfig = sqlConfig;
320
322
  if (sqlTypeConfig.type && !sqlTypeConfig.nullable) {
321
323
  switch (sqlTypeConfig.type) {
@@ -325,7 +327,7 @@ function inferDefaultFromZod(zodType, sqlConfig) {
325
327
  case "longtext":
326
328
  return "";
327
329
  case "int":
328
- return 0; // This is now only used if no `default` is provided
330
+ return 0;
329
331
  case "boolean":
330
332
  return false;
331
333
  case "date":
@@ -337,7 +339,7 @@ function inferDefaultFromZod(zodType, sqlConfig) {
337
339
  return null;
338
340
  }
339
341
  }
340
- // Fall back to Zod-based inference (this logic is fine)
342
+ // --- PRESERVED LOGIC: Fall back to Zod-based inference ---
341
343
  if (zodType instanceof z.ZodOptional) {
342
344
  return undefined;
343
345
  }
@@ -346,6 +348,12 @@ function inferDefaultFromZod(zodType, sqlConfig) {
346
348
  ? zodType._def.defaultValue()
347
349
  : zodType._def.defaultValue;
348
350
  }
351
+ // --- FIX #2: Add intelligent fallback for unrecognized Zod types ---
352
+ // This handles z.email(), z.url(), etc., by checking the base type.
353
+ if (zodType instanceof z.ZodString) {
354
+ return "";
355
+ }
356
+ // Return undefined if no other default can be determined.
349
357
  return undefined;
350
358
  }
351
359
  // 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.87",
3
+ "version": "0.5.89",
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",