cogsbox-shape 0.5.86 → 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.d.ts CHANGED
@@ -45,11 +45,11 @@ export interface IBuilderMethods<T extends SQLType | RelationConfig<any>, TSql e
45
45
  }) => TNewNext) | TNewNext, defaultValue: TDefaultNext): Prettify<Builder<"new", T, TSql, z.ZodUnion<[
46
46
  TNewNext,
47
47
  z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
48
- ]>, IsLiteralType<z.infer<TNewNext>> extends true ? TDefaultNext extends () => infer R ? R : TDefaultNext : z.infer<TNewNext>, z.ZodUnion<[
48
+ ]>, IsLiteralType<z.infer<TNewNext>> extends true ? TDefaultNext extends () => infer R ? R : TDefaultNext : z.infer<TNewNext>, (TDefaultNext extends () => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
49
49
  TSql,
50
50
  TNewNext,
51
51
  z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
52
- ]>, z.ZodUnion<[
52
+ ]>, (TDefaultNext extends () => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
53
53
  TSql,
54
54
  TNewNext,
55
55
  z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
@@ -269,10 +269,6 @@ export declare function schemaRelations<TSchema extends Schema<any>, RefObject e
269
269
  type Prettify<T> = {
270
270
  [K in keyof T]: T[K];
271
271
  } & {};
272
- /**
273
- * [INTERNAL] Recursively derives a Zod schema by inspecting the builder's config.
274
- * This version correctly uses the `schema` property from the relation's config.
275
- */
276
272
  type DeriveSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema", Depth extends any[] = []> = Depth["length"] extends 10 ? any : {
277
273
  [K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand ? never : K]: T[K] extends {
278
274
  config: {
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
- // --- THIS IS THE NEW, HIGHEST-PRIORITY CHECK ---
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
- // Exclude CURRENT_TIMESTAMP as it's a special keyword, not a value.
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 new Date();
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 type-based generation (this is the fallback)
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; // This is now only used if no `default` is provided
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 (this logic is fine)
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) {
@@ -425,8 +431,6 @@ function isRelation(value) {
425
431
  "toKey" in value &&
426
432
  "schema" in value);
427
433
  }
428
- // PASTE THIS ENTIRE FUNCTION OVER YOUR EXISTING createSchema FUNCTION
429
- // The only change is the `if` condition inside the loop.
430
434
  export function createSchema(schema, relations) {
431
435
  const sqlFields = {};
432
436
  const clientFields = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.86",
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",