cogsbox-shape 0.5.71 → 0.5.73

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,12 +55,24 @@ type SQLToZodType<T extends SQLType, TDefault extends boolean> = T["pk"] extends
55
55
  default: "CURRENT_TIMESTAMP";
56
56
  } ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
57
57
  type ZodTypeFromPrimitive<T> = T extends string ? z.ZodString : T extends number ? z.ZodNumber : T extends boolean ? z.ZodBoolean : T extends Date ? z.ZodDate : z.ZodAny;
58
+ type IsLiteralType<T> = T extends string ? string extends T ? false : true : T extends number ? number extends T ? false : true : T extends boolean ? boolean extends T ? false : true : false;
58
59
  interface IBuilderMethods<T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> {
59
60
  initialState: {
60
61
  <const TResult>(defaultValue: TResult): TResult extends () => infer R ? R extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, R, z.infer<R>, InferSmartClientType<TSql, R>, InferSmartClientType<TSql, R>>> : Prettify<Builder<"new", T, TSql, z.ZodLiteral<R>, R, z.ZodUnion<[TSql, z.ZodLiteral<R>]>, z.ZodUnion<[TSql, z.ZodLiteral<R>]>>> : TResult extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TResult, z.infer<TResult>, InferSmartClientType<TSql, TResult>, InferSmartClientType<TSql, TResult>>> : TResult extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<TResult>, TResult, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TResult>, TResult, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>>>;
61
62
  <TNewNext extends z.ZodTypeAny, const TDefaultNext>(schema: ((tools: {
62
63
  sql: TSql;
63
- }) => TNewNext) | TNewNext, defaultValue: TDefaultNext | (() => TDefaultNext)): Prettify<Builder<"new", T, TSql, TNewNext, TDefaultNext extends () => infer R ? R : TDefaultNext, InferSmartClientType<TSql, TNewNext>, InferSmartClientType<TSql, TNewNext>>>;
64
+ }) => TNewNext) | TNewNext, defaultValue: TDefaultNext): Prettify<Builder<"new", T, TSql, z.ZodUnion<[
65
+ TNewNext,
66
+ z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
67
+ ]>, IsLiteralType<z.infer<TNewNext>> extends true ? TDefaultNext extends () => infer R ? R : TDefaultNext : z.infer<TNewNext>, z.ZodUnion<[
68
+ TSql,
69
+ TNewNext,
70
+ z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
71
+ ]>, z.ZodUnion<[
72
+ TSql,
73
+ TNewNext,
74
+ z.ZodLiteral<TDefaultNext extends () => infer R ? R : TDefaultNext>
75
+ ]>>>;
64
76
  };
