cogsbox-shape 0.5.40 → 0.5.41

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 +21 -4
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -340,7 +340,7 @@ function isRelation(value) {
340
340
  typeof value === "object" &&
341
341
  "type" in value &&
342
342
  "fromKey" in value &&
343
- "toKey" in value &&
343
+ typeof value.toKey === "function" && // More specific check
344
344
  "schema" in value);
345
345
  }
346
346
  export function createSchema(schema) {
@@ -418,9 +418,13 @@ function serializeSchemaMetadata(schema) {
418
418
  continue;
419
419
  let toKeyName = null;
420
420
  try {
421
- const targetFieldBuilder = relation.toKey();
421
+ let targetFieldDefinition = relation.toKey();
422
+ if (targetFieldDefinition &&
423
+ targetFieldDefinition.type === "reference") {
424
+ targetFieldDefinition = targetFieldDefinition.to();
425
+ }
422
426
  for (const targetKey in relation.schema) {
423
- if (relation.schema[targetKey] === targetFieldBuilder) {
427
+ if (relation.schema[targetKey] === targetFieldDefinition) {
424
428
  toKeyName = targetKey;
425
429
  break;
426
430
  }
@@ -444,10 +448,23 @@ function serializeSchemaMetadata(schema) {
444
448
  fields[key] = { type: "field", sql: definition.config.sql };
445
449
  if (definition.config.sql.pk === true) {
446
450
  if (primaryKey)
447
- console.warn(`[cogsbox-shape] Multiple primary keys in schema '${schema._tableName}'. Using last one found: '${key}'.`);
451
+ console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
448
452
  primaryKey = key;
449
453
  }
450
454
  }
455
+ else if (definition && definition.type === "reference") {
456
+ const targetFieldBuilder = definition.to();
457
+ if (targetFieldBuilder &&
458
+ targetFieldBuilder.config &&
459
+ targetFieldBuilder.config.sql) {
460
+ fields[key] = { type: "field", sql: targetFieldBuilder.config.sql };
461
+ if (targetFieldBuilder.config.sql.pk) {
462
+ if (primaryKey)
463
+ console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
464
+ primaryKey = key;
465
+ }
466
+ }
467
+ }
451
468
  }
452
469
  return { _tableName: schema._tableName, primaryKey, fields, relations };
453
470
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.40",
3
+ "version": "0.5.41",
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",