mysql-wrap-production
Advanced tools
Comparing version 0.5.3 to 0.5.4
{ | ||
"name": "mysql-wrap-production", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "Mysql interface and helper functions, wrapping node-mysql", | ||
@@ -5,0 +5,0 @@ "main": "src/mysql-wrap.js", |
@@ -65,3 +65,3 @@ # node-mysql-wrap-production | ||
###query(sqlStatement, \*values, \*callback) | ||
###query(sqlStatement, \*values) | ||
```javascript | ||
@@ -88,3 +88,19 @@ sql.query('SELECT name FROM fruit WHERE color = "yellow"') | ||
###one(sqlStatement, \*values, \*callback) | ||
###queryStream(sqlStatement, \*values) | ||
```javascript | ||
sql.queryStream('SELECT name FROM fruit WHERE color = "yellow"') | ||
.then(function (stream) { | ||
stream.on('data', row => { | ||
console.log(row); | ||
//example output: { name: "banana" } | ||
}); | ||
stream.on('end', () => { | ||
console.log('end of stream';) | ||
}); | ||
}); | ||
``` | ||
###one(sqlStatement, \*values) | ||
Works the same as sql.query except it only returns a single row instead of an array | ||
@@ -94,3 +110,3 @@ of rows. Adds a "LIMIT 1" clause if a LIMIT clause is not allready present in | ||
###select(table, \*whereEqualsObject, \*callback) | ||
###select(table, \*whereEqualsObject) | ||
```javascript | ||
@@ -101,2 +117,17 @@ // equivalent to sql.query('SELECT * FROM fruit WHERE color = "yellow" AND isRipe = "true"') | ||
###selectStream(table, \*whereEqualsObject) | ||
```javascript | ||
sql.selectStream('fruit') | ||
.then(function (stream) { | ||
stream.on('data', row => { | ||
console.log(row); | ||
//example output: { name: "banana" } | ||
}); | ||
stream.on('end', () => { | ||
console.log('end of stream';) | ||
}); | ||
}); | ||
``` | ||
###selectOne(table, \*whereEqualsObject, \*callback) | ||
@@ -103,0 +134,0 @@ Same as sql.select except selectOne returns a single row instead of an array of rows. |
@@ -110,10 +110,10 @@ 'use strict'; | ||
let isSQLReadOrWrite = function (statementRaw) { | ||
return /^SELECT/i.test(statementRaw.trim()) ? 'read' : 'write'; | ||
}; | ||
self.query = function (statementRaw, values) { | ||
let statementObject = getStatementObject(statementRaw); | ||
let readOrWrite = function () { | ||
return /^SELECT/i.test(statementObject.sql.trim()) ? 'read' : 'write'; | ||
}; | ||
return getConnection(readOrWrite()) | ||
return getConnection(isSQLReadOrWrite(statementObject.sql)) | ||
.then(function (conn) { | ||
@@ -134,2 +134,22 @@ return Q.Promise(function (resolve, reject) { | ||
self.queryStream = function (statementRaw, values) { | ||
let statementObject = getStatementObject(statementRaw); | ||
return getConnection(isSQLReadOrWrite(statementObject.sql)) | ||
.then(function (conn) { | ||
let stream = conn.query(statementObject, values || []).stream(); | ||
stream.on('error', err => { | ||
console.error(err); | ||
conn && conn.release && conn.release(); | ||
}); | ||
stream.on('end', () => { | ||
conn && conn.release && conn.release(); | ||
}); | ||
return stream; | ||
}); | ||
}; | ||
self.one = function (statementRaw, values) { | ||
@@ -145,3 +165,3 @@ let statementObject = getStatementObject(statementRaw); | ||
self.select = function (tableRaw, whereEquals) { | ||
let buildSelect = function (tableRaw, whereEquals) { | ||
let statementObject = _.isObject(tableRaw) ? | ||
@@ -151,13 +171,21 @@ tableRaw : { table: tableRaw }; | ||
let values = [statementObject.table].concat(where.values); | ||
let sql = 'SELECT ' + selectedFieldsSQL(statementObject.fields) + ' ' + | ||
'FROM ?? ' + where.sql + ( | ||
statementObject.paginate ? | ||
' ' + paginateLimit(statementObject.paginate) : '' | ||
); | ||
return self.query( | ||
'SELECT ' + selectedFieldsSQL(statementObject.fields) + ' ' + | ||
'FROM ?? ' + where.sql + ( | ||
statementObject.paginate ? | ||
' ' + paginateLimit(statementObject.paginate) : '' | ||
), | ||
values | ||
); | ||
return { sql: sql, values: values }; | ||
}; | ||
self.select = function (tableRaw, whereEquals) { | ||
let query = buildSelect(tableRaw, whereEquals); | ||
return self.query(query.sql, query.values); | ||
}; | ||
self.selectStream = function (tableRaw, whereEquals) { | ||
let query = buildSelect(tableRaw, whereEquals); | ||
return self.queryStream(query.sql, query.values); | ||
}; | ||
self.selectOne = function (tableRaw, whereEquals) { | ||
@@ -164,0 +192,0 @@ let statementObject = _.isObject(tableRaw) ? |
@@ -73,2 +73,30 @@ 'use strict'; | ||
describe('queryStream', function () { | ||
it('should return a readable stream of rows', function (done) { | ||
let expected = [this.a, this.b, this.c]; | ||
this.sql.queryStream('SELECT * FROM `table` ORDER BY `id`') | ||
.then(stream => { | ||
stream.on('data', row => { | ||
chai.assert.deepEqual(row, expected.shift()); | ||
}); | ||
stream.on('end', () => done()); | ||
}).done(); | ||
}); | ||
}); | ||
describe('selectStream', function () { | ||
it('should return a readable stream of rows', function (done) { | ||
this.sql.selectStream('table', { id: this.a.id }) | ||
.then(stream => { | ||
stream.on('data', row => { | ||
chai.assert.deepEqual(row, this.a); | ||
}); | ||
stream.on('end', () => done()); | ||
}).done(); | ||
}); | ||
}); | ||
describe('query', function () { | ||
@@ -75,0 +103,0 @@ it('should select without values array', function (done) { |
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
35693
14
692
187