2020-12-30 22:26:08 +02:00
|
|
|
import { knex, Knex } from '../types';
|
2021-07-21 01:24:33 +03:00
|
|
|
import { clientConfig, User, Department, Article } from './common';
|
2020-11-20 12:46:08 +02:00
|
|
|
import { expectType } from 'tsd';
|
|
|
|
|
2020-12-30 22:26:08 +02:00
|
|
|
const knexInstance = knex(clientConfig);
|
2020-11-20 12:46:08 +02:00
|
|
|
|
|
|
|
declare module '../types/tables' {
|
|
|
|
interface Tables {
|
|
|
|
users_inferred: User;
|
|
|
|
departments_inferred: Department;
|
|
|
|
articles_inferred: Article;
|
|
|
|
users_composite: Knex.CompositeTableType<
|
|
|
|
User,
|
|
|
|
{ insert: 'insert' },
|
|
|
|
{ update: 'update' }
|
|
|
|
>;
|
|
|
|
departments_composite: Knex.CompositeTableType<
|
|
|
|
Department,
|
|
|
|
{ insert: 'insert' },
|
|
|
|
{ update: 'update' }
|
|
|
|
>;
|
|
|
|
articles_composite: Knex.CompositeTableType<
|
|
|
|
Article,
|
|
|
|
{ insert: 'insert' },
|
|
|
|
{ update: 'update' }
|
|
|
|
>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const main = async () => {
|
|
|
|
// # Select:
|
|
|
|
|
2020-12-30 22:26:08 +02:00
|
|
|
expectType<any[]>(await knexInstance('users'));
|
2020-11-20 12:46:08 +02:00
|
|
|
|
|
|
|
// This test (others similar to it) may seem useless but they are needed
|
|
|
|
// to test for left-to-right inference issues eg: #3260
|
2020-12-30 22:26:08 +02:00
|
|
|
expectType<User[]>(await knexInstance('users'));
|
|
|
|
expectType<User[]>(await knexInstance<User>('users'));
|
|
|
|
expectType<User[]>(await knexInstance('users_inferred'));
|
|
|
|
expectType<User[]>(await knexInstance('users_composite'));
|
2020-11-20 12:46:08 +02:00
|
|
|
|
2020-12-30 22:26:08 +02:00
|
|
|
expectType<any[]>(await knexInstance('users').select('id'));
|
|
|
|
expectType<Partial<User>[]>(await knexInstance('users').select('id'));
|
2020-11-20 12:46:08 +02:00
|
|
|
|
2020-12-30 22:26:08 +02:00
|
|
|
expectType<Pick<User, 'id'>[]>(await knexInstance('users_inferred').select('id'));
|
|
|
|
expectType<Pick<User, 'id'>[]>(await knexInstance('users_composite').select('id'));
|
2020-11-20 12:46:08 +02:00
|
|
|
expectType<Pick<User, 'id' | 'age'>[]>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance('users_inferred').select('id').select('age')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
expectType<Pick<User, 'id' | 'age'>[]>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance('users_composite').select('id').select('age')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
expectType<Pick<User, 'id' | 'age'>[]>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance('users_inferred').select('id', 'age')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
expectType<Pick<User, 'id' | 'age'>[]>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance('users_composite').select('id', 'age')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
expectType<Pick<User, 'id'> | undefined>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance.first('id').from('users_inferred')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
expectType<Pick<User, 'id'> | undefined>(
|
2020-12-30 22:26:08 +02:00
|
|
|
await knexInstance.first('id').from('users_composite')
|
2020-11-20 12:46:08 +02:00
|
|
|
);
|
|
|
|
};
|