cogsbox-shape 0.5.49 → 0.5.50

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 +19 -8
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -417,25 +417,36 @@ function serializeSchemaMetadata(schema) {
417
417
  if (key === "_tableName" || key.startsWith("__"))
418
418
  continue;
419
419
  const definition = schema[key];
420
- if (isFunction(definition)) {
421
- const relation = definition();
422
- if (!isRelation(relation))
423
- continue;
420
+ // Case 1: Is it a relation object? Check the `type` property.
421
+ if (definition &&
422
+ (definition.type === "hasMany" ||
423
+ definition.type === "hasOne" ||
424
+ definition.type === "belongsTo" ||
425
+ definition.type === "manyToMany")) {
426
+ const relation = definition;
424
427
  let toKeyName = null;
425
428
  try {
426
429
  let targetFieldDefinition = relation.toKey();
427
430
  if (targetFieldDefinition &&
428
431
  targetFieldDefinition.type === "reference") {
429
- targetFieldDefinition = targetFieldDefinition.to();
432
+ targetFieldDefinition = targetFieldDefinition.to;
430
433
  }
431
- for (const targetKey in relation.schema) {
432
- if (relation.schema[targetKey] === targetFieldDefinition) {
434
+ const targetSchemaObject = relation.schema();
435
+ for (const targetKey in targetSchemaObject) {
436
+ if (targetSchemaObject[targetKey] === targetFieldDefinition) {
433
437
  toKeyName = targetKey;
434
438
  break;
435
439
  }
436
440
  }
437
441
  if (!toKeyName)
438
- throw new Error(`Could not find field name for relation target in schema '${relation.schema._tableName}'.`);
442
+ throw new Error(`Could not find field name for relation target.`);
443
+ relations[key] = {
444
+ type: "relation",
445
+ relationType: relation.type,
446
+ fromKey: relation.fromKey,
447
+ toKey: toKeyName,
448
+ schema: serializeSchemaMetadata(targetSchemaObject),
449
+ };
439
450
  }
440
451
  catch (e) {
441
452
  console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.49",
3
+ "version": "0.5.50",
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",