From 27f6c0823bdc4fb7f3b031ca05d4ff98ca85bcaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sami=20Koskim=C3=A4ki?= Date: Thu, 3 Aug 2023 17:05:22 +0300 Subject: [PATCH] first draft of type config --- src/index.ts | 1 + src/query-builder/function-module.ts | 32 +++++++++++++----- src/type-config.ts | 50 ++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 src/type-config.ts diff --git a/src/index.ts b/src/index.ts index 1ce029264..334b502f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from './kysely.js' +export * from './type-config.js' export * from './query-creator.js' export * from './expression/expression.js' diff --git a/src/query-builder/function-module.ts b/src/query-builder/function-module.ts index f7f60d9e4..0c687a339 100644 --- a/src/query-builder/function-module.ts +++ b/src/query-builder/function-module.ts @@ -13,6 +13,7 @@ import { ExtractTypeFromStringReference, } from '../parser/reference-parser.js' import { parseSelectAll } from '../parser/select-parser.js' +import { GetConfig, GetConfigFallback } from '../type-config.js' import { KyselyTypeError } from '../util/type-error.js' import { Equals, IsAny } from '../util/type-utils.js' import { AggregateFunctionBuilder } from './aggregate-function-builder.js' @@ -189,7 +190,10 @@ export interface FunctionModule { * ``` */ avg< - O extends number | string | null = number | string, + O extends number | string | null = GetConfigFallback< + 'avgType', + 'floatType' + >, C extends ReferenceExpression = ReferenceExpression >( column: C @@ -318,7 +322,10 @@ export interface FunctionModule { * ``` */ count< - O extends number | string | bigint, + O extends number | string | bigint = GetConfigFallback< + 'countType', + 'bigIntType' + >, C extends ReferenceExpression = ReferenceExpression >( column: C @@ -393,15 +400,22 @@ export interface FunctionModule { * .execute() * ``` */ - countAll( + countAll< + O extends number | string | bigint = GetConfigFallback< + 'countType', + 'bigIntType' + >, + T extends TB = TB + >( table: T ): AggregateFunctionBuilder - countAll(): AggregateFunctionBuilder< - DB, - TB, - O - > + countAll< + O extends number | string | bigint = GetConfigFallback< + 'countType', + 'bigIntType' + > + >(): AggregateFunctionBuilder /** * Calls the `max` function for the column or expression given as the argument. @@ -576,7 +590,7 @@ export interface FunctionModule { * ``` */ sum< - O extends number | string | bigint | null = number | string | bigint, + O extends number | string | bigint | null = GetConfig<'sumType'>, C extends ReferenceExpression = ReferenceExpression >( column: C diff --git a/src/type-config.ts b/src/type-config.ts new file mode 100644 index 000000000..f3e9454f5 --- /dev/null +++ b/src/type-config.ts @@ -0,0 +1,50 @@ +declare global { + /** + * The interface users can extend using interface merging: + * + * ```ts + * declare global { + * interface KyselyTypeConfig { + * bigIntType: number + * } + * } + * ``` + * + * See {@link DefaultKyselyTypeConfig} for configs that can be + * overridden. + */ + interface KyselyTypeConfig {} +} + +interface DefaultKyselyTypeConfig { + dateTimeType: Date | string + floatType: number | string + bigIntType: bigint | number | string + + /** + * This type can be used to specify the `sum` function's output type. + */ + sumType: number | string | bigint + + /** + * This type can be used to specify the `avg` function's output type. + * By default the {@link floatType} is used. + */ + avgType: never + + /** + * This type can be used to specify the `count` and `countAll` functions' output type. + * By default the {@link bigIntType} is used. + */ + countType: never +} + +export type GetConfig = + K extends keyof KyselyTypeConfig + ? KyselyTypeConfig[K] + : DefaultKyselyTypeConfig[K] + +export type GetConfigFallback< + K extends keyof DefaultKyselyTypeConfig, + F extends keyof DefaultKyselyTypeConfig +> = GetConfig extends never ? GetConfig : GetConfig