GithubHelp home page GithubHelp logo

khanifikhsanudin / mysql-query-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evershopcommerce/mysql-query-builder

0.0 0.0 0.0 153 KB

A MySQL query builder for Nodejs

License: MIT License

JavaScript 100.00%

mysql-query-builder's Introduction

MySQL query builder for NodeJS

Build npm version License: MIT

A MySQL query builder for NodeJS.

Installation

npm install @nodejscart/mysql-query-builder

Usage guide

It implements async/await.

Simple select

// 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);
// });
const {select} = require('@nodejscart/mysql-query-builder')

const products = await select("*")
.from("product")
.where("product_id", ">", 1)
.execute(pool);

More complex where

// 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);
// });
const {select} = require('@nodejscart/mysql-query-builder')

const products = await select("*")
.from("product")
.where("product_id", ">", 1)
.and("sku", "LIKE", "sku")
.execute(pool);

Event more complex where

// 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);
// });
const {select} = require('@nodejscart/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

// 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);
// });
const {select} = require('@nodejscart/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 email phone status
1 David [email protected] 123456 1
// 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;
//      });
//    }
//  });
const {insert} = require('@nodejscart/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);
const {update} = require('@nodejscart/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

const {insert, getConnection, startTransaction, commit, rollback} = require('@nodejscart/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);
}

Security

All user provided data will be escaped. Please check this for more detail.

mysql-query-builder's People

Contributors

treoden avatar dependabot[bot] avatar khanifikhsanudin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.