cogsbox-shape 0.5.51 → 0.5.52

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
@@ -4784,54 +4784,13 @@ export declare function reference<TField extends object>(config: TField): {
4784
4784
  to: TField;
4785
4785
  };
4786
4786
  export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
4787
- type SchemaDefinition = {
4788
- _tableName: string;
4789
- [key: string]: any;
4790
- };
4791
- type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema"> = {
4792
- [K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
4793
- config: {
4794
- [P in Key]: infer S extends z.ZodTypeAny;
4795
- };
4796
- } ? S : T[K] extends {
4797
- type: "reference";
4798
- to: () => {
4799
- config: {
4800
- [P in Key]: infer S extends z.ZodTypeAny;
4801
- };
4802
- };
4803
- } ? S : T[K] extends () => {
4804
- type: "hasMany" | "manyToMany";
4805
- schema: infer S extends SchemaDefinition;
4806
- } ? z.ZodArray<z.ZodObject<Prettify<InferSchemaByKey<S, Key>>>> : T[K] extends () => {
4807
- type: "hasOne" | "belongsTo";
4808
- schema: infer S extends SchemaDefinition;
4809
- } ? z.ZodObject<Prettify<InferSchemaByKey<S, Key>>> : never;
4810
- };
4811
- type InferSqlSchema<T> = InferSchemaByKey<T, "zodSqlSchema">;
4812
- type InferClientSchema<T> = InferSchemaByKey<T, "zodClientSchema">;
4813
- type InferValidationSchema<T> = InferSchemaByKey<T, "zodValidationSchema">;
4814
- type InferDefaultValues2<T> = {
4815
- [K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
4816
- config: {
4817
- initialValue: infer D;
4818
- };
4819
- } ? D : T[K] extends () => {
4820
- type: "hasMany" | "manyToMany";
4821
- schema: infer S extends SchemaDefinition;
4822
- defaultCount?: number;
4823
- } ? Array<Prettify<InferDefaultValues2<S>>> : T[K] extends () => {
4824
- type: "hasOne" | "belongsTo";
4825
- schema: infer S extends SchemaDefinition;
4826
- } ? Prettify<InferDefaultValues2<S>> : never;
4827
- };
4828
4787
  export declare function createSchema<T extends {
4829
4788
  _tableName: string;
4830
4789
  }>(schema: T): {
4831
- sqlSchema: z.ZodObject<Prettify<InferSqlSchema<T>>>;
4832
- clientSchema: z.ZodObject<Prettify<InferClientSchema<T>>>;
4833
- validationSchema: z.ZodObject<Prettify<InferValidationSchema<T>>>;
4834
- defaultValues: Prettify<InferDefaultValues2<T>>;
4790
+ sqlSchema: z.ZodObject<any>;
4791
+ clientSchema: z.ZodObject<any>;
4792
+ validationSchema: z.ZodObject<any>;
4793
+ defaultValues: any;
4835
4794
  };
4836
4795
  export type InferSchemaTypes<T extends {
4837
4796
  _tableName: string;
package/dist/schema.js CHANGED
@@ -352,25 +352,51 @@ export function createSchema(schema) {
352
352
  const clientFields = {};
353
353
  const validationFields = {};
354
354
  const defaultValues = {};
355
+ // Store the functions to be executed in a second pass
356
+ const deferredFields = [];
357
+ // --- PASS 1: Process all non-function fields ---
355
358
  for (const key in schema) {
356
359
  if (key === "_tableName" || key.startsWith("__"))
357
360
  continue;
358
- let definition = schema[key];
361
+ const definition = schema[key];
359
362
  if (typeof definition === "function") {
360
- const potentialRelation = definition();
361
- if (potentialRelation &&
362
- ["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(potentialRelation.type)) {
363
- definition = potentialRelation;
363
+ // It's a hasMany, belongsTo, etc. Defer it.
364
+ deferredFields.push({ key, definition });
365
+ }
366
+ else if (definition && definition.type === "reference") {
367
+ // It's a reference. Defer it.
368
+ deferredFields.push({ key, definition });
369
+ }
370
+ else if (definition && typeof definition.config === "object") {
371
+ // It's a standard field builder. Process it now.
372
+ sqlFields[key] = definition.config.zodSqlSchema;
373
+ clientFields[key] = definition.config.zodClientSchema;
374
+ validationFields[key] = definition.config.zodValidationSchema;
375
+ defaultValues[key] = definition.config.initialValue;
376
+ }
377
+ }
378
+ // --- PASS 2: Process all deferred references and relations ---
379
+ // Now we can safely call the functions because the schemas they refer to exist.
380
+ for (const { key, definition } of deferredFields) {
381
+ let resolvedDefinition = definition;
382
+ // If it's a relation like hasMany, call the outer function to get the config object
383
+ if (typeof resolvedDefinition === "function") {
384
+ resolvedDefinition = resolvedDefinition();
385
+ }
386
+ if (resolvedDefinition && resolvedDefinition.type === "reference") {
387
+ const referencedField = resolvedDefinition.to(); // This is now safe to call
388
+ if (!referencedField || !referencedField.config) {
389
+ throw new Error(`Could not resolve reference for key "${key}"`);
364
390
  }
391
+ sqlFields[key] = referencedField.config.zodSqlSchema;
392
+ clientFields[key] = referencedField.config.zodClientSchema;
393
+ validationFields[key] = referencedField.config.zodValidationSchema;
394
+ defaultValues[key] = referencedField.config.initialValue;
365
395
  }
366
- if (definition &&
367
- (definition.type === "hasMany" ||
368
- definition.type === "hasOne" ||
369
- definition.type === "belongsTo" ||
370
- definition.type === "manyToMany")) {
371
- const relation = definition; // It's now the object we need.
372
- // Recursively create the schema for the related table
373
- const childSchemaResult = createSchema(relation.schema());
396
+ else if (resolvedDefinition &&
397
+ ["hasMany", "manyToMany", "hasOne", "belongsTo"].includes(resolvedDefinition.type)) {
398
+ const relation = resolvedDefinition;
399
+ const childSchemaResult = createSchema(relation.schema()); // Recursive call
374
400
  if (relation.type === "hasMany" || relation.type === "manyToMany") {
375
401
  sqlFields[key] = z.array(childSchemaResult.sqlSchema).optional();
376
402
  clientFields[key] = z.array(childSchemaResult.clientSchema).optional();
@@ -387,21 +413,6 @@ export function createSchema(schema) {
387
413
  defaultValues[key] = childSchemaResult.defaultValues;
388
414
  }
389
415
  }
390
- // Case 2: Is it a standard field builder object?
391
- else if (definition && typeof definition.config === "object") {
392
- sqlFields[key] = definition.config.zodSqlSchema;
393
- clientFields[key] = definition.config.zodClientSchema;
394
- validationFields[key] = definition.config.zodValidationSchema;
395
- defaultValues[key] = definition.config.initialValue;
396
- }
397
- // Case 3: Is it a reference object?
398
- else if (definition && definition.type === "reference") {
399
- const referencedField = definition.to();
400
- sqlFields[key] = referencedField.config.zodSqlSchema;
401
- clientFields[key] = referencedField.config.zodClientSchema;
402
- validationFields[key] = referencedField.config.zodValidationSchema;
403
- defaultValues[key] = referencedField.config.initialValue;
404
- }
405
416
  }
406
417
  return {
407
418
  sqlSchema: z.object(sqlFields),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.51",
3
+ "version": "0.5.52",
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",