Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global type config #631

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading