cogsbox-shape 0.5.158 → 0.5.159

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
@@ -40,7 +40,9 @@ type ZodTypeFromPrimitive<T> = T extends string ? z.ZodString : T extends number
40
40
  type CollapsedUnion<A extends z.ZodTypeAny, B extends z.ZodTypeAny> = A extends B ? (B extends A ? A : z.ZodUnion<[A, B]>) : z.ZodUnion<[A, B]>;
41
41
  export interface IBuilderMethods<T extends DbConfig, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> {
42
42
  initialState<const TValue>(options: {
43
- value: TValue | (() => TValue);
43
+ value: TValue | ((tools: {
44
+ uuid: () => string;
45
+ }) => TValue);
44
46
  schema?: never;
45
47
  clientPk?: boolean;
46
48
  }): Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, TValue extends () => infer R ? R : TValue, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>>>;
@@ -50,7 +52,9 @@ export interface IBuilderMethods<T extends DbConfig, TSql extends z.ZodTypeAny,
50
52
  clientPk?: boolean;
51
53
  }): Prettify<Builder<"new", T, TSql, TSchema, z.infer<TSchema>, CollapsedUnion<TSql, TSchema>, CollapsedUnion<TSql, TSchema>>>;
52
54
  initialState<const TValue, const TSchema extends z.ZodTypeAny>(options: {
53
- value: TValue | (() => TValue);
55
+ value: TValue | ((tools: {
56
+ uuid: () => string;
57
+ }) => TValue);
54
58
  schema: TSchema | ((base: ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>) => TSchema);
55
59
  clientPk?: boolean;
56
60
  }): Prettify<Builder<"new", T, TSql, TSchema, z.infer<TSchema>, // <-- THIS IS THE FIX: Use schema's type, not literal value
@@ -131,7 +135,9 @@ export type Reference<TGetter extends () => any> = {
131
135
  getter: TGetter;
132
136
  };
133
137
  interface ShapeAPI {
134
- initialState: <const TValue>(value: TValue | (() => TValue)) => Builder<"new", null, z.ZodUndefined, // No SQL schema
138
+ initialState: <const TValue>(value: TValue | ((tools: {
139
+ uuid: () => string;
140
+ }) => TValue)) => Builder<"new", null, z.ZodUndefined, // No SQL schema
135
141
  ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, TValue extends () => infer R ? R : TValue, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>, ZodTypeFromPrimitive<TValue extends () => infer R ? R : TValue>>;
136
142
  sql: <T extends SQLType>(sqlConfig: T) => Builder<"sql", T, SQLToZodType<T, false>, SQLToZodType<T, false>, z.infer<SQLToZodType<T, false>>, SQLToZodType<T, false>, SQLToZodType<T, false>>;
137
143
  reference: <TGetter extends () => any>(getter: TGetter) => Reference<TGetter>;
@@ -454,13 +460,11 @@ type DeriveDefaults<T, Depth extends any[] = []> = Prettify<Depth["length"] exte
454
460
  };
455
461
  } ? D extends () => infer R ? R : D : never : T[K] extends {
456
462
  config: {
457
- transforms: any;
463
+ zodNewSchema: infer TNew;
464
+ zodSqlSchema: infer TSql;
458
465
  zodClientSchema: infer TClient extends z.ZodTypeAny;
459
- };
460
- } ? z.infer<TClient> : T[K] extends {
461
- config: {
462
466
  initialValue: infer D;
463
467
  };
464
- } ? D extends () => infer R ? R : D : never;
468
+ } ? TNew extends TSql ? z.infer<TClient> : D extends () => infer R ? R : D : never;
465
469
  }>;
466
470
  export {};
package/dist/schema.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ import { v4 as uuid } from "uuid";
2
3
  export const isFunction = (fn) => typeof fn === "function";
3
4
  // Function to create a properly typed current timestamp config
4
5
  export function currentTimeStamp() {
@@ -9,7 +10,7 @@ export function currentTimeStamp() {
9
10
  }
10
11
  export const s = {
11
12
  initialState: (value) => {
12
- const actualValue = isFunction(value) ? value() : value;
13
+ const actualValue = isFunction(value) ? value({ uuid }) : value;
13
14
  // Infer the Zod type from the primitive value
14
15
  let inferredZodType;
15
16
  if (typeof actualValue === "string") {
@@ -129,7 +130,7 @@ function createBuilder(config) {
129
130
  let finalSchema;
130
131
  // 1. Determine the actual value
131
132
  if (value !== undefined) {
132
- actualValue = isFunction(value) ? value() : value;
133
+ actualValue = isFunction(value) ? value({ uuid }) : value;
133
134
  }
134
135
  else if (schemaOrModifier &&
135
136
  typeof schemaOrModifier === "object" &&
@@ -521,11 +522,6 @@ export function createSchema(schema, relations) {
521
522
  : initialValueOrFn;
522
523
  }
523
524
  }
524
- } // Apply transforms to default values so they match client schema
525
- for (const key in fieldTransforms) {
526
- if (key in defaultValues && defaultValues[key] !== undefined) {
527
- defaultValues[key] = fieldTransforms[key].toClient(defaultValues[key]);
528
- }
529
525
  }
530
526
  const generateDefaults = () => {
531
527
  const freshDefaults = {};
@@ -535,11 +531,6 @@ export function createSchema(schema, relations) {
535
531
  ? generatorOrValue() // Call the function to get a fresh value
536
532
  : generatorOrValue; // Use the static value
537
533
  }
538
- for (const key in fieldTransforms) {
539
- if (key in freshDefaults && freshDefaults[key] !== undefined) {
540
- freshDefaults[key] = fieldTransforms[key].toClient(freshDefaults[key]);
541
- }
542
- }
543
534
  return freshDefaults;
544
535
  };
545
536
  const toClient = (dbObject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.158",
3
+ "version": "0.5.159",
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",