cogsbox-shape 0.5.118 → 0.5.120
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 +95 -33
- package/dist/schema.js +52 -23
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -222,7 +222,7 @@ type ResolutionMap<S extends Record<string, SchemaWithPlaceholders>> = {
|
|
|
222
222
|
} : never;
|
|
223
223
|
};
|
|
224
224
|
};
|
|
225
|
-
type ResolveField<Field, Resolution
|
|
225
|
+
type ResolveField<Field, Resolution> = Field extends PlaceholderReference ? Resolution : Field extends Reference<any> ? Resolution : Field extends PlaceholderRelation<infer RelType> ? Resolution extends {
|
|
226
226
|
toKey: infer ToKey;
|
|
227
227
|
} ? ToKey extends {
|
|
228
228
|
__parentTableType: infer TargetSchema extends Schema<any>;
|
|
@@ -235,17 +235,17 @@ type ResolveField<Field, Resolution, AllSchemas extends Record<string, any>> = F
|
|
|
235
235
|
} : RelType extends "manyToMany" ? BaseRelationConfig<TargetSchema> & {
|
|
236
236
|
type: "manyToMany";
|
|
237
237
|
} : never, RelType extends "hasMany" | "manyToMany" ? z.ZodArray<z.ZodObject<any>> : z.ZodObject<any>, RelType extends "hasMany" | "manyToMany" ? z.ZodArray<z.ZodObject<any>> : z.ZodObject<any>, RelType extends "hasMany" | "manyToMany" ? any[] : any, RelType extends "hasMany" | "manyToMany" ? z.ZodArray<z.ZodObject<any>> : z.ZodObject<any>, RelType extends "hasMany" | "manyToMany" ? z.ZodArray<z.ZodObject<any>> : z.ZodObject<any>> : never : never : Field;
|
|
238
|
-
type ResolveSchema<Schema extends SchemaWithPlaceholders, Resolutions extends Record<string, any
|
|
239
|
-
[K in keyof Schema]: K extends keyof Resolutions ? ResolveField<Schema[K], Resolutions[K]
|
|
238
|
+
type ResolveSchema<Schema extends SchemaWithPlaceholders, Resolutions extends Record<string, any>> = {
|
|
239
|
+
[K in keyof Schema]: K extends keyof Resolutions ? ResolveField<Schema[K], Resolutions[K]> : Schema[K];
|
|
240
240
|
};
|
|
241
241
|
type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionMap<S>> = {
|
|
242
242
|
[K in keyof S]: {
|
|
243
|
-
rawSchema: ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}
|
|
243
|
+
rawSchema: ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>;
|
|
244
244
|
zodSchemas: {
|
|
245
|
-
sqlSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}
|
|
246
|
-
clientSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}
|
|
247
|
-
validationSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}
|
|
248
|
-
defaultValues: Prettify<DeriveDefaults<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}
|
|
245
|
+
sqlSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>, "zodSqlSchema">>>;
|
|
246
|
+
clientSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>, "zodClientSchema">>>;
|
|
247
|
+
validationSchema: z.ZodObject<Prettify<DeriveSchemaByKey<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>, "zodValidationSchema">>>;
|
|
248
|
+
defaultValues: Prettify<DeriveDefaults<ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}>>>;
|
|
249
249
|
toClient: (dbObject: any) => any;
|
|
250
250
|
toDb: (clientObject: any) => any;
|
|
251
251
|
};
|
|
@@ -259,17 +259,96 @@ type IsRelationField<Field> = Field extends {
|
|
|
259
259
|
};
|
|
260
260
|
};
|
|
261
261
|
} ? true : false;
|
|
262
|
-
type GetRelationRegistryKey<Field, TRegistry
|
|
262
|
+
type GetRelationRegistryKey<Field, TRegistry> = Field extends {
|
|
263
263
|
config: {
|
|
264
264
|
sql: {
|
|
265
|
-
schema: () => infer
|
|
265
|
+
schema: () => infer S;
|
|
266
266
|
};
|
|
267
267
|
};
|
|
268
|
-
} ?
|
|
269
|
-
_tableName: infer
|
|
268
|
+
} ? S extends {
|
|
269
|
+
_tableName: infer T;
|
|
270
270
|
} ? {
|
|
271
|
-
[K in keyof TRegistry]: TRegistry[K]
|
|
271
|
+
[K in keyof TRegistry]: TRegistry[K] extends {
|
|
272
|
+
definition: {
|
|
273
|
+
_tableName: T;
|
|
274
|
+
};
|
|
275
|
+
} ? K : never;
|
|
272
276
|
}[keyof TRegistry] : never : never;
|
|
277
|
+
type OmitRelationFields<Shape, RawSchema> = Omit<Shape, {
|
|
278
|
+
[K in keyof Shape]: K extends keyof RawSchema ? IsRelationField<RawSchema[K]> extends true ? K : never : never;
|
|
279
|
+
}[keyof Shape]>;
|
|
280
|
+
type _DeriveViewShape<TTableName extends keyof TRegistry, TSelection, TRegistry extends Record<string, {
|
|
281
|
+
definition: Record<string, any>;
|
|
282
|
+
schemas: {
|
|
283
|
+
client: z.ZodObject<any>;
|
|
284
|
+
validation: z.ZodObject<any>;
|
|
285
|
+
};
|
|
286
|
+
}>, TKey extends "client" | "validation", Depth extends any[] = []> = Depth["length"] extends 10 ? any : TRegistry[TTableName]["schemas"][TKey] extends z.ZodObject<infer BaseShape> ? TSelection extends Record<string, any> ? Prettify<OmitRelationFields<BaseShape, TRegistry[TTableName]["definition"]> & {
|
|
287
|
+
[K in keyof TSelection & keyof TRegistry[TTableName]["definition"] as IsRelationField<TRegistry[TTableName]["definition"][K]> extends true ? K : never]: GetRelationRegistryKey<TRegistry[TTableName]["definition"][K], TRegistry> extends infer TargetKey ? TargetKey extends keyof TRegistry ? TRegistry[TTableName]["definition"][K] extends {
|
|
288
|
+
config: {
|
|
289
|
+
sql: {
|
|
290
|
+
type: infer RelType;
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
} ? RelType extends "hasMany" | "manyToMany" ? z.ZodArray<z.ZodObject<_DeriveViewShape<TargetKey, TSelection[K], TRegistry, TKey, [
|
|
294
|
+
...Depth,
|
|
295
|
+
1
|
|
296
|
+
]>>> : z.ZodOptional<z.ZodObject<_DeriveViewShape<TargetKey, TSelection[K], TRegistry, TKey, [
|
|
297
|
+
...Depth,
|
|
298
|
+
1
|
|
299
|
+
]>>> : never : never : never;
|
|
300
|
+
}> : OmitRelationFields<BaseShape, TRegistry[TTableName]["definition"]> : never;
|
|
301
|
+
type DeriveViewDefaults<TTableName extends keyof TRegistry, TSelection, TRegistry extends Record<string, {
|
|
302
|
+
definition: Record<string, any>;
|
|
303
|
+
defaults: Record<string, any>;
|
|
304
|
+
}>, Depth extends any[] = []> = Prettify<TRegistry[TTableName]["defaults"] & (TSelection extends Record<string, any> ? {
|
|
305
|
+
-readonly [K in keyof TSelection & keyof TRegistry[TTableName]["definition"]]?: TRegistry[TTableName]["definition"][K] extends {
|
|
306
|
+
config: {
|
|
307
|
+
sql: {
|
|
308
|
+
type: infer RelType;
|
|
309
|
+
schema: any;
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
} ? GetRelationRegistryKey<TRegistry[TTableName]["definition"][K], TRegistry> extends infer TargetKey ? TargetKey extends keyof TRegistry ? RelType extends "hasMany" | "manyToMany" ? DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
313
|
+
...Depth,
|
|
314
|
+
1
|
|
315
|
+
]>[] : DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
316
|
+
...Depth,
|
|
317
|
+
1
|
|
318
|
+
]> | null : never : never : never;
|
|
319
|
+
} : {})>;
|
|
320
|
+
type _Internal_DeriveViewDefaults<TTableName extends keyof TRegistry, TSelection, TRegistry extends RegistryShape, Depth extends any[] = []> = Prettify<TRegistry[TTableName]["zodSchemas"]["defaultValues"] & (TSelection extends Record<string, any> ? {
|
|
321
|
+
-readonly [K in keyof TSelection & keyof TRegistry[TTableName]["rawSchema"]]?: TRegistry[TTableName]["rawSchema"][K] extends {
|
|
322
|
+
config: {
|
|
323
|
+
sql: {
|
|
324
|
+
type: infer RelType;
|
|
325
|
+
schema: any;
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
} ? GetRelationRegistryKey<TRegistry[TTableName]["rawSchema"][K], TRegistry> extends infer TargetKey ? TargetKey extends keyof TRegistry ? RelType extends "hasMany" | "manyToMany" ? _Internal_DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
329
|
+
...Depth,
|
|
330
|
+
1
|
|
331
|
+
]>[] : _Internal_DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
332
|
+
...Depth,
|
|
333
|
+
1
|
|
334
|
+
]> | null : never : never : never;
|
|
335
|
+
} : {})>;
|
|
336
|
+
export type DeriveViewResult<TTableName extends keyof TRegistry, TSelection, TRegistry extends Record<string, {
|
|
337
|
+
definition: Record<string, any> & {
|
|
338
|
+
_tableName: string;
|
|
339
|
+
};
|
|
340
|
+
schemas: {
|
|
341
|
+
sql: z.ZodObject<any>;
|
|
342
|
+
client: z.ZodObject<any>;
|
|
343
|
+
validation: z.ZodObject<any>;
|
|
344
|
+
};
|
|
345
|
+
defaults: Record<string, any>;
|
|
346
|
+
}>> = {
|
|
347
|
+
sql: TRegistry[TTableName]["schemas"]["sql"];
|
|
348
|
+
client: z.ZodObject<_DeriveViewShape<TTableName, TSelection, TRegistry, "client">>;
|
|
349
|
+
validation: z.ZodObject<_DeriveViewShape<TTableName, TSelection, TRegistry, "validation">>;
|
|
350
|
+
defaults: DeriveViewDefaults<TTableName, TSelection, TRegistry>;
|
|
351
|
+
};
|
|
273
352
|
type NavigationProxy<CurrentTable extends string, Registry extends RegistryShape> = CurrentTable extends keyof Registry ? {
|
|
274
353
|
[K in keyof Registry[CurrentTable]["rawSchema"] as IsRelationField<Registry[CurrentTable]["rawSchema"][K]> extends true ? K : never]: GetRelationRegistryKey<Registry[CurrentTable]["rawSchema"][K], Registry> extends infer TargetKey ? TargetKey extends keyof Registry ? NavigationProxy<TargetKey & string, Registry> : never : never;
|
|
275
354
|
} : {};
|
|
@@ -308,23 +387,6 @@ type RegistryShape = Record<string, {
|
|
|
308
387
|
toDb: (clientObject: any) => any;
|
|
309
388
|
};
|
|
310
389
|
}>;
|
|
311
|
-
type DeriveViewDefaults<TTableName extends keyof TRegistry, TSelection, TRegistry extends RegistryShape, Depth extends any[] = []> = Depth["length"] extends 10 ? any : Prettify<DeriveDefaults<TRegistry[TTableName]["rawSchema"]> & (TSelection extends Record<string, any> ? {
|
|
312
|
-
-readonly [K in keyof TSelection & keyof TRegistry[TTableName]["rawSchema"]]?: TRegistry[TTableName]["rawSchema"][K] extends {
|
|
313
|
-
config: {
|
|
314
|
-
sql: {
|
|
315
|
-
type: infer RelType;
|
|
316
|
-
schema: any;
|
|
317
|
-
};
|
|
318
|
-
};
|
|
319
|
-
} ? GetRelationRegistryKey<TRegistry[TTableName]["rawSchema"][K], TRegistry> extends infer TargetKey ? TargetKey extends keyof TRegistry ? RelType extends "hasMany" | "manyToMany" ? DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
320
|
-
...Depth,
|
|
321
|
-
1
|
|
322
|
-
]>[] : // Recursively call with the CORRECT key (TargetKey)
|
|
323
|
-
DeriveViewDefaults<TargetKey, TSelection[K], TRegistry, [
|
|
324
|
-
...Depth,
|
|
325
|
-
1
|
|
326
|
-
]> | null : never : never : never;
|
|
327
|
-
} : {})>;
|
|
328
390
|
type CreateSchemaBoxReturn<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionMap<S>, Resolved extends RegistryShape = ResolvedRegistryWithSchemas<S, R> extends RegistryShape ? ResolvedRegistryWithSchemas<S, R> : RegistryShape> = {
|
|
329
391
|
[K in keyof Resolved]: {
|
|
330
392
|
definition: Resolved[K]["rawSchema"];
|
|
@@ -342,9 +404,9 @@ type CreateSchemaBoxReturn<S extends Record<string, SchemaWithPlaceholders>, R e
|
|
|
342
404
|
RelationSelection: NavigationToSelection<NavigationProxy<K & string, Resolved>>;
|
|
343
405
|
createView: <const TSelection extends NavigationToSelection<NavigationProxy<K & string, Resolved>>>(selection: TSelection) => {
|
|
344
406
|
sql: Resolved[K]["zodSchemas"]["sqlSchema"];
|
|
345
|
-
client: z.ZodObject<BuildZodShape<K, TSelection, "clientSchema", Resolved>>;
|
|
346
|
-
validation: z.ZodObject<BuildZodShape<K, TSelection, "validationSchema", Resolved>>;
|
|
347
|
-
defaults: Prettify<
|
|
407
|
+
client: z.ZodObject<BuildZodShape<K & string, TSelection, "clientSchema", Resolved>>;
|
|
408
|
+
validation: z.ZodObject<BuildZodShape<K & string, TSelection, "validationSchema", Resolved>>;
|
|
409
|
+
defaults: Prettify<_Internal_DeriveViewDefaults<K & string, TSelection, Resolved>>;
|
|
348
410
|
};
|
|
349
411
|
};
|
|
350
412
|
};
|
package/dist/schema.js
CHANGED
|
@@ -126,53 +126,82 @@ function createBuilder(config) {
|
|
|
126
126
|
clientTransform: config.clientTransform, // <-- FIX: Make sure transform is passed through
|
|
127
127
|
validationTransform: config.validationTransform, // <-- FIX: Make sure transform is passed through
|
|
128
128
|
},
|
|
129
|
-
initialState: (value,
|
|
129
|
+
initialState: (value, schemaOrModifier) => {
|
|
130
130
|
if (completedStages.has("new")) {
|
|
131
131
|
throw new Error("initialState() can only be called once in the chain");
|
|
132
132
|
}
|
|
133
133
|
let actualValue;
|
|
134
134
|
let baseSchema;
|
|
135
|
-
|
|
135
|
+
let finalSchema;
|
|
136
|
+
// Check if value is a Zod schema (single argument case)
|
|
136
137
|
if (value && typeof value === "object" && "_def" in value) {
|
|
137
138
|
// It's a Zod schema - infer the default value
|
|
138
139
|
baseSchema = value;
|
|
139
140
|
actualValue = inferDefaultFromZod(baseSchema, config.sqlConfig);
|
|
141
|
+
finalSchema = baseSchema;
|
|
140
142
|
}
|
|
141
143
|
else {
|
|
142
144
|
// Get the actual value
|
|
143
145
|
actualValue = isFunction(value) ? value() : value;
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
baseSchema = z.literal(actualValue);
|
|
146
|
+
// If second parameter is provided and is a Zod schema, use it directly
|
|
147
|
+
if (schemaOrModifier &&
|
|
148
|
+
typeof schemaOrModifier === "object" &&
|
|
149
|
+
"_def" in schemaOrModifier) {
|
|
150
|
+
finalSchema = schemaOrModifier;
|
|
150
151
|
}
|
|
151
|
-
else if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
152
|
+
else if (isFunction(schemaOrModifier)) {
|
|
153
|
+
// It's a schema modifier function
|
|
154
|
+
// Create base Zod schema from the value type
|
|
155
|
+
if (typeof actualValue === "string" ||
|
|
156
|
+
typeof actualValue === "number" ||
|
|
157
|
+
typeof actualValue === "boolean") {
|
|
158
|
+
baseSchema = z.literal(actualValue);
|
|
159
|
+
}
|
|
160
|
+
else if (actualValue instanceof Date) {
|
|
161
|
+
baseSchema = z.date();
|
|
162
|
+
}
|
|
163
|
+
else if (actualValue === null) {
|
|
164
|
+
baseSchema = z.null();
|
|
165
|
+
}
|
|
166
|
+
else if (actualValue === undefined) {
|
|
167
|
+
baseSchema = z.undefined();
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
baseSchema = z.any();
|
|
171
|
+
}
|
|
172
|
+
// Apply the modifier
|
|
173
|
+
finalSchema = schemaOrModifier(baseSchema);
|
|
159
174
|
}
|
|
160
175
|
else {
|
|
161
|
-
|
|
176
|
+
// No schema provided, create from value type
|
|
177
|
+
if (typeof actualValue === "string" ||
|
|
178
|
+
typeof actualValue === "number" ||
|
|
179
|
+
typeof actualValue === "boolean") {
|
|
180
|
+
baseSchema = z.literal(actualValue);
|
|
181
|
+
}
|
|
182
|
+
else if (actualValue instanceof Date) {
|
|
183
|
+
baseSchema = z.date();
|
|
184
|
+
}
|
|
185
|
+
else if (actualValue === null) {
|
|
186
|
+
baseSchema = z.null();
|
|
187
|
+
}
|
|
188
|
+
else if (actualValue === undefined) {
|
|
189
|
+
baseSchema = z.undefined();
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
baseSchema = z.any();
|
|
193
|
+
}
|
|
194
|
+
finalSchema = baseSchema;
|
|
162
195
|
}
|
|
163
196
|
}
|
|
164
|
-
// Apply schema modifier if provided
|
|
165
|
-
const newSchema = isFunction(schemaModifier)
|
|
166
|
-
? schemaModifier(baseSchema)
|
|
167
|
-
: baseSchema;
|
|
168
197
|
const newCompletedStages = new Set(completedStages);
|
|
169
198
|
newCompletedStages.add("new");
|
|
170
199
|
// Create union for client/validation
|
|
171
|
-
const clientValidationSchema = z.union([config.sqlZod,
|
|
200
|
+
const clientValidationSchema = z.union([config.sqlZod, finalSchema]);
|
|
172
201
|
return createBuilder({
|
|
173
202
|
...config,
|
|
174
203
|
stage: "new",
|
|
175
|
-
newZod:
|
|
204
|
+
newZod: finalSchema,
|
|
176
205
|
initialValue: actualValue,
|
|
177
206
|
clientZod: clientValidationSchema,
|
|
178
207
|
validationZod: clientValidationSchema,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.120",
|
|
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",
|