Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

OpenFn/language-mysql

Repository files navigation

⚠️ MOVED TO OpenFn/adaptors! ⚠️

N.B.: New versions are available at: https://github.com/OpenFn/adaptors/tree/main/packages/mysql

Language MySQL (Archived) Build Status

Language Pack for building expressions and operations to run MySQL queries.

See src/Adaptor.js for the full list of available helper functions including upsert(...).

Documentation

sample configuration

{
  "host": "some-host-url.compute-1.amazonaws.com",
  "port": "3306",
  "database": "wouldntyouliketoknow",
  "user": "me",
  "password": "noway"
}

Execute a query

Execute an sql query with the node mysql package.

query({
  sql: state => {
    return `select * from ${state.data.table} where id = ?;`;
  },
  timeout: 4000,
  values: ['007'],
});

Execute a sql query

This function takes either a string or a function that takes states and returns a string.

sqlString(state => {
  return (
    `INSERT INTO untitled_table (name, the_geom) VALUES ('` +
    state.data.version +
    `', ` +
    dataValue('form.Choix_tache')(state) +
    `)`
  );
});

Insert a single record

This function is used to insert a single record in a MySQL database.

insert(
  'some_table',
  fields(
    field('firstname', dataValue('form.patient_firstname')),
    field('lastname', dataValue('form.patient_lastname'))
  )
);

Insert or update a single record

This function is used to insert a single record in a MySQL database or update it if there is a match.

upsert(
  'some_table',
  fields(
    field('firstname', dataValue('form.patient_firstname')),
    field('lastname', dataValue('form.patient_lastname'))
  )
);

Upsert many records

This function allows the upsert of a set of records inside a table all at once.

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

or

upsertMany('users', state =>
  state.data.users.map(user => {
    name: user['name'],
    email: user['email']
  })
);

Development

Clone the repo, run npm install.

Run tests using npm run test or npm run test:watch

Build the project using make.

To build the docs for this repo, run ./node_modules/.bin/jsdoc --readme ./README.md ./lib -d docs.