cogsbox-shape 0.5.105 → 0.5.106

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 +48 -29
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -472,38 +472,57 @@ 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 - create a lazy-evaluated schema
482
+ const relatedSchemaFactory = sqlConfig.schema;
483
+ // Create a lazy getter for the child schema
484
+ let cachedChildSchema = null;
485
+ const getChildSchema = () => {
486
+ if (!cachedChildSchema) {
487
+ cachedChildSchema = createSchema(relatedSchemaFactory());
488
+ }
489
+ return cachedChildSchema;
490
+ };
491
+ let baseClientSchema;
492
+ if (sqlConfig.type === "hasMany" || sqlConfig.type === "manyToMany") {
493
+ // Create a custom Zod type that validates lazily
494
+ baseClientSchema = z
495
+ .array(z.lazy(() => getChildSchema().clientSchema))
496
+ .optional();
497
+ // Default values use a getter function
498
+ defaultValues[key] = Array.from({ length: sqlConfig.defaultCount || 0 }, () => getChildSchema().defaultValues);
499
+ }
500
+ else {
501
+ // Create a custom Zod type that validates lazily
502
+ baseClientSchema = z
503
+ .lazy(() => getChildSchema().clientSchema)
504
+ .optional();
505
+ // Default value uses a getter function
506
+ defaultValues[key] = () => getChildSchema().defaultValues;
507
+ }
508
+ clientFields[key] = config.clientTransform
509
+ ? config.clientTransform(baseClientSchema)
510
+ : baseClientSchema;
511
+ validationFields[key] = clientFields[key];
485
512
  }
486
513
  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;
514
+ // Handle regular fields
515
+ sqlFields[key] = config.zodSqlSchema;
516
+ clientFields[key] = config.zodClientSchema;
517
+ validationFields[key] = config.zodValidationSchema;
518
+ if (config.transforms) {
519
+ fieldTransforms[key] = config.transforms;
520
+ }
521
+ const initialValueOrFn = config.initialValue;
522
+ defaultValues[key] = isFunction(initialValueOrFn)
523
+ ? initialValueOrFn()
524
+ : initialValueOrFn;
502
525
  }
503
- const initialValueOrFn = config.initialValue;
504
- defaultValues[key] = isFunction(initialValueOrFn)
505
- ? initialValueOrFn()
506
- : initialValueOrFn;
507
526
  }
508
527
  }
509
528
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.105",
3
+ "version": "0.5.106",
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",