Skip to content

Commit

Permalink
Start using Knex.QueryBuilder.extend api instead of monkey patching
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmosh committed Mar 5, 2020
1 parent bb4ac15 commit 96729c8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/index.js
@@ -1,7 +1,7 @@
const KnexQueryBuilder = require('knex/lib/query/builder');
const Knex = require('knex');

module.exports.attachOnDuplicateUpdate = function attachOnDuplicateUpdate() {
KnexQueryBuilder.prototype.onDuplicateUpdate = function (...columns) {
Knex.QueryBuilder.extend('onDuplicateUpdate', function onDuplicateUpdate(...columns) {
if (this._method !== 'insert') {
throw new Error('onDuplicateUpdate error: should be used only with insert query.');
}
Expand Down Expand Up @@ -31,11 +31,11 @@ module.exports.attachOnDuplicateUpdate = function attachOnDuplicateUpdate() {
bindings: originalBindings,
} = this.toSQL();

const newBindings = [ ...originalBindings, ...bindings ];
const newBindings = [...originalBindings, ...bindings];

return this.client.raw(
`${originalSQL} on duplicate key update ${placeholders.join(', ')}`,
newBindings,
);
};
});
};

0 comments on commit 96729c8

Please sign in to comment.