mysql-qbuilder
Advanced tools
Comparing version 1.4.2 to 1.5.1
31
index.js
@@ -122,11 +122,5 @@ const mysql = require('mysql') | ||
exports.execute = function () { | ||
if (parameters.command.indexOf('?') !== -1) { | ||
connection.query(parameters.command, parameters.params, (err, result) => { | ||
if (err) throw new Error(err) | ||
}) | ||
} else { | ||
connection.query(parameters.command, (err, result) => { | ||
if (err) throw new Error(err) | ||
}) | ||
} | ||
connection.query(parameters.command, parameters.params, (err, result) => { | ||
if (err) throw new Error(err) | ||
}) | ||
} | ||
@@ -136,16 +130,13 @@ | ||
* [Get the result from created sql query] | ||
* @param {fn} callback [Callback where first parameter is Error and second is list of records] | ||
* @return {Promise} [Promise of result of query string] | ||
*/ | ||
exports.getResult = (callback) => { | ||
if (parameters.command.indexOf('?') !== -1) { | ||
exports.getResult = () => { | ||
return new Promise((resolve, reject) => { | ||
connection.query(parameters.command, parameters.params, (err, rows, fields) => { | ||
if (err) return callback(err, null) | ||
callback(null, JSON.parse(JSON.stringify(rows))) | ||
if (err) return reject(err) | ||
if (rows.length === 1) return resolve(JSON.parse(JSON.stringify(rows))[0]) | ||
else return resolve(JSON.parse(JSON.stringify(rows))) | ||
}) | ||
} else { | ||
connection.query(parameters.command, (err, rows, fields) => { | ||
if (err) return callback(err, null) | ||
callback(null, JSON.parse(JSON.stringify(rows))) | ||
}) | ||
} | ||
}) | ||
} |
{ | ||
"name": "mysql-qbuilder", | ||
"version": "1.4.2", | ||
"version": "1.5.1", | ||
"description": "Query Builder for MySQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
553
README.md
@@ -28,3 +28,2 @@ # mysql-qbuilder | ||
- - [WHERE](#where) | ||
- [All Query Builder Functions](#query-builder) | ||
- [How to Execute](#how-to-execute) | ||
@@ -144,6 +143,12 @@ - [Query Model](#query-model) | ||
.from('tableName') // set Database table | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
}) | ||
``` | ||
@@ -176,5 +181,11 @@ | ||
.from('tableName') // set Database table | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
@@ -224,5 +235,11 @@ ``` | ||
.where('tableName.id' '>' 2) | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
@@ -243,6 +260,12 @@ ``` | ||
.orderBy('name', true) // Then is to be sorted in DESC | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects or just single object | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -262,6 +285,12 @@ | ||
.groupBy('name') | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -280,6 +309,12 @@ | ||
.take(500) // Get first 500 results | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -300,6 +335,12 @@ | ||
.skip(200) // Skip the first 200 results | ||
qBuilder.prepare() | ||
.getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -322,5 +363,12 @@ | ||
.where('id', '=', 10) | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -338,5 +386,12 @@ | ||
.andWhere('name', '=', 'Simon') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -353,5 +408,12 @@ | ||
.andWhere('name', '=', 'Simon') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -368,5 +430,12 @@ | ||
.orWhere('name', '=', 'Simon') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -386,5 +455,12 @@ | ||
.andOrWhere('id', '>', '<' 35) | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -401,5 +477,12 @@ | ||
.whereNull('name') // get all records which the column name is null | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -416,5 +499,12 @@ | ||
.whereNotNull('name') // get all records which the column name is NOT null | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -431,5 +521,12 @@ | ||
.whereBetween('id', [22, 300], 'OR') // Get all records between 22 and 300 | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -446,5 +543,12 @@ | ||
.whereNotBetween('id', [22, 300], 'AND') // Get all records which is not between 22 and 300 | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -461,5 +565,12 @@ | ||
.whereIn('id', [2, 4, 6, 3, 8], 'AND') // Get all records with id 2, 3, 4, 6, 8 | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -476,5 +587,12 @@ | ||
.whereNotIn('id', [2, 4, 6, 3, 8], 'OR') // Get all records which the id is not 2, 3, 4, 6, 8 | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -491,5 +609,12 @@ | ||
.whereColumn('title', 'name', '=', 'OR') // Get all records which the title and name is same | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -506,5 +631,12 @@ | ||
.whereDate('createTime', '=', '2010-04-01') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -521,5 +653,12 @@ | ||
.whereYear('createTime', '=', '2010', 'OR') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -536,5 +675,12 @@ | ||
.whereMonth('createTime', '=', '10', 'OR') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -551,133 +697,12 @@ | ||
.whereDay('createTime', '=', '22', 'OR') | ||
qBuilder.prepare().getResult((err, data) => { | ||
// data is the array of objects | ||
}) | ||
``` | ||
qBuilder.prepare().getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
## All Query Builder Functions | ||
```JavaScript | ||
// Select few columns used String or Array | ||
.select('id, title, someDiff') | ||
// Add few more columns used String or Array IS REQUIRED TO USED SELECT BEFORE USED ADDSELECT | ||
.addSelect(['count', 'name']) | ||
// INSERT clause enter the columns which You set the values on new record | ||
.add(['title', 'count', 'name']) | ||
// Create from clause where you can used if you used select clause | ||
.from('tableName') | ||
// Set the table which You used for that query | ||
.table('tableName') | ||
// delete some record from database IS REQUIRED TO USED FROM OR TABLE BEFORE USED DELETE | ||
.delete() | ||
// UPDATE that columns on the record | ||
.update(['title', 'count', 'name']) | ||
// The last parameter is optional Make join between two tables | ||
.join('roles', 'roles.id = users.roleId' 'INNER') | ||
// Get first 500 records from database | ||
.take(500) | ||
// Skip first 500 records from database | ||
// The OFFSET is working together with LIMIT so first used take function! | ||
.skip(500) | ||
// Order by count column and set to be DESC (if is false then is not used DESC) | ||
.orderBy('count', true) | ||
// Group by count column and don't set to be DESC | ||
.groupBy('count', false) | ||
// Find all records which id column is bigger from 5 second and third parameters are optional | ||
.where('id', '>', 5) | ||
// Find all records which the id column is not equal to 5 | ||
.whereNot('id', '=', 5) | ||
// Find all records which and id is equal to some parameter IS REQUIRED TO USED WHERE BEFORE USED ANDWHERE | ||
.andWhere('id') | ||
// find all records which is by first where or count is equal to 10 IS REQUIRED TO USED WHERE BEFORE USED ANDWHERE | ||
.orWhere('count', '=', 10) | ||
// Find all records which and count bigger from 10 or smaller from 30 | ||
.andOrWhere('count', '>', '<', [10, 30]) | ||
// Find all records which title is null | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereNull('title', 'OR') | ||
// Find all records which title is not null | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whreNotNull('title', 'AND') | ||
// Find all records which the column 'count' is between 10 and 30 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereBetween('count', [10, 30]) | ||
// Find all records which the column 'count' is not between 10 and 30 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereNotBetween('count', [10, 30], 'OR') | ||
// Find all records which is have value like one of the array elements | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereIn('count', [5, 10, 15, 20, 25], 'AND') | ||
// Find all records which is NOT have value like one of the array elements | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereNotIn('title', ['first', 'second', 'third', 'fourth'], 'AND') | ||
// Find all records which create time is bigger from 2010-04-01 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereDate('createTime', '>', '2010-04-01', 'OR') | ||
// Find all records which createTime is have with Day = 22 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereDay('createTime', '=', 22, 'AND') | ||
// Find all records which createTime is have with Month = 10 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereMonth('createTime', '=', 10, 'OR') | ||
// Find all records which createTime is have with Year = 2010 | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereYear('createTime', '=', 2010, 'OR') | ||
// Find all records where title and name is the same | ||
// If before You call the some another where method then is possible to choose between AND and OR (Default = AND) | ||
.whereColumn('title', 'name', '=', 'OR') | ||
}) | ||
qBuilder.prepare().getResult((err, data) => { // Old version less of 1.5.1 | ||
// data is the array of objects or just single object | ||
}) | ||
``` | ||
@@ -710,3 +735,10 @@ | ||
// Get the result of executed query | ||
.getResult((err, data) => { // is return the Array | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
@@ -755,9 +787,16 @@ console.log(err) | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
@@ -774,9 +813,16 @@ | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
@@ -793,9 +839,16 @@ | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
@@ -815,9 +868,16 @@ | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
@@ -838,9 +898,16 @@ | ||
qBuilder.getResult((err, data) => { | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
qBuilder.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
qBuilder.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
console.log(err) | ||
} else { | ||
// make something with data which is result of mysql query execution | ||
} | ||
}) | ||
``` | ||
@@ -859,3 +926,10 @@ | ||
// Get the result of executed query | ||
.getResult((err, data) => { // is return the Array | ||
.getResult() // New version bigger of 1.5.1 | ||
.then(result => { | ||
}) | ||
.catch(err => { | ||
}) | ||
.getResult((err, data) => { // Old version less of 1.5.1 | ||
if (err) { | ||
@@ -866,3 +940,3 @@ console.log(err) | ||
} | ||
}) | ||
}) | ||
``` | ||
@@ -879,2 +953,5 @@ | ||
## Change log | ||
* v1.5.1 | ||
* * Replace the callback of the getResult with Promise | ||
* * If the getResult() return only one element then is return directly object. Not like to now array of one object. | ||
* v1.4.1 | ||
@@ -881,0 +958,0 @@ * * Add one more option for findById and findByFields searching not only by one value |
65000
937
1000