Comparing version 0.8.0 to 0.9.0
@@ -70,2 +70,4 @@ (function(exports) { | ||
stmt: '', | ||
table: '', | ||
sets: {}, | ||
sql: '', | ||
@@ -84,6 +86,2 @@ join: [], | ||
/** | ||
*/ | ||
Monologue.prototype.select = function select( col, tbl ) { | ||
@@ -105,2 +103,3 @@ if( typeof tbl === 'undefined' ) { | ||
this.parts.table = tbl; | ||
this.parts.stmt = `SELECT ${col} FROM ${tbl}`; | ||
@@ -179,5 +178,4 @@ | ||
d.shift(); | ||
d = d.join(','); | ||
col = `(${p}) VALUES ${d}`; | ||
col = `(${p}) VALUES (${d.join('),(')})`; | ||
} | ||
@@ -194,3 +192,4 @@ | ||
col = `(${a.shift()}) VALUES ${a.join(',')}`; | ||
// col = `(${a.shift()}) VALUES ${a.join(',')}`; | ||
col = `(${a.shift()}) VALUES (${a.join('),(')})`; | ||
} | ||
@@ -202,2 +201,3 @@ | ||
this.parts.columns = col; | ||
this.parts.stmt = `INSERT INTO ${tbl} ${col}`; | ||
@@ -225,6 +225,11 @@ | ||
else { | ||
else if( typeof p === 'string' ) { | ||
col = p; | ||
} | ||
else { | ||
throw new Error('Params arg must be string or object'); | ||
} | ||
this.parts.sets = p; | ||
this.parts.table = tbl; | ||
this.parts.stmt = `UPDATE ${tbl} ${col}`; | ||
@@ -235,2 +240,23 @@ | ||
Monologue.prototype.set = function set(p) { | ||
let col = ''; | ||
if( typeof p === 'object' ){//} && typeof this.parts.sets === 'object' ) { | ||
let sets = {...this.parts.sets, ...p }; | ||
col = this.stringify( sets ).join( ', ' ); | ||
col = `SET ${col}`; | ||
this.parts.sets = sets; | ||
} | ||
else if( typeof p === 'string' ) { | ||
// this.parts.sets += `, ${p}`; | ||
col = this.parts.sets; | ||
// [ this.parts.sets, p ].join(', '); | ||
} | ||
this.parts.stmt = `UPDATE ${this.parts.table} ${col}`; | ||
return this; | ||
}; | ||
Monologue.prototype.on_duplicate = function on_duplicate(tbl, p) { | ||
@@ -244,3 +270,3 @@ | ||
*/ | ||
Monologue.prototype.query = function query(stmt) { | ||
@@ -254,2 +280,3 @@ this.parts.stmt = stmt; | ||
/** | ||
* Delete statements | ||
*/ | ||
@@ -536,3 +563,3 @@ | ||
* Takes an object or an array of objects and builds a SQL string | ||
* p: params, s: separator | ||
* p: params, s: separator, ns: NULL seperator (IS/IS NOT), j: joiner | ||
*/ | ||
@@ -566,3 +593,4 @@ | ||
c.push( `(${str})` ); | ||
// c.push( `(${str})` ); | ||
c.push( `${str}` ); | ||
} | ||
@@ -626,3 +654,3 @@ | ||
// } | ||
if( v === 'NULL' ) | ||
if( v === 'NULL' ) | ||
s = ns; | ||
@@ -729,2 +757,2 @@ } | ||
})( typeof window === 'undefined' ? module.exports : window ); | ||
})( typeof window === 'undefined' ? module.exports : window ); |
{ | ||
"name": "monologue", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"description": "Streamlined MySQL query building", | ||
@@ -5,0 +5,0 @@ "main": "./monologue.js", |
@@ -8,5 +8,11 @@ Monologue - Streamlined query building | ||
# Updates | ||
### 0.9.0 | ||
* Added .set() method for augmenting/overwriting SET statements when using UPDATE (INSERT coming soon) | ||
* Breaking change: stringify() no longer automatically encapsulates arrays with (). Must be done by hand now (improves consistency with stringify() on objects) | ||
# Examples | ||
*These are a little out of date due to recent changes but are pretty close to correct* | ||
*These may be slightly little out of date due to recent changes but are pretty close to correct. See test.js for some great examples* | ||
@@ -32,2 +38,4 @@ | ||
// call the SQL wrappers in any order, see below: where, group, where, order | ||
// output: SELECT * FROM users WHERE id IN (1,2,3,4,5,6) AND date_time BETWEEN '2012-09-12' AND '2013-01-20' OR name LIKE 'ro%en' GROUP BY type, hamster ASC ORDER BY id ASC LIMIT 1000, 300 | ||
var mono = monologue() | ||
@@ -43,5 +51,4 @@ .select( "*", "users") | ||
// output: SELECT * FROM users WHERE id IN (1,2,3,4,5,6) AND date_time BETWEEN '2012-09-12' AND '2013-01-20' OR name LIKE 'ro%en' GROUP BY type, hamster ASC ORDER BY id ASC LIMIT 1000, 300 | ||
// the different JOIN methods | ||
mono().ljoin( table, statement ); // LEFT JOIN | ||
@@ -76,3 +83,2 @@ mono().rjoin( table, statement ); // RIGHT JOIN | ||
monologue() | ||
@@ -125,2 +131,3 @@ .select( "*", "users" ) | ||
// UPDATE | ||
@@ -134,3 +141,16 @@ // output: UPDATE users SET email = 'some@email.com', password = 'abcdefg', username = 'yoyo' WHERE id = 23 | ||
// augment/overwrite previous SET statements! very handy for conditional cases | ||
let query = monologue() | ||
.update( "files", { name: 'MyFile.pdf', path: "/path/to/myfile.pdf", status: 1, user_id: 23 }) | ||
.where({ id: 867 }) | ||
if( someCondition ) { | ||
query.set({ status: 0, user_id: 45, hidden: 1 }) | ||
} | ||
// output: UPDATE `files` SET `name` = 'MyFile.pdf', `path` = '/path/to/myfile.pdf', `status` = 0, `user_id` = 45, `hidden` = 1 WHERE `id` = 867 | ||
update.sql() | ||
// DELETE | ||
@@ -213,2 +233,2 @@ // output: DELETE FROM users WHERE first_name = 'me' AND password = '1234' AND username = 'test' | ||
.having('count').gte(42) | ||
.sql(); | ||
.sql(); |
542
228
23538
3