cogsbox-shape 0.5.66 → 0.5.68
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 +17 -62
- package/dist/schema.js +9 -7
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -57,10 +57,10 @@ type SQLToZodType<T extends SQLType, TDefault extends boolean> = T["pk"] extends
|
|
|
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
58
|
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
59
|
initialState: {
|
|
60
|
-
<TResult>(defaultValue: () =>
|
|
61
|
-
<TNewNext extends z.ZodTypeAny, TDefaultNext>(schema: ((tools: {
|
|
60
|
+
<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
|
+
<TNewNext extends z.ZodTypeAny, const TDefaultNext>(schema: ((tools: {
|
|
62
62
|
sql: TSql;
|
|
63
|
-
}) => TNewNext) | TNewNext, defaultValue: () => TDefaultNext): Prettify<Builder<"new", T, TSql, TNewNext,
|
|
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
64
|
};
|
|
65
65
|
client: <TClientNext extends z.ZodTypeAny>(schema: ((tools: {
|
|
66
66
|
sql: TSql;
|
|
@@ -176,9 +176,10 @@ type EnrichedField<K extends string, V, TSchema extends ShapeSchema> = V & {
|
|
|
176
176
|
type EnrichFields<T extends ShapeSchema> = {
|
|
177
177
|
[K in keyof T]: K extends string ? EnrichedField<K, T[K], T> : T[K];
|
|
178
178
|
};
|
|
179
|
+
declare const SchemaWrapperBrand: unique symbol;
|
|
179
180
|
export declare function schema<T extends ShapeSchema>(schema: T): EnrichFields<T> & {
|
|
180
181
|
_tableName: T["_tableName"];
|
|
181
|
-
|
|
182
|
+
[SchemaWrapperBrand]: true;
|
|
182
183
|
};
|
|
183
184
|
export type RelationType = "hasMany" | "hasOne" | "manyToMany";
|
|
184
185
|
type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
@@ -194,14 +195,14 @@ type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
|
194
195
|
type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T>;
|
|
195
196
|
export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>> = {
|
|
196
197
|
_tableName: string;
|
|
197
|
-
_schemaWrapper: true;
|
|
198
198
|
__schemaId?: string;
|
|
199
199
|
[key: string]: T[keyof T] | string | ((id: number) => string) | true | undefined;
|
|
200
200
|
};
|
|
201
201
|
type ValidShapeField = ReturnType<typeof s.sql>;
|
|
202
202
|
export type ShapeSchema = {
|
|
203
203
|
_tableName: string;
|
|
204
|
-
[
|
|
204
|
+
[SchemaWrapperBrand]?: true;
|
|
205
|
+
[key: string]: string | ((id: number) => string) | ValidShapeField | true | undefined;
|
|
205
206
|
};
|
|
206
207
|
type Relation<U extends Schema<any>> = {
|
|
207
208
|
type: RelationType;
|
|
@@ -213,38 +214,13 @@ type Relation<U extends Schema<any>> = {
|
|
|
213
214
|
type Prettify<T> = {
|
|
214
215
|
[K in keyof T]: T[K];
|
|
215
216
|
} & {};
|
|
216
|
-
export type InferDBSchema<T> = {
|
|
217
|
-
[K in keyof T as K extends "_tableName" | "_schemaWrapper" | "__schemaId" ? never : K]: T[K] extends {
|
|
218
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
219
|
-
} ? DbType : T[K] extends {
|
|
220
|
-
dbType: infer DbType extends z.ZodTypeAny;
|
|
221
|
-
} ? DbType : T[K] extends () => {
|
|
222
|
-
type: "hasMany";
|
|
223
|
-
schema: infer S;
|
|
224
|
-
} ? z.ZodArray<z.ZodObject<{
|
|
225
|
-
[P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
|
|
226
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
227
|
-
} ? DbType : never;
|
|
228
|
-
}>> : T[K] extends () => {
|
|
229
|
-
type: "hasOne" | "belongsTo";
|
|
230
|
-
schema: infer S;
|
|
231
|
-
} ? z.ZodObject<{
|
|
232
|
-
[P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
|
|
233
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
234
|
-
} ? DbType : never;
|
|
235
|
-
}> : never;
|
|
236
|
-
};
|
|
237
|
-
export declare function reference<TField extends object>(config: TField): {
|
|
238
|
-
type: "reference";
|
|
239
|
-
to: TField;
|
|
240
|
-
};
|
|
241
217
|
export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
|
|
242
218
|
type SchemaDefinition = {
|
|
243
219
|
_tableName: string;
|
|
244
220
|
[key: string]: any;
|
|
245
221
|
};
|
|
246
222
|
type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodValidationSchema", Depth extends any[] = []> = Depth["length"] extends 10 ? any : {
|
|
247
|
-
[K in keyof T as K extends "_tableName" ? never : K]: T[K] extends {
|
|
223
|
+
[K in keyof T as K extends "_tableName" | typeof SchemaWrapperBrand ? never : K]: T[K] extends {
|
|
248
224
|
config: {
|
|
249
225
|
sql: {
|
|
250
226
|
type: "hasMany" | "manyToMany";
|
|
@@ -253,7 +229,7 @@ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodVa
|
|
|
253
229
|
};
|
|
254
230
|
} ? z.ZodArray<S extends {
|
|
255
231
|
_tableName: string;
|
|
256
|
-
} ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : z.ZodObject<any>> : T[K] extends {
|
|
232
|
+
} ? z.ZodObject<Omit<InferSchemaByKey<S, Key, [...Depth, 1]>, typeof SchemaWrapperBrand>> : z.ZodObject<any>> : T[K] extends {
|
|
257
233
|
config: {
|
|
258
234
|
sql: {
|
|
259
235
|
type: "hasOne" | "belongsTo";
|
|
@@ -262,17 +238,7 @@ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodVa
|
|
|
262
238
|
};
|
|
263
239
|
} ? S extends {
|
|
264
240
|
_tableName: string;
|
|
265
|
-
} ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : z.ZodObject<any> : T[K] extends
|
|
266
|
-
type: "hasMany" | "manyToMany";
|
|
267
|
-
schema: infer S extends {
|
|
268
|
-
_tableName: string;
|
|
269
|
-
};
|
|
270
|
-
} ? z.ZodArray<z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>>> : T[K] extends () => {
|
|
271
|
-
type: "hasOne" | "belongsTo";
|
|
272
|
-
schema: infer S extends {
|
|
273
|
-
_tableName: string;
|
|
274
|
-
};
|
|
275
|
-
} ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : T[K] extends {
|
|
241
|
+
} ? z.ZodObject<Omit<InferSchemaByKey<S, Key, [...Depth, 1]>, typeof SchemaWrapperBrand>> : z.ZodObject<any> : T[K] extends {
|
|
276
242
|
type: "reference";
|
|
277
243
|
to: () => infer RefField;
|
|
278
244
|
} ? RefField extends {
|
|
@@ -302,28 +268,17 @@ type InferDefaultValues2<T> = {
|
|
|
302
268
|
schema: infer S extends SchemaDefinition;
|
|
303
269
|
} ? Prettify<InferDefaultValues2<S>> : never;
|
|
304
270
|
};
|
|
305
|
-
type PrettifiedSchema<T, K extends "Sql" | "Client" | "Validation"> = z.ZodObject<Prettify<Omit<K extends "Sql" ? InferSqlSchema<T> : K extends "Client" ? InferClientSchema<T> : InferValidationSchema<T>, "_schemaWrapper" | "_tableName">>>;
|
|
306
271
|
export declare function createSchema<T extends {
|
|
307
272
|
_tableName: string;
|
|
308
|
-
|
|
309
|
-
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R,
|
|
310
|
-
sqlSchema:
|
|
311
|
-
clientSchema:
|
|
312
|
-
validationSchema:
|
|
273
|
+
[SchemaWrapperBrand]?: true;
|
|
274
|
+
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R, typeof SchemaWrapperBrand> = Omit<T & R, typeof SchemaWrapperBrand>>(schema: T, relations?: R): {
|
|
275
|
+
sqlSchema: z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>;
|
|
276
|
+
clientSchema: z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>;
|
|
277
|
+
validationSchema: z.ZodObject<Prettify<InferValidationSchema<TActualSchema>>>;
|
|
313
278
|
defaultValues: Prettify<InferDefaultValues2<TActualSchema>>;
|
|
314
|
-
toClient: (dbObject: z.infer<
|
|
315
|
-
toDb: (clientObject: z.infer<
|
|
279
|
+
toClient: (dbObject: z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>;
|
|
280
|
+
toDb: (clientObject: z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>;
|
|
316
281
|
};
|
|
317
|
-
export type InferSchemaTypes<T extends Schema<any>> = Prettify<{
|
|
318
|
-
/** The TypeScript type for data as it exists in the database. */
|
|
319
|
-
sql: z.infer<ReturnType<typeof createSchema<T>>["sqlSchema"]>;
|
|
320
|
-
/** The TypeScript type for data as it is represented on the client. */
|
|
321
|
-
client: z.infer<ReturnType<typeof createSchema<T>>["clientSchema"]>;
|
|
322
|
-
/** The TypeScript type for data during validation, often the most flexible shape. */
|
|
323
|
-
validation: z.infer<ReturnType<typeof createSchema<T>>["validationSchema"]>;
|
|
324
|
-
/** The TypeScript type for the default values object. */
|
|
325
|
-
defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
|
|
326
|
-
}>;
|
|
327
282
|
type RelationBuilders<TSchema> = {
|
|
328
283
|
reference: <TField extends object>(fieldGetter: () => TField) => {
|
|
329
284
|
type: "reference";
|
package/dist/schema.js
CHANGED
|
@@ -257,9 +257,12 @@ function createBuilder(config) {
|
|
|
257
257
|
return builderObject;
|
|
258
258
|
}
|
|
259
259
|
// The table function that enriches fields with their key information
|
|
260
|
+
const SchemaWrapperBrand = Symbol("SchemaWrapper");
|
|
261
|
+
// Update the schema function to use the symbol
|
|
260
262
|
export function schema(schema) {
|
|
261
263
|
const enrichedSchema = {
|
|
262
264
|
_tableName: schema._tableName,
|
|
265
|
+
[SchemaWrapperBrand]: true, // Add the symbol property
|
|
263
266
|
};
|
|
264
267
|
for (const key in schema) {
|
|
265
268
|
if (key !== "_tableName" &&
|
|
@@ -270,7 +273,6 @@ export function schema(schema) {
|
|
|
270
273
|
_key: key,
|
|
271
274
|
_fieldType: schema[key],
|
|
272
275
|
},
|
|
273
|
-
// FIX: Assign the parent schema directly and only once.
|
|
274
276
|
__parentTableType: schema,
|
|
275
277
|
};
|
|
276
278
|
}
|
|
@@ -326,12 +328,12 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
326
328
|
}
|
|
327
329
|
return undefined;
|
|
328
330
|
}
|
|
329
|
-
export function reference(config) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
331
|
+
// export function reference<TField extends object>(config: TField) {
|
|
332
|
+
// return {
|
|
333
|
+
// type: "reference" as const,
|
|
334
|
+
// to: config,
|
|
335
|
+
// };
|
|
336
|
+
// }
|
|
335
337
|
export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
|
|
336
338
|
// If schemas are provided, use them (to avoid circular calls)
|
|
337
339
|
if (clientSchema && dbSchema) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.68",
|
|
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",
|