mysql-wrap-production
Advanced tools
Comparing version 0.7.5 to 0.8.0
module.exports = function (grunt) { | ||
grunt.initConfig({ | ||
mochaTest: { | ||
all: { src: ['test/**/*.js'] } | ||
all: { | ||
options: { reporter: 'dot' }, | ||
src: ['test/**/*.js'] | ||
} | ||
} | ||
@@ -6,0 +9,0 @@ }); |
{ | ||
"name": "mysql-wrap-production", | ||
"version": "0.7.5", | ||
"version": "0.8.0", | ||
"description": "Mysql interface and helper functions, wrapping node-mysql", | ||
@@ -5,0 +5,0 @@ "main": "src/mysql-wrap.js", |
@@ -9,3 +9,3 @@ 'use strict'; | ||
let createMySQLWrap = function (poolCluster, options) { | ||
const createMySQLWrap = (poolCluster, options, connection) => { | ||
options = options || {}; | ||
@@ -15,14 +15,10 @@ | ||
let stripLimit = function (sql) { | ||
return sql.replace(/ LIMIT .*/i, ''); | ||
}; | ||
const stripLimit = sql => sql.replace(/ LIMIT .*/i, ''); | ||
let paginateLimit = function (fig) { | ||
return fig ? | ||
'LIMIT ' + fig.resultsPerPage + ' ' + | ||
'OFFSET ' + ((fig.page - 1) * fig.resultsPerPage) : ''; | ||
}; | ||
const paginateLimit = fig => fig ? | ||
'LIMIT ' + fig.resultsPerPage + ' ' + | ||
'OFFSET ' + ((fig.page - 1) * fig.resultsPerPage) : ''; | ||
let addCalcFoundRows = function (sql) { | ||
let pieces = sql.split(' '); | ||
const addCalcFoundRows = sql => { | ||
const pieces = sql.split(' '); | ||
pieces.splice(1, 0, 'SQL_CALC_FOUND_ROWS'); | ||
@@ -32,4 +28,4 @@ return pieces.join(' '); | ||
let getStatementObject = function (statementOrObject) { | ||
let statement = _.isObject(statementOrObject) ? | ||
const getStatementObject = statementOrObject => { | ||
const statement = _.isObject(statementOrObject) ? | ||
statementOrObject : { | ||
@@ -53,5 +49,5 @@ sql: statementOrObject, | ||
let prepareWhereEquals = function (whereEquals) { | ||
let values = []; | ||
let sql = _.map(whereEquals, function (val, key) { | ||
const prepareWhereEquals = whereEquals => { | ||
const values = []; | ||
const sql = _.map(whereEquals, (val, key) => { | ||
values.push(key, val); | ||
@@ -67,37 +63,26 @@ return '?? = ?'; | ||
let getConnection = function (readOrWrite) { | ||
return Q.Promise(function (resolve, reject) { | ||
const getConnection = readOrWrite => Q.Promise((resolve, reject) => { | ||
if(connection) { | ||
resolve(connection); | ||
} | ||
else { | ||
if(options.replication) { | ||
poolCluster.getConnection( | ||
options.replication[readOrWrite], | ||
function (err, conn) { | ||
if(err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(conn); | ||
} | ||
} | ||
(err, conn) => err ? reject(err) : resolve(conn) | ||
); | ||
} | ||
else { | ||
poolCluster.getConnection(function (err, conn) { | ||
if(err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(conn); | ||
} | ||
}); | ||
poolCluster.getConnection( | ||
(err, conn) => err ? reject(err) : resolve(conn) | ||
); | ||
} | ||
}); | ||
}; | ||
} | ||
}); | ||
let selectedFieldsSQL = function (fields) { | ||
return fields ? fields.join(', ') : '*'; | ||
}; | ||
const selectedFieldsSQL = fields => fields ? fields.join(', ') : '*'; | ||
let prepareInsertRows = function (rowOrRows) { | ||
let values = []; | ||
let fields = _.isArray(rowOrRows) ? | ||
const prepareInsertRows = rowOrRows => { | ||
const values = []; | ||
const fields = _.isArray(rowOrRows) ? | ||
_.keys(_.first(rowOrRows)) : _.keys(rowOrRows); | ||
@@ -107,3 +92,3 @@ | ||
// (because the order of the values array would otherwise be incorrect) | ||
let fieldsSQL = '(' + _.map(fields, function (field) { | ||
const fieldsSQL = '(' + _.map(fields, field => { | ||
values.push(field); | ||
@@ -113,10 +98,8 @@ return '??'; | ||
let processValuesSQL = function (row) { | ||
return '(' + _.map(fields, function (field) { | ||
values.push(row[field]); | ||
return '?'; | ||
}) + ')'; | ||
}; | ||
const processValuesSQL = row => '(' + _.map(fields, field => { | ||
values.push(row[field]); | ||
return '?'; | ||
}) + ')'; | ||
let valuesSQL = _.isArray(rowOrRows) ? | ||
const valuesSQL = _.isArray(rowOrRows) ? | ||
_.map(rowOrRows, processValuesSQL).join(', ') : | ||
@@ -131,29 +114,155 @@ processValuesSQL(rowOrRows); | ||
let isSQLReadOrWrite = function (statementRaw) { | ||
return /^SELECT/i.test(statementRaw.trim()) ? 'read' : 'write'; | ||
const isSQLReadOrWrite = statementRaw => /^SELECT/i.test(statementRaw.trim()) ? | ||
'read' : 'write'; | ||
const runCursor = (q, fig) => { | ||
const orderBy = _.map( | ||
_.isArray(fig.orderBy) ? fig.orderBy : [fig.orderBy], | ||
o => _.isString(o) ? | ||
{ field: o, isAscending: true } : | ||
_.extend( | ||
_.omit(_.clone(o), 'direction'), | ||
{ isAscending: o.direction === 'DESC' ? false : true } | ||
) | ||
); | ||
const isAscending = fig.last && !fig.first ? false : true; | ||
const delimeter = '#'; | ||
const decodeCursor = c => _.map( | ||
new Buffer(c, 'base64') | ||
.toString('ascii').split(delimeter), | ||
(v, i) => orderBy[i].deserialize ? orderBy[i].deserialize(v) : v | ||
); | ||
const encodeCursor = r => new Buffer(_.map( | ||
orderBy, | ||
o => o.serialize ? o.serialize(r[o.field]) : String(r[o.field]) | ||
).join(delimeter)).toString('base64'); | ||
const buildWhereArgs = (values, isGreaterThan) => { | ||
const build = (values, orderBy, isGreaterThan) => { | ||
const sql = _.map( | ||
orderBy, | ||
(o, i) => i === values.length - 1 ? | ||
`${o.field} ${ | ||
(o.isAscending ? isGreaterThan : !isGreaterThan) ? | ||
'>' : '<' | ||
} ?` : | ||
`${o.field} = ?` | ||
).join(' AND '); | ||
let sqls = [sql]; | ||
let mappedValues = [values]; | ||
if(values.length > 1) { | ||
const w = build( | ||
_.initial(values), | ||
_.initial(orderBy), | ||
isGreaterThan | ||
); | ||
sqls = sqls.concat(w.sqls); | ||
mappedValues = mappedValues.concat(w.mappedValues); | ||
} | ||
return { | ||
sqls: sqls, | ||
mappedValues: mappedValues | ||
}; | ||
}; | ||
const w = build(values, orderBy, isGreaterThan); | ||
return [w.sqls.reverse().join(' OR ')] | ||
.concat(_.flatten(w.mappedValues.reverse())); | ||
}; | ||
_.each(orderBy, o => { | ||
q.order( | ||
o.field, | ||
o.isAscending ? isAscending : !isAscending | ||
); | ||
}); | ||
if(fig.after) { | ||
q.where.apply(q, buildWhereArgs(decodeCursor(fig.after), true)); | ||
} | ||
if(fig.before) { | ||
q.where.apply(q, buildWhereArgs(decodeCursor(fig.before), false)); | ||
} | ||
q.limit(isAscending ? fig.first : fig.last); | ||
const query = q.toParam(); | ||
return self.query({ | ||
sql: query.text, | ||
resultCount: true | ||
}, query.values) | ||
.then(resp => { | ||
if(isAscending && fig.last && fig.last < resp.results.length) { | ||
resp.results = resp.results.slice( | ||
resp.results.length - fig.last, | ||
resp.results.length | ||
); | ||
} | ||
else if(!isAscending && fig.last && fig.last < resp.results.length) { | ||
resp.results = resp.results.slice(0, fig.last); | ||
} | ||
if(!isAscending) { | ||
resp.results = resp.results.reverse(); | ||
} | ||
return resp; | ||
}) | ||
.then(resp => ({ | ||
resultCount: resp.resultCount, | ||
pageInfo: { | ||
hasPreviousPage: fig.last ? resp.resultCount > fig.last : false, | ||
hasNextPage: fig.first ? resp.resultCount > fig.first : false | ||
}, | ||
edges: _.map( | ||
resp.results, | ||
r => ({ node: r, cursor: encodeCursor(r) }) | ||
) | ||
})); | ||
}; | ||
self.build = () => { | ||
const wrap = method => { | ||
return () => { | ||
let s = squel[method](); | ||
const wrap = method => () => { | ||
const s = squel[method](); | ||
s.run = fig => { | ||
let p = s.toParam(); | ||
s.run = fig => { | ||
fig = fig || {}; | ||
if(fig.cursor) { | ||
return runCursor(s, fig.cursor); | ||
} | ||
else { | ||
const p = s.toParam(); | ||
return self.query( | ||
_.extend({ sql: p.text }, fig || {}), | ||
_.extend({ sql: p.text }, fig), | ||
p.values | ||
); | ||
}; | ||
} | ||
}; | ||
s.one = fig => { | ||
let p = s.toParam(); | ||
return self.one( | ||
_.extend({ sql: p.text }, fig || {}), | ||
p.values | ||
); | ||
}; | ||
s.one = fig => { | ||
const p = s.toParam(); | ||
return self.one( | ||
_.extend({ sql: p.text }, fig || {}), | ||
p.values | ||
); | ||
}; | ||
s.whereIfDefined = (sql, value) => { | ||
if(value !== undefined) { | ||
s.where(sql, value); | ||
} | ||
return s; | ||
}; | ||
return s; | ||
}; | ||
@@ -171,62 +280,74 @@ | ||
self.query = function (statementRaw, values) { | ||
let statementObject = getStatementObject(statementRaw); | ||
self.connection = () => getConnection('write') | ||
.then(conn => { | ||
let sql = createMySQLWrap(null, options, conn); | ||
sql.release = () => conn && conn.release && conn.release(); | ||
return sql; | ||
}); | ||
const finishedWithConnection = conn => { | ||
if(!connection) { | ||
conn && conn.release && conn.release(); | ||
} | ||
}; | ||
self.query = (statementRaw, values) => { | ||
const statementObject = getStatementObject(statementRaw); | ||
return getConnection(isSQLReadOrWrite(statementObject.sql)) | ||
.then(function (conn) { | ||
return Q.Promise(function (resolve, reject) { | ||
conn.query(statementObject, values || [], function (err, rows) { | ||
if(err) { | ||
conn.release(); | ||
reject(err); | ||
} | ||
else if (statementObject.paginate || statementObject.resultCount) { | ||
conn.query('SELECT FOUND_ROWS() AS count', function (err, result) { | ||
conn.release(); | ||
if(err) { | ||
reject(err); | ||
} | ||
else if(statementObject.paginate){ | ||
resolve({ | ||
resultCount: _.first(result).count, | ||
pageCount: Math.ceil( | ||
_.first(result).count / | ||
statementObject.paginate.resultsPerPage | ||
), | ||
currentPage: statementObject.paginate.page, | ||
results: rows | ||
}); | ||
} | ||
else if(statementObject.resultCount) { | ||
resolve({ | ||
resultCount: _.first(result).count, | ||
results: rows | ||
}); | ||
} | ||
}); | ||
} | ||
else { | ||
conn.release(); | ||
resolve(rows); | ||
} | ||
}); | ||
.then(conn => Q.Promise((resolve, reject) => { | ||
conn.query(statementObject, values || [], (err, rows) => { | ||
if(err) { | ||
finishedWithConnection(conn); | ||
reject(err); | ||
} | ||
else if ( | ||
statementObject.paginate || | ||
statementObject.resultCount | ||
) { | ||
conn.query('SELECT FOUND_ROWS() AS count', (err, result) => { | ||
finishedWithConnection(conn); | ||
if(err) { | ||
reject(err); | ||
} | ||
else if(statementObject.paginate){ | ||
resolve({ | ||
resultCount: _.first(result).count, | ||
pageCount: Math.ceil( | ||
_.first(result).count / | ||
statementObject.paginate.resultsPerPage | ||
), | ||
currentPage: statementObject.paginate.page, | ||
results: rows | ||
}); | ||
} | ||
else if(statementObject.resultCount) { | ||
resolve({ | ||
resultCount: _.first(result).count, | ||
results: rows | ||
}); | ||
} | ||
}); | ||
} | ||
else { | ||
finishedWithConnection(conn); | ||
resolve(rows); | ||
} | ||
}); | ||
}); | ||
})); | ||
}; | ||
self.queryStream = function (statementRaw, values) { | ||
let statementObject = getStatementObject(statementRaw); | ||
self.queryStream = (statementRaw, values) => { | ||
const statementObject = getStatementObject(statementRaw); | ||
return getConnection(isSQLReadOrWrite(statementObject.sql)) | ||
.then(function (conn) { | ||
let stream = conn.query(statementObject, values || []).stream(); | ||
.then(conn => { | ||
const stream = conn.query(statementObject, values || []).stream(); | ||
stream.on('error', err => { | ||
console.error(err); | ||
conn && conn.release && conn.release(); | ||
finishedWithConnection(conn); | ||
}); | ||
stream.on('end', () => { | ||
conn && conn.release && conn.release(); | ||
}); | ||
stream.on('end', () => finishedWithConnection(conn)); | ||
@@ -237,18 +358,16 @@ return stream; | ||
self.one = function (statementRaw, values) { | ||
let statementObject = getStatementObject(statementRaw); | ||
self.one = (statementRaw, values) => { | ||
const statementObject = getStatementObject(statementRaw); | ||
statementObject.sql = stripLimit(statementObject.sql) + ' LIMIT 1'; | ||
return self.query(statementObject, values) | ||
.then(function (rows) { | ||
return _.first(rows) || null; | ||
}); | ||
.then(rows => _.first(rows) || null); | ||
}; | ||
let buildSelect = function (tableRaw, whereEquals) { | ||
let statementObject = _.isObject(tableRaw) ? | ||
const buildSelect = (tableRaw, whereEquals) => { | ||
const statementObject = _.isObject(tableRaw) ? | ||
tableRaw : { table: tableRaw }; | ||
let where = prepareWhereEquals(whereEquals); | ||
let values = [statementObject.table].concat(where.values); | ||
let sql = 'SELECT ' + selectedFieldsSQL(statementObject.fields) + ' ' + | ||
const where = prepareWhereEquals(whereEquals); | ||
const values = [statementObject.table].concat(where.values); | ||
const sql = 'SELECT ' + selectedFieldsSQL(statementObject.fields) + ' ' + | ||
'FROM ?? ' + where.sql + ( | ||
@@ -262,17 +381,17 @@ statementObject.paginate ? | ||
self.select = function (tableRaw, whereEquals) { | ||
let query = buildSelect(tableRaw, whereEquals); | ||
self.select = (tableRaw, whereEquals) => { | ||
const query = buildSelect(tableRaw, whereEquals); | ||
return self.query(query.sql, query.values); | ||
}; | ||
self.selectStream = function (tableRaw, whereEquals) { | ||
let query = buildSelect(tableRaw, whereEquals); | ||
self.selectStream = (tableRaw, whereEquals) => { | ||
const query = buildSelect(tableRaw, whereEquals); | ||
return self.queryStream(query.sql, query.values); | ||
}; | ||
self.selectOne = function (tableRaw, whereEquals) { | ||
let statementObject = _.isObject(tableRaw) ? | ||
self.selectOne = (tableRaw, whereEquals) => { | ||
const statementObject = _.isObject(tableRaw) ? | ||
tableRaw : { table: tableRaw }; | ||
let where = prepareWhereEquals(whereEquals); | ||
let values = [statementObject.table].concat(where.values); | ||
const where = prepareWhereEquals(whereEquals); | ||
const values = [statementObject.table].concat(where.values); | ||
@@ -286,4 +405,4 @@ return self.one( | ||
self.insert = function (table, rowOrRows) { | ||
let rows = prepareInsertRows(rowOrRows); | ||
self.insert = (table, rowOrRows) => { | ||
const rows = prepareInsertRows(rowOrRows); | ||
return self.query( | ||
@@ -295,4 +414,4 @@ 'INSERT INTO ?? ' + rows.sql, | ||
self.replace = function (table, rowRaw, callback) { | ||
let row = prepareInsertRows(rowRaw); | ||
self.replace = (table, rowRaw, callback) => { | ||
const row = prepareInsertRows(rowRaw); | ||
return self.query( | ||
@@ -304,10 +423,10 @@ 'REPLACE INTO ?? ' + row.sql, | ||
self.save = function (table, rowOrRows) { | ||
let rows = _.isArray(rowOrRows) ? rowOrRows : [rowOrRows]; | ||
self.save = (table, rowOrRows) => { | ||
const rows = _.isArray(rowOrRows) ? rowOrRows : [rowOrRows]; | ||
let prepareSaveRows = function () { | ||
let insertRow = prepareInsertRows(rows); | ||
let setValues = []; | ||
const prepareSaveRows = () => { | ||
const insertRow = prepareInsertRows(rows); | ||
const setValues = []; | ||
let setSQL = _.map(_.first(rows), function (val, key) { | ||
const setSQL = _.map(_.first(rows), (val, key) => { | ||
setValues.push(key, key); | ||
@@ -323,3 +442,3 @@ return '?? = VALUES(??)'; | ||
let row = prepareSaveRows(); | ||
const row = prepareSaveRows(); | ||
@@ -332,6 +451,6 @@ return self.query( | ||
self.update = function (table, setData, whereEquals) { | ||
let prepareSetRows = function (setData) { | ||
let values = []; | ||
let sql = ' SET ' + _.map(setData, function (val, key) { | ||
self.update = (table, setData, whereEquals) => { | ||
const prepareSetRows = setData => { | ||
const values = []; | ||
const sql = ' SET ' + _.map(setData, (val, key) => { | ||
values.push(key, val); | ||
@@ -343,11 +462,11 @@ return '?? = ?'; | ||
let set = prepareSetRows(setData); | ||
let where = prepareWhereEquals(whereEquals); | ||
let values = [table].concat(set.values).concat(where.values); | ||
const set = prepareSetRows(setData); | ||
const where = prepareWhereEquals(whereEquals); | ||
const values = [table].concat(set.values).concat(where.values); | ||
return self.query('UPDATE ??' + set.sql + where.sql, values); | ||
}; | ||
self.delete = function (table, whereEquals) { | ||
let where = prepareWhereEquals(whereEquals); | ||
let values = [table].concat(where.values); | ||
self.delete = (table, whereEquals) => { | ||
const where = prepareWhereEquals(whereEquals); | ||
const values = [table].concat(where.values); | ||
return self.query('DELETE FROM ?? ' + where.sql, values); | ||
@@ -354,0 +473,0 @@ }; |
780
test/test.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
const chai = require('chai'); | ||
const expect = require('chai').expect; | ||
const config = require('../config'); | ||
@@ -11,13 +12,11 @@ const mysql = require('mysql'); | ||
describe('mysqlWrap', function () { | ||
before(function (done) { | ||
let that = this; | ||
describe('mysqlWrap', () => { | ||
before(done => { | ||
this.stripIds = data => _.isArray(data) ? | ||
_.map(data, this.stripIds) : _.omit(data, 'id'); | ||
that.stripIds = data => _.isArray(data) ? | ||
_.map(data, that.stripIds) : _.omit(data, 'id'); | ||
let pool = mysql.createPool(config.mysql); | ||
that.sql = createNodeMySQL(pool); | ||
pool.getConnection(function (err, conn) { | ||
that.masterConn = conn; | ||
this.sql = createNodeMySQL(pool); | ||
pool.getConnection((err, conn) => { | ||
this.masterConn = conn; | ||
done(); | ||
@@ -27,23 +26,20 @@ }); | ||
beforeEach(function (done) { | ||
let that = this; | ||
that.masterConn.query('TRUNCATE TABLE `table`', function (err, res) { | ||
that.a = { id: 1, unique: 'a', field: 'foo' }; | ||
that.b = { id: 2, unique: 'b', field: 'bar' }; | ||
that.c = { id: 3, unique: 'c', field: 'foo' }; | ||
that.masterConn.query( | ||
beforeEach(done => { | ||
this.masterConn.query('TRUNCATE TABLE `table`', (err, res) => { | ||
this.a = { id: 1, unique: 'a', field: 'foo' }; | ||
this.b = { id: 2, unique: 'b', field: 'bar' }; | ||
this.c = { id: 3, unique: 'c', field: 'foo' }; | ||
this.masterConn.query( | ||
'INSERT INTO `table` (`unique`, `field`) ' + | ||
'VALUES ' + _.map([that.a, that.b, that.c], function (row) { | ||
'VALUES ' + _.map([this.a, this.b, this.c], row => { | ||
return '("' + row.unique + '", "' + row.field + '")'; | ||
}).join(', '), | ||
function (err, res) { | ||
that.masterConn.query( | ||
(err, res) => { | ||
this.masterConn.query( | ||
'TRUNCATE TABLE `table2`', | ||
function (err, res) { | ||
that.masterConn.query( | ||
(err, res) => { | ||
this.masterConn.query( | ||
'INSERT INTO `table2` (`field`) ' + | ||
'VALUES ("bar")', | ||
function (err) { | ||
done(); | ||
} | ||
err => done() | ||
); | ||
@@ -57,5 +53,372 @@ } | ||
describe('build', function () { | ||
it('should return query generator', function (done) { | ||
this.sql.build().select().from('table') | ||
describe('connection', () => { | ||
it('should get a single connection', done => { | ||
this.sql.connection() | ||
.then(c => { | ||
return Q.all(_.map( | ||
_.range(2), | ||
() => c.query('SELECT CONNECTION_ID()') | ||
)) | ||
.then(resp => { | ||
expect(resp[0]).to.deep.equal(resp[1]); | ||
c.release(); | ||
done(); | ||
}); | ||
}).done(); | ||
}); | ||
}); | ||
describe('build', () => { | ||
before(done => { | ||
this.rowsToEdges = (rows, fields) => _.map(rows, r => ({ | ||
node: r, | ||
cursor: this.toCursor(r, fields || ['id']) | ||
})); | ||
this.toCursor = (r, fields) => new Buffer(_.map( | ||
_.isArray(fields) ? fields : [fields], | ||
f => String(r[f]) | ||
).join('#')).toString('base64'); | ||
this.cursorFig = od => ({ | ||
cursor: _.extend({ orderBy: 'id' }, od) | ||
}); | ||
done(); | ||
}); | ||
it('should have cursor option', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 100 | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.a, this.b, this.c]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle orderBy with direction', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 100, | ||
orderBy: { field: 'id', direction: 'DESC' } | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.c, this.b, this.a]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle orderBy with serialization', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 100, | ||
orderBy: { | ||
field: 'id', | ||
serialize: v => String(v + 1) | ||
} | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: _.map( | ||
[this.a, this.b, this.c], | ||
r => ({ | ||
node: r, | ||
cursor: this.toCursor({ id: r.id + 1 }, 'id') | ||
}) | ||
) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle orderBy with deserialization', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 100, | ||
after: this.toCursor(this.a, 'id'), | ||
orderBy: { | ||
field: 'id', | ||
deserialize: v => Number(v) + 1 | ||
} | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 1, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.c]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with "first" field', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 1 | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: true, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.a]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with the "last" field', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
last: 1 | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: true | ||
}, | ||
edges: this.rowsToEdges([this.c]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should enable next page selection with the "after" field', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 100, | ||
after: this.toCursor(this.a, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.b, this.c]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should enable previous page selection with the "before" field', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
last: 100, | ||
before: this.toCursor(this.c, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.a, this.b]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with "first" and "after"', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 1, | ||
after: this.toCursor(this.a, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: true, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.b]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with "last" and "after"', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
last: 1, | ||
after: this.toCursor(this.a, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: true | ||
}, | ||
edges: this.rowsToEdges([this.c]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with "first" and "before"', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
first: 1, | ||
before: this.toCursor(this.c, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: true, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.a]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should limit with "last" and "before"', done => { | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
last: 1, | ||
before: this.toCursor(this.c, 'id') | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: true | ||
}, | ||
edges: this.rowsToEdges([this.b]) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle compound orderBy', done => { | ||
const orderBy = ['field', 'id']; | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
orderBy: orderBy, | ||
first: 100 | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.b, this.a, this.c], orderBy) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle compound orderBy with direction', done => { | ||
const orderBy = [ | ||
{ field: 'field' }, | ||
{ field: 'id', direction: 'DESC' } | ||
]; | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
orderBy: orderBy, | ||
first: 100 | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 3, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges( | ||
[this.b, this.c, this.a], | ||
_.map(orderBy, o => o.field) | ||
) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle compound orderBy with complex fig', done => { | ||
const orderBy = ['field', 'id']; | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
orderBy: ['field', 'id'], | ||
first: 2, | ||
before: this.toCursor(this.c, orderBy), | ||
after: this.toCursor(this.b, orderBy) | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 1, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges([this.a], orderBy) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should handle compound orderBy with complex fig with direction', done => { | ||
const orderBy = [ | ||
{ field: 'field' }, | ||
{ field: 'id', direction: 'DESC' } | ||
]; | ||
this.sql.build().select().from('`table`').run(this.cursorFig({ | ||
orderBy: orderBy, | ||
first: 2, | ||
before: this.toCursor(this.a, _.map(orderBy, o => o.field)) | ||
})) | ||
.then(resp => { | ||
expect(resp).to.deep.equal({ | ||
resultCount: 2, | ||
pageInfo: { | ||
hasNextPage: false, | ||
hasPreviousPage: false | ||
}, | ||
edges: this.rowsToEdges( | ||
[this.b, this.c], | ||
_.map(orderBy, o => o.field) | ||
) | ||
}); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should have whereIfDefined method', done => { | ||
this.sql.build().select().from('`table`') | ||
.whereIfDefined('id = ?', undefined).run() | ||
.then(resp => { | ||
expect(resp).to.have.deep.members([this.a, this.b, this.c]); | ||
return this.sql.build().select().from('`table`') | ||
.whereIfDefined('id = ?', 0).run(); | ||
}) | ||
.then(resp => { | ||
expect(resp).to.deep.equal([]); | ||
done(); | ||
}).done(); | ||
}); | ||
it('should return query generator', done => { | ||
this.sql.build().select().from('`table`') | ||
.where('field = ?', this.b.field).run() | ||
@@ -68,4 +431,4 @@ .then(resp => { | ||
it('should be able to pass query options through "run" command', function (done) { | ||
this.sql.build().select().from('table') | ||
it('should be able to pass query options through "run" command', done => { | ||
this.sql.build().select().from('`table`') | ||
.where('id = ?', this.b.id).run({ resultCount: true }) | ||
@@ -84,4 +447,4 @@ .then(resp => { | ||
it('should be invokable through a "one" command', function (done) { | ||
this.sql.build().select().from('table') | ||
it('should be invokable through a "one" command', done => { | ||
this.sql.build().select().from('`table`') | ||
.where('id = ?', this.b.id).one() | ||
@@ -95,4 +458,4 @@ .then(resp => { | ||
describe('queryStream', function () { | ||
it('should return a readable stream of rows', function (done) { | ||
describe('queryStream', () => { | ||
it('should return a readable stream of rows', done => { | ||
let expected = [this.a, this.b, this.c]; | ||
@@ -111,4 +474,4 @@ | ||
describe('selectStream', function () { | ||
it('should return a readable stream of rows', function (done) { | ||
describe('selectStream', () => { | ||
it('should return a readable stream of rows', done => { | ||
this.sql.selectStream('table', { id: this.a.id }) | ||
@@ -125,27 +488,22 @@ .then(stream => { | ||
describe('query', function () { | ||
it('should select without values array', function (done) { | ||
let that = this; | ||
that.sql.query('SELECT * FROM `table`') | ||
.then(function (rows) { | ||
chai.assert.sameDeepMembers(rows, [that.a, that.b, that.c]); | ||
describe('query', () => { | ||
it('should select without values array', done => { | ||
this.sql.query('SELECT * FROM `table`') | ||
.then(rows => { | ||
chai.assert.sameDeepMembers(rows, [this.a, this.b, this.c]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have variable parameters using "?"', function (done) { | ||
let that = this; | ||
that.sql.query('SELECT * FROM `table` WHERE id = ?', [2]) | ||
.then(function (rows) { | ||
chai.assert.deepEqual(rows, [that.b]); | ||
it('should have variable parameters using "?"', done => { | ||
this.sql.query('SELECT * FROM `table` WHERE id = ?', [2]) | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [this.b]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have table/field parameters using "??"', function (done) { | ||
let that = this; | ||
that.sql.query('SELECT ?? FROM `table`', ['unique']) | ||
.then(function (rows) { | ||
it('should have table/field parameters using "??"', done => { | ||
this.sql.query('SELECT ?? FROM `table`', ['unique']) | ||
.then(rows => { | ||
chai.assert.sameDeepMembers(rows, [ | ||
@@ -157,10 +515,8 @@ { unique: 'a' }, | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should propogate stack trace to application code', function (done) { | ||
let that = this; | ||
that.sql.query('SELECT wrong FROM `table`') | ||
.catch(function (err) { | ||
it('should propogate stack trace to application code', done => { | ||
this.sql.query('SELECT wrong FROM `table`') | ||
.catch(err => { | ||
chai.assert.ok(/test\.js/.test(err.stack)); | ||
@@ -171,24 +527,21 @@ done(); | ||
it('should be case insensitive', function (done) { | ||
let that = this; | ||
that.sql.query('sElEcT * FRoM `table` Where id = ?', [3]) | ||
.then(function (rows) { | ||
chai.assert.deepEqual(rows, [that.c]); | ||
it('should be case insensitive', done => { | ||
this.sql.query('sElEcT * FRoM `table` Where id = ?', [3]) | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [this.c]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should insert', function (done) { | ||
let that = this; | ||
that.sql.query( | ||
it('should insert', done => { | ||
this.sql.query( | ||
'INSERT INTO `table` (`unique`, `field`) ' + | ||
'VALUES ("testUniqueValue", "testFieldValue")' | ||
) | ||
.then(function (res) { | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 1, 'affectedRows'); | ||
chai.assert.strictEqual(res.insertId, 4, 'insertId'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE id = 4', | ||
function (err, rows) { | ||
(err, rows) => { | ||
chai.assert.deepEqual(rows, [{ | ||
@@ -202,18 +555,16 @@ id: 4, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should update', function (done) { | ||
let that = this; | ||
that.sql.query( | ||
it('should update', done => { | ||
this.sql.query( | ||
'UPDATE `table` SET `field` = "edit" ' + | ||
'WHERE `field` = "foo"' | ||
) | ||
.then(function (res) { | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 2, 'affectedRows'); | ||
chai.assert.strictEqual(res.changedRows, 2, 'changedRows'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `field` = "edit"', | ||
function (err, rows) { | ||
(err, rows) => { | ||
chai.assert.sameDeepMembers(rows, [ | ||
@@ -226,14 +577,12 @@ { id: 1, unique: 'a', field: 'edit' }, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should delete', function (done) { | ||
let that = this; | ||
that.sql.query('DELETE FROM `table` WHERE `field` = "foo"') | ||
.then(function (res) { | ||
it('should delete', done => { | ||
this.sql.query('DELETE FROM `table` WHERE `field` = "foo"') | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 2, 'affectedRows'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `field` = "foo"', | ||
function (err, rows) { | ||
(err, rows) => { | ||
chai.assert.deepEqual(rows, [], 'fields deleted'); | ||
@@ -243,9 +592,7 @@ done(); | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to nest join', function (done) { | ||
let that = this; | ||
that.sql.query({ | ||
it('should have option to nest join', done => { | ||
this.sql.query({ | ||
sql: 'SELECT * FROM `table` ' + | ||
@@ -256,3 +603,3 @@ 'INNER JOIN `table2` ' + | ||
}) | ||
.then(function (rows) { | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [{ | ||
@@ -270,9 +617,7 @@ table: { | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to paginate', function (done) { | ||
let that = this; | ||
that.sql.query({ | ||
it('should have option to paginate', done => { | ||
this.sql.query({ | ||
sql: 'SELECT * FROM `table`', | ||
@@ -284,3 +629,3 @@ paginate: { | ||
}) | ||
.then(function (resp) { | ||
.then(resp => { | ||
chai.assert.deepEqual(_.omit(resp, 'results'), { | ||
@@ -291,51 +636,43 @@ resultCount: 3, | ||
}); | ||
chai.assert.sameDeepMembers(resp.results, [that.a, that.b]); | ||
chai.assert.sameDeepMembers(resp.results, [this.a, this.b]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to include result count', function (done) { | ||
let that = this; | ||
that.sql.query({ | ||
it('should have option to include result count', done => { | ||
this.sql.query({ | ||
sql: 'SELECT * FROM `table` LIMIT 2', | ||
resultCount: true | ||
}) | ||
.then(function (resp) { | ||
.then(resp => { | ||
chai.assert.deepEqual(_.omit(resp, 'results'), { | ||
resultCount: 3 | ||
}); | ||
chai.assert.sameDeepMembers(resp.results, [that.a, that.b]); | ||
chai.assert.sameDeepMembers(resp.results, [this.a, this.b]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('one', function () { | ||
it('should select a single row', function (done) { | ||
let that = this; | ||
that.sql.one('SELECT * FROM `table` WHERE id = 1') | ||
.then(function (row) { | ||
chai.assert.deepEqual(row, that.a); | ||
describe('one', () => { | ||
it('should select a single row', done => { | ||
this.sql.one('SELECT * FROM `table` WHERE id = 1') | ||
.then(row => { | ||
chai.assert.deepEqual(row, this.a); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('select', function () { | ||
it('should select by table and basic where clause', function (done) { | ||
let that = this; | ||
that.sql.select('table', { id: 3, field: 'foo' }) | ||
.then(function (rows) { | ||
chai.assert.deepEqual(rows, [that.c]); | ||
describe('select', () => { | ||
it('should select by table and basic where clause', done => { | ||
this.sql.select('table', { id: 3, field: 'foo' }) | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [this.c]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to paginate', function (done) { | ||
let that = this; | ||
that.sql.select({ | ||
it('should have option to paginate', done => { | ||
this.sql.select({ | ||
table: 'table', | ||
@@ -347,52 +684,44 @@ paginate: { | ||
}) | ||
.then(function (rows) { | ||
chai.assert.deepEqual(rows, [that.a , that.b]); | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [this.a , this.b]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to select field', function (done) { | ||
let that = this; | ||
that.sql.select({ table: 'table', fields: ['id'] }) | ||
.then(function (rows) { | ||
it('should have option to select field', done => { | ||
this.sql.select({ table: 'table', fields: ['id'] }) | ||
.then(rows => { | ||
chai.assert.deepEqual(rows, [{ id: 1 }, { id: 2 }, { id: 3 }]); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('selectOne', function () { | ||
it('should select single row by table and basic where clause', function (done) { | ||
let that = this; | ||
that.sql.selectOne('table', { field: 'foo' }) | ||
.then(function (row) { | ||
chai.assert.deepEqual(row, that.a); | ||
describe('selectOne', () => { | ||
it('should select single row by table and basic where clause', done => { | ||
this.sql.selectOne('table', { field: 'foo' }) | ||
.then(row => { | ||
chai.assert.deepEqual(row, this.a); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should have option to select fields', function (done) { | ||
let that = this; | ||
that.sql.selectOne({ table: 'table', fields: ['id'] }) | ||
.then(function (row) { | ||
it('should have option to select fields', done => { | ||
this.sql.selectOne({ table: 'table', fields: ['id'] }) | ||
.then(row => { | ||
chai.assert.deepEqual(row, { id: 1 }); | ||
done(); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('insert', function () { | ||
it('should insert a single row', function (done) { | ||
let that = this; | ||
that.sql.insert('table', { unique: 'd', field: 'baz' }) | ||
.then(function (res) { | ||
describe('insert', () => { | ||
it('should insert a single row', done => { | ||
this.sql.insert('table', { unique: 'd', field: 'baz' }) | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 1, 'affectedRows'); | ||
chai.assert.strictEqual(res.insertId, 4, 'insertId'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `id` = 4', | ||
function (err, rows) { | ||
(err, rows) => { | ||
chai.assert.deepEqual( | ||
@@ -406,18 +735,16 @@ rows, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should insert multiple rows', function (done) { | ||
let that = this; | ||
that.sql.insert('table', [ | ||
it('should insert multiple rows', done => { | ||
this.sql.insert('table', [ | ||
{ unique: 'd', field: 'new' }, | ||
{ unique: 'e', field: 'new' } | ||
]) | ||
.then(function (res) { | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 2, 'affectedRows'); | ||
chai.assert.strictEqual(res.insertId, 4, 'insertId'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `field` = "new"', | ||
function (err, rows) { | ||
(err, rows) => { | ||
chai.assert.deepEqual(rows, [ | ||
@@ -430,17 +757,15 @@ { id: 4, unique: 'd', field: 'new' }, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('replace', function () { | ||
it('should insert row', function (done) { | ||
let that = this; | ||
that.sql.replace('table', { unique: 'd', field: 'baz' }) | ||
.then(function (res) { | ||
describe('replace', () => { | ||
it('should insert row', done => { | ||
this.sql.replace('table', { unique: 'd', field: 'baz' }) | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 1, 'affectedRows'); | ||
chai.assert.strictEqual(res.insertId, 4, 'insertId'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `id` = 4', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.deepEqual(res, [ | ||
@@ -452,13 +777,11 @@ { id: 4, unique: 'd', field: 'baz' } | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should replace row with same unique key', function (done) { | ||
let that = this; | ||
that.sql.replace('table', { unique: 'c', field: 'replaced' }) | ||
.then(function () { | ||
that.masterConn.query( | ||
it('should replace row with same unique key', done => { | ||
this.sql.replace('table', { unique: 'c', field: 'replaced' }) | ||
.then(() => { | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `unique` = "c"', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.deepEqual( | ||
@@ -472,17 +795,15 @@ res, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('save', function () { | ||
it('should insert row if does not exist', function (done) { | ||
let that = this; | ||
that.sql.save('table', { unique: 'd', field: 'baz' }) | ||
.then(function (res) { | ||
describe('save', () => { | ||
it('should insert row if does not exist', done => { | ||
this.sql.save('table', { unique: 'd', field: 'baz' }) | ||
.then(res => { | ||
chai.assert.strictEqual(res.affectedRows, 1, 'returns affectedRows'); | ||
chai.assert.strictEqual(res.insertId, 4, 'returns insert id'); | ||
that.masterConn.query( | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `id` = 4', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.deepEqual( | ||
@@ -495,13 +816,11 @@ res, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should update row if exists by unique constraint', function (done) { | ||
let that = this; | ||
that.sql.save('table', { unique: 'c', field: 'update' }) | ||
.then(function () { | ||
that.masterConn.query( | ||
it('should update row if exists by unique constraint', done => { | ||
this.sql.save('table', { unique: 'c', field: 'update' }) | ||
.then(() => { | ||
this.masterConn.query( | ||
'SELECT * FROM `table` WHERE `unique` = "c"', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.deepEqual( | ||
@@ -514,8 +833,6 @@ res, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
it('should handle bulk save', function (done) { | ||
let that = this; | ||
it('should handle bulk save', done => { | ||
let rows = [ | ||
@@ -526,10 +843,10 @@ { unique: 'a', field: 'edit-a' }, | ||
]; | ||
that.sql.save('table', rows) | ||
.then(function () { | ||
that.masterConn.query( | ||
this.sql.save('table', rows) | ||
.then(() => { | ||
this.masterConn.query( | ||
'SELECT * FROM `table`', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.sameDeepMembers( | ||
that.stripIds(res), | ||
that.stripIds(rows.concat([that.c])) | ||
this.stripIds(res), | ||
this.stripIds(rows.concat([this.c])) | ||
); | ||
@@ -539,15 +856,13 @@ done(); | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('update', function () { | ||
it('should update row', function (done) { | ||
let that = this; | ||
that.sql.update('table', { field: 'edit', unique: 'd' }, { id: 1 }) | ||
.then(function (res) { | ||
that.masterConn.query( | ||
describe('update', () => { | ||
it('should update row', done => { | ||
this.sql.update('table', { field: 'edit', unique: 'd' }, { id: 1 }) | ||
.then(res => { | ||
this.masterConn.query( | ||
'SELECT * FROM `table`', | ||
function (err, res) { | ||
(err, res) => { | ||
chai.assert.deepEqual(res, [ | ||
@@ -561,23 +876,20 @@ { id: 1, unique: 'd', field: 'edit' }, | ||
); | ||
}) | ||
.done(); | ||
}).done(); | ||
}); | ||
}); | ||
describe('delete', function () { | ||
it('should delete rows by where equals config', function (done) { | ||
let that = this; | ||
that.sql.delete('table', { field: 'foo' }) | ||
.then(function (res) { | ||
that.masterConn.query( | ||
describe('delete', () => { | ||
it('should delete rows by where equals config', done => { | ||
this.sql.delete('table', { field: 'foo' }) | ||
.then(res => { | ||
this.masterConn.query( | ||
'SELECT * FROM `table`', | ||
function (err, res) { | ||
chai.assert.deepEqual(res, [that.b]); | ||
(err, res) => { | ||
chai.assert.deepEqual(res, [this.b]); | ||
done(); | ||
} | ||
); | ||
}) | ||
.done(); | ||
}).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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
55456
1213
1