65
77
  client: <TClientNext extends z.ZodTypeAny>(schema: ((tools: {
66
78
  sql: TSql;
@@ -211,9 +223,6 @@ type Relation<U extends Schema<any>> = {
211
223
  schema: U;
212
224
  defaultCount?: number;
213
225
  };
214
- type Prettify<T> = {
215
- [K in keyof T]: T[K];
216
- } & {};
217
226
  export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
218
227
  type SchemaDefinition = {
219
228
  _tableName: string;
@@ -310,4 +319,94 @@ export declare function schemaRelations<TSchema extends Schema<any>, RefObject e
310
319
  __parentTableType: TSchema & RefObject;
311
320
  };
312
321
  };
322
+ type Prettify<T> = {
323
+ [K in keyof T]: T[K];
324
+ } & {};
325
+ /**
326
+ * [INTERNAL] Core recursive utility to inspect the schema definition.
327
+ * It iterates through the schema, finds the `config` object in each
328
+ * builder, and extracts the specified Zod schema.
329
+ */
330
+ type InferByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema", Depth extends any[] = []> = Depth["length"] extends 10 ? any : {
331
+ [K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand ? never : K]: T[K] extends {
332
+ config: {
333
+ sql: {
334
+ type: "hasMany" | "manyToMany";
335
+ schema: () => infer S;
336
+ };
337
+ };
338
+ } ? z.ZodArray<S extends {
339
+ _tableName: string;
340
+ } ? z.ZodObject<Prettify<InferByKey<S, Key, [...Depth, 1]>>> : z.ZodObject<any>> : T[K] extends {
341
+ config: {
342
+ sql: {
343
+ type: "hasOne" | "belongsTo";
344
+ schema: () => infer S;
345
+ };
346
+ };
347
+ } ? S extends {
348
+ _tableName: string;
349
+ } ? z.ZodObject<Prettify<InferByKey<S, Key, [...Depth, 1]>>> : z.ZodObject<any> : T[K] extends {
350
+ type: "reference";
351
+ to: () => infer RefField;
352
+ } ? RefField extends {
353
+ config: {
354
+ [P in Key]: infer ZodSchema;
355
+ };
356
+ } ? ZodSchema : never : T[K] extends {
357
+ config: {
358
+ [P in Key]: infer ZodSchema extends z.ZodTypeAny;
359
+ };
360
+ } ? ZodSchema : never;
361
+ };
362
+ /**
363
+ * [INTERNAL] Core utility to infer default values directly from the schema definition.
364
+ */
365
+ type InferDefaults<T> = {
366
+ [K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand ? never : K]: T[K] extends {
367
+ config: {
368
+ initialValue: infer D;
369
+ };
370
+ } ? D extends () => infer R ? R : D : never;
371
+ };
372
+ /**
373
+ * A new, non-conflicting namespace for directly inferring types from your schema definitions.
374
+ * This is more performant than using `ReturnType<typeof createSchema>`.
375
+ */
376
+ export declare namespace Infer {
377
+ /**
378
+ * Directly infers the Zod schema for the **SQL (database)** layer.
379
+ */
380
+ type SqlSchema<T extends {
381
+ _tableName: string;
382
+ }> = z.ZodObject<Prettify<InferByKey<T, "zodSqlSchema">>>;
383
+ /**
384
+ * Directly infers the Zod schema for the **Client** layer.
385
+ */
386
+ type ClientSchema<T extends {
387
+ _tableName: string;
388
+ }> = z.ZodObject<Prettify<InferByKey<T, "zodClientSchema">>>;
389
+ /**
390
+ * Directly infers the Zod schema for the **Validation** layer.
391
+ */
392
+ type ValidationSchema<T extends {
393
+ _tableName: string;
394
+ }> = z.ZodObject<Prettify<InferByKey<T, "zodValidationSchema">>>;
395
+ /** The TypeScript type for data as it exists in the database. */
396
+ type Sql<T extends {
397
+ _tableName: string;
398
+ }> = z.infer<SqlSchema<T>>;
399
+ /** The TypeScript type for data as it is represented on the client. */
400
+ type Client<T extends {
401
+ _tableName: string;
402
+ }> = z.infer<ClientSchema<T>>;
403
+ /** The TypeScript type for validation data, often the most flexible shape. */
404
+ type Validation<T extends {
405
+ _tableName: string;
406
+ }> = z.infer<ValidationSchema<T>>;
407
+ /** The TypeScript type for the default values object. */
408
+ type Defaults<T extends {
409
+ _tableName: string;
410
+ }> = Prettify<InferDefaults<T>>;
411
+ }
313
412
  export {};
package/dist/schema.js CHANGED
@@ -156,8 +156,12 @@ function createBuilder(config) {
156
156
  : schemaOrDefault
157
157
  : config.sqlZod; // If only a primitive is passed, the "new" schema is still the SQL one.
158
158
  const finalDefaultValue = hasSchemaArg
159
- ? defaultValue()
160
- : schemaOrDefault;
159
+ ? isFunction(defaultValue)
160
+ ? defaultValue() // If it's a function, call it
161
+ : defaultValue // If it's a direct value, use it as-is
162
+ : isFunction(schemaOrDefault)
163
+ ? schemaOrDefault()
164
+ : schemaOrDefault;
161
165
  const newCompletedStages = new Set(completedStages);
162
166
  newCompletedStages.add("new");
163
167
  // ---- THIS IS THE RUNTIME FIX THAT MATCHES YOUR INTERFACE ----
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.71",
3
+ "version": "0.5.73",
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",