Skip to content

Commit

Permalink
first draft of type config
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Aug 3, 2023
1 parent 2a75720 commit 27f6c08
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './kysely.js'
export * from './type-config.js'
export * from './query-creator.js'

export * from './expression/expression.js'
Expand Down
32 changes: 23 additions & 9 deletions src/query-builder/function-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -189,7 +190,10 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ```
*/
avg<
O extends number | string | null = number | string,
O extends number | string | null = GetConfigFallback<
'avgType',
'floatType'
>,
C extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>
>(
column: C
Expand Down Expand Up @@ -318,7 +322,10 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ```
*/
count<
O extends number | string | bigint,
O extends number | string | bigint = GetConfigFallback<
'countType',
'bigIntType'
>,
C extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>
>(
column: C
Expand Down Expand Up @@ -393,15 +400,22 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* .execute()
* ```
*/
countAll<O extends number | string | bigint, T extends TB = TB>(
countAll<
O extends number | string | bigint = GetConfigFallback<
'countType',
'bigIntType'
>,
T extends TB = TB
>(
table: T
): AggregateFunctionBuilder<DB, TB, O>

countAll<O extends number | string | bigint>(): AggregateFunctionBuilder<
DB,
TB,
O
>
countAll<
O extends number | string | bigint = GetConfigFallback<
'countType',
'bigIntType'
>
>(): AggregateFunctionBuilder<DB, TB, O>

/**
* Calls the `max` function for the column or expression given as the argument.
Expand Down Expand Up @@ -576,7 +590,7 @@ export interface FunctionModule<DB, TB extends keyof DB> {
* ```
*/
sum<
O extends number | string | bigint | null = number | string | bigint,
O extends number | string | bigint | null = GetConfig<'sumType'>,
C extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>
>(
column: C
Expand Down
50 changes: 50 additions & 0 deletions src/type-config.ts
Original file line number Diff line number Diff line change
@@ -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 DefaultKyselyTypeConfig> =
K extends keyof KyselyTypeConfig
? KyselyTypeConfig[K]
: DefaultKyselyTypeConfig[K]

export type GetConfigFallback<
K extends keyof DefaultKyselyTypeConfig,
F extends keyof DefaultKyselyTypeConfig
> = GetConfig<K> extends never ? GetConfig<F> : GetConfig<K>

0 comments on commit 27f6c08

Please sign in to comment.