cogsbox-shape 0.5.132 → 0.5.134
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 +3 -11
- package/dist/schema.js +49 -4
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -36,11 +36,10 @@ 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 NonLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T;
|
|
40
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]>;
|
|
41
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> {
|
|
42
41
|
initialState: {
|
|
43
|
-
<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>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<R>,
|
|
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, ZodTypeFromPrimitive<R>, R, CollapsedUnion<TSql, ZodTypeFromPrimitive<R>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<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, ZodTypeFromPrimitive<TValue>, TValue, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>>> : Prettify<Builder<"new", T, TSql, ZodTypeFromPrimitive<TValue>, TValue, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>, CollapsedUnion<TSql, ZodTypeFromPrimitive<TValue>>>>;
|
|
44
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>>>;
|
|
45
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>>>;
|
|
46
45
|
};
|
|
@@ -289,15 +288,8 @@ type _DeriveViewShape<TTableName extends keyof TRegistry, TSelection, TRegistry
|
|
|
289
288
|
1
|
|
290
289
|
]>>> : never : never : never;
|
|
291
290
|
}> : OmitRelationFields<BaseShape, TRegistry[TTableName]["rawSchema"]> : never;
|
|
292
|
-
type
|
|
293
|
-
|
|
294
|
-
[K in keyof TRegistry[TTableName]["rawSchema"] as K extends "_tableName" ? never : K]: TRegistry[TTableName]["rawSchema"][K] extends {
|
|
295
|
-
config: {
|
|
296
|
-
sql: infer TSql extends SQLType;
|
|
297
|
-
};
|
|
298
|
-
} ? TSql["nullable"] extends true ? SQLTypeToTS<TSql> | null : SQLTypeToTS<TSql> : never;
|
|
299
|
-
} & (TSelection extends Record<string, any> ? {
|
|
300
|
-
[K in keyof TSelection & keyof TRegistry[TTableName]["rawSchema"]]?: TRegistry[TTableName]["rawSchema"][K] extends {
|
|
291
|
+
type DeriveViewDefaults<TTableName extends keyof TRegistry, TSelection, TRegistry extends RegistryShape, Depth extends any[] = []> = Prettify<TRegistry[TTableName]["zodSchemas"]["defaultValues"] & (TSelection extends Record<string, any> ? {
|
|
292
|
+
-readonly [K in keyof TSelection & keyof TRegistry[TTableName]["rawSchema"]]?: TRegistry[TTableName]["rawSchema"][K] extends {
|
|
301
293
|
config: {
|
|
302
294
|
sql: {
|
|
303
295
|
type: infer RelType;
|
package/dist/schema.js
CHANGED
|
@@ -135,11 +135,13 @@ function createBuilder(config) {
|
|
|
135
135
|
let finalSchema;
|
|
136
136
|
// Check if value is a Zod schema (single argument case)
|
|
137
137
|
if (value && typeof value === "object" && "_def" in value) {
|
|
138
|
+
// It's a Zod schema - infer the default value
|
|
138
139
|
baseSchema = value;
|
|
139
140
|
actualValue = inferDefaultFromZod(baseSchema, config.sqlConfig);
|
|
140
141
|
finalSchema = baseSchema;
|
|
141
142
|
}
|
|
142
143
|
else {
|
|
144
|
+
// Get the actual value
|
|
143
145
|
actualValue = isFunction(value) ? value() : value;
|
|
144
146
|
// If second parameter is provided and is a Zod schema, use it directly
|
|
145
147
|
if (schemaOrModifier &&
|
|
@@ -148,13 +150,56 @@ function createBuilder(config) {
|
|
|
148
150
|
finalSchema = schemaOrModifier;
|
|
149
151
|
}
|
|
150
152
|
else if (isFunction(schemaOrModifier)) {
|
|
151
|
-
//
|
|
152
|
-
|
|
153
|
+
// It's a schema modifier function
|
|
154
|
+
// Create base Zod schema from the value type
|
|
155
|
+
if (typeof actualValue === "string") {
|
|
156
|
+
baseSchema = z.string();
|
|
157
|
+
}
|
|
158
|
+
else if (typeof actualValue === "number") {
|
|
159
|
+
baseSchema = z.number();
|
|
160
|
+
}
|
|
161
|
+
else if (typeof actualValue === "boolean") {
|
|
162
|
+
baseSchema = z.boolean();
|
|
163
|
+
}
|
|
164
|
+
else if (actualValue instanceof Date) {
|
|
165
|
+
baseSchema = z.date();
|
|
166
|
+
}
|
|
167
|
+
else if (actualValue === null) {
|
|
168
|
+
baseSchema = z.null();
|
|
169
|
+
}
|
|
170
|
+
else if (actualValue === undefined) {
|
|
171
|
+
baseSchema = z.undefined();
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
baseSchema = z.any();
|
|
175
|
+
}
|
|
176
|
+
// Apply the modifier
|
|
153
177
|
finalSchema = schemaOrModifier(baseSchema);
|
|
154
178
|
}
|
|
155
179
|
else {
|
|
156
|
-
// No schema provided,
|
|
157
|
-
|
|
180
|
+
// No schema provided, create from value type
|
|
181
|
+
if (typeof actualValue === "string") {
|
|
182
|
+
baseSchema = z.string();
|
|
183
|
+
}
|
|
184
|
+
else if (typeof actualValue === "number") {
|
|
185
|
+
baseSchema = z.number();
|
|
186
|
+
}
|
|
187
|
+
else if (typeof actualValue === "boolean") {
|
|
188
|
+
baseSchema = z.boolean();
|
|
189
|
+
}
|
|
190
|
+
else if (actualValue instanceof Date) {
|
|
191
|
+
baseSchema = z.date();
|
|
192
|
+
}
|
|
193
|
+
else if (actualValue === null) {
|
|
194
|
+
baseSchema = z.null();
|
|
195
|
+
}
|
|
196
|
+
else if (actualValue === undefined) {
|
|
197
|
+
baseSchema = z.undefined();
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
baseSchema = z.any();
|
|
201
|
+
}
|
|
202
|
+
finalSchema = baseSchema;
|
|
158
203
|
}
|
|
159
204
|
}
|
|
160
205
|
const newCompletedStages = new Set(completedStages);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.134",
|
|
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",
|