Update deps and fix styles

This commit is contained in:
Raymond Feng 2017-03-06 15:40:47 -08:00
parent 050cfa5249
commit e77b300384
15 changed files with 73 additions and 66 deletions

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var SG = require('strong-globalize'); var SG = require('strong-globalize');
SG.SetRootDir(__dirname); SG.SetRootDir(__dirname);

View File

@ -18,7 +18,7 @@ module.exports = BinaryPacker;
* - Date * - Date
*/ */
function BinaryPacker() { function BinaryPacker() {
this._packer = msgpack({ forceFloat64: true }); this._packer = msgpack({forceFloat64: true});
this._packer.register(1, Date, encodeDate, decodeDate); this._packer.register(1, Date, encodeDate, decodeDate);
} }

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var SG = require('strong-globalize'); var SG = require('strong-globalize');
var g = SG(); var g = SG();
var debug = require('debug')('loopback:connector'); var debug = require('debug')('loopback:connector');
@ -49,7 +50,7 @@ Connector.prototype.getTypes = function() {
* @returns {Function} The default type for ID * @returns {Function} The default type for ID
*/ */
Connector.prototype.getDefaultIdType = function(prop) { Connector.prototype.getDefaultIdType = function(prop) {
/*jshint unused:false */ /* jshint unused:false */
return String; return String;
}; };
@ -278,6 +279,3 @@ Connector.defineAliases = function(cls, methodOrPropertyName, aliases) {
*/ */
Connector.defineAliases(Connector.prototype, 'execute', ['command', 'query']); Connector.defineAliases(Connector.prototype, 'execute', ['command', 'query']);

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var PLACEHOLDER = '?'; var PLACEHOLDER = '?';

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var SG = require('strong-globalize'); var SG = require('strong-globalize');
var g = SG(); var g = SG();
@ -121,11 +122,10 @@ SQLConnector.prototype.isActual = function(models, cb) {
return cb && cb(err); return cb && cb(err);
} }
var actual = (changes.length === 0); var actual = (changes.length === 0);
cb && cb(null, actual); if (cb) cb(null, actual);
}); });
}; };
SQLConnector.prototype.getAddModifyColumns = function(model, fields) { SQLConnector.prototype.getAddModifyColumns = function(model, fields) {
throw new Error(g.f('{{getAddModifyColumns()}} must be implemented by the connector')); throw new Error(g.f('{{getAddModifyColumns()}} must be implemented by the connector'));
}; };
@ -922,7 +922,7 @@ SQLConnector.prototype._executeAlteringQuery = function(model, sql, params, opti
var self = this; var self = this;
this.execute(sql, params, options, function(err, info) { this.execute(sql, params, options, function(err, info) {
var affectedRows = self.getCountForAffectedRows(model, info); var affectedRows = self.getCountForAffectedRows(model, info);
cb(err, { count: affectedRows }); cb(err, {count: affectedRows});
}); });
}; };
@ -1468,7 +1468,7 @@ SQLConnector.prototype.find = function(model, id, options, cb) {
var idName = this.idName(model); var idName = this.idName(model);
where[idName] = id; where[idName] = id;
var filter = { limit: 1, offset: 0, order: idName, where: where }; var filter = {limit: 1, offset: 0, order: idName, where: where};
return this.all(model, filter, options, function(err, results) { return this.all(model, filter, options, function(err, results) {
cb(err, (results && results[0]) || null); cb(err, (results && results[0]) || null);
}); });

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var util = require('util'); var util = require('util');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
@ -68,7 +69,7 @@ Transaction.begin = function(connector, options, cb) {
options = {}; options = {};
} }
if (typeof options === 'string') { if (typeof options === 'string') {
options = { isolationLevel: options }; options = {isolationLevel: options};
} }
var isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED; var isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
assert(isolationLevel === Transaction.SERIALIZABLE || assert(isolationLevel === Transaction.SERIALIZABLE ||

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var Promise = require('bluebird'); var Promise = require('bluebird');
exports.createPromiseCallback = createPromiseCallback; exports.createPromiseCallback = createPromiseCallback;

View File

@ -1,6 +1,6 @@
{ {
"name": "loopback-connector", "name": "loopback-connector",
"version": "3.0.0", "version": "4.0.0-alpha.1",
"description": "Building blocks for LoopBack connectors", "description": "Building blocks for LoopBack connectors",
"engines": { "engines": {
"node": ">=4" "node": ">=4"
@ -22,17 +22,17 @@
}, },
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"async": "^1.0.0", "async": "^2.1.5",
"bluebird": "^3.4.6", "bluebird": "^3.4.6",
"debug": "^2.2.0", "debug": "^2.2.0",
"msgpack5": "^3.4.1", "msgpack5": "^3.4.1",
"strong-globalize": "^2.5.8" "strong-globalize": "^2.5.8"
}, },
"devDependencies": { "devDependencies": {
"chai": "~2.3.0", "chai": "~3.5.0",
"eslint": "^2.7.0", "eslint": "^3.17.1",
"eslint-config-loopback": "^1.0.0", "eslint-config-loopback": "^8.0.0",
"loopback-datasource-juggler": "^3.0.0", "loopback-datasource-juggler": "^3.0.0",
"mocha": "^2.2.5" "mocha": "^3.2.0"
} }
} }

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var expect = require('chai').expect; var expect = require('chai').expect;
var testConnector = require('./connectors/test-sql-connector'); var testConnector = require('./connectors/test-sql-connector');

