import { knex, Knex } from '../types'; import { expectType } from '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; } /** * Dict is used in these tests to denote an object with string keys and values of various types. * If you need a formal definition, you can do something like: * * type Dict = Record; * * But for the purpose of these tests, we keep it as is. */ const main = async () => { // $ExpectType Dict[] const c1 = await knexInstance('users').count(); expectType>>(c1); // $ExpectType Dict[] const c2 = await knexInstance('users_inferred').count(); expectType>>(c2); // $ExpectType Dict[] const c3 = await knexInstance('users_composite').count(); expectType>>(c3); // $ExpectType Dict[] const c4 = await knexInstance('users').count('age'); expectType>>(c4); // $ExpectType Dict[] const c5 = await knexInstance('users_inferred').count('age'); expectType>>(c5); // $ExpectType Dict[] const c6 = await knexInstance('users_composite').count('age'); expectType>>(c6); // $ExpectType Dict[] const c7 = await knexInstance('users').count('age'); expectType>>(c7); // $ExpectType { c: string | number; }[] const c8 = await knexInstance('users').count('id', { as: 'c' }); expectType>(c8); // $ExpectType (Pick & { c: string | number; })[] const c9 = await knexInstance('users') .select('departmentId') .count('id', { as: 'c' }); expectType & { c: string | number }>>(c9); // $ExpectType { count: number; } const c10 = await knexInstance('foo') .first() .count<{ count: number }>({ count: '*' }); expectType<{ count: number }>(c10); // $ExpectType { count: number; } const c11 = await knexInstance('foo') .first() .countDistinct<{ count: number }>({ count: '*' }); expectType<{ count: number }>(c11); // $ExpectType { count?: string | number | undefined; } const c12 = await knexInstance('foo').first().count({ count: '*' }); expectType<{ count?: string | number }>(c12); // $ExpectType { count?: string | number | undefined; } const c13 = await knexInstance('foo').first().countDistinct({ count: '*' }); expectType<{ count?: string | number }>(c13); // $ExpectType Dict const c14 = await knexInstance('users').first().count('age'); expectType>(c14); // $ExpectType Dict const c15 = await knexInstance('users_inferred').first().count('age'); expectType>(c15); // $ExpectType Dict const c16 = await knexInstance('users_composite').first().count('age'); expectType>(c16); // $ExpectType Dict const c17 = await knexInstance('users').first().count('age', 'id'); expectType>(c17); // $ExpectType Dict const c18 = await knexInstance('users').first().count(); expectType>(c18); // $ExpectType Dict const c19 = await knexInstance('users_inferred').first().count(); expectType>(c19); // $ExpectType Dict const c20 = await knexInstance('users_composite').first().count(); expectType>(c20); // $ExpectType Dict[] const c21 = await knexInstance.count().from('users'); expectType>>(c21); // $ExpectType Dict[] const c22 = await knexInstance.count().from('users_inferred'); expectType>>(c22); // $ExpectType Dict[] const c23 = await knexInstance.count().from('users_composite'); expectType>>(c23); // $ExpectType Dict[] const c24 = await knexInstance.count('age').from('users'); expectType>>(c24); // $ExpectType Dict[] const c25 = await knexInstance.count('age').from('users_inferred'); expectType>>(c25); // $ExpectType Dict[] const c26 = await knexInstance.count('age').from('users_composite'); expectType>>(c26); // $ExpectType Dict[] const c27 = await knexInstance.count('age').from('users'); expectType>>(c27); // $ExpectType { count: number; } const c28 = await knexInstance .first() .count<{ count: number }>({ count: '*' }) .from('foo'); expectType<{ count: number }>(c28); // $ExpectType { count: number; } const c29 = await knexInstance .first() .countDistinct<{ count: number }>({ count: '*' }) .from('foo'); expectType<{ count: number }>(c29); // $ExpectType { count?: string | number | undefined; } const c30 = await knexInstance.first().count({ count: '*' }).from('foo'); expectType<{ count?: string | number }>(c30); // $ExpectType { count?: string | number | undefined; } const c31 = await knexInstance .first() .countDistinct({ count: '*' }) .from('foo'); expectType<{ count?: string | number }>(c31); // $ExpectType Dict const c32 = await knexInstance.first().count('age').from('users'); expectType>(c32); // $ExpectType Dict const c33 = await knexInstance.first().count('age').from('users_inferred'); expectType>(c33); // $ExpectType Dict const c34 = await knexInstance.first().count('age').from('users_composite'); expectType>(c34); // $ExpectType Dict const c35 = await knexInstance.first().count('age', 'id').from('users'); expectType>(c35); // $ExpectType Dict const c36 = await knexInstance.first().count().from('users'); expectType>(c36); // $ExpectType Dict const c37 = await knexInstance.first().count().from('users_inferred'); expectType>(c37); // $ExpectType Dict const c38 = await knexInstance.first().count().from('users_composite'); expectType>(c38); // $ExpectType Dict[] const m1 = await knexInstance('users').max('age'); expectType>>(m1); // $ExpectType Dict[] const m2 = await knexInstance('users_inferred').max('age'); expectType>>(m2); // $ExpectType Dict[] const m3 = await knexInstance('users_composite').max('age'); expectType>>(m3); // $ExpectType Dict const m4 = await knexInstance('users').first().max('age'); expectType>(m4); // $ExpectType Dict const m5 = await knexInstance('users_inferred').first().max('age'); expectType>(m5); // $ExpectType Dict const m6 = await knexInstance('users_composite').first().max('age'); expectType>(m6); // $ExpectType Dict[] const m7 = await knexInstance('users').max('age'); expectType>>(m7); // $ExpectType Dict[] const mn1 = await knexInstance('users').min('age'); expectType>>(mn1); // $ExpectType Dict[] const mn2 = await knexInstance('users_inferred').min('age'); expectType>>(mn2); // $ExpectType Dict[] const mn3 = await knexInstance('users_composite').min('age'); expectType>>(mn3); // $ExpectType Dict const mn4 = await knexInstance('users').first().min('age'); expectType>(mn4); // $ExpectType Dict const mn5 = await knexInstance('users_inferred').first().min('age'); expectType>(mn5); // $ExpectType Dict const mn6 = await knexInstance('users_composite').first().min('age'); expectType>(mn6); // $ExpectType Dict[] const m8 = await knexInstance.max('age').from('users'); expectType>>(m8); // $ExpectType Dict[] const m9 = await knexInstance.max('age').from('users_inferred'); expectType>>(m9); // $ExpectType Dict[] const m10 = await knexInstance.max('age').from('users_composite'); expectType>>(m10); // $ExpectType Dict const m11 = await knexInstance.first().max('age').from('users'); expectType>(m11); // $ExpectType Dict const m12 = await knexInstance.first().max('age').from('users_inferred'); expectType>(m12); // $ExpectType Dict const m13 = await knexInstance.first().max('age').from('users_composite'); expectType>(m13); // $ExpectType Dict[] const m14 = await knexInstance.max('age').from('users'); expectType>>(m14); // $ExpectType Dict[] const mn7 = await knexInstance.min('age').from('users'); expectType>>(mn7); // $ExpectType Dict[] const mn8 = await knexInstance.min('age').from('users_inferred'); expectType>>(mn8); // $ExpectType Dict[] const mn9 = await knexInstance.min('age').from('users_composite'); expectType>>(mn9); // $ExpectType Dict const mn10 = await knexInstance.first().min('age').from('users'); expectType>(mn10); // $ExpectType ({ a: Date; } & { b: Date; })[] const t1 = await knexInstance('tickets') .min('at', { as: 'a' }) .max('at', { as: 'b' }); // FixMe // expectType>(t1); // $ExpectType ({ dep: any; } & { a: any; } & { b: any; })[] const ab1 = await knexInstance .select({ dep: 'departmentId' }) .min('age', { as: 'a' }) .max('age', { as: 'b' }) .from('users'); expectType>(ab1); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] const ab2 = await knexInstance .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }) .from('users'); expectType>(ab2); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] const ab3 = await knexInstance .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }) .from('users_inferred'); expectType>(ab3); // $ExpectType ({ dep: any; } & { a?: any; } & { b?: any; })[] const ab4 = await knexInstance .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }) .from('users_composite'); expectType>(ab4); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] const ab5 = await knexInstance('users') .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }); expectType>(ab5); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] const ab6 = await knexInstance('users_inferred') .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }); expectType>(ab6); // $ExpectType ({ dep: number; } & { a?: any; } & { b?: any; })[] const ab7 = await knexInstance('users_composite') .select({ dep: 'departmentId' }) .min({ a: 'age' }) .max({ b: 'age' }); expectType>(ab7); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] const ab8 = await knexInstance('users') .select({ dep: 'departmentId' }) .count({ a: 'age' }); expectType>(ab8); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] const ab9 = await knexInstance('users_inferred') .select({ dep: 'departmentId' }) .count({ a: 'age' }); expectType>(ab9); // $ExpectType ({ dep: number; } & { a?: string | number | undefined; })[] const ab10 = await knexInstance('users_composite') .select({ dep: 'departmentId' }) .count({ a: 'age' }); expectType>(ab10); // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] const ab11 = await knexInstance .select({ dep: 'departmentId' }) .count({ a: 'age' }) .from('users'); expectType>(ab11); // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] const ab12 = await knexInstance .select({ dep: 'departmentId' }) .count({ a: 'age' }) .from('users_inferred'); expectType>(ab12); // $ExpectType ({ dep: any; } & { a?: string | number | undefined; })[] const ab13 = await knexInstance .select({ dep: 'departmentId' }) .count({ a: 'age' }) .from('users_composite'); expectType>(ab13); // Analytic // $ExpectType (Pick & { rowNum: number; })[] const rNum1 = await knexInstance('users') .select('age') .rowNumber('rowNum', 'age'); expectType & { rowNum: number }>>(rNum1); // $ExpectError (we won't actually run expectError here, it's just a comment from the original code) // .rowNumber('rowNum', 'non_existing_field'); // $ExpectType (Pick & { rowNum: number; })[] const rNum2 = await knexInstance('users') .select('age') .rowNumber('rowNum', ['age']); expectType & { rowNum: number }>>(rNum2); // $ExpectType (Pick & { rowNum: number; })[] const rNum3 = await knexInstance('users') .select('age') .rowNumber('rowNum', (builder) => { builder.orderBy('age'); }); expectType & { rowNum: number }>>(rNum3); // $ExpectType (Pick & { rowNum: number; })[] const rNum4 = await knexInstance('users') .select('age') .rowNumber('rowNum', 'age', 'departmentId'); expectType & { rowNum: number }>>(rNum4); // $ExpectError (again, just a comment from original) // .rowNumber('rowNum', 'age', 'non_existing_field'); // $ExpectType (Pick & { rowNum: number; })[] const rNum5 = await knexInstance('users') .select('age') .rowNumber('rowNum', 'age', ['departmentId', 'active']); expectType & { rowNum: number }>>(rNum5); // $ExpectType (Pick & { rowNum: number; })[] const rNum6 = await knexInstance('users') .select('age') .rowNumber('rowNum', (builder) => { builder.orderBy('age').partitionBy('departmentId'); }); expectType & { rowNum: number }>>(rNum6); // ## With inner query: // ### For column selection: // $ExpectType any[] const colSel1 = await knexInstance('users').select( knexInstance('foo').select('bar').as('colName') ); expectType(colSel1); // $ExpectType any[] const colSel2 = await knexInstance('users').select( knexInstance('foo').select('bar').as('colName') ); expectType(colSel2); // $ExpectType Pick[] const colSel3 = await knexInstance('users').select< Pick[] >(knexInstance('foo').select('bar').as('colName')); expectType>>(colSel3); // ### For condition: // $ExpectType any[] const cond1 = await knexInstance('users').whereNot(function () { this.where('id', 1).orWhereNot('id', '>', 10); }); expectType(cond1); // $ExpectType User[] const cond2 = await knexInstance('users').whereNot(function () { this.where('id', 1).orWhereNot('id', '>', 10); }); expectType(cond2); // $ExpectType User[] const cond3 = await knexInstance('users_inferred').whereNot(function () { this.where('id', 1).orWhereNot('id', '>', 10); }); expectType(cond3); }; class ExcelClient extends knex.Client {}