I'm trying to implement "upsert" functionality that works across MySQL, Postgres and SQLite.
I haven't found one way to get it working in all three but I have a solution using ON CONFLICT in Postgres and REPLACE in MySQL and SQLite.
I've implemented it like so:
upsert(key, value) {
let upsert = `REPLACE INTO keyv (key, value) VALUES ("${key}", "${value}")`;
if(this.sql.dialectName === 'postgres') {
upsert = this.entry
.insert({ key, value })
.onConflict({ columns: ['key'], update: ['value'] })
.toString();
}
return this.query(upsert);
}
As far as I can see there's no way to use REPLACE with node-sql. Is that correct? Would it be simple to implement?
I'm trying to implement "upsert" functionality that works across MySQL, Postgres and SQLite.
I haven't found one way to get it working in all three but I have a solution using
ON CONFLICTin Postgres andREPLACEin MySQL and SQLite.I've implemented it like so:
As far as I can see there's no way to use
REPLACEwith node-sql. Is that correct? Would it be simple to implement?