View File

@ -37,7 +37,7 @@ describe('BinaryPacker', function() {
describe('roundtrip', function() { describe('roundtrip', function() {
var TEST_CASES = { var TEST_CASES = {
String: 'a-value', String: 'a-value',
Object: { a: 1, b: 2 }, Object: {a: 1, b: 2},
Buffer: new Buffer([1, 2, 3]), Buffer: new Buffer([1, 2, 3]),
Date: new Date('2016-08-03T11:53:03.470Z'), Date: new Date('2016-08-03T11:53:03.470Z'),
Integer: 12345, Integer: 12345,

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
/* /*
* A mockup connector that extends SQL connector * A mockup connector that extends SQL connector
*/ */
@ -102,7 +103,7 @@ TestConnector.prototype._buildLimit = function(model, limit, offset) {
TestConnector.prototype.applyPagination = TestConnector.prototype.applyPagination =
function(model, stmt, filter) { function(model, stmt, filter) {
/*jshint unused:false */ /* jshint unused:false */
var limitClause = this._buildLimit(model, filter.limit, var limitClause = this._buildLimit(model, filter.limit,
filter.offset || filter.skip); filter.offset || filter.skip);
return stmt.merge(limitClause); return stmt.merge(limitClause);
@ -155,7 +156,7 @@ TestConnector.prototype._buildLimit = function(model, limit, offset) {
TestConnector.prototype.applyPagination = TestConnector.prototype.applyPagination =
function(model, stmt, filter) { function(model, stmt, filter) {
/*jshint unused:false */ /* jshint unused:false */
var limitClause = this._buildLimit(model, filter.limit, var limitClause = this._buildLimit(model, filter.limit,
filter.offset || filter.skip); filter.offset || filter.skip);
return stmt.merge(limitClause); return stmt.merge(limitClause);
@ -215,7 +216,7 @@ TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
if (sql.indexOf('INSERT') === 0) { if (sql.indexOf('INSERT') === 0) {
transaction.connection.data[model] = transaction.connection.data[model] =
transaction.connection.data[model] || []; transaction.connection.data[model] || [];
transaction.connection.data[model].push({ sql: sql, params: params }); transaction.connection.data[model].push({sql: sql, params: params});
debug('INSERT', transaction.connection.data, sql, debug('INSERT', transaction.connection.data, sql,
transaction.connection.name); transaction.connection.name);
callback(null, 1); callback(null, 1);
@ -227,7 +228,7 @@ TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
} else { } else {
if (sql.indexOf('INSERT') === 0) { if (sql.indexOf('INSERT') === 0) {
this.data[model] = this.data[model] || []; this.data[model] = this.data[model] || [];
this.data[model].push({ sql: sql, params: params }); this.data[model].push({sql: sql, params: params});
debug('INSERT', this.data, sql); debug('INSERT', this.data, sql);
callback(null, 1); callback(null, 1);
} else { } else {

View File

@ -37,7 +37,7 @@ describe('JSONStringPacker', function() {
describe('roundtrip', function() { describe('roundtrip', function() {
var TEST_CASES = { var TEST_CASES = {
String: 'a-value', String: 'a-value',
Object: { a: 1, b: 2 }, Object: {a: 1, b: 2},
Buffer: new Buffer([1, 2, 3]), Buffer: new Buffer([1, 2, 3]),
Date: new Date('2016-08-03T11:53:03.470Z'), Date: new Date('2016-08-03T11:53:03.470Z'),
Integer: 12345, Integer: 12345,

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var connector = require('../'); var connector = require('../');

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var expect = require('chai').expect; var expect = require('chai').expect;
var SQLConnector = require('../lib/sql'); var SQLConnector = require('../lib/sql');
var ParameterizedSQL = SQLConnector.ParameterizedSQL; var ParameterizedSQL = SQLConnector.ParameterizedSQL;
@ -41,7 +42,7 @@ describe('sql connector', function() {
}, },
address: String, address: String,
}, },
{ testdb: { table: 'CUSTOMER' }}); {testdb: {table: 'CUSTOMER'}});
}); });
it('should map table name', function() { it('should map table name', function() {
@ -94,7 +95,7 @@ describe('sql connector', function() {
}); });
it('builds where', function() { it('builds where', function() {
var where = connector.buildWhere('customer', { name: 'John' }); var where = connector.buildWhere('customer', {name: 'John'});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME`=?', sql: 'WHERE `NAME`=?',
params: ['John'], params: ['John'],
@ -102,7 +103,7 @@ describe('sql connector', function() {
}); });
it('builds where with null', function() { it('builds where with null', function() {
var where = connector.buildWhere('customer', { name: null }); var where = connector.buildWhere('customer', {name: null});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` IS NULL', sql: 'WHERE `NAME` IS NULL',
params: [], params: [],
@ -110,7 +111,7 @@ describe('sql connector', function() {
}); });
it('builds where with inq', function() { it('builds where with inq', function() {
var where = connector.buildWhere('customer', { name: { inq: ['John', 'Mary'] }}); var where = connector.buildWhere('customer', {name: {inq: ['John', 'Mary']}});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE `NAME` IN (?,?)', sql: 'WHERE `NAME` IN (?,?)',
params: ['John', 'Mary'], params: ['John', 'Mary'],
@ -119,7 +120,7 @@ describe('sql connector', function() {
it('builds where with or', function() { it('builds where with or', function() {
var where = connector.buildWhere('customer', var where = connector.buildWhere('customer',
{ or: [{ name: 'John' }, { name: 'Mary' }] }); {or: [{name: 'John'}, {name: 'Mary'}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) OR (`NAME`=?)', sql: 'WHERE (`NAME`=?) OR (`NAME`=?)',
params: ['John', 'Mary'], params: ['John', 'Mary'],
@ -128,7 +129,7 @@ describe('sql connector', function() {
it('builds where with and', function() { it('builds where with and', function() {
var where = connector.buildWhere('customer', var where = connector.buildWhere('customer',
{ and: [{ name: 'John' }, { vip: true }] }); {and: [{name: 'John'}, {vip: true}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) AND (`VIP`=?)', sql: 'WHERE (`NAME`=?) AND (`VIP`=?)',
params: ['John', true], params: ['John', true],
@ -209,7 +210,7 @@ describe('sql connector', function() {
it('builds where with nesting and/or', function() { it('builds where with nesting and/or', function() {
var where = connector.buildWhere('customer', var where = connector.buildWhere('customer',
{ and: [{ name: 'John' }, { or: [{ vip: true }, { address: null }] }] }); {and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]});
expect(where.toJSON()).to.eql({ expect(where.toJSON()).to.eql({
sql: 'WHERE (`NAME`=?) AND ((`VIP`=?) OR (`ADDRESS` IS NULL))', sql: 'WHERE (`NAME`=?) AND ((`VIP`=?) OR (`ADDRESS` IS NULL))',
params: ['John', true], params: ['John', true],
@ -233,17 +234,17 @@ describe('sql connector', function() {
it('builds fields for columns', function() { it('builds fields for columns', function() {
var fields = connector.buildFields('customer', var fields = connector.buildFields('customer',
{ name: 'John', vip: true, unknown: 'Random' }); {name: 'John', vip: true, unknown: 'Random'});
expect(fields.names).to.eql(['`NAME`', '`VIP`']); expect(fields.names).to.eql(['`NAME`', '`VIP`']);
expect(fields.columnValues[0].toJSON()).to.eql( expect(fields.columnValues[0].toJSON()).to.eql(
{ sql: '?', params: ['John'] }); {sql: '?', params: ['John']});
expect(fields.columnValues[1].toJSON()).to.eql( expect(fields.columnValues[1].toJSON()).to.eql(
{ sql: '?', params: [true] }); {sql: '?', params: [true]});
}); });
it('builds fields for UPDATE without ids', function() { it('builds fields for UPDATE without ids', function() {
var fields = connector.buildFieldsForUpdate('customer', var fields = connector.buildFieldsForUpdate('customer',
{ name: 'John', vip: true }); {name: 'John', vip: true});
expect(fields.toJSON()).to.eql({ expect(fields.toJSON()).to.eql({
sql: 'SET `VIP`=?', sql: 'SET `VIP`=?',
params: [true], params: [true],
@ -252,7 +253,7 @@ describe('sql connector', function() {
it('builds fields for UPDATE with ids', function() { it('builds fields for UPDATE with ids', function() {
var fields = connector.buildFieldsForUpdate('customer', var fields = connector.buildFieldsForUpdate('customer',
{ name: 'John', vip: true }, false); {name: 'John', vip: true}, false);
expect(fields.toJSON()).to.eql({ expect(fields.toJSON()).to.eql({
sql: 'SET `NAME`=?,`VIP`=?', sql: 'SET `NAME`=?,`VIP`=?',
params: ['John', true], params: ['John', true],
@ -265,22 +266,22 @@ describe('sql connector', function() {
}); });
it('builds column names with true fields filter for SELECT', function() { it('builds column names with true fields filter for SELECT', function() {
var cols = connector.buildColumnNames('customer', { fields: { name: true }}); var cols = connector.buildColumnNames('customer', {fields: {name: true}});
expect(cols).to.eql('`NAME`'); expect(cols).to.eql('`NAME`');
}); });
it('builds column names with false fields filter for SELECT', function() { it('builds column names with false fields filter for SELECT', function() {
var cols = connector.buildColumnNames('customer', { fields: { name: false }}); var cols = connector.buildColumnNames('customer', {fields: {name: false}});
expect(cols).to.eql('`VIP`,`ADDRESS`'); expect(cols).to.eql('`VIP`,`ADDRESS`');
}); });
it('builds column names with array fields filter for SELECT', function() { it('builds column names with array fields filter for SELECT', function() {
var cols = connector.buildColumnNames('customer', { fields: ['name'] }); var cols = connector.buildColumnNames('customer', {fields: ['name']});
expect(cols).to.eql('`NAME`'); expect(cols).to.eql('`NAME`');
}); });
it('builds DELETE', function() { it('builds DELETE', function() {
var sql = connector.buildDelete('customer', { name: 'John' }); var sql = connector.buildDelete('customer', {name: 'John'});
expect(sql.toJSON()).to.eql({ expect(sql.toJSON()).to.eql({
sql: 'DELETE FROM `CUSTOMER` WHERE `NAME`=$1', sql: 'DELETE FROM `CUSTOMER` WHERE `NAME`=$1',
params: ['John'], params: ['John'],
@ -288,7 +289,7 @@ describe('sql connector', function() {
}); });
it('builds UPDATE', function() { it('builds UPDATE', function() {
var sql = connector.buildUpdate('customer', { name: 'John' }, { vip: false }); var sql = connector.buildUpdate('customer', {name: 'John'}, {vip: false});
expect(sql.toJSON()).to.eql({ expect(sql.toJSON()).to.eql({
sql: 'UPDATE `CUSTOMER` SET `VIP`=$1 WHERE `NAME`=$2', sql: 'UPDATE `CUSTOMER` SET `VIP`=$1 WHERE `NAME`=$2',
params: [false, 'John'], params: [false, 'John'],
@ -297,7 +298,7 @@ describe('sql connector', function() {
it('builds SELECT', function() { it('builds SELECT', function() {
var sql = connector.buildSelect('customer', var sql = connector.buildSelect('customer',
{ order: 'name', limit: 5, where: { name: 'John' }}); {order: 'name', limit: 5, where: {name: 'John'}});
expect(sql.toJSON()).to.eql({ expect(sql.toJSON()).to.eql({
sql: 'SELECT `NAME`,`VIP`,`ADDRESS` FROM `CUSTOMER`' + sql: 'SELECT `NAME`,`VIP`,`ADDRESS` FROM `CUSTOMER`' +
' WHERE `NAME`=$1 ORDER BY `NAME` LIMIT 5', ' WHERE `NAME`=$1 ORDER BY `NAME` LIMIT 5',
@ -306,7 +307,7 @@ describe('sql connector', function() {
}); });
it('builds INSERT', function() { it('builds INSERT', function() {
var sql = connector.buildInsert('customer', { name: 'John', vip: true }); var sql = connector.buildInsert('customer', {name: 'John', vip: true});
expect(sql.toJSON()).to.eql({ expect(sql.toJSON()).to.eql({
sql: 'INSERT INTO `CUSTOMER`(`NAME`,`VIP`) VALUES($1,$2)', sql: 'INSERT INTO `CUSTOMER`(`NAME`,`VIP`) VALUES($1,$2)',
params: ['John', true], params: ['John', true],
@ -316,49 +317,49 @@ describe('sql connector', function() {
it('normalizes a SQL statement from string', function() { it('normalizes a SQL statement from string', function() {
var sql = 'SELECT * FROM `CUSTOMER`'; var sql = 'SELECT * FROM `CUSTOMER`';
var stmt = new ParameterizedSQL(sql); var stmt = new ParameterizedSQL(sql);
expect(stmt.toJSON()).to.eql({ sql: sql, params: [] }); expect(stmt.toJSON()).to.eql({sql: sql, params: []});
}); });
it('normalizes a SQL statement from object without params', function() { it('normalizes a SQL statement from object without params', function() {
var sql = { sql: 'SELECT * FROM `CUSTOMER`' }; var sql = {sql: 'SELECT * FROM `CUSTOMER`'};
var stmt = new ParameterizedSQL(sql); var stmt = new ParameterizedSQL(sql);
expect(stmt.toJSON()).to.eql({ sql: sql.sql, params: [] }); expect(stmt.toJSON()).to.eql({sql: sql.sql, params: []});
}); });
it('normalizes a SQL statement from object with params', function() { it('normalizes a SQL statement from object with params', function() {
var sql = var sql =
{ sql: 'SELECT * FROM `CUSTOMER` WHERE `NAME`=?', params: ['John'] }; {sql: 'SELECT * FROM `CUSTOMER` WHERE `NAME`=?', params: ['John']};
var stmt = new ParameterizedSQL(sql); var stmt = new ParameterizedSQL(sql);
expect(stmt.toJSON()).to.eql({ sql: sql.sql, params: ['John'] }); expect(stmt.toJSON()).to.eql({sql: sql.sql, params: ['John']});
}); });
it('should throw if the statement is not a string or object', function() { it('should throw if the statement is not a string or object', function() {
expect(function() { expect(function() {
/*jshint unused:false */ /* jshint unused:false */
var stmt = new ParameterizedSQL(true); var stmt = new ParameterizedSQL(true);
}).to.throw('sql must be a string'); }).to.throw('sql must be a string');
}); });
it('concats SQL statements', function() { it('concats SQL statements', function() {
var stmt1 = { sql: 'SELECT * from `CUSTOMER`' }; var stmt1 = {sql: 'SELECT * from `CUSTOMER`'};
var where = { sql: 'WHERE `NAME`=?', params: ['John'] }; var where = {sql: 'WHERE `NAME`=?', params: ['John']};
stmt1 = ParameterizedSQL.append(stmt1, where); stmt1 = ParameterizedSQL.append(stmt1, where);
expect(stmt1.toJSON()).to.eql( expect(stmt1.toJSON()).to.eql(
{ sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John'] }); {sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John']});
}); });
it('concats string SQL statements', function() { it('concats string SQL statements', function() {
var stmt1 = 'SELECT * from `CUSTOMER`'; var stmt1 = 'SELECT * from `CUSTOMER`';
var where = { sql: 'WHERE `NAME`=?', params: ['John'] }; var where = {sql: 'WHERE `NAME`=?', params: ['John']};
stmt1 = ParameterizedSQL.append(stmt1, where); stmt1 = ParameterizedSQL.append(stmt1, where);
expect(stmt1.toJSON()).to.eql( expect(stmt1.toJSON()).to.eql(
{ sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John'] }); {sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John']});
}); });
it('should throw if params does not match placeholders', function() { it('should throw if params does not match placeholders', function() {
expect(function() { expect(function() {
var stmt1 = 'SELECT * from `CUSTOMER`'; var stmt1 = 'SELECT * from `CUSTOMER`';
var where = { sql: 'WHERE `NAME`=?', params: ['John', 'Mary'] }; var where = {sql: 'WHERE `NAME`=?', params: ['John', 'Mary']};
stmt1 = ParameterizedSQL.append(stmt1, where); stmt1 = ParameterizedSQL.append(stmt1, where);
}).to.throw('must match the number of params'); }).to.throw('must match the number of params');
}); });
@ -374,7 +375,7 @@ describe('sql connector', function() {
it('should allow execute(sql, params, options, callback)', function(done) { it('should allow execute(sql, params, options, callback)', function(done) {
connector.execute('SELECT * FROM `CUSTOMER` WHERE `NAME`=$1', connector.execute('SELECT * FROM `CUSTOMER` WHERE `NAME`=$1',
['xyz'], { transaction: true }, done); ['xyz'], {transaction: true}, done);
}); });
it('should throw if params is not an array for execute()', function() { it('should throw if params is not an array for execute()', function() {

View File

@ -3,6 +3,7 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var Transaction = require('../index').Transaction; var Transaction = require('../index').Transaction;
var expect = require('chai').expect; var expect = require('chai').expect;
@ -18,14 +19,14 @@ describe('transactions', function() {
}); });
db.once('connected', function() { db.once('connected', function() {
Post = db.define('PostTX', { Post = db.define('PostTX', {
title: { type: String, length: 255, index: true }, title: {type: String, length: 255, index: true},
content: { type: String }, content: {type: String},
}); });
Review = db.define('ReviewTX', { Review = db.define('ReviewTX', {
author: String, author: String,
content: { type: String }, content: {type: String},
}); });
Post.hasMany(Review, { as: 'reviews', foreignKey: 'postId' }); Post.hasMany(Review, {as: 'reviews', foreignKey: 'postId'});
done(); done();
}); });
}); });
@ -61,7 +62,7 @@ describe('transactions', function() {
next(); next();
}); });
currentTx = tx; currentTx = tx;
Post.create(post, { transaction: tx, model: 'Post' }, Post.create(post, {transaction: tx, model: 'Post'},
function(err, p) { function(err, p) {
if (err) { if (err) {
done(err); done(err);
@ -69,7 +70,7 @@ describe('transactions', function() {
p.reviews.create({ p.reviews.create({
author: 'John', author: 'John',
content: 'Review for ' + p.title, content: 'Review for ' + p.title,
}, { transaction: tx, model: 'Review' }, }, {transaction: tx, model: 'Review'},
function(err, c) { function(err, c) {
done(err); done(err);
}); });
@ -83,11 +84,11 @@ describe('transactions', function() {
// records to equal to the count // records to equal to the count
function expectToFindPosts(where, count, inTx) { function expectToFindPosts(where, count, inTx) {
return function(done) { return function(done) {
var options = { model: 'Post' }; var options = {model: 'Post'};
if (inTx) { if (inTx) {
options.transaction = currentTx; options.transaction = currentTx;
} }
Post.find({ where: where }, options, Post.find({where: where}, options,
function(err, posts) { function(err, posts) {
if (err) return done(err); if (err) return done(err);
expect(posts.length).to.be.eql(count); expect(posts.length).to.be.eql(count);
@ -109,7 +110,7 @@ describe('transactions', function() {
} }
describe('commit', function() { describe('commit', function() {
var post = { title: 't1', content: 'c1' }; var post = {title: 't1', content: 'c1'};
before(createPostInTx(post)); before(createPostInTx(post));
it('should not see the uncommitted insert', expectToFindPosts(post, 0)); it('should not see the uncommitted insert', expectToFindPosts(post, 0));
@ -140,7 +141,7 @@ describe('transactions', function() {
db.connector.data = {}; db.connector.data = {};
}); });
var post = { title: 't2', content: 'c2' }; var post = {title: 't2', content: 'c2'};
before(createPostInTx(post)); before(createPostInTx(post));
it('should not see the uncommitted insert', expectToFindPosts(post, 0)); it('should not see the uncommitted insert', expectToFindPosts(post, 0));
@ -171,12 +172,12 @@ describe('transactions', function() {
db.connector.data = {}; db.connector.data = {};
}); });
var post = { title: 't3', content: 'c3' }; var post = {title: 't3', content: 'c3'};
before(createPostInTx(post, 50)); before(createPostInTx(post, 50));
it('should report timeout', function(done) { it('should report timeout', function(done) {
setTimeout(function() { setTimeout(function() {
Post.find({ where: { title: 't3' }}, { transaction: currentTx }, Post.find({where: {title: 't3'}}, {transaction: currentTx},
function(err, posts) { function(err, posts) {
if (err) return done(err); if (err) return done(err);
expect(posts.length).to.be.eql(1); expect(posts.length).to.be.eql(1);