cogsbox-shape 0.5.60 → 0.5.61
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 +33 -1
- package/package.json +1 -1
package/dist/schema.js
CHANGED
|
@@ -437,7 +437,7 @@ export function createSchema(schema) {
|
|
|
437
437
|
}
|
|
438
438
|
}
|
|
439
439
|
}
|
|
440
|
-
//
|
|
440
|
+
// In createSchema function, in PASS 2 where relations are processed:
|
|
441
441
|
for (const { key, definition } of deferredFields) {
|
|
442
442
|
let resolvedDefinition = definition;
|
|
443
443
|
// If it's a relation like hasMany, call the outer function to get the config object
|
|
@@ -456,6 +456,7 @@ export function createSchema(schema) {
|
|
|
456
456
|
}
|
|
457
457
|
else if (resolvedDefinition &&
|
|
458
458
|
["hasMany", "manyToMany", "hasOne", "belongsTo"].includes(resolvedDefinition.type)) {
|
|
459
|
+
// Handle legacy function-style relations
|
|
459
460
|
const relation = resolvedDefinition;
|
|
460
461
|
const childSchemaResult = createSchema(relation.schema);
|
|
461
462
|
if (relation.type === "hasMany" || relation.type === "manyToMany") {
|
|
@@ -474,6 +475,37 @@ export function createSchema(schema) {
|
|
|
474
475
|
defaultValues[key] = childSchemaResult.defaultValues;
|
|
475
476
|
}
|
|
476
477
|
}
|
|
478
|
+
else if (definition &&
|
|
479
|
+
definition.config &&
|
|
480
|
+
definition.config.sql &&
|
|
481
|
+
typeof definition.config.sql === "object" &&
|
|
482
|
+
["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(definition.config.sql.type)) {
|
|
483
|
+
// Handle builder-style relations (shape.hasMany().client())
|
|
484
|
+
const relationConfig = definition.config.sql;
|
|
485
|
+
const childSchemaResult = createSchema(relationConfig.schema);
|
|
486
|
+
// Create base schemas
|
|
487
|
+
let baseSqlSchema, baseClientSchema, baseValidationSchema;
|
|
488
|
+
if (relationConfig.type === "hasMany" ||
|
|
489
|
+
relationConfig.type === "manyToMany") {
|
|
490
|
+
baseSqlSchema = z.array(childSchemaResult.sqlSchema).optional();
|
|
491
|
+
baseClientSchema = z.array(childSchemaResult.clientSchema).optional();
|
|
492
|
+
baseValidationSchema = z
|
|
493
|
+
.array(childSchemaResult.validationSchema)
|
|
494
|
+
.optional();
|
|
495
|
+
defaultValues[key] = Array.from({ length: relationConfig.defaultCount || 0 }, () => childSchemaResult.defaultValues);
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
baseSqlSchema = childSchemaResult.sqlSchema.optional();
|
|
499
|
+
baseClientSchema = childSchemaResult.clientSchema.optional();
|
|
500
|
+
baseValidationSchema = childSchemaResult.validationSchema.optional();
|
|
501
|
+
defaultValues[key] = childSchemaResult.defaultValues;
|
|
502
|
+
}
|
|
503
|
+
// Apply the transforms from the builder config
|
|
504
|
+
sqlFields[key] = definition.config.zodSqlSchema || baseSqlSchema;
|
|
505
|
+
clientFields[key] = definition.config.zodClientSchema || baseClientSchema;
|
|
506
|
+
validationFields[key] =
|
|
507
|
+
definition.config.zodValidationSchema || baseValidationSchema;
|
|
508
|
+
}
|
|
477
509
|
}
|
|
478
510
|
return {
|
|
479
511
|
sqlSchema: z.object(sqlFields),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.61",
|
|
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",
|