cogsbox-shape 0.5.25 → 0.5.26

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 CHANGED
@@ -431,8 +431,34 @@ export function createSchema(schema) {
431
431
  if (key === "_tableName")
432
432
  continue;
433
433
  const field = schema[key];
434
- if (field && typeof field === "object" && "config" in field) {
435
- sqlFields[key] = field.config.zodSqlSchema; //field.config' is of type 'unknown
434
+ // Case 1: Handle relation functions (hasMany, hasOne, etc.)
435
+ if (typeof field === "function") {
436
+ const relation = field();
437
+ if (!isRelation(relation)) {
438
+ continue;
439
+ }
440
+ // Recursively process the nested schema
441
+ const childSchemaResult = createSchema(relation.schema);
442
+ // For to-many relations, wrap schemas in z.array()
443
+ if (relation.type === "hasMany" || relation.type === "manyToMany") {
444
+ sqlFields[key] = z.array(childSchemaResult.sqlSchema);
445
+ clientFields[key] = z.array(childSchemaResult.clientSchema);
446
+ validationFields[key] = z.array(childSchemaResult.validationSchema);
447
+ // Create an array of default values for the relation
448
+ const count = relation.defaultCount || 0;
449
+ defaultValues[key] = Array.from({ length: count }, () => childSchemaResult.defaultValues);
450
+ }
451
+ else {
452
+ // For to-one relations, use schemas directly
453
+ sqlFields[key] = childSchemaResult.sqlSchema;
454
+ clientFields[key] = childSchemaResult.clientSchema;
455
+ validationFields[key] = childSchemaResult.validationSchema;
456
+ defaultValues[key] = childSchemaResult.defaultValues;
457
+ }
458
+ }
459
+ // Case 2: Handle regular builder fields
460
+ else if (field && typeof field === "object" && "config" in field) {
461
+ sqlFields[key] = field.config.zodSqlSchema;
436
462
  clientFields[key] = field.config.zodClientSchema;
437
463
  validationFields[key] = field.config.zodValidationSchema;
438
464
  defaultValues[key] = field.config.initialValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.25",
3
+ "version": "0.5.26",
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",