Skip to content

Support REPLACE #367

@lukechilds

Description

@lukechilds

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions