mysql-qbuilder
Advanced tools
Comparing version 1.3.3 to 1.4.1
@@ -11,10 +11,11 @@ const queryBuilder = require('./QueryBuilder') | ||
function addMoreConditions (conditions, andOr) { | ||
andOr = andOr.toLowerCase() | ||
if (andOr !== 'and' && andOr !== 'or') { | ||
throw new Error('The andOr variable is required to be AND or OR!') | ||
} | ||
let method = `${andOr}Where` | ||
let method = checkWhereMethod(andOr) | ||
for (let key in conditions) { | ||
if (conditions.hasOwnProperty(key)) { | ||
queryBuilder[method](key, '=', conditions[key]) | ||
if (Array.isArray(conditions[key])) { | ||
queryBuilder.whereIn(key, conditions[key], andOr) | ||
} else { | ||
queryBuilder[method](key, '=', conditions[key]) | ||
} | ||
} | ||
@@ -24,2 +25,11 @@ } | ||
function checkWhereMethod (andOr) { | ||
andOr = andOr.toLowerCase() | ||
if (andOr !== 'and' && andOr !== 'or') { | ||
throw new Error('The andOr variable is required to be AND or OR!') | ||
} | ||
return `${andOr}Where` | ||
} | ||
/** | ||
@@ -52,4 +62,3 @@ * [setModel description] | ||
throw new Error('The table is not set!') | ||
} | ||
if (typeof params !== 'object') { | ||
} else if (typeof params !== 'object') { | ||
throw new Error('The params is need to be object with key column and value value of column!') | ||
@@ -75,4 +84,3 @@ } | ||
throw new Error('The table is not set!') | ||
} | ||
if (!columns) { | ||
} else if (!columns) { | ||
throw new Error('The columns is not set!') | ||
@@ -94,4 +102,3 @@ } | ||
throw new Error('The table is not set!') | ||
} | ||
if (!columns) { | ||
} else if (!columns) { | ||
throw new Error('The columns is not set!') | ||
@@ -115,4 +122,3 @@ } | ||
throw new Error('The table is not set!') | ||
} | ||
if (!columns) { | ||
} else if (!columns) { | ||
throw new Error('The columns is not set!') | ||
@@ -127,3 +133,3 @@ } | ||
* [Make query for can find record by id on table] | ||
* @param {String} id [The Id of the record] | ||
* @param {Object} id [The Id of the record, Or Array of Ids] | ||
* @param {String} [columns='*'] [All columns which You want to get from database] | ||
@@ -134,13 +140,17 @@ */ | ||
throw new Error('The table is not set!') | ||
} | ||
if (!columns) { | ||
} else if (!columns) { | ||
throw new Error('The columns is not set!') | ||
} | ||
if (!id) { | ||
} else if (!id) { | ||
throw new Error('The id is not set!') | ||
} | ||
queryBuilder.from(_table) | ||
.select(columns) | ||
.where('id', '=', id) | ||
queryBuilder.from(_table).select(columns) | ||
if (Array.isArray(id)) { | ||
for (let i = 0, len = id.length; i < len; i++) { | ||
queryBuilder.orWhere('id', '=', id[0]) | ||
} | ||
} else { | ||
queryBuilder.where('id', '=', id) | ||
} | ||
parameters.prepare() | ||
@@ -158,7 +168,5 @@ } | ||
throw new Error('The table is not set!') | ||
} | ||
if (!columns) { | ||
} else if (!columns) { | ||
throw new Error('The columns is not set!') | ||
} | ||
if (!conditions) { | ||
} else if (!conditions) { | ||
throw new Error('The id is not set!') | ||
@@ -168,6 +176,10 @@ } | ||
let firstKey = Object.keys(conditions)[0] | ||
queryBuilder.from(_table) | ||
.select(columns) | ||
.where(firstKey, '=', conditions[firstKey]) | ||
queryBuilder.from(_table).select(columns) | ||
if (Array.isArray(conditions[firstKey])) { | ||
queryBuilder.whereIn(firstKey, conditions[firstKey]) | ||
} else { | ||
queryBuilder.where(firstKey, '=', conditions[firstKey]) | ||
} | ||
delete conditions[firstKey] | ||
@@ -174,0 +186,0 @@ if (conditions) { |
{ | ||
"name": "mysql-qbuilder", | ||
"version": "1.3.3", | ||
"version": "1.4.1", | ||
"description": "Query Builder for MySQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,3 +10,3 @@ # mysql-qbuilder | ||
[![npm](https://img.shields.io/npm/dt/mysql-qbuilder.svg)]() | ||
## Table of Contents | ||
@@ -785,2 +785,4 @@ | ||
.findById(2, 'email') | ||
// OR | ||
.findById([2, 3, 4], 'email') // Is find records with id 2, 3 or 4 | ||
@@ -806,2 +808,4 @@ qBuilder.getResult((err, data) => { | ||
.findByFields({ 'id': 2, 'username': 'administrator' }, 'email', 'or') | ||
// Or Get all records which is with id 2, 3 or 4 or have username administrator or userNaMe | ||
.findByFields({ 'id': [2, 3, 4], 'username': ['administrator', 'userNaMe'] }, 'email', 'or') | ||
@@ -846,2 +850,4 @@ qBuilder.getResult((err, data) => { | ||
## Change log | ||
* v1.4.1 | ||
* * Add one more option for findById and findByFields searching not only by one value | ||
* v1.3.1 | ||
@@ -848,0 +854,0 @@ * * Add Helper Query functions with for some common cases like: |
63520
990
860