cogsbox-shape 0.5.100 → 0.5.102
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 +4 -22
- package/dist/schema.js +36 -36
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -36,29 +36,12 @@ type SQLToZodType<T extends SQLType, TDefault extends boolean> = T["pk"] extends
|
|
|
36
36
|
default: "CURRENT_TIMESTAMP";
|
|
37
37
|
} ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
|
|
38
38
|
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;
|
|
39
|
-
type
|
|
39
|
+
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]>;
|
|
40
40
|
export interface IBuilderMethods<T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> {
|
|
41
41
|
initialState: {
|
|
42
|
-
<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
sql: TSql;
|
|
46
|
-
}) => TDefaultNext)): Prettify<Builder<"new", T, TSql, z.ZodUnion<[
|
|
47
|
-
TNewNext,
|
|
48
|
-
z.ZodLiteral<TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext>
|
|
49
|
-
]>, IsLiteralType<z.infer<TNewNext>> extends true ? TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext : z.infer<TNewNext>, (TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
|
|
50
|
-
TSql,
|
|
51
|
-
TNewNext,
|
|
52
|
-
z.ZodLiteral<TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext>
|
|
53
|
-
]>, (TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext) extends z.infer<TNewNext> ? z.ZodUnion<[TSql, TNewNext]> : z.ZodUnion<[
|
|
54
|
-
TSql,
|
|
55
|
-
TNewNext,
|
|
56
|
-
z.ZodLiteral<TDefaultNext extends (...args: any[]) => infer R ? R : TDefaultNext>
|
|
57
|
-
]>>>;
|
|
58
|
-
<const TResult>(defaultValue: (tools: {
|
|
59
|
-
sql: TSql;
|
|
60
|
-
}) => TResult): TResult extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TResult, z.infer<TResult>, InferSmartClientType<TSql, TResult>, InferSmartClientType<TSql, TResult>>> : Prettify<Builder<"new", T, TSql, z.ZodLiteral<TResult>, TResult, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>, z.ZodUnion<[TSql, z.ZodLiteral<TResult>]>>>;
|
|
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>>>>;
|
|
42
|
+
<const TValue>(value: TValue extends (...args: any[]) => void | undefined ? never : TValue): TValue extends (...args: any[]) => infer R ? R extends void | undefined ? never : TValue extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TValue, z.infer<TValue>, CollapsedUnion<TSql, TValue>, CollapsedUnion<TSql, TValue>>> : R extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<R>, R, CollapsedUnion<TSql, z.ZodLiteral<R>>, CollapsedUnion<TSql, z.ZodLiteral<R>>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<R>, R, CollapsedUnion<TSql, ZodTypeFromPrimitive<R>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<R>>>> : TValue extends z.ZodTypeAny ? Prettify<Builder<"new", T, TSql, TValue, z.infer<TValue>, CollapsedUnion<TSql, TValue>, CollapsedUnion<TSql, TValue>>> : TValue extends string | number | boolean ? Prettify<Builder<"new", T, TSql, z.ZodLiteral<TValue>, TValue, CollapsedUnion<TSql, z.ZodLiteral<TValue>>, CollapsedUnion<TSql, z.ZodLiteral<TValue>>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TValue>, TValue, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>>>;
|
|
43
|
+
<const TValue, TSchema extends z.ZodTypeAny>(value: TValue extends (...args: any[]) => void | undefined ? never : TValue, schema: TSchema): Prettify<Builder<"new", T, TSql, TSchema, TValue extends () => infer R ? R : TValue, CollapsedUnion<TSql, TSchema>, CollapsedUnion<TSql, TSchema>>>;
|
|
44
|
+
<const TValue, TSchema extends z.ZodTypeAny>(value: TValue extends (...args: any[]) => void | undefined ? never : TValue, schemaModifier: (baseSchema: TValue extends () => infer R ? R extends string | number | boolean ? z.ZodLiteral<R> : ZodTypeFromPrimitive<R> : TValue extends string | number | boolean ? z.ZodLiteral<TValue> : ZodTypeFromPrimitive<TValue>) => TSchema): Prettify<Builder<"new", T, TSql, TSchema, TValue extends () => infer R ? R : TValue, CollapsedUnion<TSql, TSchema>, CollapsedUnion<TSql, TSchema>>>;
|
|
62
45
|
};
|
|
63
46
|
reference: <TRefSchema extends {
|
|
64
47
|
_tableName: string;
|
|
@@ -107,7 +90,6 @@ type StageMethods = {
|
|
|
107
90
|
validation: "transform";
|
|
108
91
|
done: never;
|
|
109
92
|
};
|
|
110
|
-
type InferSmartClientType<TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny> = z.infer<TNew> extends z.infer<TSql> ? TNew : z.ZodUnion<[TSql, TNew]>;
|
|
111
93
|
type BuilderConfig<T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny> = {
|
|
112
94
|
sql: T;
|
|
113
95
|
zodSqlSchema: TSql;
|
package/dist/schema.js
CHANGED
|
@@ -84,7 +84,7 @@ export const s = {
|
|
|
84
84
|
sqlConfig: sqlConfig,
|
|
85
85
|
sqlZod: sqlZodType,
|
|
86
86
|
newZod: sqlZodType,
|
|
87
|
-
initialValue:
|
|
87
|
+
initialValue: inferDefaultFromZod(sqlZodType, sqlConfig),
|
|
88
88
|
clientZod: sqlZodType,
|
|
89
89
|
validationZod: sqlZodType,
|
|
90
90
|
});
|
|
@@ -153,56 +153,56 @@ function createBuilder(config) {
|
|
|
153
153
|
clientTransform: config.clientTransform, // <-- FIX: Make sure transform is passed through
|
|
154
154
|
validationTransform: config.validationTransform, // <-- FIX: Make sure transform is passed through
|
|
155
155
|
},
|
|
156
|
-
initialState: (
|
|
156
|
+
initialState: (value, schemaModifier) => {
|
|
157
157
|
if (completedStages.has("new")) {
|
|
158
158
|
throw new Error("initialState() can only be called once in the chain");
|
|
159
159
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
: config.sqlZod; // If only a primitive is passed, the "new" schema is still the SQL one.
|
|
168
|
-
let finalDefaultValue;
|
|
169
|
-
if (hasSchemaArg) {
|
|
170
|
-
// Handles two arguments: .initialState(schema, defaultValue)
|
|
171
|
-
finalDefaultValue = isFunction(defaultValue)
|
|
172
|
-
? defaultValue()
|
|
173
|
-
: defaultValue;
|
|
160
|
+
let actualValue;
|
|
161
|
+
let baseSchema;
|
|
162
|
+
// Check if value is a Zod schema
|
|
163
|
+
if (value && typeof value === "object" && "_def" in value) {
|
|
164
|
+
// It's a Zod schema - infer the default value
|
|
165
|
+
baseSchema = value;
|
|
166
|
+
actualValue = inferDefaultFromZod(baseSchema, config.sqlConfig);
|
|
174
167
|
}
|
|
175
168
|
else {
|
|
176
|
-
//
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
169
|
+
// Get the actual value
|
|
170
|
+
actualValue = isFunction(value) ? value() : value;
|
|
171
|
+
// Create base Zod schema from the value type
|
|
172
|
+
// Check if it's a literal value (string, number, boolean)
|
|
173
|
+
if (typeof actualValue === "string" ||
|
|
174
|
+
typeof actualValue === "number" ||
|
|
175
|
+
typeof actualValue === "boolean") {
|
|
176
|
+
baseSchema = z.literal(actualValue);
|
|
177
|
+
}
|
|
178
|
+
else if (actualValue instanceof Date) {
|
|
179
|
+
baseSchema = z.date();
|
|
180
|
+
}
|
|
181
|
+
else if (actualValue === null) {
|
|
182
|
+
baseSchema = z.null();
|
|
183
|
+
}
|
|
184
|
+
else if (actualValue === undefined) {
|
|
185
|
+
baseSchema = z.undefined();
|
|
183
186
|
}
|
|
184
187
|
else {
|
|
185
|
-
|
|
186
|
-
finalDefaultValue = isFunction(singleArg)
|
|
187
|
-
? singleArg({ sql: config.sqlZod })
|
|
188
|
-
: singleArg;
|
|
188
|
+
baseSchema = z.any();
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
|
+
// Apply schema modifier if provided
|
|
192
|
+
const newSchema = schemaModifier
|
|
193
|
+
? schemaModifier(baseSchema)
|
|
194
|
+
: baseSchema;
|
|
191
195
|
const newCompletedStages = new Set(completedStages);
|
|
192
196
|
newCompletedStages.add("new");
|
|
193
|
-
//
|
|
194
|
-
|
|
195
|
-
// If ONLY a primitive was passed, we MUST also create a union.
|
|
196
|
-
const newClientZod = hasSchemaArg
|
|
197
|
-
? z.union([config.sqlZod, newSchema])
|
|
198
|
-
: z.union([config.sqlZod, z.any()]); // Create the union for the primitive case
|
|
197
|
+
// Create union for client/validation
|
|
198
|
+
const clientValidationSchema = z.union([config.sqlZod, newSchema]);
|
|
199
199
|
return createBuilder({
|
|
200
200
|
...config,
|
|
201
201
|
stage: "new",
|
|
202
202
|
newZod: newSchema,
|
|
203
|
-
initialValue:
|
|
204
|
-
clientZod:
|
|
205
|
-
validationZod:
|
|
203
|
+
initialValue: actualValue,
|
|
204
|
+
clientZod: clientValidationSchema,
|
|
205
|
+
validationZod: clientValidationSchema,
|
|
206
206
|
completedStages: newCompletedStages,
|
|
207
207
|
});
|
|
208
208
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.102",
|
|
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",
|