Skip to main content

mysql@3.0.0

insert(table, fields)
sql(sqlQuery, [options])
upsert(table, fields)
upsertMany(table, data)

This adaptor exports the following from common:

alterState()
arrayToString()
as()
combine()
cursor()
dataPath()
dataValue()
dateFns
each()
field()
fields()
fn()
fnIf()
lastReferenceValue()
merge()
sourceValue()

Functions

insert

insert(table, fields) ⇒ Operation

Insert a record

ParamTypeDescription
tablestringThe target table
fieldsobjectA fields object

Example: Insert a record into the users table

insert("users", { name: (state) => state.data.name });

sql

sql(sqlQuery, [options]) ⇒ Operation

Execute a SQL statement. Take care when inserting values from state directly into a query, as this can be a vector for injection attacks. See OWASP SQL Injection Prevention Cheat Sheet for guidelines

ParamTypeDefaultDescription
sqlQuerystring | functionThe SQL query as a string or a function that returns a string using state.
[options]objectOptional options argument.
[options.writeSql]booleanfalseIf true, logs the generated SQL statement. Defaults to false.
[options.execute]booleantrueIf false, does not execute the SQL, just logs it and adds to state.queries. Defaults to true.

Example

sql(state => `select * from ${state.data.tableName};`, { writeSql: true })

upsert

upsert(table, fields) ⇒ Operation

Insert or Update a record if matched

ParamTypeDescription
tablestringThe target table
fieldsobjectA fields object

Example: Upsert a record

upsert("table", { name: (state) => state.data.name });

upsertMany

upsertMany(table, data) ⇒ Operation

Insert or update multiple records using ON DUPLICATE KEY

ParamTypeDescription
tablestringThe target table
dataarrayAn array of objects or a function that returns an array

Example: Upsert multiple records

upsertMany(
'users', // the DB table
[
{ name: 'one', email: 'one@openfn.org' },
{ name: 'two', email: 'two@openfn.org' },
]
)