2021-03-08 23:18:49 +02:00
|
|
|
import { knex } from '../types';
|
2020-11-16 23:55:58 +02:00
|
|
|
import { expectType } from 'tsd';
|
2020-11-20 12:46:08 +02:00
|
|
|
import { clientConfig } from './common';
|
2020-11-16 23:55:58 +02:00
|
|
|
|
2020-12-30 22:26:08 +02:00
|
|
|
const knexInstance = knex(clientConfig);
|
2020-11-16 23:55:58 +02:00
|
|
|
|
|
|
|
// Use:
|
2020-12-30 22:26:08 +02:00
|
|
|
// import { Knex } from 'knex'
|
2020-11-16 23:55:58 +02:00
|
|
|
|
2020-11-20 12:46:08 +02:00
|
|
|
// This would be `declare module 'knex'` in runtime code
|
2020-11-16 23:55:58 +02:00
|
|
|
declare module '../types' {
|
2020-12-30 22:26:08 +02:00
|
|
|
namespace Knex {
|
|
|
|
interface QueryBuilder {
|
2023-07-12 21:42:57 +01:00
|
|
|
customSelect<TRecord extends {}, TResult>(
|
2020-12-30 22:26:08 +02:00
|
|
|
value: number
|
2022-06-14 13:10:01 +02:00
|
|
|
): Promise<Knex.QueryBuilder<TRecord, TResult>>;
|
2020-12-30 22:26:08 +02:00
|
|
|
}
|
2020-11-16 23:55:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 23:18:49 +02:00
|
|
|
knex.QueryBuilder.extend('customSelect', function (value: number) {
|
2023-07-12 21:42:57 +01:00
|
|
|
return new Promise((r) =>
|
|
|
|
r(this.select(this.client.raw(`${value} as value`)))
|
|
|
|
);
|
2020-11-16 23:55:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const main = async () => {
|
2023-07-12 21:42:57 +01:00
|
|
|
expectType<number[]>(
|
|
|
|
await await knexInstance('users').customSelect<any, number[]>(42)
|
|
|
|
);
|
2020-11-20 12:46:08 +02:00
|
|
|
};
|