|
| 1 | +import { Kysely, ExpressionBuilder, SelectQueryBuilder, Generated } from '..' |
| 2 | + |
| 3 | +import { expectAssignable, expectType } from 'tsd' |
| 4 | +import { Database } from '../shared' |
| 5 | + |
| 6 | +// TODO: type-checking this is crazy slow. Figure out the cause. |
| 7 | +function testSelectQueryBuilderExtends() { |
| 8 | + type A = { a: number } |
| 9 | + type B = { b: string } |
| 10 | + |
| 11 | + type T1 = SelectQueryBuilder<{ a: A }, 'a', unknown> |
| 12 | + |
| 13 | + // This type extends T1 and should be assignable to it. |
| 14 | + type T2 = SelectQueryBuilder<{ a: A; b: B }, 'a' | 'b', { a: number }> |
| 15 | + |
| 16 | + const t2 = {} as T2 |
| 17 | + expectAssignable<T1>(t2) |
| 18 | +} |
| 19 | + |
| 20 | +// TODO: type-checking this is crazy slow. Figure out the cause. |
| 21 | +function testExpressionBuilderExtends() { |
| 22 | + type A = { a: number } |
| 23 | + type B = { b: string } |
| 24 | + |
| 25 | + type T1 = ExpressionBuilder<{ a: A }, 'a'> |
| 26 | + |
| 27 | + // This type extends T1 and should be assignable to it. |
| 28 | + type T2 = ExpressionBuilder<{ a: A; b: B }, 'a' | 'b'> |
| 29 | + |
| 30 | + const t2 = {} as T2 |
| 31 | + expectAssignable<T1>(t2) |
| 32 | +} |
| 33 | + |
| 34 | +// TODO: type-checking this is crazy slow. Figure out the cause. |
| 35 | +function testExpressionBuilderExtendsFuncArg() { |
| 36 | + type A = { a: number } |
| 37 | + type B = { b: string } |
| 38 | + type C = { c: boolean } |
| 39 | + |
| 40 | + // This type extends T1 and should be assignable to it. |
| 41 | + type T2 = ExpressionBuilder<{ a: A; b: B; c: C }, 'a' | 'b' | 'c'> |
| 42 | + |
| 43 | + function test(eb: ExpressionBuilder<{ a: A; b: B; c: C }, 'b' | 'c'>) { |
| 44 | + console.log(eb) |
| 45 | + } |
| 46 | + |
| 47 | + const t2 = {} as T2 |
| 48 | + test(t2) |
| 49 | +} |
| 50 | + |
| 51 | +// TODO: type-checking this is crazy slow. Figure out the cause. |
| 52 | +async function testGenericSelectHelper() { |
| 53 | + type Parent = { id: Generated<string> } |
| 54 | + type Person = { id: Generated<string>; parent_id: string } |
| 55 | + type Pet = { owner_id: string; name: string } |
| 56 | + const db: Kysely<{ person: Person; parent: Parent; pet: Pet }> = undefined! |
| 57 | + |
| 58 | + function personPetSelect( |
| 59 | + eb: ExpressionBuilder< |
| 60 | + { |
| 61 | + parent: { id: string | null } |
| 62 | + petJoin: { name: string | null } |
| 63 | + }, |
| 64 | + 'parent' | 'petJoin' |
| 65 | + > |
| 66 | + ) { |
| 67 | + return ['parent.id'] as const |
| 68 | + } |
| 69 | + |
| 70 | + const result = await db |
| 71 | + .selectFrom('parent') |
| 72 | + .leftJoin('person as personJoin', 'personJoin.parent_id', 'parent.id') |
| 73 | + .leftJoin('pet as petJoin', 'petJoin.owner_id', 'personJoin.id') |
| 74 | + .select(personPetSelect) |
| 75 | + .execute() |
| 76 | +} |
| 77 | + |
| 78 | +async function testGenericSelect<T extends keyof Database>( |
| 79 | + db: Kysely<Database>, |
| 80 | + table: T |
| 81 | +) { |
| 82 | + const r1 = await db.selectFrom(table).select('id').executeTakeFirstOrThrow() |
| 83 | + expectAssignable<string | number>(r1.id) |
| 84 | +} |
| 85 | + |
| 86 | +async function testGenericUpdate(db: Kysely<Database>, table: 'pet' | 'movie') { |
| 87 | + await db.updateTable(table).set({ id: '123' }).execute() |
| 88 | +} |
| 89 | + |
| 90 | +async function testSelectsInVariable(db: Kysely<Database>) { |
| 91 | + const selects = [ |
| 92 | + 'first_name', |
| 93 | + (eb: ExpressionBuilder<Database, 'person'>) => |
| 94 | + eb |
| 95 | + .selectFrom('pet') |
| 96 | + .select('name') |
| 97 | + .whereRef('pet.owner_id', '=', 'person.id') |
| 98 | + .as('pet_name'), |
| 99 | + ] as const |
| 100 | + |
| 101 | + const r1 = await db |
| 102 | + .selectFrom('person') |
| 103 | + .select(selects) |
| 104 | + .executeTakeFirstOrThrow() |
| 105 | + |
| 106 | + expectType<{ first_name: string; pet_name: string | null }>(r1) |
| 107 | +} |
0 commit comments