mysql

Utility class to interact with MySQL

query

Run your queries by autocommitting as soon as the query runs.

Syntax

mysql.query({db,sql,[timeout,values,nestTables,pagination]})
  • db db name

  • sql query to execute

  • [timeout] query timeout

  • [values] opzionale, valori da passare nella query

  • [nestTables] overlapping column names

  • [pagination] paginate result if script is connect to a Grid component

  • return a Promise

Example

const queryArgs = {
    db: "MASTER",
    sql: "select id from table where name = ?",
    values: ["PIPPO"]
}
const ret = await mysql.query(queryArgs);

Different value types are escaped differently, here is how:

  • Numbers are left untouched

  • Booleans are converted to true / false

  • Date objects are converted to 'YYYY-mm-dd HH:ii:ss' strings

  • Buffers are converted to hex strings, e.g. X'0fa5'

  • Strings are safely escaped

  • Arrays are turned into list, e.g. ['a', 'b'] turns into 'a', 'b'

  • Nested arrays are turned into grouped lists (for bulk inserts), e.g. [['a', 'b'], ['c', 'd']] turns into ('a', 'b'), ('c', 'd')

  • Objects that have a toSqlString method will have .toSqlString()called and the returned value is used as the raw SQL.

  • Objects are turned into key = 'val' pairs for each enumerable property on the object. If the property's value is a function, it is skipped; if the property's value is an object, toString() is called on it and the returned value is used. undefined / null are converted to NULL

  • NaN / Infinity are left as-is. MySQL does not support these, and trying to insert them as values will trigger MySQL errors until they implement support.

Joins with overlapping column names

When executing joins, you are likely to get result sets with overlapping column names. Docs

By default, node-mysql will overwrite colliding column names in the order the columns are received from MySQL, causing some of the received values to be unavailable.

However, you can also specify that you want your columns to be nested below the table name using nestTables properties.

Examples

Example 1

Example 2

Pagination vs Grid component

If your query is connected to a grid store and you want it to be paginated you must specify it through the pagination property, the query will adapt to the grid setup.

Insert a record

Insert a record example:

Custom toSqlString

Insert a record with a custom toSqlString

Parameter advanced

escape

If you feel the need to escape queries by yourself, you can also use the escaping function directly:

Syntax

  • value string to exape

  • return escape string

Example

escapeId

If you can't trust an SQL identifier (database / table / column name) because it is provided by a user, you should escape it.

Syntax

  • value to escaple

  • return escape value

Example

transactionBlock

Use this function to execute the code inside a transaction. In case of an error (eg throw) the system will rollback, otherwise the commit will be done

Syntax

  • db db name

  • transaction function execute in a transaction

Manual Transaction

Use getConnection, beginTransaction, commit and rollback to manualy manage the transactions.

getConnection

Use this function to manage the transactions manually.

See mysql-promise docs

Syntax

  • db db name

  • return a Promise with the transaction

Example

Last updated