cogsbox-shape 0.5.48 → 0.5.49

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 +22 -20
  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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.48",
3
+ "version": "0.5.49",
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",