import { knex, Knex } from 'knex'; import { PassThrough } from 'stream'; //////////////////////////////////////////////////////////////////////////////////////// // // // dtslint tests are deprecated, please add new tests for tsd instead (see /test-tsd) // // // //////////////////////////////////////////////////////////////////////////////////////// const clientConfig = { client: 'sqlite3', connection: { filename: './mydb.sqlite', }, }; const knexInstance = knex(clientConfig); const knex2 = knex({ ...clientConfig, log: { debug(msg: string) {} }, pool: { log: (msg: string, level: string) => {} } }); knexInstance.initialize(); knexInstance.initialize({}); interface User { id: number; age: number; name: string; active: boolean; departmentId: number; } interface Department { id: number; departmentName: string; } interface Article { id: number; subject: string; body?: string; authorId?: string; } interface Ticket { name: string; from: string; to: string; at: Date; } // Interface to witness type compatibility interface ExtendsWitness { _t: T1; } // Ensure that Knex type with parameters is compatible with Knex with default parameters // // This ensures that a value of Parameterized Knex can always be passed to a function that // consumes unparameterized Knex. type _T1 = ExtendsWitness, Knex>; type _T2 = ExtendsWitness[]>, Knex>; type _T3 = ExtendsWitness[]>, Knex.Where>; type _T4 = ExtendsWitness, Knex.Where>; type _T5 = ExtendsWitness< Knex.QueryInterface>, Knex.QueryInterface >; type _T6 = ExtendsWitness, Knex.QueryBuilder>; type _T7 = ExtendsWitness< Knex.QueryBuilder, Knex.QueryBuilder >; type _T8 = ExtendsWitness, Knex.QueryBuilder>; type _T9 = ExtendsWitness, Knex.QueryBuilder>; type _T10 = ExtendsWitness, Knex.QueryBuilder>; declare module './tables' { interface Tables { users_inferred: User; departments_inferred: Department; articles_inferred: Article; users_composite: Knex.CompositeTableType; departments_composite: Knex.CompositeTableType; articles_composite: Knex.CompositeTableType; } } const main = async () => { // $ExpectType any await knexInstance.raw('select * from users'); // $ExpectType User[] await knexInstance.raw('select * from users'); // $ExpectType any await knexInstance.raw( 'select * from users where id in ?', knexInstance('contacts').select('name') ); // $ExpectType User[] await knexInstance.raw( 'select * from users where id in ?', knexInstance('contacts').select('name') ); // $ExpectType User[] await knexInstance.raw( 'select * from users where departmentId in ?', knexInstance('departments').select('id') ); // $ExpectType User[] await knexInstance.raw( 'select * from users where departmentId in ? & active in ?', [ knexInstance('departments').select('name'), [true, false] ] ); // $ExpectType Article[] await knexInstance.raw( 'select * from articles where authorId = ?', [ null ] ); // $ExpectType PassThrough & AsyncIterable knexInstance('users').select('*').stream(); // $ExpectType PassThrough knexInstance('users').select('*').stream().pipe(new PassThrough()); // $ExpectType User[] await knexInstance('user').where('name', ['a', 'b', 'c']); // $ExpectType User[] await knexInstance('users_inferred').where('name', ['a', 'b', 'c']); // $ExpectType User[] await knexInstance('users_composite').where('name', ['a', 'b', 'c']); // $ExpectType User[] await knexInstance('user').whereRaw('name = ?', 'L'); // $ExpectType User[] await knexInstance('users_inferred').whereRaw('name = ?', 'L'); // $ExpectType User[] await knexInstance('users_composite').whereRaw('name = ?', 'L'); // $ExpectType User[] await knexInstance('user').whereRaw('name = ?', 'L').clearWhere(); // $ExpectType User[] await knexInstance('users_inferred').whereRaw('name = ?', 'L').clearWhere(); // $ExpectType User[] await knexInstance('users_composite').whereRaw('name = ?', 'L').clearWhere(); // $ExpectType User[] await knexInstance('user').whereBetween("id", [1, 2]); // $ExpectType User[] await knexInstance('users_inferred').whereBetween('id', [1, 2]); // $ExpectType User[] await knexInstance('users_composite').whereBetween('id', [1, 2]); const range = [1, 2] as const; // $ExpectType User[] await knexInstance('user').whereBetween("id", range); // $ExpectType User[] await knexInstance('users_inferred').whereBetween('id', range); // $ExpectType User[] await knexInstance('users_composite').whereBetween('id', range); // $ExpectType { id: number; }[] const r3 = await knexInstance('users').select(knexInstance.ref('id')); // $ExpectType { id: number; }[] await knexInstance('users_inferred').select(knexInstance.ref('id')); // $ExpectType { id: number; }[] await knexInstance('users_composite').select(knexInstance.ref('id')); // $ExpectType (Pick & { id: number; })[] const r4 = await knexInstance('users').select(knexInstance.ref('id'), 'name'); type _TR4 = ExtendsWitness>; // $ExpectType (Pick & { id: number; })[] await knexInstance('users_inferred').select(knexInstance.ref('id'), 'name'); // $ExpectType (Pick & { id: number; })[] await knexInstance('users_composite').select(knexInstance.ref('id'), 'name'); // $ExpectType (Pick & { identifier: number; })[] const r5 = await knexInstance('users').select(knexInstance.ref('id').as('identifier'), 'name'); type _TR5 = ExtendsWitness; // $ExpectType (Pick & { identifier: number; })[] await knexInstance('users_inferred').select(knexInstance.ref('id').as('identifier'), 'name'); // $ExpectType (Pick & { identifier: number; })[] await knexInstance('users_composite').select(knexInstance.ref('id').as('identifier'), 'name'); // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] const r6 = await knexInstance('users').select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ); type _TR6 = ExtendsWitness; // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] await knexInstance('users_inferred').select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ); // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] await knexInstance('users_composite').select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ); // $ExpectType { id: number; }[] const r7 = await knexInstance.select(knexInstance.ref('id')).from('users'); type _TR7 = ExtendsWitness>; // $ExpectType { id: number; }[] await knexInstance.select(knexInstance.ref('id')).from('users_inferred'); // $ExpectType { id: number; }[] await knexInstance.select(knexInstance.ref('id')).from('users_composite'); // $ExpectType (Pick & { id: number; })[] const r8 = await knexInstance.select(knexInstance.ref('id'), 'name').from('users'); type _TR8 = ExtendsWitness>; // $ExpectType (Pick & { id: number; })[] await knexInstance.select(knexInstance.ref('id'), 'name').from('users_inferred'); // $ExpectType (Pick & { id: number; })[] await knexInstance.select(knexInstance.ref('id'), 'name').from('users_composite'); // $ExpectType (Pick & { identifier: number; })[] const r9 = await knexInstance.select(knexInstance.ref('id').as('identifier'), 'name').from('users'); type _TR9 = ExtendsWitness; // $ExpectType (Pick & { identifier: number; })[] await knexInstance.select(knexInstance.ref('id').as('identifier'), 'name').from('users_inferred'); // $ExpectType (Pick & { identifier: number; })[] await knexInstance.select(knexInstance.ref('id').as('identifier'), 'name').from('users_composite'); // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] const r10 = await knexInstance.select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ).from('users'); type _TR10 = ExtendsWitness; // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] await knexInstance.select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ).from('users_inferred'); // $ExpectType (Pick & { identifier: number; yearsSinceBirth: number; })[] await knexInstance.select( knexInstance.ref('id').as('identifier'), 'name', knexInstance.ref('age').as('yearsSinceBirth') ).from('users_composite'); // $ExpectType { id: number; age: any; }[] await knexInstance('users').select(knexInstance.ref('id'), {age: 'users.age'}); // $ExpectType { id: number; age: any; }[] await knexInstance('users_inferred').select(knexInstance.ref('id'), {age: 'users.age'}); // $ExpectType { id: number; age: any; }[] await knexInstance('users_composite').select(knexInstance.ref('id'), {age: 'users.age'}); // $ExpectType { id: number; age: any; } | undefined await knexInstance('users').select(knexInstance.ref('id'), {age: 'users.age'}).first(); // $ExpectType { id: number; age: any; } | undefined await knexInstance('users_inferred').select(knexInstance.ref('id'), {age: 'users.age'}).first(); // $ExpectType { id: number; age: any; } | undefined await knexInstance('users_composite').select(knexInstance.ref('id'), {age: 'users.age'}).first(); // $ExpectType { identifier: number; username: string; }[] (await knexInstance('users') .select('id', 'name')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: number; username: string; }[] (await knexInstance('users_inferred') .select('id', 'name')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: number; username: string; }[] (await knexInstance('users_composite') .select('id', 'name')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: number; username: string; }[] (await knexInstance .select('id', 'name') .from('users')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: number; username: string; }[] (await knexInstance .select('id', 'name') .from('users_inferred')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: number; username: string; }[] (await knexInstance .select('id', 'name') .from('users_composite')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType { identifier: any; username: any; }[] (await knexInstance .select('id', 'name') .from('users')) .map((u) => ({ identifier: u.id, username: u.name })); // $ExpectType number (await knexInstance .select('id', 'name', 'age') .from('users')) .reduce((maxAge: number, user) => (user.age > maxAge ? user.age : maxAge), 0); // $ExpectType number (await knexInstance .select('id', 'name', 'age') .from('users_inferred')) .reduce((maxAge: number, user) => (user.age > maxAge ? user.age : maxAge), 0); // $ExpectType number (await knexInstance .select('id', 'name', 'age') .from('users_composite')) .reduce((maxAge: number, user) => (user.age > maxAge ? user.age : maxAge), 0); // $ExpectType any (await knexInstance('table') .select('key', 'value') .where({ namespace: 'foo' })) .reduce( (aggr, { value, key }) => ({ ...aggr, [key]: value > 10 ? (aggr[key] || 0) + 1 : aggr[key], }), {} as any ); // $ExpectType Pick[] await knexInstance('users').select(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred').select(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite').select(['id', 'age']); // $ExpectType Pick | undefined await knexInstance('users') .select(['id', 'age']) .first(); // $ExpectType Pick | undefined await knexInstance('users_inferred') .select(['id', 'age']) .first(); // $ExpectType Pick | undefined await knexInstance('users_composite') .select(['id', 'age']) .first(); // $ExpectType any[] await knexInstance('users').select('users.id'); // $ExpectType Pick[] await knexInstance('users') .select(['id', 'age']) .clearSelect() .select(['name']); // $ExpectType Pick[] await knexInstance('users_inferred') .select(['id', 'age']) .clearSelect() .select(['name']); // $ExpectType Pick[] await knexInstance('users_composite') .select(['id', 'age']) .clearSelect() .select(['name']); // $ExpectType Pick[] await knexInstance('users') .select('id') .select('age') .clearSelect() .select('name') .select('departmentId'); // $ExpectType Pick[] await knexInstance('users_inferred') .select('id') .select('age') .clearSelect() .select('name') .select('departmentId'); // $ExpectType Pick[] await knexInstance('users_composite') .select('id') .select('age') .clearSelect() .select('name') .select('departmentId'); // $ExpectType Pick[] await knexInstance .select('id') .select('age') .clearSelect() .select('name') .select('departmentId') .from('users'); // $ExpectType Pick[] await knexInstance .select('id') .select('age') .clearSelect() .select('name') .select('departmentId') .from('users_inferred'); // $ExpectType Pick[] await knexInstance .select('id') .select('age') .clearSelect() .select('name') .select('departmentId') .from('users_composite'); // $ExpectType any[] await knexInstance('users') .select('users.id') .select('age'); // $ExpectType any[] await knexInstance('users') .select('id') .from('departments'); // $ExpectType Pick[] await knexInstance('users') .select('id') .from('departments'); // $ExpectType Pick[] await knexInstance('users_inferred') .select('id') .from('departments_inferred'); // $ExpectType Pick[] await knexInstance('users_composite') .select('id') .from('departments_composite'); // $ExpectType any[] await knexInstance.select('id').from('users'); // $ExpectType Pick[] await knexInstance.select('id').from('users'); // $ExpectType Pick[] await knexInstance.select('id').from('users_inferred'); // $ExpectType Pick[] await knexInstance.select('id').from('users_composite'); // $ExpectType Pick[] await knexInstance.select('id', 'age').from('users'); // $ExpectType Pick[] await knexInstance.select('id', 'age').from('users_inferred'); // $ExpectType Pick[] await knexInstance.select('id', 'age').from('users_composite'); // $ExpectType Pick[] await knexInstance.from('users').select('id'); // $ExpectType Pick[] await knexInstance.from('users_inferred').select('id'); // $ExpectType Pick[] await knexInstance.from('users_composite').select('id'); // $ExpectType any[] await knexInstance.from('users').select('users.id'); // $ExpectType Pick[] await knexInstance.from('users').select[]>('users.id'); // $ExpectType any[] await knexInstance .column('id', 'age') .select() .from('users'); // $ExpectType Pick[] await knexInstance .column('id', 'age') .select() .from('users'); // $ExpectType Pick[] await knexInstance .column('id', 'age') .select() .from('users_inferred'); // $ExpectType Pick[] await knexInstance .column('id', 'age') .select() .from('users_composite'); // $ExpectType Pick[] await knexInstance('users') .column('id', 'age') .select(); // $ExpectType Pick[] await knexInstance('users_inferred') .column('id', 'age') .select(); // $ExpectType Pick[] await knexInstance('users_composite') .column('id', 'age') .select(); // $ExpectType Pick[] await knexInstance('users').column('id', 'age'); // $ExpectType Pick[] await knexInstance('users_inferred').column('id', 'age'); // $ExpectType Pick[] await knexInstance('users_composite').column('id', 'age'); // $ExpectType any[] await knexInstance('users').distinct('name', 'age'); // $ExpectType Pick[] await knexInstance('users').distinct('name', 'age'); // $ExpectType Pick[] await knexInstance('users_inferred').distinct('name', 'age'); // $ExpectType Pick[] await knexInstance('users_composite').distinct('name', 'age'); // $ExpectType Pick[] await knexInstance('users').distinct(); // $ExpectType Pick[] await knexInstance('users_inferred').distinct(); // $ExpectType Pick[] await knexInstance('users_composite').distinct(); // $ExpectType User[] await knexInstance.select('*').from('users'); // $ExpectType User[] await knexInstance.select('*').from('users_inferred'); // $ExpectType User[] await knexInstance.select('*').from('users_composite'); const r1 = await knexInstance .column('id', { yearsSinceBirth: 'age' }) .select() .from('users'); type TR1_1 = ExtendsWitness< { id: number; yearsSinceBirth: any }, typeof r1[0] >; type TR1_2 = ExtendsWitness; type TR1_3 = ExtendsWitness; // $ExpectType (Pick & { yearsSinceBirth: any; })[] await knexInstance .column('id', { yearsSinceBirth: 'age' }) .select() .from('users_inferred'); // $ExpectType (Pick & { yearsSinceBirth: any; })[] await knexInstance .column('id', { yearsSinceBirth: 'age' }) .select() .from('users_composite'); const r2 = await knexInstance .column('id', { yearsSinceBirth: 'age' as 'age' }) .select() .from('users'); type TR2_1 = ExtendsWitness< { id: number; yearsSinceBirth: number }, typeof r2[0] >; type TR2_2 = ExtendsWitness; type TR2_3 = ExtendsWitness; // $ExpectType (Pick & { yearsSinceBirth: number; })[] await knexInstance .column('id', { yearsSinceBirth: 'age' as 'age' }) .select() .from('users_inferred'); // $ExpectType (Pick & { yearsSinceBirth: number; })[] await knexInstance .column('id', { yearsSinceBirth: 'age' as 'age' }) .select() .from('users_composite'); // ## Conditional Selection: // $ExpectType User[] await knexInstance('users').where({ id: 10 }); // $ExpectType User[] await knexInstance('users_inferred').where({ id: 10 }); // $ExpectType User[] await knexInstance('users_composite').where({ id: 10 }); // $ExpectType Pick[] await knexInstance('users') .select('id', 'name') .where({ id: 10 }); // $ExpectType Pick[] await knexInstance('users_inferred') .select('id', 'name') .where({ id: 10 }); // $ExpectType Pick[] await knexInstance('users_composite') .select('id', 'name') .where({ id: 10 }); // $ExpectType Partial[] await knexInstance .select[]>(['users.*', 'departments.name as depName']) .from('users') .join('departments', 'departments.id', '=', 'users.department_id') .orderBy('name'); // $ExpectType Partial | undefined await knexInstance .select[]>(['users.*', 'departments.name as depName']) .from('users') .join('departments', 'departments.id', '=', 'users.department_id') .orderBy('name') .first(); // $ExpectType Partial[] await knexInstance .select[]>(['users.*', 'departments.name as depName']) .from('users') .join('departments', 'departments.id', '=', 'users.department_id') .orderByRaw('name DESC'); // $ExpectType User | undefined await knexInstance('users') .where({ id: 10 }) .first(); // $ExpectType User | undefined await knexInstance('users_inferred') .where({ id: 10 }) .first(); // $ExpectType User | undefined await knexInstance('users_composite') .where({ id: 10 }) .first(); // $ExpectType any[] await knexInstance.where({ id: 10 }).from('users'); // $ExpectType any await knexInstance.where({ id: 10 }).from('users').first(); // $ExpectType User[] await knexInstance.where({ id: 10 }).from('users'); // $ExpectType User[] await knexInstance.where({ id: 10 }).from('users_inferred'); // $ExpectType User[] await knexInstance.where({ id: 10 }).from('users_composite'); // $ExpectType User | undefined await knexInstance .where({ id: 10 }) .from('users') .clearWhere() .where({ id: 11 }) .orWhere({ id: 12 }) .first(); // $ExpectType User | undefined await knexInstance .where({ id: 10 }) .from('users_inferred') .clearWhere() .where({ id: 11 }) .orWhere({ id: 12 }) .first(); // $ExpectType User | undefined await knexInstance .where({ id: 10 }) .from('users_composite') .clearWhere() .where({ id: 11 }) .orWhere({ id: 12 }) .first(); // $ExpectType User[] await knexInstance .where({ id: 10 }) .where('age', '>', 20) .from('users'); // $ExpectType User[] await knexInstance .where({ id: 10 }) .where('age', '>', 20) .from('users_inferred'); // $ExpectType User[] await knexInstance .where({ id: 10 }) .where('age', '>', 20) .from('users_composite'); // $ExpectType User[] await knexInstance('users').whereNot('age', '>', 100); // $ExpectType User[] await knexInstance('users_inferred').whereNot('age', '>', 100); // $ExpectType User[] await knexInstance('users_composite').whereNot('age', '>', 100); // ### Boolean operations // $ExpectType User[] await knexInstance('users').not.where('id', 10); // $ExpectType User[] await knexInstance('users_inferred').not.where('id', 10); // $ExpectType User[] await knexInstance('users_composite').not.where('id', 10); // $ExpectType User[] await knexInstance('user').where('name', 'L').and.where('age', 20); // $ExpectType User[] await knexInstance('users_inferred').where('name', 'L').and.where('age', 20); // $ExpectType User[] await knexInstance('users_composite').where('name', 'L').and.where('age', 20); // $ExpectType User[] await knexInstance('user').where('name', 'L').or.where('age', 20); // $ExpectType User[] await knexInstance('users_inferred').where('name', 'L').or.where('age', 20); // $ExpectType User[] await knexInstance('users_composite').where('name', 'L').or.where('age', 20); // ## Aggregation: const u4: User[] = await knexInstance('users') .groupBy('count') .orderBy('name', 'desc') .having('age', '>', 10); const u5: User[] = await knexInstance('users') .groupBy('count') .orderBy('name', 'desc') .having(knexInstance.raw('age > ?', 10)); const u6: User[] = await knexInstance('users') .groupBy('count') .orderBy('name', 'desc') .having(knexInstance.raw('age'), '>', 10); // $ExpectType User[] await knexInstance('users') .groupBy('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users_inferred') .groupBy('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users_composite') .groupBy('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users') .groupByRaw('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users_inferred') .groupByRaw('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users_composite') .groupByRaw('count') .orderBy('name', 'desc') .having('age', '>', 10); // $ExpectType User[] await knexInstance('users') .groupByRaw('count') .orderBy('name', 'desc') .havingRaw('age > ?', [10]); // $ExpectType User[] await knexInstance('users_inferred') .groupByRaw('count') .orderBy('name', 'desc') .havingRaw('age > ?', [10]); // $ExpectType User[] await knexInstance('users_composite') .groupByRaw('count') .orderBy('name', 'desc') .havingRaw('age > ?', [10]); // $ExpectType User[] await knexInstance('users') .select() .orderBy( knexInstance('users') .select('u.id') .from('users as u') .where('users.id', 'u.id') ); // $ExpectType User[] await knexInstance('users_inferred') .select() .orderBy( knexInstance('users_inferred') .select('u.id') .from('users as u') .where('users.id', 'u.id') ); // $ExpectType User[] await knexInstance('users_composite') .select() .orderBy( knexInstance('users_composite') .select('u.id') .from('users as u') .where('users.id', 'u.id') ); // $ExpectType User[] await knexInstance('users') .select() .orderBy([{ column: knexInstance('users') .select('u.id') .from('users as u') .where('users.id', 'u.id'), order: 'desc' }]); // $ExpectType User[] await knexInstance('users_inferred') .select() .orderBy([{ column: knexInstance('users_inferred') .select('u.id') .from('users as u') .where('users.id', 'u.id'), order: 'desc' }]); // $ExpectType User[] await knexInstance('users_composite') .select() .orderBy([{ column: knexInstance('users_composite') .select('u.id') .from('users as u') .where('users.id', 'u.id'), order: 'desc' }]); // $ExpectType User[] await knexInstance('users') .select() .orderBy([{ column: 'id', order: 'desc' }, { column: 'name', order: 'desc' }]); // $ExpectType User[] await knexInstance('users_inferred') .select() .orderBy([{ column: 'id', order: 'desc' }, { column: 'name', order: 'desc' }]); // $ExpectType User[] await knexInstance('users_composite') .select() .orderBy([{ column: 'id', order: 'desc' }, { column: 'name', order: 'desc' }]); // $ExpectType User[] await knexInstance('users') .select() .orderBy([{ column: 'id', order: 'desc' }, 'name']); // $ExpectType User[] await knexInstance('users_inferred') .select() .orderBy([{ column: 'id', order: 'desc' }, 'name']); // $ExpectType User[] await knexInstance('users_composite') .select() .orderBy([{ column: 'id', order: 'desc' }, 'name']); // $ExpectType Dict[] await knexInstance('users').count(); // $ExpectType Dict[] await knexInstance('users_inferred').count(); // $ExpectType Dict[] await knexInstance('users_composite').count(); // $ExpectType Dict[] await knexInstance('users').count('age'); // $ExpectType Dict[] await knexInstance('users_inferred').count('age'); // $ExpectType Dict[] await knexInstance('users_composite').count('age'); // $ExpectType Dict[] await knexInstance('users').count('age'); // $ExpectType { count: number; } await knexInstance('foo').first().count<{count: number}>({count: '*'}); // $ExpectType { count: number; } await knexInstance('foo').first().countDistinct<{count: number}>({count: '*'}); // $ExpectType { count?: string | number | undefined; } await knexInstance('foo').first().count({count: '*'}); // $ExpectType { count?: string | number | undefined; } await knexInstance('foo').first().countDistinct({count: '*'}); // $ExpectType Dict await knexInstance('users').first().count('age'); // $ExpectType Dict await knexInstance('users_inferred').first().count('age'); // $ExpectType Dict await knexInstance('users_composite').first().count('age'); // $ExpectType Dict await knexInstance('users').first().count('age', 'id'); // $ExpectType Dict await knexInstance('users').first().count(); // $ExpectType Dict await knexInstance('users_inferred').first().count(); // $ExpectType Dict await knexInstance('users_composite').first().count(); // $ExpectType Dict[] await knexInstance.count().from('users'); // $ExpectType Dict[] await knexInstance.count().from('users_inferred'); // $ExpectType Dict[] await knexInstance.count().from('users_composite'); // $ExpectType Dict[] await knexInstance.count('age').from('users'); // $ExpectType Dict[] await knexInstance.count('age').from('users_inferred'); // $ExpectType Dict[] await knexInstance.count('age').from('users_composite'); // $ExpectType Dict[] await knexInstance.count('age').from('users'); // $ExpectType { count: number; } await knexInstance.first().count<{count: number}>({count: '*'}).from('foo'); // $ExpectType { count: number; } await knexInstance.first().countDistinct<{count: number}>({count: '*'}).from('foo'); // $ExpectType { count?: string | number | undefined; } await knexInstance.first().count({count: '*'}).from('foo'); // $ExpectType { count?: string | number | undefined; } await knexInstance.first().countDistinct({count: '*'}).from('foo'); // $ExpectType Dict await knexInstance.first().count('age').from('users'); // $ExpectType Dict await knexInstance.first().count('age').from('users_inferred'); // $ExpectType Dict await knexInstance.first().count('age').from('users_composite'); // $ExpectType Dict await knexInstance.first().count('age', 'id').from('users'); // $ExpectType Dict await knexInstance.first().count().from('users'); // $ExpectType Dict await knexInstance.first().count().from('users_inferred'); // $ExpectType Dict await knexInstance.first().count().from('users_composite'); // $ExpectType Dict[] await knexInstance('users').max('age'); // $ExpectType Dict[] await knexInstance('users_inferred').max('age'); // $ExpectType Dict[] await knexInstance('users_composite').max('age'); // $ExpectType Dict await knexInstance('users').first().max('age'); // $ExpectType Dict await knexInstance('users_inferred').first().max('age'); // $ExpectType Dict await knexInstance('users_composite').first().max('age'); // $ExpectType Dict[] await knexInstance('users').max('age'); // $ExpectType Dict[] await knexInstance('users').min('age'); // $ExpectType Dict[] await knexInstance('users_inferred').min('age'); // $ExpectType Dict[] await knexInstance('users_composite').min('age'); // $ExpectType Dict await knexInstance('users').first().min('age'); // $ExpectType Dict await knexInstance('users_inferred').first().min('age'); // $ExpectType Dict await knexInstance('users_composite').first().min('age'); // $ExpectType Dict[] await knexInstance.max('age').from('users'); // $ExpectType Dict[] await knexInstance.max('age').from('users_inferred'); // $ExpectType Dict[] await knexInstance.max('age').from('users_composite'); // $ExpectType Dict await knexInstance.first().max('age').from('users'); // $ExpectType Dict await knexInstance.first().max('age').from('users_inferred'); // $ExpectType Dict await knexInstance.first().max('age').from('users_composite'); // $ExpectType Dict[] await knexInstance.max('age').from('users'); // $ExpectType Dict[] await knexInstance.min('age').from('users'); // $ExpectType Dict[] await knexInstance.min('age').from('users_inferred'); // $ExpectType Dict[] await knexInstance.min('age').from('users_composite'); // $ExpectType Dict await knexInstance.first().min('age').from('users'); // $ExpectType ({ a: string | Date; } & { b: string | Date; })[] await knexInstance('tickets') .min('at', {as: 'a'}) .max('at', {as: 'b'}); // $ExpectType ({ dep: any; } & { a: any; } & { b: any; })[] await knexInstance .select({dep: 'departmentId'}) .min('age', {as: 'a'}) .max('age', {as: 'b'}) .from('users'); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] await knexInstance .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}) .from('users'); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] await knexInstance .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}) .from('users_inferred'); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] await knexInstance .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}) .from('users_composite'); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] await knexInstance('users') .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] await knexInstance('users_inferred') .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] await knexInstance('users_composite') .select({dep: 'departmentId'}) .min({a: 'age'}) .max({b: 'age'}); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] await knexInstance('users') .select({dep: 'departmentId'}) .count({a: 'age'}); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] await knexInstance('users_inferred') .select({dep: 'departmentId'}) .count({a: 'age'}); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] await knexInstance('users_composite') .select({dep: 'departmentId'}) .count({a: 'age'}); // Type of dep can't be inferred if User type is not available // at the time of select // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] await knexInstance .select({dep: 'departmentId'}) .count({a: 'age'}) .from('users'); // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] await knexInstance .select({dep: 'departmentId'}) .count({a: 'age'}) .from('users_inferred'); // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] await knexInstance .select({dep: 'departmentId'}) .count({a: 'age'}) .from('users_composite'); // ## With inner query: // ### For column selection: // $ExpectType any[] await knexInstance('users').select( knexInstance('foo') .select('bar') .as('colName') ); // $ExpectType any[] await knexInstance('users').select( knexInstance('foo') .select('bar') .as('colName') ); // $ExpectType Pick[] await knexInstance('users').select[]>( knexInstance('foo') .select('bar') .as('colName') ); // ### For condition: // $ExpectType any[] await knexInstance('users').whereNot(function() { this.where('id', 1).orWhereNot('id', '>', 10); }); // $ExpectType User[] await knexInstance('users').whereNot(function() { this.where('id', 1).orWhereNot('id', '>', 10); }); // $ExpectType User[] await knexInstance('users_inferred').whereNot(function() { this.where('id', 1).orWhereNot('id', '>', 10); }); // $ExpectType User[] await knexInstance('users_composite').whereNot(function() { this.where('id', 1).orWhereNot('id', '>', 10); }); // $ExpectType User[] await knexInstance('users') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }); // $ExpectType User[] await knexInstance('users_inferred') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }); // $ExpectType User[] await knexInstance('users_composite') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }); // $ExpectType User | undefined await knexInstance('users') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }) .first(); // $ExpectType User | undefined await knexInstance('users_inferred') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }) .first(); // $ExpectType User | undefined await knexInstance('users_composite') .where((builder) => builder.whereIn('id', [1, 11, 15]).whereNotIn('id', [17, 19]) ) .andWhere(function() { this.where('id', '>', 10); }) .first(); const values = [[1, 'a'], [2, 'b']] as const; const cols = ['id', 'name'] as const; // $ExpectType User[] await knexInstance('users') .whereIn<"id" | "name">(cols, values); // $ExpectType User[] await knexInstance('users_inferred') .whereIn<"id" | "name">(cols, values); // $ExpectType User[] await knexInstance('users_composite') .whereIn<"id" | "name">(cols, values); // $ExpectType User[] await knexInstance('user').whereIn('id', [1, 2]); const col = 'id'; const idList = [1, 2] as const; // $ExpectType User[] await knexInstance('user').whereIn(col, idList); // $ExpectType User[] await knexInstance('users_inferred').whereIn(col, idList); // $ExpectType User[] await knexInstance('users_composite').whereIn(col, idList); // $ExpectType User[] await knexInstance('users').whereNotExists(function() { this.select('*') .from('accounts') .whereRaw('users.account_id = accounts.id'); }); // $ExpectType User[] await knexInstance('users_inferred').whereNotExists(function() { this.select('*') .from('accounts') .whereRaw('users.account_id = accounts.id'); }); // $ExpectType User[] await knexInstance('users_composite').whereNotExists(function() { this.select('*') .from('accounts') .whereRaw('users.account_id = accounts.id'); }); // ## Union Queries: // $ExpectType any[] await knexInstance .select('*') .from('users') .whereNull('last_name') .union(function() { this.select('*') .from('users') .whereNull('first_name'); }); // $ExpectType User[] await knexInstance('users') .select('*') .whereNull('name') .union(function() { this.select('*') .from('users') .whereNull('first_name'); }); // $ExpectType User[] await knexInstance('users_inferred') .select('*') .whereNull('name') .union(function() { this.select('*') .from('users_inferred') .whereNull('first_name'); }); // $ExpectType User[] await knexInstance('users_composite') .select('*') .whereNull('name') .union(function() { this.select('*') .from('users_composite') .whereNull('first_name'); }); // ## Joins: // $ExpectType any[] await knexInstance('users').innerJoin( 'departments', 'users.departmentId', 'departments.id' ); // $ExpectType any[] await knexInstance('users').innerJoin( 'departments', 'users.departmentId', 'departments.id' ); // $ExpectType any[] await knexInstance('users').innerJoin( 'departments', 'users.departmentId', 'departments.id' ); // $ExpectType (User & Department)[] await knexInstance('users').innerJoin( 'departments', 'users.departmentId', 'departments.id' ); // $ExpectType (User & Department)[] await knexInstance('users_inferred').innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ); // $ExpectType (User & Department)[] await knexInstance('users_composite').innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ); // $ExpectType (User & Department & Article)[] await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .innerJoin
('articles', 'articles.authorId', 'users.id'); // $ExpectType (User & Department & Article)[] await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ) .innerJoin('articles_inferred', 'articles_inferred.authorId', 'users_inferred.id'); // $ExpectType (User & Department & Article)[] await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ) .innerJoin('articles_composite', 'articles_composite.authorId', 'users_composite.id'); // $ExpectType any[] await knexInstance('users') .innerJoin('departments', 'users.departmentId', 'departments.id') .innerJoin
('articles', 'articles.authorId', 'users.id'); // $ExpectType any[] await knexInstance('users_inferred') .innerJoin('departments', 'users_inferred.departmentId', 'departments.id') .innerJoin('articles_inferred', 'articles_inferred.authorId', 'users.id'); // $ExpectType (User & Department)[] await knexInstance('users').innerJoin( 'departments', 'users.departmentId', '=', 'departments.id' ); // $ExpectType (User & Department)[] await knexInstance('users_inferred').innerJoin( 'departments_inferred', 'users_inferred.departmentId', '=', 'departments_inferred.id' ); // $ExpectType (User & Department)[] await knexInstance('users_composite').innerJoin( 'departments_composite', 'users_composite.departmentId', '=', 'departments_composite.id' ); // $ExpectType { username: any; }[] (await knexInstance('users') .innerJoin('departments', 'users.departmentId', 'departments.id')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' )) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' )) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' )) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .select('*')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ) .select('*')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ) .select('*')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .select()) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ) .select()) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ) .select()) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .select('name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ) .select('name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: string; }[] (await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ) .select('name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: any; }[] (await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .select('users.name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: any; }[] (await knexInstance('users_inferred') .innerJoin( 'departments_inferred', 'users_inferred.departmentId', 'departments_inferred.id' ) .select('users_inferred.name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType { username: any; }[] (await knexInstance('users_composite') .innerJoin( 'departments_composite', 'users_composite.departmentId', 'departments_composite.id' ) .select('users_composite.name', 'age')) .map(function(joined) { return { username: joined.name, }; }); // $ExpectType (User & Department)[] await knexInstance .select<(User & Department)[]>('users') .innerJoin('departments', 'users.departmentId', 'departments.id'); // $ExpectType (User & Department)[] await knexInstance('users').innerJoin('departments', function() { this.on('users.id', '=', 'departments.id'); }); // $ExpectType (User & Department)[] await knexInstance('users_inferred').innerJoin('departments_inferred', function() { this.on('users_inferred.id', '=', 'departments_inferred.id'); }); // $ExpectType (User & Department)[] await knexInstance('users_composite').innerJoin('departments_composite', function() { this.on('users_composite.id', '=', 'departments_composite.id'); }); // $ExpectType any[] await knexInstance('users') .innerJoin( 'departments', 'users.departmentId', 'departments.id' ) .select('users.id', 'departments.id'); // $ExpectType (User & Department)[] await knexInstance .select('*') .from('users') .join('departments', function() { this.on(function() { this.on('departments.id', '=', 'users.department_id'); this.orOn('departments.owner_id', '=', 'users.id'); }); }); // $ExpectType (User & Department)[] await knexInstance .select('*') .from('users_inferred') .join('departments_inferred', function() { this.on(function() { this.on('departments_inferred.id', '=', 'users_inferred.department_id'); this.orOn('departments_inferred.owner_id', '=', 'users_inferred.id'); }); }); // $ExpectType (User & Department)[] await knexInstance .select('*') .from('users_composite') .join('departments_composite', function() { this.on(function() { this.on('departments_composite.id', '=', 'users_composite.department_id'); this.orOn('departments_composite.owner_id', '=', 'users_composite.id'); }); }); // # Insertion // $ExpectType number[] await knexInstance('users').insert({ id: 10 }); // $ExpectType number[] await knexInstance('users').insert({ id: 10 }); // $ExpectType number[] await knexInstance('users_inferred').insert({ id: 10 }); // $ExpectError await knexInstance('users_composite').insert({ id: 10 }); // $ExpectError await knexInstance('users_composite').insert({}); // $ExpectType number[] await knexInstance('users_composite').insert({ insert: 'insert' }); // $ExpectType number[] await knexInstance('users').insert([{ id: 10 }]); // $ExpectType number[] await knexInstance('users').insert([{ id: 10 }]); // $ExpectType number[] await knexInstance('users_inferred').insert([{ id: 10 }]); // $ExpectError await knexInstance('users_composite').insert([{ id: 10 }]); // $ExpectError await knexInstance('users_composite').insert([{}]); // $ExpectType number[] await knexInstance('users_composite').insert([{ insert: 'insert' }]); const qb2 = knexInstance('users'); qb2.returning(['id', 'name']); const qb2ReturnCols = ['id', 'name'] as const; qb2.returning(qb2ReturnCols); // $ExpectType Partial[] await qb2.insert[]>({ id: 10 }); // ## With returning // $ExpectType any[] await knexInstance('users') .insert({ id: 10 }) .returning('id'); // $ExpectType number[] await knexInstance('users') .insert({ id: 10 }) .returning('id'); // $ExpectType string[] await knexInstance('users') .insert({ id: 10 }) .returning('id'); // $ExpectType number[] await knexInstance('users') .insert({ id: 10 }) .returning('id'); // $ExpectType number[] await knexInstance('users_inferred') .insert({ id: 10 }) .returning('id'); // $ExpectType number[] await knexInstance('users_composite') .insert({ insert: 'insert' }) .returning('id'); await knexInstance('users_composite') // $ExpectError .insert({ id: 10 }) .returning('id'); // Require insert argument to satisfy "insert" interface fully when composite type is available. await knexInstance('users_composite') // $ExpectError .insert({}) .returning('id'); // $ExpectType any[] await knexInstance('users') .insert([{ id: 10 }]) .returning('id'); // $ExpectType number[] await knexInstance('users') .insert([{ id: 10 }]) .returning('id'); // $ExpectType string[] await knexInstance('users') .insert([{ id: 10 }]) .returning('id'); // $ExpectType number[] await knexInstance('users') .insert([{ id: 10 }]) .returning('id'); // $ExpectType number[] await knexInstance('users_inferred') .insert([{ id: 10 }]) .returning('id'); // $ExpectType number[] await knexInstance('users_composite') .insert([{ insert: 'insert' }]) .returning('id'); await knexInstance('users_composite') // $ExpectError .insert([{ id: 10 }]) .returning('id'); // Require insert argument to satisfy "insert" interface fully when composite type is available. await knexInstance('users_composite') // $ExpectError .insert({}) .returning('id'); // $ExpectType Pick[] await knexInstance('users') .insert({ id: 10 }) .returning(['id', 'age']); // $ExpectType number[] await knexInstance('users').insert({ id: 10 }, 'id'); // $ExpectType User[] await knexInstance('users').insert({ id: 10 }, '*'); // $ExpectType User[] await knexInstance('users_inferred').insert({ id: 10 }, '*'); // $ExpectError await knexInstance('users_composite').insert({ id: 10 }, '*'); // $ExpectError await knexInstance('users_composite').insert({}, '*'); // $ExpectType User[] await knexInstance('users_composite').insert({ insert: 'insert' }, '*'); // $ExpectType User[] await knexInstance('users').insert([{ id: 10 }], '*'); // $ExpectType User[] await knexInstance('users_inferred').insert([{ id: 10 }], '*'); // $ExpectError await knexInstance('users_composite').insert([{ id: 10 }], '*'); // $ExpectError await knexInstance('users_composite').insert([{}], '*'); // $ExpectType User[] await knexInstance('users_composite').insert([{ insert: 'insert' }], '*'); // $ExpectType number[] await knexInstance.insert({ id: 10 }, 'id').into('users'); // $ExpectType number[] await knexInstance.insert({ id: 10 }, 'id').into('users_inferred'); // $ExpectType number[] await knexInstance.insert({ insert: 'insert' }, 'id').into('users_composite'); // $ExpectType Pick[] await knexInstance('users') .insert({ id: 10 }) .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred') .insert({ id: 10 }) .returning(['id', 'age']); await knexInstance('users_composite') // $ExpectError .insert({ id: 10 }) .returning(['id', 'age']); await knexInstance('users_composite') // $ExpectError .insert({}) .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite') .insert({ insert: 'insert' }) .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users') .insert({ id: 10 }, 'id') .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred') .insert({ id: 10 }, 'id') .returning(['id', 'age']); await knexInstance('users_composite') // $ExpectError .insert({ id: 10 }, 'id') .returning(['id', 'age']); await knexInstance('users_composite') // $ExpectError .insert({}, 'id') .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite') .insert({ insert: 'insert' }, 'id') .returning(['id', 'age']); // $ExpectType any[] await knexInstance('users') .insert({id: 10}) .returning('*'); // $ExpectType User[] await knexInstance('users') .insert({id: 10}) .returning('*'); // $ExpectType User[] await knexInstance('users_inferred') .insert({id: 10}) .returning('*'); await knexInstance('users_composite') // $ExpectError .insert({id: 10}) .returning('*'); await knexInstance('users_composite') // $ExpectError .insert({}) .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .insert({insert: 'insert'}) .returning('*'); // $ExpectType any[] await knexInstance .insert({id: 10}) .into('users') .returning('*'); // $ExpectType User[] await knexInstance .insert({id: 10}) .into('users') .returning('*'); // $ExpectType User[] await knexInstance .insert({id: 10}) .into('users_inferred') .returning('*'); // $ExpectType User[] await knexInstance .insert({insert: 'insert'}) .into('users_composite') .returning('*'); // $ExpectType User[] await knexInstance .insert({id: 10}) .returning('*') .into('users'); // $ExpectType User[] await knexInstance .insert({id: 10}) .returning('*') .into('users_inferred'); // $ExpectType User[] await knexInstance .insert({id: 10}) .returning('*') .into('users_composite'); // $ExpectType User[] await knexInstance('users') .insert({id: 10}, 'id') .returning('*'); // $ExpectType User[] await knexInstance('users_inferred') .insert({id: 10}, 'id') .returning('*'); await knexInstance('users_composite') // $ExpectError .insert({id: 10}, 'id') .returning('*'); await knexInstance('users_composite') // $ExpectError .insert({}, 'id') .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .insert({insert: 'insert'}, 'id') .returning('*'); // $ExpectType Pick[] await knexInstance .insert({ id: 10 }) .returning(['id', 'age']) .into('users'); // $ExpectType Pick[] await knexInstance .insert({ id: 10 }) .returning(['id', 'age']) .into('users_inferred'); // $ExpectType Pick[] await knexInstance .insert({ id: 10 }) .returning(['id', 'age']) .into('users_composite'); // $ExpectType any[] await knexInstance('users') .insert({ id: 10 }) .returning(['id', 'age']); // # Update // $ExpectType number await knexInstance('users') .where('id', 10) .update({ active: true }); const qb1 = knexInstance('users').where('id', 10); qb1.returning(['id', 'name']); // $ExpectType Partial[] await qb1.update[]>({ active: true }); // $ExpectType number await knexInstance('users') .where('id', 10) .update({ active: true }); // $ExpectType number await knexInstance('users_inferred') .where('id', 10) .update({ active: true }); // $ExpectType number await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }); await knexInstance('users_composite') // $ExpectError .update({ id: 10 }); await knexInstance('users_composite') // $ExpectError .update({}); // $ExpectType number await knexInstance .where('id', 10) .update({ active: true }) .from('users'); // ## With Returning // $ExpectType any[] await knexInstance('users') .where('id', 10) .update({ active: true }) .returning(['id', 'age']); // $ExpectType any[] await knexInstance('users') .where('id', 10) .update({ active: true }) .returning('*'); // $ExpectType User[] await knexInstance('users') .where('id', 10) .update({ active: true }) .returning('*'); // $ExpectType User[] await knexInstance('users_inferred') .where('id', 10) .update({ active: true }) .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({}) .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }) .returning('*'); // $ExpectType any[] await knexInstance .where('id', 10) .update({ active: true }) .returning('*') .from('users'); // $ExpectType User[] await knexInstance .where('id', 10) .update({ active: true }) .returning('*') .from('users'); // $ExpectType User[] await knexInstance .where('id', 10) .update({ active: true }) .returning('*') .from('users_inferred'); // $ExpectType User[] await knexInstance .where('id', 10) .update({ update: 'update' }) .returning('*') .from('users_composite'); // $ExpectType Pick[] await knexInstance('users') .where('id', 10) .update({ active: true }) .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred') .where('id', 10) .update({ active: true }) .returning(['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }) .returning(['id', 'age']); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({ id: 11 }) .returning(['id', 'age']); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({}) .returning(['id', 'age']); // $ExpectType number[] await knexInstance('users') .where('id', 10) .update({ active: true }, 'id'); // $ExpectType number[] await knexInstance('users_inferred') .where('id', 10) .update({ active: true }, 'id'); // $ExpectType number[] await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }, 'id'); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({ id: 11 }, 'id'); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({}, 'id'); // $ExpectType number[] await knexInstance('users') .where('id', 10) .update('active', true, 'id'); // $ExpectType number[] await knexInstance('users_inferred') .where('id', 10) .update('active', true, 'id'); // $ExpectType number[] await knexInstance('users_composite') .where('id', 10) .update('update', 'update', 'id'); // $ExpectType Pick[] await knexInstance('users') .where('id', 10) .update({ active: true }, ['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred') .where('id', 10) .update({ active: true }, ['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }, ['id', 'age']); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({ id: 11 }, ['id', 'age']); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({}, ['id', 'age']); // $ExpectType Pick[] await knexInstance('users') .where('id', 10) .update('active', true, ['id', 'age']); // $ExpectType Pick[] await knexInstance('users_inferred') .where('id', 10) .update('active', true, ['id', 'age']); // $ExpectType Pick[] await knexInstance('users_composite') .where('id', 10) .update('update', 'update', ['id', 'age']); const userUpdateReturnCols = ['id', 'age'] as const; // $ExpectType Pick[] await knexInstance('users') .where('id', 10) .update({ active: true }, userUpdateReturnCols); // $ExpectType Pick[] await knexInstance('users_inferred') .where('id', 10) .update({ active: true }, userUpdateReturnCols); // $ExpectType Pick[] await knexInstance('users_composite') .where('id', 10) .update({ update: 'update' }, userUpdateReturnCols); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({ id: 11 }, userUpdateReturnCols); await knexInstance('users_composite') .where('id', 10) // $ExpectError .update({}, userUpdateReturnCols); // TODO: .update('active', true', ['id', 'age']) does not works correctly // $ExpectType Pick[] await knexInstance .where('id', 10) .update({ active: true }, ['id', 'age']) .into('users'); // $ExpectType Pick[] await knexInstance .where('id', 10) .update({ active: true }, ['id', 'age']) .into('users_inferred'); // $ExpectType Pick[] await knexInstance .where('id', 10) .update({ update: 'update' }, ['id', 'age']) .into('users_composite'); // # Insert onConflict await knexInstance .table('users') .insert({ id: 10, active: true }) .onConflict('id') .merge({ active: true }) .returning('*'); // # Regression test (https://github.com/knex/knex/issues/4101) // # Ensure that .debug() can be called on a query containing an onConflict clause. await knexInstance .table('users') .insert({ id: 10, active: true }) .onConflict('id') .merge({ active: true }) .debug(true); // # Deletion // $ExpectType number await knexInstance('users') .where('id', 10) .delete(); // $ExpectType number await knexInstance('users_inferred') .where('id', 10) .delete(); // $ExpectType number await knexInstance('users_composite') .where('id', 10) .delete(); // $ExpectType number await knexInstance('users') .where('id', 10) .del(); // $ExpectType number await knexInstance('users_inferred') .where('id', 10) .del(); // $ExpectType number await knexInstance('users_composite') .where('id', 10) .del(); // $ExpectType number[] await knexInstance('users') .where('id', 10) .delete('id'); // $ExpectType number[] await knexInstance('users_inferred') .where('id', 10) .delete('id'); // $ExpectType number[] await knexInstance('users_composite') .where('id', 10) .delete('id'); // $ExpectType any[] await knexInstance('users') .where('id', 10) .del() .returning('*'); // $ExpectType any[] await knexInstance('users') .where('id', 10) .delete() .returning('*'); // $ExpectType User[] await knexInstance('users') .where('id', 10) .del() .returning('*'); // $ExpectType User[] await knexInstance('users_inferred') .where('id', 10) .del() .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .where('id', 10) .del() .returning('*'); // $ExpectType User[] await knexInstance('users') .where('id', 10) .delete() .returning('*'); // $ExpectType User[] await knexInstance('users_inferred') .where('id', 10) .delete() .returning('*'); // $ExpectType User[] await knexInstance('users_composite') .where('id', 10) .delete() .returning('*'); // $ExpectType User[] await knexInstance .where('id', 10) .del() .returning('*') .from('users'); // $ExpectType User[] await knexInstance .where('id', 10) .del() .returning('*') .from('users_inferred'); // $ExpectType User[] await knexInstance .where('id', 10) .del() .returning('*') .from('users_composite'); // $ExpectType User[] await knexInstance .where('id', 10) .delete() .returning('*') .from('users'); // $ExpectType User[] await knexInstance .where('id', 10) .delete() .returning('*') .from('users_inferred'); // $ExpectType User[] await knexInstance .where('id', 10) .delete() .returning('*') .from('users_composite'); // $ExpectType number[] await knexInstance .where('id', 10) .delete('id') .from('users'); // $ExpectType number[] await knexInstance .where('id', 10) .delete('id') .from('users_inferred'); // $ExpectType number[] await knexInstance .where('id', 10) .delete('id') .from('users_composite'); // $ExpectType void await knexInstance('users').truncate(); // $ExpectType void await knexInstance('users_inferred').truncate(); // $ExpectType void await knexInstance('users_composite').truncate(); // $ExpectType void await knexInstance('users').truncate(); // # Column Info: // $ExpectType ColumnInfo await knexInstance('users').columnInfo(); // $ExpectType ColumnInfo await knexInstance('users').columnInfo(); // $ExpectType ColumnInfo await knexInstance('users_inferred').columnInfo(); // $ExpectType ColumnInfo await knexInstance('users_composite').columnInfo(); // # Modify: function withUserName(queryBuilder: Knex.QueryBuilder, foreignKey: string) { queryBuilder .leftJoin('users', foreignKey, 'users.id') .select('users.user_name'); } // $ExpectType QueryBuilder knexInstance .table('articles') .select('title', 'body') .modify(withUserName, 'articles_user.id'); const withAge = (queryBuilder: Knex.QueryBuilder) => queryBuilder.select('age'); // $ExpectType Pick await knexInstance .table('users') .select('id') .modify>(withAge); // With: // $ExpectType any[] await knexInstance .with('with_alias', knexInstance.raw('select * from "users" where "id" = ?', 1)) .select('*') .from('with_alias'); // $ExpectType any[] await knexInstance .with( 'with_alias', knexInstance.raw('select * from "users" where "id" = ?', 1) ) .select('*') .from('with_alias'); // $ExpectType User[] await knexInstance .with( 'with_alias', knexInstance.raw('select * from "users" where "id" = ?', 1) ) .select('*') .from('with_alias'); // $ExpectType any[] await knexInstance .with('with_alias', (qb) => { qb.select('*') .from('books') .where('author', 'Test'); }) .select('*') .from('with_alias'); // $ExpectType any[] await knexInstance .withRecursive('ancestors', (qb) => { qb.select('*') .from('users') .where('users.id', 1); }) .select('*') .from('ancestors'); // $ExpectType any[] await knexInstance .withRecursive('ancestors', (qb) => { qb.select('*') .from('users') .where('users.id', 1); }) .select('*') .from('ancestors'); // $ExpectType User[] await knexInstance .withRecursive('ancestors', (qb) => { qb.select('*') .from('users') .where('users.id', 1); }) .select('*') .from('ancestors'); // $ExpectType User[] await knexInstance .withRecursive('ancestors', (qb: Knex.QueryBuilder) => { qb.select('*') .from('users') .where('users.id', 1); }) .select('*') .from('ancestors'); // $ExpectType Pick[] await knexInstance .withRecursive('ancestors', (qb) => { qb.select('*') .from('users') .where('users.id', 1); }) .select('id', 'name') .from('ancestors'); // $ExpectType any[] await knexInstance .withSchema('public') .select('*') .from('users'); // $ExpectType User[] await knexInstance .withSchema('public') .select('*') .from('users'); // Seed: // $ExpectType string await knexInstance.seed.make('test'); // $ExpectType string await knexInstance.seed.make('test', { extension: 'ts', directory: 'lib/seeds' }); // $ExpectType [string[]] await knexInstance.seed.run(); // $ExpectType [string[]] await knexInstance.seed.run({ extension: 'ts', directory: 'lib/seeds' }); // $ExpectType any[] await knexInstance('users', { only: true }); // $ExpectType any[] await knexInstance .select('*') .from('users', { only: true }); // $ExpectType any knexInstance.queryBuilder().queryContext(); // .raw() support // $ExpectType User[] await knexInstance('users') .where({ id: knexInstance.raw('a') }); // $ExpectType User[] await knexInstance('users') .where('id', knexInstance.raw('a')); // $ExpectType Ticket[] await knexInstance('users') .where({ at: knexInstance.fn.now() }); // $ExpectType User[] await knexInstance('users') // we can't do anything here for now .where('id', knexInstance.raw('string')); // $ExpectType number[] await knexInstance('users') .insert({ id: knexInstance.raw('a') }); // $ExpectType User[] await knexInstance('users') .insert([{ id: knexInstance.raw('a') }], '*'); // $ExpectType number[] await knexInstance('users') .update({ id: knexInstance.raw('a') }, 'id'); // $ExpectType string[] await knexInstance('users') .update<'active', 'name'>('active', knexInstance.raw('true'), 'name'); // $ExpectType Pick[] await knexInstance('users') .update<'active', 'name'>('active', knexInstance.raw('true'), ['name']); };