cogsbox-shape 0.5.160 → 0.5.162

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
@@ -218,6 +218,7 @@ export declare function createSchema<T extends {
218
218
  clientSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodClientSchema">>>;
219
219
  validationSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodValidationSchema">>>;
220
220
  defaultValues: Prettify<DeriveDefaults<TActualSchema>>;
221
+ stateType: Prettify<DeriveStateType<TActualSchema>>;
221
222
  generateDefaults: () => Prettify<DeriveDefaults<TActualSchema>>;
222
223
  toClient: (dbObject: z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodSqlSchema">>>>) => z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodClientSchema">>>>;
223
224
  toDb: (clientObject: z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodClientSchema">>>>) => z.infer<z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodSqlSchema">>>>;
@@ -285,6 +286,7 @@ type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders
285
286
  clientSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>, "zodClientSchema">>>;
286
287
  validationSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>, "zodValidationSchema">>>;
287
288
  defaultValues: Prettify<DeriveDefaults<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>>>;
289
+ stateType: Prettify<DeriveStateType<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>>>;
288
290
  toClient: (dbObject: any) => any;
289
291
  toDb: (clientObject: any) => any;
290
292
  };
@@ -390,6 +392,7 @@ type RegistryShape = Record<string, {
390
392
  clientSchema: z.ZodObject<any>;
391
393
  validationSchema: z.ZodObject<any>;
392
394
  defaultValues: any;
395
+ stateType: any;
393
396
  toClient: (dbObject: any) => any;
394
397
  toDb: (clientObject: any) => any;
395
398
  };
@@ -408,6 +411,7 @@ type CreateSchemaBoxReturn<S extends Record<string, SchemaWithPlaceholders>, R e
408
411
  toDb: Resolved[K]["zodSchemas"]["toDb"];
409
412
  };
410
413
  defaults: Resolved[K]["zodSchemas"]["defaultValues"];
414
+ stateType: Resolved[K]["zodSchemas"]["stateType"];
411
415
  nav: NavigationProxy<K & string, Resolved>;
412
416
  RelationSelection: NavigationToSelection<NavigationProxy<K & string, Resolved>>;
413
417
  createView: <const TSelection extends NavigationToSelection<NavigationProxy<K & string, Resolved>>>(selection: TSelection) => DeriveViewResult<K & string, TSelection, Resolved>;
@@ -468,4 +472,21 @@ type DeriveDefaults<T, Depth extends any[] = []> = Prettify<Depth["length"] exte
468
472
  };
469
473
  } ? TNew extends TSql ? z.infer<TClient> : D extends () => infer R ? R : D : never;
470
474
  }>;
475
+ type DeriveStateType<T, Depth extends any[] = []> = Prettify<Depth["length"] extends 10 ? any : {
476
+ [K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand | "__primaryKeySQL" | "__isClientChecker" | "primaryKeySQL" | "isClient" ? never : K extends keyof T ? T[K] extends Reference<any> ? K : T[K] extends {
477
+ config: {
478
+ sql: {
479
+ type: "hasMany" | "manyToMany" | "hasOne" | "belongsTo";
480
+ };
481
+ };
482
+ } ? never : K : never]: T[K] extends Reference<infer TGetter> ? ReturnType<TGetter> extends {
483
+ config: {
484
+ zodClientSchema: infer TClient extends z.ZodTypeAny;
485
+ };
486
+ } ? z.infer<TClient> : never : T[K] extends {
487
+ config: {
488
+ zodClientSchema: infer TClient extends z.ZodTypeAny;
489
+ };
490
+ } ? z.infer<TClient> : never;
491
+ }>;
471
492
  export {};
package/dist/schema.js CHANGED
@@ -171,16 +171,25 @@ function createBuilder(config) {
171
171
  }
172
172
  const newCompletedStages = new Set(completedStages);
173
173
  newCompletedStages.add("new");
174
- // Create union of the SQL type and the new client type
175
- const hasProvidedSchema = !!schemaOrModifier;
176
- const clientAndServerSchema = hasProvidedSchema
177
- ? finalSchema
178
- : z.union([config.sqlZod, finalSchema]);
179
174
  const newConfig = { ...config.sqlConfig };
180
175
  if (clientPk) {
181
- // Add our metadata flag to the config
182
176
  newConfig.isClientPk = true;
183
177
  }
178
+ // When clientPk is true, ALWAYS union the SQL type with the client type
179
+ // because records can be either DB-sourced (number) or client-created (string)
180
+ let clientAndServerSchema;
181
+ if (clientPk) {
182
+ // Always union for clientPk fields
183
+ clientAndServerSchema = z.union([config.sqlZod, finalSchema]);
184
+ }
185
+ else if (schemaOrModifier) {
186
+ // Schema provided without clientPk — use as-is
187
+ clientAndServerSchema = finalSchema;
188
+ }
189
+ else {
190
+ // No schema provided — union with SQL type
191
+ clientAndServerSchema = z.union([config.sqlZod, finalSchema]);
192
+ }
184
193
  return createBuilder({
185
194
  ...config,
186
195
  stage: "new",
@@ -188,7 +197,7 @@ function createBuilder(config) {
188
197
  newZod: finalSchema,
189
198
  initialValue: actualValue,
190
199
  clientZod: clientAndServerSchema,
191
- validationZod: clientAndServerSchema, // Our internal name
200
+ validationZod: clientAndServerSchema,
192
201
  completedStages: newCompletedStages,
193
202
  });
194
203
  },
@@ -587,6 +596,7 @@ export function createSchema(schema, relations) {
587
596
  clientSchema: z.object(clientFields),
588
597
  validationSchema: z.object(serverFields),
589
598
  defaultValues: defaultValues,
599
+ stateType: {},
590
600
  generateDefaults,
591
601
  toClient,
592
602
  toDb,
@@ -792,6 +802,7 @@ export function createSchemaBox(schemas, resolver) {
792
802
  toDb: entry.zodSchemas.toDb,
793
803
  },
794
804
  defaults: entry.zodSchemas.defaultValues,
805
+ stateType: entry.zodSchemas.stateType,
795
806
  generateDefaults: entry.zodSchemas.generateDefaults,
796
807
  // ADD: Expose PK info and resolver
797
808
  pk: entry.zodSchemas.pk,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.160",
3
+ "version": "0.5.162",
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",