Created
August 28, 2016 07:15
-
-
Save avimar/80b8ad0518b244d56bc358cf9a89ab3b to your computer and use it in GitHub Desktop.
knex mysql update on duplicate and replace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function upsert(table, data, update){ | |
| if(!update) update=data; | |
| var insert = knex(table).insert(data).toString(); | |
| var update = knex(table).update(update).toString().replace(/^update .* set /i, ''); | |
| return knex.raw(insert + ' on duplicate key update ' + update); | |
| } | |
| function replace(table, data){ | |
| var insert = knex(table).insert(data).toString().replace(/^INSERT/i, 'REPLACE'); | |
| return knex.raw(insert); | |
| } |
@ohjames I believe this is related to the sql statement REPLACE not UPDATE
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slightly better: