cogsbox-shape 0.5.48 → 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 +41 -28
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -343,6 +343,8 @@ function isRelation(value) {
343
343
  "toKey" in value &&
344
344
  "schema" in value);
345
345
  }
346
+ // In your cogsbox-shape file...
347
+ // Replace the entire existing createSchema function with this SINGLE, CORRECT version.
346
348
  export function createSchema(schema) {
347
349
  const sqlFields = {};
348
350
  const clientFields = {};
@@ -351,16 +353,16 @@ export function createSchema(schema) {
351
353
  for (const key in schema) {
352
354
  if (key === "_tableName" || key.startsWith("__"))
353
355
  continue;
354
- const field = schema[key];
355
- console.log("FIELD", field);
356
- console.log("is fucntion", isFunction(field));
357
- // Case 1: Handle relation functions (hasMany, hasOne, etc.)
358
- if (isFunction(field)) {
359
- const relation = field();
360
- console.log("isRelation(relation)", isRelation(relation));
361
- if (!isRelation(relation)) {
362
- continue;
363
- }
356
+ const definition = schema[key];
357
+ // --- THIS IS THE CORRECT, SIMPLE LOGIC ---
358
+ // Case 1: Is it a relation object?
359
+ // A relation object will have a `type` like "hasMany" and a `schema` property.
360
+ if (definition &&
361
+ (definition.type === "hasMany" ||
362
+ definition.type === "hasOne" ||
363
+ definition.type === "belongsTo" ||
364
+ definition.type === "manyToMany")) {
365
+ const relation = definition; // It's already the object we need.
364
366
  const childSchemaResult = createSchema(relation.schema());
365
367
  if (relation.type === "hasMany" || relation.type === "manyToMany") {
366
368
  validationFields[key] = z
@@ -378,21 +380,21 @@ export function createSchema(schema) {
378
380
  defaultValues[key] = childSchemaResult.defaultValues;
379
381
  }
380
382
  }
381
- // Case 2: Handle reference() objects
382
- else if (field && field.type === "reference") {
383
- const referencedField = field.to();
383
+ // Case 2: Is it a standard field builder object?
384
+ else if (definition && typeof definition.config === "object") {
385
+ sqlFields[key] = definition.config.zodSqlSchema;
386
+ clientFields[key] = definition.config.zodClientSchema;
387
+ validationFields[key] = definition.config.zodValidationSchema;
388
+ defaultValues[key] = definition.config.initialValue;
389
+ }
390
+ // Case 3: Is it a reference object?
391
+ else if (definition && definition.type === "reference") {
392
+ const referencedField = definition.to();
384
393
  sqlFields[key] = referencedField.config.zodSqlSchema;
385
394
  clientFields[key] = referencedField.config.zodClientSchema;
386
395
  validationFields[key] = referencedField.config.zodValidationSchema;
387
396
  defaultValues[key] = referencedField.config.initialValue;
388
397
  }
389
- // Case 3: Handle standard shape.sql() fields
390
- else if (field && typeof field === "object" && "config" in field) {
391
- sqlFields[key] = field.config.zodSqlSchema;
392
- clientFields[key] = field.config.zodClientSchema;
393
- validationFields[key] = field.config.zodValidationSchema;
394
- defaultValues[key] = field.config.initialValue;
395
- }
396
398
  }
397
399
  // Return the final, correctly typed Zod objects
398
400
  return {
@@ -415,25 +417,36 @@ function serializeSchemaMetadata(schema) {
415
417
  if (key === "_tableName" || key.startsWith("__"))
416
418
  continue;
417
419
  const definition = schema[key];
418
- if (isFunction(definition)) {
419
- const relation = definition();
420
- if (!isRelation(relation))
421
- 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;
422
427
  let toKeyName = null;
423
428
  try {
424
429
  let targetFieldDefinition = relation.toKey();
425
430
  if (targetFieldDefinition &&
426
431
  targetFieldDefinition.type === "reference") {
427
- targetFieldDefinition = targetFieldDefinition.to();
432
+ targetFieldDefinition = targetFieldDefinition.to;
428
433
  }
429
- for (const targetKey in relation.schema) {
430
- if (relation.schema[targetKey] === targetFieldDefinition) {
434
+ const targetSchemaObject = relation.schema();
435
+ for (const targetKey in targetSchemaObject) {
436
+ if (targetSchemaObject[targetKey] === targetFieldDefinition) {
431
437
  toKeyName = targetKey;
432
438
  break;
433
439
  }
434
440
  }
435
441
  if (!toKeyName)
436
- 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
+ };
437
450
  }
438
451
  catch (e) {
439
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.48",
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",