cogsbox-shape 0.5.43 → 0.5.45

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.d.ts CHANGED
@@ -4827,9 +4827,7 @@ type InferDefaultValues2<T> = {
4827
4827
  };
4828
4828
  export declare function createSchema<T extends {
4829
4829
  _tableName: string;
4830
- }>(schema: T extends {
4831
- _tableName: string;
4832
- } ? T : never): {
4830
+ }>(schema: T): {
4833
4831
  sqlSchema: z.ZodObject<Prettify<InferSqlSchema<T>>>;
4834
4832
  clientSchema: z.ZodObject<Prettify<InferClientSchema<T>>>;
4835
4833
  validationSchema: z.ZodObject<Prettify<InferValidationSchema<T>>>;
package/dist/schema.js CHANGED
@@ -349,43 +349,43 @@ export function createSchema(schema) {
349
349
  const validationFields = {};
350
350
  const defaultValues = {};
351
351
  for (const key in schema) {
352
- if (key === "_tableName")
352
+ if (key === "_tableName" || key.startsWith("__"))
353
353
  continue;
354
354
  const field = schema[key];
355
355
  // Case 1: Handle relation functions (hasMany, hasOne, etc.)
356
- if (typeof field === "function") {
356
+ if (isFunction(field)) {
357
357
  const relation = field();
358
358
  if (!isRelation(relation)) {
359
359
  continue;
360
360
  }
361
- // Recursively process the nested schema
362
361
  const childSchemaResult = createSchema(relation.schema);
363
- // For to-many relations, wrap schemas in z.array()
364
362
  if (relation.type === "hasMany" || relation.type === "manyToMany") {
365
- sqlFields[key] = z.array(childSchemaResult.sqlSchema);
366
- clientFields[key] = z.array(childSchemaResult.clientSchema);
367
- validationFields[key] = z.array(childSchemaResult.validationSchema);
368
- // Create an array of default values for the relation
369
- const count = relation.defaultCount || 0;
370
- defaultValues[key] = Array.from({ length: count }, () => childSchemaResult.defaultValues);
363
+ // CORRECTLY ADD THE RELATION TO THE LIST OF VALIDATION FIELDS
364
+ // Make it optional, as relations are often not included on create/update.
365
+ validationFields[key] = z
366
+ .array(childSchemaResult.validationSchema)
367
+ .optional();
368
+ clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
369
+ sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
370
+ defaultValues[key] = Array.from({ length: relation.defaultCount || 0 }, () => childSchemaResult.defaultValues);
371
371
  }
372
372
  else {
373
- // For to-one relations, use schemas directly
374
- sqlFields[key] = childSchemaResult.sqlSchema;
375
- clientFields[key] = childSchemaResult.clientSchema;
376
- validationFields[key] = childSchemaResult.validationSchema;
373
+ // hasOne or belongsTo
374
+ validationFields[key] = childSchemaResult.validationSchema.optional();
375
+ clientFields[key] = childSchemaResult.clientSchema.optional();
376
+ sqlFields[key] = childSchemaResult.sqlSchema.optional();
377
377
  defaultValues[key] = childSchemaResult.defaultValues;
378
378
  }
379
379
  }
380
- else if (field &&
381
- typeof field === "object" &&
382
- field.type === "reference") {
380
+ // Case 2: Handle reference() objects
381
+ else if (field && field.type === "reference") {
383
382
  const referencedField = field.to();
384
383
  sqlFields[key] = referencedField.config.zodSqlSchema;
385
384
  clientFields[key] = referencedField.config.zodClientSchema;
386
385
  validationFields[key] = referencedField.config.zodValidationSchema;
387
386
  defaultValues[key] = referencedField.config.initialValue;
388
387
  }
388
+ // Case 3: Handle standard shape.sql() fields
389
389
  else if (field && typeof field === "object" && "config" in field) {
390
390
  sqlFields[key] = field.config.zodSqlSchema;
391
391
  clientFields[key] = field.config.zodClientSchema;
@@ -393,6 +393,7 @@ export function createSchema(schema) {
393
393
  defaultValues[key] = field.config.initialValue;
394
394
  }
395
395
  }
396
+ // Return the final, correctly typed Zod objects
396
397
  return {
397
398
  sqlSchema: z.object(sqlFields),
398
399
  clientSchema: z.object(clientFields),
@@ -404,6 +405,7 @@ export function createSchema(schema) {
404
405
  /**
405
406
  * (This is the smart function from the last answer that resolves `toKey` functions)
406
407
  */
408
+ // In your cogsbox-shape file, replace the entire `serializeSchemaMetadata` function.
407
409
  function serializeSchemaMetadata(schema) {
408
410
  const fields = {};
409
411
  const relations = {};
@@ -436,6 +438,7 @@ function serializeSchemaMetadata(schema) {
436
438
  console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
437
439
  throw e;
438
440
  }
441
+ // This is the critical part: ADD the processed relation to the relations object.
439
442
  relations[key] = {
440
443
  type: "relation",
441
444
  relationType: relation.type,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.43",
3
+ "version": "0.5.45",
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",