
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
mysql-bricks
Advanced tools
sql-bricks extension for MySQL
mysql-bricks is an extension to the awesome sql-bricks library, that helps generate SQL statements for MySQL DB. It adds MySQL specific functions and formatting on top of the sql-bricks functionality.
Requires Node.js V6+ and ECMAScript 6 to run
$ npm install mysql-bricks
see sql-bricks documentation for common SELECT, INSERT, UPDATE and DELETE functionality. Below are examples of usage for the MySQL-specific functionality:
let values = [[123, 'Daniel', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let query = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.values(values)
.onDuplicateKeyUpdate(['grade'])
.toString();
/*
INSERT INTO main.some_table (id, name, age, grade)
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
ON DUPLICATE KEY UPDATE grade = VALUES(grade)
*/
ON DUPLICATE KEY UPDATE clause can also contain a column-value pair, where the value is a free text sql.
Note that free-text sql will not be automatically quoted if it contains reserved words.
let values = [[123, 'Daniel', 41, 92, 0], [456, 'David', 34, 87, 0], [789, 'Rachel', 22, 98, 0]];
let query = sql.insert('main.some_table', 'id', 'name', 'age', 'grade', 'counter')
.values(values)
.onDuplicateKeyUpdate(['age', 'grade', { counter: 'counter + 1' }])
.toString();
/*
INSERT INTO main.some_table (id, name, age, grade, counter)
VALUES (123, \'Daniel\', 41, 92, 0), (456, \'David\', 34, 87, 0), (789, \'Rachel\', 22, 98, 0)
ON DUPLICATE KEY UPDATE age = VALUES(age), grade = VALUES(grade), counter = counter + 1
*/
let values = [[123, 'Daniel', 41, 92], [456, 'David', 34, 87], [789, 'Rachel', 22, 98]];
let query = sql.insert('main.some_table', 'id', 'name', 'age', 'grade')
.ignore()
.values(values)
.toString();
/*
INSERT IGNORE INTO main.some_table (id, name, age, grade)
VALUES (123, \'Daniel\', 41, 92), (456, \'David\', 34, 87), (789, \'Rachel\', 22, 98)
*/
let query = sql.select().from('main.some_table').limit(100).toString();
/*
SELECT * FROM main.some_table LIMIT 100
*/
MySQL supports optional 'limit' clause in UPDATE and DELETE queries:
let query = sql.delete('main.some_table').limit(100).toString();
/*
DELETE FROM main.some_table LIMIT 100
*/
let query = sql.select().from('main.some_table').limit(100).offset(50).toString();
/*
SELECT * FROM main.some_table LIMIT 100 OFFSET 50
*/
MySQL supports optional 'order by' clause in UPDATE and DELETE queries:
let query = sql.update('main.some_table', { grade: 90})
.where(sql.gt('grade', 90))
.orderBy('age desc')
.limit(100)
.toString();
/*
UPDATE main.some_table SET grade = 90 WHERE grade > 90 ORDER BY age desc LIMIT 100
*/
Supported MySQL specific functions are the most common ones as used by me, but if you find something missing - feel free to open an issue, or better yet - a pull request!
FAQs
sql-bricks extension for MySQL
The npm package mysql-bricks receives a total of 1,246 weekly downloads. As such, mysql-bricks popularity was classified as popular.
We found that mysql-bricks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.