Skip to content

Commit

Permalink
re-format
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Aug 15, 2019
1 parent 7a8dbb1 commit 654a738
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
13 changes: 9 additions & 4 deletions __tests__/index.spec.js
Expand Up @@ -47,14 +47,16 @@ describe('onDuplicateUpdate', () => {
describe('behaviour', () => {
it('should allow insert new row', async () => {
const person = { id: 2, name: 'test' };
await db.insert(person).into('persons').onDuplicateUpdate('name');
await db.insert(person).into('persons')
.onDuplicateUpdate('name');
const insertPerson = await getById(2);
expect(insertPerson).toEqual(expect.objectContaining(person));
});

it('should update on duplicate', async () => {
await db.insert({ id: 3, name: 'test3' }).into('persons');
await db.insert({ id: 3, name: 'test33' }).into('persons').onDuplicateUpdate('name');
await db.insert({ id: 3, name: 'test33' }).into('persons')
.onDuplicateUpdate('name');
const person = await getById(3);

expect(person.name).toBe('test33');
Expand All @@ -67,7 +69,8 @@ describe('onDuplicateUpdate', () => {
id,
name: 'test5',
email: '5@5.com'
}).into('persons').onDuplicateUpdate('name', 'email');
}).into('persons')
.onDuplicateUpdate('name', 'email');
const person = await getById(id);

expect(person).toEqual(expect.objectContaining({ name: 'test5', email: '5@5.com' }));
Expand All @@ -80,7 +83,9 @@ describe('onDuplicateUpdate', () => {
id,
name: 'test6',
email: '6@6.com'
}).into('persons').onDuplicateUpdate('name', {'email': 'updated-email'});
}).into('persons')
.onDuplicateUpdate('name', { 'email': 'updated-email' });

const person = await getById(id);
expect(person).toEqual(expect.objectContaining({
name: 'test6',
Expand Down
22 changes: 11 additions & 11 deletions lib/index.js
Expand Up @@ -11,27 +11,27 @@ module.exports.attachOnDuplicateUpdate = function attachOnDuplicateUpdate() {
}

const columnsPlaceHolders = columns.map((column) => {
if (typeof column === 'string'){
return `??=Values(??)`
if (typeof column === 'string') {
return `??=Values(??)`;
}
if (column && typeof column === 'object'){
return Object.keys(column).map(() => `??=?`).join(', ')
if (column && typeof column === 'object') {
return Object.keys(column).map(() => `??=?`).join(', ');
}
throw new Error('onDuplicateUpdate error: expected column name to be string or object.');
}).join(', ');

const binds = columns.reduce((bindings, column) => {
if (typeof column === 'string'){
return bindings.concat([column, column])
if (typeof column === 'string') {
return bindings.concat([column, column]);
}
if (column && typeof column === 'object'){
if (column && typeof column === 'object') {
const objectColumns = Object.keys(column).map((col) => {
return [col, column[col]]
})
return bindings.concat(...objectColumns)
return [col, column[col]];
});
return bindings.concat(...objectColumns);
}
throw new Error('onDuplicateUpdate error: expected column name to be string or object.');
}, [])
}, []);

return this.client.raw(
`${this.toString()} on duplicate key update ${columnsPlaceHolders}`,
Expand Down
2 changes: 1 addition & 1 deletion types.d.ts
Expand Up @@ -2,7 +2,7 @@ import { QueryBuilder as KnexQB } from 'knex';

declare module 'knex' {
interface QueryBuilder {
onDuplicateUpdate(...columnNames: Array<{[key: string]: string} | string>): KnexQB;
onDuplicateUpdate(...columnNames: Array<{ [key: string]: string } | string>): KnexQB;
}
}

Expand Down

0 comments on commit 654a738

Please sign in to comment.