cogsbox-shape 0.5.65 → 0.5.66
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 +66 -64
- package/dist/schema.js +227 -158
- package/package.json +7 -3
- package/dist/example/schema.d.ts +0 -554
- package/dist/example/schema.js +0 -6
- package/dist/example/user.d.ts +0 -552
- package/dist/example/user.js +0 -77
package/dist/schema.d.ts
CHANGED
|
@@ -54,9 +54,10 @@ type SQLToZodType<T extends SQLType, TDefault extends boolean> = T["pk"] extends
|
|
|
54
54
|
} ? TDefault extends true ? never : z.ZodNullable<z.ZodDate> : z.ZodNullable<z.ZodDate> : never : T["type"] extends "varchar" | "char" | "text" | "longtext" ? z.ZodString : T["type"] extends "int" ? z.ZodNumber : T["type"] extends "boolean" ? z.ZodBoolean : T["type"] extends "date" | "datetime" ? T extends {
|
|
55
55
|
default: "CURRENT_TIMESTAMP";
|
|
56
56
|
} ? TDefault extends true ? never : z.ZodDate : z.ZodDate : never;
|
|
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;
|
|
57
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> {
|
|
58
59
|
initialState: {
|
|
59
|
-
<
|
|
60
|
+
<TResult>(defaultValue: () => 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, ZodTypeFromPrimitive<TResult>, TResult, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>, InferSmartClientType<TSql, ZodTypeFromPrimitive<TResult>>>>;
|
|
60
61
|
<TNewNext extends z.ZodTypeAny, TDefaultNext>(schema: ((tools: {
|
|
61
62
|
sql: TSql;
|
|
62
63
|
}) => TNewNext) | TNewNext, defaultValue: () => TDefaultNext): Prettify<Builder<"new", T, TSql, TNewNext, z.infer<TNewNext>, InferSmartClientType<TSql, TNewNext>, InferSmartClientType<TSql, TNewNext>>>;
|
|
@@ -152,7 +153,7 @@ interface ShapeAPI {
|
|
|
152
153
|
defaultCount?: number;
|
|
153
154
|
}) => Builder<"relation", RelationConfig<T>, z.ZodOptional<z.ZodArray<z.ZodAny>>, z.ZodOptional<z.ZodArray<z.ZodAny>>, any[], z.ZodOptional<z.ZodArray<z.ZodAny>>, z.ZodOptional<z.ZodArray<z.ZodAny>>>;
|
|
154
155
|
}
|
|
155
|
-
export declare const
|
|
156
|
+
export declare const s: ShapeAPI;
|
|
156
157
|
declare function createBuilder<TStage extends "sql" | "relation" | "new" | "client" | "validation", T extends SQLType | RelationConfig<any>, TSql extends z.ZodTypeAny, TNew extends z.ZodTypeAny, TInitialValue, TClient extends z.ZodTypeAny, TValidation extends z.ZodTypeAny>(config: {
|
|
157
158
|
stage: TStage;
|
|
158
159
|
sqlConfig: T;
|
|
@@ -165,49 +166,19 @@ declare function createBuilder<TStage extends "sql" | "relation" | "new" | "clie
|
|
|
165
166
|
clientTransform?: (schema: z.ZodTypeAny) => z.ZodTypeAny;
|
|
166
167
|
validationTransform?: (schema: z.ZodTypeAny) => z.ZodTypeAny;
|
|
167
168
|
}): Builder<TStage, T, TSql, TNew, TInitialValue, TClient, TValidation>;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
type: "hasMany";
|
|
175
|
-
fromKey: string;
|
|
176
|
-
toKey: T[keyof T];
|
|
177
|
-
schema: T;
|
|
178
|
-
defaultCount: number | undefined;
|
|
179
|
-
};
|
|
180
|
-
export declare function hasOne<T extends Schema<any>>(config: {
|
|
181
|
-
fromKey: string;
|
|
182
|
-
toKey: () => T[keyof T];
|
|
183
|
-
schema: () => T;
|
|
184
|
-
}): () => {
|
|
185
|
-
type: "hasOne";
|
|
186
|
-
fromKey: string;
|
|
187
|
-
toKey: T[keyof T];
|
|
188
|
-
schema: T;
|
|
169
|
+
type EnrichedField<K extends string, V, TSchema extends ShapeSchema> = V & {
|
|
170
|
+
__meta: {
|
|
171
|
+
_key: K;
|
|
172
|
+
_fieldType: V;
|
|
173
|
+
};
|
|
174
|
+
__parentTableType: TSchema;
|
|
189
175
|
};
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
toKey: () => T[keyof T];
|
|
193
|
-
schema: () => T;
|
|
194
|
-
}): () => {
|
|
195
|
-
type: "belongsTo";
|
|
196
|
-
fromKey: string;
|
|
197
|
-
toKey: T[keyof T];
|
|
198
|
-
schema: T;
|
|
176
|
+
type EnrichFields<T extends ShapeSchema> = {
|
|
177
|
+
[K in keyof T]: K extends string ? EnrichedField<K, T[K], T> : T[K];
|
|
199
178
|
};
|
|
200
|
-
export declare function
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
schema: () => T;
|
|
204
|
-
defaultCount?: number;
|
|
205
|
-
}): () => {
|
|
206
|
-
type: "manyToMany";
|
|
207
|
-
fromKey: string;
|
|
208
|
-
toKey: T[keyof T];
|
|
209
|
-
schema: T;
|
|
210
|
-
defaultCount: number | undefined;
|
|
179
|
+
export declare function schema<T extends ShapeSchema>(schema: T): EnrichFields<T> & {
|
|
180
|
+
_tableName: T["_tableName"];
|
|
181
|
+
_schemaWrapper: true;
|
|
211
182
|
};
|
|
212
183
|
export type RelationType = "hasMany" | "hasOne" | "manyToMany";
|
|
213
184
|
type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
@@ -220,18 +191,14 @@ type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
|
220
191
|
toClient?: (dbValue: any) => any;
|
|
221
192
|
toDb?: (clientValue: any) => any;
|
|
222
193
|
};
|
|
223
|
-
type
|
|
224
|
-
type ReferenceField<TField extends AnyFieldDefinition> = {
|
|
225
|
-
type: "reference";
|
|
226
|
-
to: () => TField;
|
|
227
|
-
};
|
|
228
|
-
type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T> | ReferenceField<AnyFieldDefinition>;
|
|
194
|
+
type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T>;
|
|
229
195
|
export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>> = {
|
|
230
196
|
_tableName: string;
|
|
197
|
+
_schemaWrapper: true;
|
|
231
198
|
__schemaId?: string;
|
|
232
|
-
[key: string]: T[keyof T] | string | ((id: number) => string) | undefined;
|
|
199
|
+
[key: string]: T[keyof T] | string | ((id: number) => string) | true | undefined;
|
|
233
200
|
};
|
|
234
|
-
type ValidShapeField = ReturnType<typeof
|
|
201
|
+
type ValidShapeField = ReturnType<typeof s.sql>;
|
|
235
202
|
export type ShapeSchema = {
|
|
236
203
|
_tableName: string;
|
|
237
204
|
[key: string]: string | ((id: number) => string) | ValidShapeField;
|
|
@@ -247,7 +214,7 @@ type Prettify<T> = {
|
|
|
247
214
|
[K in keyof T]: T[K];
|
|
248
215
|
} & {};
|
|
249
216
|
export type InferDBSchema<T> = {
|
|
250
|
-
[K in keyof T as K extends "_tableName" | "__schemaId" ? never : K]: T[K] extends {
|
|
217
|
+
[K in keyof T as K extends "_tableName" | "_schemaWrapper" | "__schemaId" ? never : K]: T[K] extends {
|
|
251
218
|
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
252
219
|
} ? DbType : T[K] extends {
|
|
253
220
|
dbType: infer DbType extends z.ZodTypeAny;
|
|
@@ -307,8 +274,12 @@ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodVa
|
|
|
307
274
|
};
|
|
308
275
|
} ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : T[K] extends {
|
|
309
276
|
type: "reference";
|
|
310
|
-
to: infer
|
|
311
|
-
} ?
|
|
277
|
+
to: () => infer RefField;
|
|
278
|
+
} ? RefField extends {
|
|
279
|
+
config: {
|
|
280
|
+
[P in Key]: infer ZodSchema;
|
|
281
|
+
};
|
|
282
|
+
} ? ZodSchema : never : T[K] extends {
|
|
312
283
|
config: {
|
|
313
284
|
[P in Key]: infer ZodSchema extends z.ZodTypeAny;
|
|
314
285
|
};
|
|
@@ -331,19 +302,19 @@ type InferDefaultValues2<T> = {
|
|
|
331
302
|
schema: infer S extends SchemaDefinition;
|
|
332
303
|
} ? Prettify<InferDefaultValues2<S>> : never;
|
|
333
304
|
};
|
|
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">>>;
|
|
334
306
|
export declare function createSchema<T extends {
|
|
335
307
|
_tableName: string;
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
308
|
+
_schemaWrapper: true;
|
|
309
|
+
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R, "_schemaWrapper"> = T & R>(schema: T, relations?: R): {
|
|
310
|
+
sqlSchema: PrettifiedSchema<TActualSchema, "Sql">;
|
|
311
|
+
clientSchema: PrettifiedSchema<TActualSchema, "Client">;
|
|
312
|
+
validationSchema: PrettifiedSchema<TActualSchema, "Validation">;
|
|
313
|
+
defaultValues: Prettify<InferDefaultValues2<TActualSchema>>;
|
|
314
|
+
toClient: (dbObject: z.infer<PrettifiedSchema<TActualSchema, "Sql">>) => z.infer<PrettifiedSchema<TActualSchema, "Client">>;
|
|
315
|
+
toDb: (clientObject: z.infer<PrettifiedSchema<TActualSchema, "Client">>) => z.infer<PrettifiedSchema<TActualSchema, "Sql">>;
|
|
341
316
|
};
|
|
342
|
-
export type InferSchemaTypes<T extends {
|
|
343
|
-
_tableName: string;
|
|
344
|
-
} & {
|
|
345
|
-
[key: string]: any;
|
|
346
|
-
}> = Prettify<{
|
|
317
|
+
export type InferSchemaTypes<T extends Schema<any>> = Prettify<{
|
|
347
318
|
/** The TypeScript type for data as it exists in the database. */
|
|
348
319
|
sql: z.infer<ReturnType<typeof createSchema<T>>["sqlSchema"]>;
|
|
349
320
|
/** The TypeScript type for data as it is represented on the client. */
|
|
@@ -353,4 +324,35 @@ export type InferSchemaTypes<T extends {
|
|
|
353
324
|
/** The TypeScript type for the default values object. */
|
|
354
325
|
defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
|
|
355
326
|
}>;
|
|
327
|
+
type RelationBuilders<TSchema> = {
|
|
328
|
+
reference: <TField extends object>(fieldGetter: () => TField) => {
|
|
329
|
+
type: "reference";
|
|
330
|
+
to: () => TField;
|
|
331
|
+
};
|
|
332
|
+
hasMany: <T extends Schema<any>, K extends keyof T & string, TField extends EnrichedField<K, T[K], T>>(config: {
|
|
333
|
+
fromKey: keyof TSchema & string;
|
|
334
|
+
toKey: () => TField;
|
|
335
|
+
defaultCount?: number;
|
|
336
|
+
}) => Builder<"relation", RelationConfig<TField["__parentTableType"]>, z.ZodArray<z.ZodObject<InferSqlSchema<TField["__parentTableType"]>>>, z.ZodArray<z.ZodObject<InferClientSchema<TField["__parentTableType"]>>>, any[], z.ZodArray<z.ZodObject<InferClientSchema<TField["__parentTableType"]>>>, z.ZodArray<z.ZodObject<InferValidationSchema<TField["__parentTableType"]>>>>;
|
|
337
|
+
hasOne: <T extends Schema<any>>(config: {
|
|
338
|
+
fromKey: keyof TSchema & string;
|
|
339
|
+
toKey: () => T[keyof T];
|
|
340
|
+
schema: () => T;
|
|
341
|
+
}) => Builder<"relation", RelationConfig<T>, z.ZodArray<any>, z.ZodArray<any>, any[], z.ZodArray<any>, z.ZodArray<any>>;
|
|
342
|
+
manyToMany: <T extends Schema<any>>(config: {
|
|
343
|
+
fromKey: keyof TSchema & string;
|
|
344
|
+
toKey: () => T[keyof T];
|
|
345
|
+
schema: () => T;
|
|
346
|
+
defaultCount?: number;
|
|
347
|
+
}) => Builder<"relation", RelationConfig<T>, z.ZodOptional<z.ZodArray<z.ZodAny>>, z.ZodOptional<z.ZodArray<z.ZodAny>>, any[], z.ZodOptional<z.ZodArray<z.ZodAny>>, z.ZodOptional<z.ZodArray<z.ZodAny>>>;
|
|
348
|
+
};
|
|
349
|
+
export declare function schemaRelations<TSchema extends Schema<any>, RefObject extends Record<string, any>>(baseSchema: TSchema, referencesBuilder: (rel: RelationBuilders<TSchema>) => RefObject): {
|
|
350
|
+
[K in keyof RefObject]: RefObject[K] & {
|
|
351
|
+
__meta: {
|
|
352
|
+
_key: K;
|
|
353
|
+
_fieldType: RefObject[K];
|
|
354
|
+
};
|
|
355
|
+
__parentTableType: TSchema;
|
|
356
|
+
};
|
|
357
|
+
};
|
|
356
358
|
export {};
|