Skip to main content

mysql@4.0.1

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

This adaptor exports the following from common:

alterState
arrayToString()
as()
assert()
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

This operation writes the following keys to state:

State KeyDescription
datathe query results object
data.resultthe query result rows
data.fieldsthe query result fields
queriesan array of queries executed. Queries are added if options.writeSql is true.
referencesan array of all previous data objects used in the Job

Example: Insert a record into a table

insert("users", { name: "one", email: "one@openfn.org" });

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

ParamTypeDescription
sqlQuerystringThe sql query string.
[options]sqlOptionsThe sql query options.

This operation writes the following keys to state:

State KeyDescription
datathe query results object
data.resultthe query result rows
data.fieldsthe query result fields
queriesan array of queries executed. Queries are added if options.writeSql is true.
referencesan array of all previous data objects used in the Job

Example

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

Example: Prepared statements

sql(state => `select * from ?? where id = ?;`, {
values: state => [state.data.tableName, state.data.id],
});

upsert

upsert(table, fields) ⇒ Operation

Insert or Update a record if matched

ParamTypeDescription
tablestringThe target table
fieldsobjectA fields object

This operation writes the following keys to state:

State KeyDescription
datathe query results object
data.resultthe query result rows
data.fieldsthe query result fields
queriesan array of queries executed. Queries are added if options.writeSql is true.
referencesan array of all previous data objects used in the Job

Example: Upsert a record into a table

upsert("users", { name: "Tuchi Dev" });

upsertMany

upsertMany(table, data) ⇒ Operation

Insert or update multiple records using ON DUPLICATE KEY

ParamTypeDescription
tablestringThe target table
dataarrayAn array of objects fields

This operation writes the following keys to state:

State KeyDescription
datathe query results object
data.resultthe query result rows
data.fieldsthe query result fields
queriesan array of queries executed. Queries are added if options.writeSql is true.
referencesan array of all previous data objects used in the Job

Interfaces

sqlOptions

Properties

NameTypeDefaultDescription
[values]arrayAn array of values for prepared statements.
[writeSql]booleanfalseIf true, logs the generated SQL statement. Defaults to false.
[execute]booleantrueIf false, does not execute the SQL, just logs it and adds to state.queries. Defaults to true.