Skip to content

Commit

Permalink
fix(query-builder): support onConflict().ignore() without parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan authored and jsprw committed May 7, 2023
1 parent 8174a83 commit 54392ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export class QueryBuilderHelper {

appendOnConflictClause<T>(type: QueryType, onConflict: { fields: string[]; ignore?: boolean; merge?: EntityData<T> | Field<T>[]; where?: QBFilterQuery<T> }[], qb: Knex.QueryBuilder): void {
onConflict.forEach(item => {
const sub = qb.onConflict(item.fields);
const sub = item.fields.length > 0 ? qb.onConflict(item.fields) : qb.onConflict();
Utils.runIfNotEmpty(() => sub.ignore(), item.ignore);
Utils.runIfNotEmpty(() => {
let mergeParam: Dictionary | string[] = item.merge!;
Expand Down
13 changes: 13 additions & 0 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,19 @@ describe('QueryBuilder', () => {
expect(qb.getParams()).toEqual(['baz', 1]);
}

{
const timestamp = new Date();
const qb = pg.em.createQueryBuilder(Author2).insert({
createdAt: timestamp,
email: 'ignore@example.com',
name: 'John Doe',
updatedAt: timestamp,
}).onConflict().ignore();

expect(qb.getQuery()).toEqual('insert into "author2" ("created_at", "email", "name", "updated_at") values ($1, $2, $3, $4) on conflict do nothing returning "id", "created_at", "updated_at", "age", "terms_accepted"');
expect(qb.getParams()).toEqual([timestamp, 'ignore@example.com', 'John Doe', timestamp]);
}

{
const qb = pg.em.createQueryBuilder(FooBar2, 'fb1');
qb.select('*')
Expand Down

0 comments on commit 54392ac

Please sign in to comment.