cogsbox-shape 0.5.105 → 0.5.107

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 +40 -29
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -472,38 +472,49 @@ export function createSchema(schema, relations) {
472
472
  if (definition && definition.config) {
473
473
  const config = definition.config;
474
474
  const sqlConfig = config.sql;
475
- if (sqlConfig &&
476
- typeof sqlConfig === "object" &&
477
- ["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
478
- // Handle relations
479
- const relatedSchemaFactory = sqlConfig.schema;
480
- const childSchemaResult = createSchema(relatedSchemaFactory());
481
- let baseClientSchema;
482
- if (sqlConfig.type === "hasMany" || sqlConfig.type === "manyToMany") {
483
- baseClientSchema = z.array(childSchemaResult.clientSchema).optional();
484
- defaultValues[key] = Array.from({ length: sqlConfig.defaultCount || 0 }, () => childSchemaResult.defaultValues);
475
+ if (definition && definition.config) {
476
+ const config = definition.config;
477
+ const sqlConfig = config.sql;
478
+ if (sqlConfig &&
479
+ typeof sqlConfig === "object" &&
480
+ ["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
481
+ // Handle relations
482
+ const relatedSchemaFactory = sqlConfig.schema;
483
+ let baseClientSchema;
484
+ if (sqlConfig.type === "hasMany" || sqlConfig.type === "manyToMany") {
485
+ baseClientSchema = z.array(z.any()).optional();
486
+ // Make it a FUNCTION that returns the array
487
+ defaultValues[key] = () => Array.from({ length: sqlConfig.defaultCount || 0 }, () => {
488
+ const childSchema = createSchema(relatedSchemaFactory());
489
+ return childSchema.defaultValues;
490
+ });
491
+ }
492
+ else {
493
+ baseClientSchema = z.any().optional();
494
+ // Make it a FUNCTION that returns the object
495
+ defaultValues[key] = () => {
496
+ const childSchema = createSchema(relatedSchemaFactory());
497
+ return childSchema.defaultValues;
498
+ };
499
+ }
500
+ clientFields[key] = config.clientTransform
501
+ ? config.clientTransform(baseClientSchema)
502
+ : baseClientSchema;
503
+ validationFields[key] = clientFields[key];
485
504
  }
486
505
  else {
487
- baseClientSchema = childSchemaResult.clientSchema.optional();
488
- defaultValues[key] = childSchemaResult.defaultValues;
489
- }
490
- clientFields[key] = config.clientTransform
491
- ? config.clientTransform(baseClientSchema)
492
- : baseClientSchema;
493
- validationFields[key] = clientFields[key];
494
- }
495
- else {
496
- // Handle regular fields
497
- sqlFields[key] = config.zodSqlSchema;
498
- clientFields[key] = config.zodClientSchema;
499
- validationFields[key] = config.zodValidationSchema;
500
- if (config.transforms) {
501
- fieldTransforms[key] = config.transforms;
506
+ // Handle regular fields
507
+ sqlFields[key] = config.zodSqlSchema;
508
+ clientFields[key] = config.zodClientSchema;
509
+ validationFields[key] = config.zodValidationSchema;
510
+ if (config.transforms) {
511
+ fieldTransforms[key] = config.transforms;
512
+ }
513
+ const initialValueOrFn = config.initialValue;
514
+ defaultValues[key] = isFunction(initialValueOrFn)
515
+ ? initialValueOrFn()
516
+ : initialValueOrFn;
502
517
  }
503
- const initialValueOrFn = config.initialValue;
504
- defaultValues[key] = isFunction(initialValueOrFn)
505
- ? initialValueOrFn()
506
- : initialValueOrFn;
507
518
  }
508
519
  }
509
520
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.105",
3
+ "version": "0.5.107",
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",