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 * import type { NotNull } from 'kysely' * * 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; }>; /** * Returns converted value if it extends `From`, else returns the value itself. * * @example * ```ts * CastType // string * CastType // string | null | undefined * CastType // number * ``` */ export type CastType = From | null | undefined extends Value ? To | null | undefined : From | null extends Value ? To | null : From | undefined extends Value ? To | undefined : From extends Value ? To : Value; /** * Converts all properties of object type `O` from type `From` to type `To`. * * @example * ```ts * type Obj = { * created_at: Date * updated_at: Date | null * } * * CastObjectTypes * /// { * /// created_at: string * /// updated_at: string | null * /// } * ``` */ export type CastObjectTypes = { [K in keyof O]: CastType; }; /** * Deeply converts all properties of object type `O` from type `From` to type `To`. * * @example * ```ts * type Obj = { * created_at: Date * updated_at: Date | null * nested: { * created_at: Date * updated_at: Date | null * } * } * * DeepCastObjectTypes * /// { * /// created_at: string * /// updated_at: string | null * /// nested: { * /// created_at: string * /// updated_at: string | null * /// } * /// } * ``` */ export type DeepCastObjectTypes = { [K in keyof O]: CastType extends object | null | undefined ? DeepCastObjectTypes : CastType; }; /** * Converts all `Date` like properties of object `O` to `string`. * Useful for object aggregations like `jsonbObjectFrom`. */ export type CastDatesToStrings = CastObjectTypes; /** * Deeply converts all `Date` like properties of object `O` to `string`. */ export type DeepCastDatesToStrings = DeepCastObjectTypes; /** * Prepares object type which was created via jsonbObjectFrom or jsonArrayFrom. * All dates in object will be converted to strings. * * @example * ```ts * type T1Type = { * created_at: Date * updated_at: Date | null * } * * type T2Type = { * created_at: Date * updated_at: Date | null * } * * const result: T1Type & { * obj: KyselyJson * } = await kyselyClient * .selectFrom('t1') * .select(['created_at', 'updated_at']) * .select((eb) => * jsonObjectFrom( * eb * .selectFrom('t2') * .whereRef('t2.id', '=', 't1.id') * .select(['created_at', 'updated_at']) * ).as('obj') * ) * .executeTakeFirstOrThrow() * * /// result will be like * type Result = { * created_at: Date * updated_at: Date | null * obj: { * created_at: string * updated_at: string | null * } * } * ``` */ export type KyselyJson = DeepCastDatesToStrings;