cogsbox-shape 0.5.91 → 0.5.93

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
@@ -55,6 +55,11 @@ export interface IBuilderMethods<T extends SQLType | RelationConfig<any>, TSql e
55
55
  z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
56
56
  ]>>>;
57
57
  };
58
+ references: <TRefSchema extends {
59
+ _tableName: string;
60
+ }>(fieldGetter: () => any) => Builder<"sql", T & {
61
+ references: typeof fieldGetter;
62
+ }, TSql, TNew, TInitialValue, TClient, TValidation>;
58
63
  client: <TClientNext extends z.ZodTypeAny>(schema: ((tools: {
59
64
  sql: TSql;
60
65
  initialState: TNew;
@@ -90,7 +95,7 @@ export type RelationConfig<T extends Schema<any>> = (BaseRelationConfig<T> & {
90
95
  });
91
96
  type Stage = "sql" | "relation" | "new" | "client" | "validation" | "done";
92
97
  type StageMethods = {
93
- sql: "initialState" | "client" | "validation" | "transform";
98
+ sql: "initialState" | "client" | "validation" | "transform" | "references";
94
99
  relation: "validation" | "transform";
95
100
  new: "client" | "validation" | "transform";
96
101
  client: "validation" | "transform";
package/dist/schema.js CHANGED
@@ -206,6 +206,15 @@ function createBuilder(config) {
206
206
  completedStages: newCompletedStages,
207
207
  });
208
208
  },
209
+ references: (fieldGetter) => {
210
+ return createBuilder({
211
+ ...config,
212
+ sqlConfig: {
213
+ ...config.sqlConfig,
214
+ references: fieldGetter,
215
+ },
216
+ });
217
+ },
209
218
  client: (assert) => {
210
219
  if (completedStages.has("client")) {
211
220
  throw new Error("client() can only be called once in the chain");
@@ -486,19 +495,15 @@ export function createSchema(schema, relations) {
486
495
  ["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
487
496
  const relatedSchemaFactory = sqlConfig.schema;
488
497
  const childSchemaResult = createSchema(relatedSchemaFactory());
489
- let baseSqlSchema;
490
498
  let baseClientSchema;
491
499
  if (sqlConfig.type === "hasMany" || sqlConfig.type === "manyToMany") {
492
- baseSqlSchema = z.array(childSchemaResult.sqlSchema);
493
500
  baseClientSchema = z.array(childSchemaResult.clientSchema);
494
501
  defaultValues[key] = Array.from({ length: sqlConfig.defaultCount || 0 }, () => childSchemaResult.defaultValues);
495
502
  }
496
503
  else {
497
- baseSqlSchema = childSchemaResult.sqlSchema;
498
504
  baseClientSchema = childSchemaResult.clientSchema;
499
505
  defaultValues[key] = childSchemaResult.defaultValues;
500
506
  }
501
- sqlFields[key] = baseSqlSchema.optional();
502
507
  clientFields[key] = config.clientTransform
503
508
  ? config.clientTransform(baseClientSchema)
504
509
  : baseClientSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.91",
3
+ "version": "0.5.93",
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",