import { InsertResult } from '../query-builder/insert-result.js'; import { DeleteResult } from '../query-builder/delete-result.js'; import { UpdateResult } from '../query-builder/update-result.js'; import { KyselyTypeError } from './type-error.js'; import { MergeResult } from '../query-builder/merge-result.js'; /** * Given a database type and a union of table names in that db, returns * a union type with all possible column names. * * Example: * * ```ts * interface Person { * id: number * } * * interface Pet { * name: string * species: 'cat' | 'dog' * } * * interface Movie { * stars: number * } * * interface Database { * person: Person * pet: Pet * movie: Movie * } * * type Columns = AnyColumn * * // Columns == 'id' | 'name' | 'species' * ``` */ export type AnyColumn = { [T in TB]: keyof DB[T]; }[TB] & string; /** * Extracts a column type. */ export type ExtractColumnType = { [T in TB]: C extends keyof DB[T] ? DB[T][C] : never; }[TB]; /** * Given a database type and a union of table names in that db, returns * a union type with all possible `table`.`column` combinations. * * Example: * * ```ts * interface Person { * id: number * } * * interface Pet { * name: string * species: 'cat' | 'dog' * } * * interface Movie { * stars: number * } * * interface Database { * person: Person * pet: Pet * movie: Movie * } * * type Columns = AnyColumnWithTable * * // Columns == 'person.id' | 'pet.name' | 'pet.species' * ``` */ export type AnyColumnWithTable = { [T in TB]: `${T & string}.${keyof DB[T] & string}`; }[TB]; /** * Just like {@link AnyColumn} but with a ` as ` suffix. */ export type AnyAliasedColumn = `${AnyColumn} as ${string}`; /** * Just like {@link AnyColumnWithTable} but with a ` as ` suffix. */ export type AnyAliasedColumnWithTable = `${AnyColumnWithTable} as ${string}`; /** * Extracts the item type of an array. */ export type ArrayItemType = T extends ReadonlyArray ? I : never; export type SimplifySingleResult = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : O extends MergeResult ? O : Simplify | undefined; export type SimplifyResult = O extends InsertResult ? O : O extends DeleteResult ? O : O extends UpdateResult ? O : O extends MergeResult ? O : Simplify; export type Simplify = DrainOuterGeneric<{ [K in keyof T]: T[K]; } & {}>; /** * Represents a database row whose column names and their types are unknown. */ export type UnknownRow = Record; /** * Makes all properties of object type `T` nullable. */ export type Nullable = { [P in keyof T]: T[P] | null; }; /** * Evaluates to `true` if `T` is `never`. */ export type IsNever = [T] extends [never] ? true : false; /** * Evaluates to `true` if `T` is `any`. */ export type IsAny = 0 extends T & 1 ? true : false; /** * Evaluates to `true` if the types `T` and `U` are equal. */ export type Equals = (() => G extends T ? 1 : 2) extends () => G extends U ? 1 : 2 ? true : false; export type NarrowPartial = DrainOuterGeneric : T[K] extends O[K] ? T[K] : KyselyTypeError<`$narrowType() call failed: passed type does not exist in '${K}'s type union`> : O[K]; } : never>; /** * A type constant for marking a column as not null. Can be used with `$narrowPartial`. * * Example: * * ```ts * const person = await db.selectFrom('person') * .where('nullable_column', 'is not', null) * .selectAll() * .$narrowType<{ nullable_column: NotNull }>() * .executeTakeFirstOrThrow() * ``` */ export type NotNull = { readonly __notNull__: unique symbol; }; export type SqlBool = boolean | 0 | 1; /** * Utility to reduce depth of TypeScript's internal type instantiation stack. * * Example: * * ```ts * type A = { item: T } * * type Test = A< * A>>>>>>>>>>>>>>>>>>>>>>> * > * * type Error = Test // Type instantiation is excessively deep and possibly infinite.ts (2589) * ``` * * To fix this, we can use `DrainOuterGeneric`: * * ```ts * type A = DrainOuterGeneric<{ item: T }> * * type Test = A< * A>>>>>>>>>>>>>>>>>>>>>>> * > * * type Ok = Test // Ok * ``` */ export type DrainOuterGeneric = [T] extends [unknown] ? T : never; export type ShallowRecord = DrainOuterGeneric<{ [P in K]: T; }>;