Socket
Socket
Sign inDemoInstall

mysql-bricks

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mysql-bricks

sql-bricks extension for MySQL


Version published
Weekly downloads
636
decreased by-25.09%
Maintainers
1
Install size
320 kB
Created
Weekly downloads
 

Readme

Source

mysql-bricks

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.

Installation

Requires Node.js V6+ and ECMAScript 6 to run

$ npm install mysql-bricks

Usage

see sql-bricks documentation for common SELECT, INSERT, UPDATE and DELETE functionality. Below are examples of usage for the MySQL-specific functionality:

INSERT ... ON DUPLICATE KEY UPDATE ...
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
*/
INSERT IGNORE ...
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)
*/
LIMIT (SELECT / UPDATE / DELETE)
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
*/
OFFSET
let query = sql.select().from('main.some_table').limit(100).offset(50).toString();
/*                
SELECT * FROM main.some_table LIMIT 100 OFFSET 50
*/
ORDER BY (UPDATE / DELETE)

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
*/

Contribute

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!

Keywords

FAQs

Last updated on 27 Sep 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc