Skip to content

A Knex plugin that adds .onDuplicateUpdate() function to knex's query builder.

License

Notifications You must be signed in to change notification settings

enapupe/knex-on-duplicate-update

This branch is 1 commit ahead of, 41 commits behind felixmosh/knex-on-duplicate-update:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

dd79d8f · Apr 11, 2022

History

47 Commits
Mar 7, 2021
Apr 21, 2021
Apr 11, 2022
Apr 11, 2022
May 8, 2019
May 7, 2019
Aug 17, 2021
Aug 15, 2019
Nov 1, 2020
Aug 17, 2021
Apr 21, 2021
Aug 17, 2021

Repository files navigation

Knex-onDuplicateUpdate

npm CircleCI

Simple patcher for Knex. It adds the .onDuplicateUpdate() function to knex's query builder in order to add support MySQL's on duplicate key update columnName=Values(columnName).

Note

Knex (v0.21.10) added an official upsert functionality with similar capabilities which has support in MySQL, Postgress & SQLite.

How to set up

To use this lib, first you will have to install it:

npm i knex-on-duplicate-update --save

or

yarn add knex-on-duplicate-update

Then, add the following lines to your Knex set up:

const knex = require('knex')(config);

const {attachOnDuplicateUpdate} = require('knex-on-duplicate-update');
attachOnDuplicateUpdate();

Function definition

onDuplicateUpdate(...columns: Array<{[key: string]: string} | string>): Knex.QueryBuilder

How to use

Example

await knex.insert({id: 1, name: 'John', email: '[email protected]'})
    .into('persons')
    .onDuplicateUpdate('name', 'email');

Setting a fallback value for a column

await knex.insert({id: 1, name: 'John', email: '[email protected]'})
    .into('persons')
    .onDuplicateUpdate('name', {email: '[email protected]'});

This lib got inspiration from knex-paginator.

About

A Knex plugin that adds .onDuplicateUpdate() function to knex's query builder.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 89.9%
  • TypeScript 10.1%