cogsbox-shape 0.5.65 → 0.5.67

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 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
- <TDefaultNext>(defaultValue: () => TDefaultNext): Prettify<Builder<"new", T, TSql, TSql, TDefaultNext, TSql, TSql>>;
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 shape: ShapeAPI;
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,20 @@ 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
- export declare function hasMany<T extends Schema<any>>(config: {
169
- fromKey: string;
170
- toKey: () => T[keyof T];
171
- schema: () => T;
172
- defaultCount?: number;
173
- }): () => {
174
- type: "hasMany";
175
- fromKey: string;
176
- toKey: T[keyof T];
177
- schema: T;
178
- defaultCount: number | undefined;
169
+ type EnrichedField<K extends string, V, TSchema extends ShapeSchema> = V & {
170
+ __meta: {
171
+ _key: K;
172
+ _fieldType: V;
173
+ };
174
+ __parentTableType: TSchema;
179
175
  };
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;
176
+ type EnrichFields<T extends ShapeSchema> = {
177
+ [K in keyof T]: K extends string ? EnrichedField<K, T[K], T> : T[K];
189
178
  };
190
- export declare function belongsTo<T extends Schema<any>>(config: {
191
- fromKey: string;
192
- toKey: () => T[keyof T];
193
- schema: () => T;
194
- }): () => {
195
- type: "belongsTo";
196
- fromKey: string;
197
- toKey: T[keyof T];
198
- schema: T;
199
- };
200
- export declare function manyToMany<T extends Schema<any>>(config: {
201
- fromKey: string;
202
- toKey: () => T[keyof T];
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
+ declare const SchemaWrapperBrand: unique symbol;
180
+ export declare function schema<T extends ShapeSchema>(schema: T): EnrichFields<T> & {
181
+ _tableName: T["_tableName"];
182
+ [SchemaWrapperBrand]: true;
211
183
  };
212
184
  export type RelationType = "hasMany" | "hasOne" | "manyToMany";
213
185
  type BaseSchemaField<T extends SQLType = SQLType> = {
@@ -220,21 +192,17 @@ type BaseSchemaField<T extends SQLType = SQLType> = {
220
192
  toClient?: (dbValue: any) => any;
221
193
  toDb?: (clientValue: any) => any;
222
194
  };
223
- type AnyFieldDefinition = ReturnType<typeof shape.sql>;
224
- type ReferenceField<TField extends AnyFieldDefinition> = {
225
- type: "reference";
226
- to: () => TField;
227
- };
228
- type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T> | ReferenceField<AnyFieldDefinition>;
195
+ type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T>;
229
196
  export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>> = {
230
197
  _tableName: string;
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 shape.sql>;
201
+ type ValidShapeField = ReturnType<typeof s.sql>;
235
202
  export type ShapeSchema = {
236
203
  _tableName: string;
237
- [key: string]: string | ((id: number) => string) | ValidShapeField;
204
+ [SchemaWrapperBrand]?: true;
205
+ [key: string]: string | ((id: number) => string) | ValidShapeField | true | undefined;
238
206
  };
239
207
  type Relation<U extends Schema<any>> = {
240
208
  type: RelationType;
@@ -246,31 +214,6 @@ type Relation<U extends Schema<any>> = {
246
214
  type Prettify<T> = {
247
215
  [K in keyof T]: T[K];
248
216
  } & {};
249
- export type InferDBSchema<T> = {
250
- [K in keyof T as K extends "_tableName" | "__schemaId" ? never : K]: T[K] extends {
251
- zodDbSchema: infer DbType extends z.ZodTypeAny;
252
- } ? DbType : T[K] extends {
253
- dbType: infer DbType extends z.ZodTypeAny;
254
- } ? DbType : T[K] extends () => {
255
- type: "hasMany";
256
- schema: infer S;
257
- } ? z.ZodArray<z.ZodObject<{
258
- [P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
259
- zodDbSchema: infer DbType extends z.ZodTypeAny;
260
- } ? DbType : never;
261
- }>> : T[K] extends () => {
262
- type: "hasOne" | "belongsTo";
263
- schema: infer S;
264
- } ? z.ZodObject<{
265
- [P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
266
- zodDbSchema: infer DbType extends z.ZodTypeAny;
267
- } ? DbType : never;
268
- }> : never;
269
- };
270
- export declare function reference<TField extends object>(config: TField): {
271
- type: "reference";
272
- to: TField;
273
- };
274
217
  export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
275
218
  type SchemaDefinition = {
276
219
  _tableName: string;
@@ -307,8 +250,12 @@ type InferSchemaByKey<T, Key extends "zodSqlSchema" | "zodClientSchema" | "zodVa
307
250
  };
308
251
  } ? z.ZodObject<InferSchemaByKey<S, Key, [...Depth, 1]>> : T[K] extends {
309
252
  type: "reference";
310
- to: infer ToFn;
311
- } ? ToFn extends () => any ? z.ZodAny : never : T[K] extends {
253
+ to: () => infer RefField;
254
+ } ? RefField extends {
255
+ config: {
256
+ [P in Key]: infer ZodSchema;
257
+ };
258
+ } ? ZodSchema : never : T[K] extends {
312
259
  config: {
313
260
  [P in Key]: infer ZodSchema extends z.ZodTypeAny;
314
261
  };
@@ -333,24 +280,44 @@ type InferDefaultValues2<T> = {
333
280
  };
334
281
  export declare function createSchema<T extends {
335
282
  _tableName: string;
336
- }>(schema: T): {
337
- sqlSchema: z.ZodObject<Prettify<InferSqlSchema<T>>>;
338
- clientSchema: z.ZodObject<Prettify<InferClientSchema<T>>>;
339
- validationSchema: z.ZodObject<Prettify<InferValidationSchema<T>>>;
340
- defaultValues: Prettify<InferDefaultValues2<T>>;
283
+ [SchemaWrapperBrand]?: true;
284
+ }, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R, typeof SchemaWrapperBrand> = Omit<T & R, typeof SchemaWrapperBrand>>(schema: T, relations?: R): {
285
+ sqlSchema: z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>;
286
+ clientSchema: z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>;
287
+ validationSchema: z.ZodObject<Prettify<InferValidationSchema<TActualSchema>>>;
288
+ defaultValues: Prettify<InferDefaultValues2<TActualSchema>>;
289
+ toClient: (dbObject: z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>;
290
+ toDb: (clientObject: z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>;
291
+ };
292
+ type RelationBuilders<TSchema> = {
293
+ reference: <TField extends object>(fieldGetter: () => TField) => {
294
+ type: "reference";
295
+ to: () => TField;
296
+ };
297
+ hasMany: <T extends Schema<any>, K extends keyof T & string, TField extends EnrichedField<K, T[K], T>>(config: {
298
+ fromKey: keyof TSchema & string;
299
+ toKey: () => TField;
300
+ defaultCount?: number;
301
+ }) => 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"]>>>>;
302
+ hasOne: <T extends Schema<any>>(config: {
303
+ fromKey: keyof TSchema & string;
304
+ toKey: () => T[keyof T];
305
+ schema: () => T;
306
+ }) => Builder<"relation", RelationConfig<T>, z.ZodArray<any>, z.ZodArray<any>, any[], z.ZodArray<any>, z.ZodArray<any>>;
307
+ manyToMany: <T extends Schema<any>>(config: {
308
+ fromKey: keyof TSchema & string;
309
+ toKey: () => T[keyof T];
310
+ schema: () => T;
311
+ defaultCount?: number;
312
+ }) => 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>>>;
313
+ };
314
+ export declare function schemaRelations<TSchema extends Schema<any>, RefObject extends Record<string, any>>(baseSchema: TSchema, referencesBuilder: (rel: RelationBuilders<TSchema>) => RefObject): {
315
+ [K in keyof RefObject]: RefObject[K] & {
316
+ __meta: {
317
+ _key: K;
318
+ _fieldType: RefObject[K];
319
+ };
320
+ __parentTableType: TSchema;
321
+ };
341
322
  };
342
- export type InferSchemaTypes<T extends {
343
- _tableName: string;
344
- } & {
345
- [key: string]: any;
346
- }> = Prettify<{
347
- /** The TypeScript type for data as it exists in the database. */
348
- sql: z.infer<ReturnType<typeof createSchema<T>>["sqlSchema"]>;
349
- /** The TypeScript type for data as it is represented on the client. */
350
- client: z.infer<ReturnType<typeof createSchema<T>>["clientSchema"]>;
351
- /** The TypeScript type for data during validation, often the most flexible shape. */
352
- validation: z.infer<ReturnType<typeof createSchema<T>>["validationSchema"]>;
353
- /** The TypeScript type for the default values object. */
354
- defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
355
- }>;
356
323
  export {};