[pagination] paginate result if script is connect to a Grid component
return a Promise
Example
constqueryArgs={db:"MASTER",sql:"select id from table where name = ?",values: ["PIPPO"]}constret=awaitmysql.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.
const queryArgs = {
db: "MASTER",
sql: "select id from table where name = ?",
values: ["PIPPO"],
pagination : true
}
const ret = await mysql.query(queryArgs);
const queryArgs = {
db: "MASTER",
sql: 'INSERT INTO posts SET ?',
values: {id: 1, title: 'Hello MySQL'};
}
connection.query(queryArgs);
// INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
const CURRENT_TIMESTAMP = {
toSqlString: function() { return 'CURRENT_TIMESTAMP()'; }
};
const queryArgs = {
db: "MASTER",
sql: 'UPDATE posts SET modified = ? WHERE id = ?',
values:[CURRENT_TIMESTAMP, 42]
}
connection.query(queryArgs);
//UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id = 42
var userId = 1;
var columns = ['username', 'email'];
const queryArgs = {
db: "MASTER",
sql: 'SELECT ?? FROM ?? WHERE id = ?',
values:[columns, 'users', userId]
}
mysql.query(queryArgs);
// SELECT `username`, `email` FROM `users` WHERE id = 1
mysql.escape(value)
var query = "SELECT * FROM posts WHERE title=" + mysql.escape("Hello MySQL");
mysql.escapeId(value)
var sql = 'SELECT * FROM posts ORDER BY ' + mysql.escapeId(sorter);