Skip to content

Commit

Permalink
feat: Add columns names & types inference
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Apr 21, 2021
1 parent 0c55c58 commit b9ed0ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
19 changes: 11 additions & 8 deletions __tests-tsd__/types.test-d.ts
Expand Up @@ -4,13 +4,16 @@ import '../types';
const db = knex({});

(async () => {
await db('table').select('*').onDuplicateUpdate('name', 'column2');
await db('table').insert({ id: 1, name: 'test' }).onDuplicateUpdate('name', 'column2');

await db('table').select('*').onDuplicateUpdate({ name: 'new name' }, 'xx');
await db('table').select('*').onDuplicateUpdate({
name: db.raw('Concat(name, "_test")'),
numericCol: 2,
nullCol: null,
dateCol: new Date()
});
await db('table').insert({ id: 1, name: 'test' }).onDuplicateUpdate({ name: 'new name' }, 'xx');
await db<{ id: number; name: string }>('table')
.insert({ id: 1, name: 'test' })
.onDuplicateUpdate({
name: db.raw('Concat(name, "_test")'),
});

const response = await db<{ id: number; name: string }>('table')
.insert({ id: 1, name: 'bla' })
.onDuplicateUpdate('name', 'id', { name: db.raw('Concat(name, "_test")') });
})();
9 changes: 7 additions & 2 deletions types.d.ts
Expand Up @@ -2,8 +2,13 @@ import { Knex } from 'knex';

declare module 'knex' {
namespace Knex {
interface QueryBuilder {
onDuplicateUpdate(...columnNames: Array<{ [key: string]: any } | string>): Knex.QueryBuilder<any, any>;
interface QueryBuilder<TRecord extends {} = any, TResult = any> {
onDuplicateUpdate(
...columnNames: Array<
| Knex.DbRecord<Knex.ResolveTableType<TRecord, 'insert'>>
| keyof TRecord
>
): Knex.QueryBuilder<TRecord, TResult>;
}
}
}
Expand Down

0 comments on commit b9ed0ab

Please sign in to comment.