MQSP
MQSP is a tool for mysql that supports for multiple read and write replica.
It also support object parsing(escaped) for your query, and result caching.
Any property that is not mapped in the query will have a default value of NULL
.
; const mqsp = ; const result = await mqsp; console; // { message: 'hello', val: null }
Quickstart
; ; const mqsp = ; let result = await mqsp; console; // { message: 'hello' } result = await mqsp; console; // [{ message: 'hello' }, { message: 'world' }];
You can also pass a config like this, if your read and write hosts are the same.
Instead of adding them both to writePool
and readPool
array.
Caching
Read operations are being cached with a max age of 5 minutes.
const res = await mqsp; await Promise; const cached = await mqsp; assert;
Transaction
MQSP supports mysql transaction.
const transaction = mqsp; await transaction; await transaction; await transaction;
API
Constructor
Creates a pool of the given config. The config is passed to the createPool
function
of the library mysql
. Only that, the host is being replaced with the values under
the writePool
and readPool
.
Query single row (read)
Get a single row, of the query.
const row = await mqsp; // `row` will contain an Object(not an Array) of the user. // If no result match of the given query, the return is `undefined`
Query multiple row (read)
Gets all the rows based on the query.
const rows = await mqsp; // `rows` will contain an Array of Object of the user.
Exists (read)
Determines whether the query does return a value.
let res = await mqsp; console; // true res = await mqsp; console; // false
Exec (write)
Executes the query and give the query result. Suggested not to use this for select statements or any other read operation.
const result = await mqsp; // `result` will contain the same object when you call `mysql.query`.
Get Transaction
Retrieve a transaction from an mqsp instance. Transaction API is the same with MQSP API, only is that under utilities is not included.
const transaction = mqsp; await transaction; await transaction; await transaction;
Utilities
toTimestamp(date, [excludeMs = true])
Converts the javascript date object to MySQL Timestamp format.
const date = '2017-07-07 07:07:07.777'; const timestamp = MQSP; console; // 2017-07-07 07:07:07.00
escape(val)
Escapes the value to prevent sql injection.
const res = await mqsp; console; console; // undefined // RowDataPacket { // val: ";;DROP mysql;", // }
Close
Closes the read and write connection pool
; await ; await mqsp; // Will throw an error