Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evershopcommerce/mysql-query-builder
A MySQL query builder for Nodejs
https://github.com/evershopcommerce/mysql-query-builder
mysql nodejs-mysql query-builder
Last synced: 8 days ago
JSON representation
A MySQL query builder for Nodejs
- Host: GitHub
- URL: https://github.com/evershopcommerce/mysql-query-builder
- Owner: evershopcommerce
- License: mit
- Created: 2021-05-09T10:22:34.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-02T15:25:39.000Z (over 1 year ago)
- Last Synced: 2024-11-02T12:11:57.825Z (16 days ago)
- Topics: mysql, nodejs-mysql, query-builder
- Language: JavaScript
- Homepage:
- Size: 206 KB
- Stars: 6
- Watchers: 2
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MySQL query builder for Node
[![Build](https://github.com/kt65/mysql-query-builder/actions/workflows/build.yml/badge.svg)](https://github.com/kt65/mysql-query-builder/actions/workflows/build.yml)
[![npm version](https://badge.fury.io/js/%40evershop%2Fmysql-query-builder.svg)](https://badge.fury.io/js/%40evershop%2Fmysql-query-builder)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)A MySQL query builder for NodeJS.
## Installation
```javascript
npm install @evershop/mysql-query-builder
```## Usage guide
It implements async/await.
### Simple select
```javascript
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host : 'localhost',
// user : 'me',
// password : 'secret',
// database : 'my_db'
// });// connection.connect();
// connection.query('SELECT * FROM product WHERE product_id > ?', [1], function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results[0].solution);
// });
```
```javascript
const {select} = require('@evershop/mysql-query-builder')const products = await select("*")
.from("product")
.where("product_id", ">", 1)
.execute(pool);
```
### More complex where
```javascript
// var mysql = require('mysql');
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host : 'localhost',
// user : 'me',
// password : 'secret',
// database : 'my_db'
// });// connection.query('SELECT * FROM product WHERE product_id > ? AND sku LIKE ?', [1, "sku"], function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results[0].solution);
// });
```
```javascript
const {select} = require('@evershop/mysql-query-builder')const products = await select("*")
.from("product")
.where("product_id", ">", 1)
.and("sku", "LIKE", "sku")
.execute(pool);
```
### Event more complex where
```javascript
// var mysql = require('mysql');
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host : 'localhost',
// user : 'me',
// password : 'secret',
// database : 'my_db'
// });// connection.query('SELECT * FROM product WHERE (product_id > ? AND sku LIKE ?) OR price > ?', [1, "sku", 100], function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results[0].solution);
// });
```
```javascript
const {select} = require('@evershop/mysql-query-builder')const query = select("*").from("product");
query.where("product_id", ">", 1).and("sku", "LIKE", "sku");
query.orWhere("price", ">", 100);const products = await query.execute(pool);
```### Join table
```javascript
// var mysql = require('mysql');
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host : 'localhost',
// user : 'me',
// password : 'secret',
// database : 'my_db'
// });// connection.query('SELECT * FROM product LEFT JOIN price ON product.id = price.id WHERE (product_id > ? AND sku LIKE ?) OR price > ?', [1, "sku", 100], function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results[0].solution);
// });
```
```javascript
const {select} = require('@evershop/mysql-query-builder')const query = select("*").from("product");
query.leftJoin('price').on('product.`product_id`', '=', 'price.`product_id`');
query.where("product_id", ">", 1).and("sku", "LIKE", "sku");
query.andWhere("price", ">", 100);const products = await query.execute(pool);
```### Insert&update
user_id
name
phone
status1
David
123456
1
```javascript
// var mysql = require('mysql');
// var mysql = require('mysql');
// var connection = mysql.createConnection({
// host : 'localhost',
// user : 'me',
// password : 'secret',
// database : 'my_db'
// });// connection.query('INSERT INTO user SET name=?, email=?, phone=?, status=?', ["David", "[email protected]", "123456", 1], function (error, results, fields) {
// if (error) {
// return connection.rollback(function() {
// throw error;
// });
// }
// });
```
```javascript
const {insert} = require('@evershop/mysql-query-builder')const query = insert("user")
.given({name: "David", email: "[email protected]", "phone": "123456", status: 1, notExistedColumn: "This will not be a part of the query"});
await query.execute(pool);
```
```javascript
const {update} = require('@evershop/mysql-query-builder')const query = update("user")
.given({name: "David", email: "[email protected]", "phone": "123456", status: 1, notExistedColumn: "This will not be a part of query"})
.where("user_id", "=", 1);
await query.execute(pool);
```
### Working with transaction```javascript
const {insert, getConnection, startTransaction, commit, rollback} = require('@evershop/mysql-query-builder');const pool = mysql.createPool({
host: "localhost",
user: "root",
password: "123456",
database: "test",
dateStrings: true
});// Create a connection from the pool
const connection = await getConnection(pool);// Start a transaction
await startTransaction(connection);
try {
await insert("user")
.given({name: "David", email: "[email protected]", "phone": "123456", status: 1, notExistedColumn: "This will not be a part of the query"})
.execute(connection);
await commit(connection);
} catch(e) {
await rollback(connection);
}
```
## SecurityAll user provided data will be escaped. Please check [this](https://github.com/mysqljs/mysql#escaping-query-values) for more detail.