mysql-simple-query
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "mysql-simple-query", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Simple query wrapper for mysql-promise to make querying, inserting, updating, and deleting easier for developers.", | ||
@@ -36,4 +36,3 @@ "main": "mysqlSimpleQuery.js", | ||
}, | ||
"dependencies": { | ||
} | ||
"dependencies": {} | ||
} |
@@ -10,2 +10,4 @@ const dbQuery = require('./util/dbQuery'); | ||
this.whereStatement = {}; | ||
this.whereLikeStatement = {}; | ||
this.whereLikeCondition = null; | ||
this.groupByStatement = ''; | ||
@@ -41,2 +43,10 @@ this.orderByStatement = ''; | ||
whereLike(key, value, condition) { | ||
this.whereLikeStatement[key] = value; | ||
if (condition) { | ||
this.whereLikeCondition = condition; | ||
} | ||
} | ||
parseWhere() { | ||
@@ -50,2 +60,10 @@ if(!isEmpty(this.whereStatement)) { | ||
parseWhereLike() { | ||
if(!isEmpty(this.whereLikeStatement)) { | ||
return dbQuery.parseWhereLike(this.whereLikeStatement, this.whereLikeCondition); | ||
} | ||
return ''; | ||
} | ||
groupBy(key) { | ||
@@ -93,2 +111,6 @@ if(key) | ||
if(this.parseWhereLike() !== '') { | ||
queryStatement += ` ${this.parseWhereLike()}`; | ||
} | ||
if(this.groupByStatement !== '') { | ||
@@ -95,0 +117,0 @@ queryStatement += ` ${this.groupByStatement}`; |
@@ -58,2 +58,17 @@ const select = (select) => { | ||
const parseWhereLike = (whereObject, orStatement = false) => { | ||
let whereStatement = []; | ||
for (let key of Object.keys(whereObject)) { | ||
const value = whereObject[key]; | ||
whereStatement.push(`${key} LIKE "${value}"`); | ||
} | ||
if (orStatement) { | ||
return `WHERE ${whereStatement.join(' OR ')}`; | ||
} | ||
return `WHERE ${whereStatement.join(' AND ')}`; | ||
}; | ||
const groupBy = (key) => { | ||
@@ -102,2 +117,3 @@ if(key) | ||
parseWhere, | ||
parseWhereLike, | ||
groupBy, | ||
@@ -104,0 +120,0 @@ orderBy, |
@@ -20,2 +20,53 @@ const expect = require('chai').expect; | ||
it('query with like statement', () => { | ||
const test = new mysqlSimpleQuery(); | ||
test.select('*'); | ||
test.from('table'); | ||
test.join('table', 'table_1 = table_2'); | ||
test.whereLike('key', 'value'); | ||
test.groupBy('key'); | ||
test.orderBy('key'); | ||
const testResults = test.query(); | ||
console.log(testResults); | ||
expect(testResults).to.equal('SELECT * FROM table INNER JOIN table ON table_1 = table_2 WHERE key LIKE "value" GROUP BY key ORDER BY key ASC;'); | ||
}); | ||
it('where like with wildcard', () => { | ||
const test = new mysqlSimpleQuery(); | ||
test.select('*'); | ||
test.from('table'); | ||
test.whereLike('key', '%value%'); | ||
const testResults = test.query(); | ||
expect(testResults).to.equal('SELECT * FROM table WHERE key LIKE "%value%";'); | ||
}); | ||
it('where like with multiple statements AND', () => { | ||
const test = new mysqlSimpleQuery(); | ||
test.select('*'); | ||
test.from('table'); | ||
test.whereLike('key', '%value%'); | ||
test.whereLike('key2', '%value2%'); | ||
const testResults = test.query(); | ||
expect(testResults).to.equal('SELECT * FROM table WHERE key LIKE "%value%" AND key2 LIKE "%value2%";'); | ||
}); | ||
it('where like with multiple statements OR', () => { | ||
const test = new mysqlSimpleQuery(); | ||
test.select('*'); | ||
test.from('table'); | ||
test.whereLike('key', '%value%', 'OR'); | ||
test.whereLike('key2', '%value2%'); | ||
const testResults = test.query(); | ||
expect(testResults).to.equal('SELECT * FROM table WHERE key LIKE "%value%" OR key2 LIKE "%value2%";'); | ||
}); | ||
it('insert', () => { | ||
@@ -22,0 +73,0 @@ const test = new mysqlSimpleQuery(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24101
603
12