cogsbox-shape 0.5.51 → 0.5.53

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 +39 -28
  2. package/package.json +1 -1
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.53",
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",