mysql-upsert
Upsert (insert or update) multiple rows into MySQL
Upsert
Upsert = An operation that inserts rows into a database table if they do not already exist, or updates them if they do.
Install
npm install mysql-upsert --save
Usage
Basic usage is:
const upsert = table data fields
mysqlConnection
is a mysql connection that has aquery
function that returns a Promise.table
is a the table name to upsert intodata
is an array of objects. Each object is a row to insertfields
is an array of fields to upsert. (optional, defaults to the keys of the first object indata
)
Following examples use async
/await
syntax but can be used with regular Promise syntax.
const mysql = const upsert = const table = 'users' const data = id: 1 name: 'Steve' company: 'Apple' id: 2 name: 'Bill' company: 'Microsoft' // With single connection const connection = await mysql const affectedRows = await table data await connection // Limit fields const affectedRows = await table data 'name' 'company' // With pools const pool = mysql const affectedRows = await table data 'name' 'company' // With single connection from pool const connection = await pool const affectedRows = await table data await connection
Who made this?
- Torii - https://toriihq.com
- Tal Bereznitskey. Find me on Twitter as @ketacode at https://twitter.com/ketacode