chore: update deps and fix lint violations
This commit is contained in:
parent
473c90ae63
commit
4c4d05fe87
2
index.js
2
index.js
|
@ -4,7 +4,7 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var SG = require('strong-globalize');
|
||||
const SG = require('strong-globalize');
|
||||
SG.SetRootDir(__dirname);
|
||||
|
||||
exports.Connector = require('./lib/connector');
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
var msgpack = require('msgpack5');
|
||||
const createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
const msgpack = require('msgpack5');
|
||||
|
||||
module.exports = BinaryPacker;
|
||||
|
||||
|
@ -36,7 +36,7 @@ BinaryPacker.prototype.encode = function(value, cb) {
|
|||
try {
|
||||
// msgpack5 returns https://www.npmjs.com/package/bl instead of Buffer
|
||||
// use .slice() to convert to a Buffer
|
||||
var data = this._packer.encode(value).slice();
|
||||
const data = this._packer.encode(value).slice();
|
||||
setImmediate(function() {
|
||||
cb(null, data);
|
||||
});
|
||||
|
@ -59,7 +59,7 @@ BinaryPacker.prototype.encode = function(value, cb) {
|
|||
BinaryPacker.prototype.decode = function(binary, cb) {
|
||||
cb = cb || createPromiseCallback();
|
||||
try {
|
||||
var value = this._packer.decode(binary);
|
||||
const value = this._packer.decode(binary);
|
||||
setImmediate(function() {
|
||||
cb(null, value);
|
||||
});
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var SG = require('strong-globalize');
|
||||
var g = SG();
|
||||
var debug = require('debug')('loopback:connector');
|
||||
const SG = require('strong-globalize');
|
||||
const g = SG();
|
||||
const debug = require('debug')('loopback:connector');
|
||||
|
||||
module.exports = Connector;
|
||||
|
||||
|
@ -62,9 +62,9 @@ Connector.prototype.getDefaultIdType = function(prop) {
|
|||
*/
|
||||
|
||||
Connector.prototype.generateUniqueId = function(modelName) {
|
||||
var idType = this.getDefaultIdType && this.getDefaultIdType();
|
||||
var isTypeFunction = (typeof idType === 'function');
|
||||
var id = this.generateValueByColumnType ? this.generateValueByColumnType(idType) :
|
||||
const idType = this.getDefaultIdType && this.getDefaultIdType();
|
||||
const isTypeFunction = (typeof idType === 'function');
|
||||
const id = this.generateValueByColumnType ? this.generateValueByColumnType(idType) :
|
||||
(typeof idType === 'function' ? idType() : null);
|
||||
return id;
|
||||
};
|
||||
|
@ -124,7 +124,7 @@ Connector.prototype.getModelDefinition = function(modelName) {
|
|||
* @returns {Object} The connector specific settings
|
||||
*/
|
||||
Connector.prototype.getConnectorSpecificSettings = function(modelName) {
|
||||
var settings = this.getModelDefinition(modelName).settings || {};
|
||||
const settings = this.getModelDefinition(modelName).settings || {};
|
||||
return settings[this.name];
|
||||
};
|
||||
|
||||
|
@ -181,7 +181,7 @@ Connector.getNestedPropertyDefinition = function(definition, propPath) {
|
|||
* @returns {DataSource} The data source
|
||||
*/
|
||||
Connector.prototype.getDataSource = function(model) {
|
||||
var m = this.getModelDefinition(model);
|
||||
const m = this.getModelDefinition(model);
|
||||
if (!m) {
|
||||
debug('Model not found: ' + model);
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ Connector.prototype.idNames = function(model) {
|
|||
* of the primary key
|
||||
*/
|
||||
Connector.prototype.id = function(model, prop) {
|
||||
var p = this.getModelDefinition(model).properties[prop];
|
||||
const p = this.getModelDefinition(model).properties[prop];
|
||||
return p && p.id;
|
||||
};
|
||||
|
||||
|
@ -234,7 +234,7 @@ Connector.prototype.define = function(modelDefinition) {
|
|||
* @param {Object} propertyDefinition The object for property definition
|
||||
*/
|
||||
Connector.prototype.defineProperty = function(model, propertyName, propertyDefinition) {
|
||||
var modelDef = this.getModelDefinition(model);
|
||||
const modelDef = this.getModelDefinition(model);
|
||||
modelDef.properties[propertyName] = propertyDefinition;
|
||||
};
|
||||
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
const createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
|
||||
module.exports = JSONStringPacker;
|
||||
|
||||
var ISO_DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
|
||||
const ISO_DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
|
||||
|
||||
/**
|
||||
* Create a new Packer instance that can be used to convert between JavaScript
|
||||
|
@ -31,11 +31,11 @@ function JSONStringPacker(encoding) {
|
|||
* @promise
|
||||
*/
|
||||
JSONStringPacker.prototype.encode = function(value, cb) {
|
||||
var encoding = this.encoding;
|
||||
const encoding = this.encoding;
|
||||
|
||||
cb = cb || createPromiseCallback();
|
||||
try {
|
||||
var data = JSON.stringify(value, function(key, value) {
|
||||
const data = JSON.stringify(value, function(key, value) {
|
||||
if (Buffer.isBuffer(this[key])) {
|
||||
return {
|
||||
type: 'Buffer',
|
||||
|
@ -66,11 +66,11 @@ JSONStringPacker.prototype.encode = function(value, cb) {
|
|||
* @promise
|
||||
*/
|
||||
JSONStringPacker.prototype.decode = function(jsonString, cb) {
|
||||
var encoding = this.encoding;
|
||||
const encoding = this.encoding;
|
||||
|
||||
cb = cb || createPromiseCallback();
|
||||
try {
|
||||
var value = JSON.parse(jsonString, function(k, v) {
|
||||
const value = JSON.parse(jsonString, function(k, v) {
|
||||
if (v && v.type && v.type === 'Buffer') {
|
||||
return new Buffer(v.data, encoding);
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
var debug = require('debug')('loopback:connector:model-key-composer');
|
||||
var g = require('strong-globalize')();
|
||||
const createPromiseCallback = require('./utils').createPromiseCallback;
|
||||
const debug = require('debug')('loopback:connector:model-key-composer');
|
||||
const g = require('strong-globalize')();
|
||||
|
||||
/**
|
||||
* Build a single key string from a tuple (modelName, key).
|
||||
|
@ -27,7 +27,7 @@ exports.compose = function composeKeyFromModelNameAndKey(modelName, key, cb) {
|
|||
|
||||
// Escape model name to prevent collision
|
||||
// 'model' + 'foo:bar' --vs-- 'model:foo' + 'bar'
|
||||
var value = encodeURIComponent(modelName) + ':' + key;
|
||||
const value = encodeURIComponent(modelName) + ':' + key;
|
||||
|
||||
setImmediate(function() {
|
||||
cb(null, value);
|
||||
|
@ -35,7 +35,7 @@ exports.compose = function composeKeyFromModelNameAndKey(modelName, key, cb) {
|
|||
return cb.promise;
|
||||
};
|
||||
|
||||
var PARSE_KEY_REGEX = /^([^:]*):(.*)/;
|
||||
const PARSE_KEY_REGEX = /^([^:]*):(.*)/;
|
||||
|
||||
/**
|
||||
* Parse a composed key string into a tuple (modelName, key).
|
||||
|
@ -53,9 +53,9 @@ var PARSE_KEY_REGEX = /^([^:]*):(.*)/;
|
|||
exports.parse = function(composed, cb) {
|
||||
cb = cb || createPromiseCallback();
|
||||
|
||||
var matchResult = composed.match(PARSE_KEY_REGEX);
|
||||
const matchResult = composed.match(PARSE_KEY_REGEX);
|
||||
if (matchResult) {
|
||||
var result = {
|
||||
const result = {
|
||||
modelName: matchResult[1],
|
||||
key: matchResult[2],
|
||||
};
|
||||
|
@ -64,9 +64,10 @@ exports.parse = function(composed, cb) {
|
|||
});
|
||||
} else {
|
||||
debug('Invalid key - missing model-name prefix: %s', composed);
|
||||
var err = new Error(g.f(
|
||||
const err = new Error(g.f(
|
||||
'Invalid key %j - missing model-name prefix',
|
||||
composed));
|
||||
composed
|
||||
));
|
||||
err.code = 'NO_MODEL_PREFIX';
|
||||
setImmediate(function() {
|
||||
cb(err);
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var assert = require('assert');
|
||||
var PLACEHOLDER = '?';
|
||||
const assert = require('assert');
|
||||
const PLACEHOLDER = '?';
|
||||
|
||||
module.exports = ParameterizedSQL;
|
||||
|
||||
|
@ -34,7 +34,7 @@ function ParameterizedSQL(sql, params) {
|
|||
assert(typeof this.sql === 'string', 'sql must be a string');
|
||||
assert(Array.isArray(this.params), 'params must be an array');
|
||||
|
||||
var parts = this.sql.split(PLACEHOLDER);
|
||||
const parts = this.sql.split(PLACEHOLDER);
|
||||
assert(parts.length - 1 === this.params.length,
|
||||
'The number of ? (' + (parts.length - 1) +
|
||||
') in the sql (' + this.sql + ') must match the number of params (' +
|
||||
|
@ -96,8 +96,8 @@ ParameterizedSQL.append = function(currentStmt, stmt, separator) {
|
|||
*/
|
||||
ParameterizedSQL.join = function(sqls, separator) {
|
||||
assert(Array.isArray(sqls), 'sqls must be an array');
|
||||
var ps = new ParameterizedSQL('', []);
|
||||
for (var i = 0, n = sqls.length; i < n; i++) {
|
||||
const ps = new ParameterizedSQL('', []);
|
||||
for (let i = 0, n = sqls.length; i < n; i++) {
|
||||
this.append(ps, sqls[i], separator);
|
||||
}
|
||||
return ps;
|
||||
|
|
398
lib/sql.js
398
lib/sql.js
File diff suppressed because it is too large
Load Diff
|
@ -4,11 +4,11 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var assert = require('assert');
|
||||
var util = require('util');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var debug = require('debug')('loopback:connector:transaction');
|
||||
var uuid = require('uuid');
|
||||
const assert = require('assert');
|
||||
const util = require('util');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const debug = require('debug')('loopback:connector:transaction');
|
||||
const uuid = require('uuid');
|
||||
const {createPromiseCallback} = require('./utils');
|
||||
|
||||
module.exports = Transaction;
|
||||
|
@ -86,7 +86,7 @@ Transaction.begin = function(connector, options, cb) {
|
|||
if (typeof options === 'string') {
|
||||
options = {isolationLevel: options};
|
||||
}
|
||||
var isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
|
||||
const isolationLevel = options.isolationLevel || Transaction.READ_COMMITTED;
|
||||
assert(isolationLevel === Transaction.SERIALIZABLE ||
|
||||
isolationLevel === Transaction.REPEATABLE_READ ||
|
||||
isolationLevel === Transaction.READ_COMMITTED ||
|
||||
|
@ -99,7 +99,7 @@ Transaction.begin = function(connector, options, cb) {
|
|||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
var tx = connection;
|
||||
let tx = connection;
|
||||
|
||||
// When the connector and juggler node module have different version of this module as a dependency,
|
||||
// the transaction is not an instanceof Transaction.
|
||||
|
@ -117,7 +117,7 @@ Transaction.begin = function(connector, options, cb) {
|
|||
// see: https://github.com/strongloop/loopback-datasource-juggler/pull/1484
|
||||
if (options.timeout) {
|
||||
tx.timeout = setTimeout(function() {
|
||||
var context = {
|
||||
const context = {
|
||||
transaction: tx,
|
||||
operation: 'timeout',
|
||||
};
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var Promise = require('bluebird');
|
||||
const Promise = require('bluebird');
|
||||
|
||||
exports.createPromiseCallback = createPromiseCallback;
|
||||
|
||||
function createPromiseCallback() {
|
||||
var cb;
|
||||
var promise = new Promise(function(resolve, reject) {
|
||||
let cb;
|
||||
const promise = new Promise(function(resolve, reject) {
|
||||
cb = function(err, data) {
|
||||
if (err) return reject(err);
|
||||
return resolve(data);
|
||||
|
|
13
package.json
13
package.json
|
@ -18,24 +18,25 @@
|
|||
"main": "index.js",
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"posttest": "npm run lint",
|
||||
"test": "mocha"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"async": "^2.1.5",
|
||||
"async": "^3.1.0",
|
||||
"bluebird": "^3.4.6",
|
||||
"debug": "^3.1.0",
|
||||
"debug": "^4.1.1",
|
||||
"msgpack5": "^4.2.0",
|
||||
"strong-globalize": "^4.1.1",
|
||||
"strong-globalize": "^5.0.0",
|
||||
"uuid": "^3.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"eslint": "^4.19.1",
|
||||
"eslint-config-loopback": "^10.0.0",
|
||||
"eslint": "^6.1.0",
|
||||
"eslint-config-loopback": "^13.1.0",
|
||||
"loopback-datasource-juggler": "^3.12.0",
|
||||
"mocha": "^5.2.0"
|
||||
"mocha": "^6.2.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,11 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var expect = require('chai').expect;
|
||||
var testConnector = require('./connectors/test-sql-connector');
|
||||
const expect = require('chai').expect;
|
||||
const testConnector = require('./connectors/test-sql-connector');
|
||||
|
||||
var juggler = require('loopback-datasource-juggler');
|
||||
var ds = new juggler.DataSource({
|
||||
const juggler = require('loopback-datasource-juggler');
|
||||
const ds = new juggler.DataSource({
|
||||
connector: testConnector,
|
||||
debug: true,
|
||||
});
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var BinaryPacker = require('../lib/binary-packer');
|
||||
var expect = require('chai').expect;
|
||||
const BinaryPacker = require('../lib/binary-packer');
|
||||
const expect = require('chai').expect;
|
||||
|
||||
describe('BinaryPacker', function() {
|
||||
var packer;
|
||||
let packer;
|
||||
|
||||
beforeEach(function createPacker() {
|
||||
packer = new BinaryPacker();
|
||||
|
@ -35,7 +35,7 @@ describe('BinaryPacker', function() {
|
|||
});
|
||||
|
||||
describe('roundtrip', function() {
|
||||
var TEST_CASES = {
|
||||
const TEST_CASES = {
|
||||
String: 'a-value',
|
||||
Object: {a: 1, b: 2},
|
||||
Buffer: new Buffer([1, 2, 3]),
|
||||
|
@ -47,7 +47,7 @@ describe('BinaryPacker', function() {
|
|||
|
||||
Object.keys(TEST_CASES).forEach(function(tc) {
|
||||
it('works for ' + tc + ' values', function() {
|
||||
var value = TEST_CASES[tc];
|
||||
const value = TEST_CASES[tc];
|
||||
return encodeAndDecode(value)
|
||||
.then(function(result) {
|
||||
expect(result).to.eql(value);
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
/*
|
||||
* A mockup connector that extends SQL connector
|
||||
*/
|
||||
var util = require('util');
|
||||
var SQLConnector = require('../../lib/sql');
|
||||
var debug = require('debug')('loopback:connector:test-sql');
|
||||
const util = require('util');
|
||||
const SQLConnector = require('../../lib/sql');
|
||||
const debug = require('debug')('loopback:connector:test-sql');
|
||||
|
||||
var transactionId = 0;
|
||||
let transactionId = 0;
|
||||
|
||||
function MockTransaction(connector, name) {
|
||||
this.connector = connector;
|
||||
|
@ -20,11 +20,11 @@ function MockTransaction(connector, name) {
|
|||
}
|
||||
|
||||
MockTransaction.prototype.commit = function(cb) {
|
||||
var self = this;
|
||||
const self = this;
|
||||
// Merge data from this TX to the global data var
|
||||
for (var m in this.data) {
|
||||
for (const m in this.data) {
|
||||
self.connector.data[m] = self.connector.data[m] || [];
|
||||
for (var i = 0, n = this.data[m].length; i < n; i++) {
|
||||
for (let i = 0, n = this.data[m].length; i < n; i++) {
|
||||
self.connector.data[m].push(this.data[m]);
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ MockTransaction.prototype.rollback = function(cb) {
|
|||
exports.initialize = function initializeDataSource(dataSource, callback) {
|
||||
process.nextTick(function() {
|
||||
if (callback) {
|
||||
var connector = new TestConnector(dataSource.settings);
|
||||
const connector = new TestConnector(dataSource.settings);
|
||||
connector.dataSource = dataSource;
|
||||
dataSource.connector = connector;
|
||||
callback(null, connector);
|
||||
|
@ -104,7 +104,7 @@ TestConnector.prototype._buildLimit = function(model, limit, offset) {
|
|||
TestConnector.prototype.applyPagination =
|
||||
function(model, stmt, filter) {
|
||||
/* jshint unused:false */
|
||||
var limitClause = this._buildLimit(model, filter.limit,
|
||||
const limitClause = this._buildLimit(model, filter.limit,
|
||||
filter.offset || filter.skip);
|
||||
return stmt.merge(limitClause);
|
||||
};
|
||||
|
@ -157,14 +157,14 @@ TestConnector.prototype._buildLimit = function(model, limit, offset) {
|
|||
TestConnector.prototype.applyPagination =
|
||||
function(model, stmt, filter) {
|
||||
/* jshint unused:false */
|
||||
var limitClause = this._buildLimit(model, filter.limit,
|
||||
const limitClause = this._buildLimit(model, filter.limit,
|
||||
filter.offset || filter.skip);
|
||||
return stmt.merge(limitClause);
|
||||
};
|
||||
|
||||
TestConnector.prototype.dropTable = function(model, cb) {
|
||||
var err;
|
||||
var exists = model in this._tables;
|
||||
let err;
|
||||
const exists = model in this._tables;
|
||||
if (!exists) {
|
||||
err = new Error('Model doesn\'t exist: ' + model);
|
||||
} else {
|
||||
|
@ -176,8 +176,8 @@ TestConnector.prototype.dropTable = function(model, cb) {
|
|||
};
|
||||
|
||||
TestConnector.prototype.createTable = function(model, cb) {
|
||||
var err;
|
||||
var exists = model in this._tables;
|
||||
let err;
|
||||
const exists = model in this._tables;
|
||||
if (exists) {
|
||||
err = new Error('Model already exists: ' + model);
|
||||
} else {
|
||||
|
@ -197,7 +197,7 @@ TestConnector.prototype.fromColumnValue = function(propertyDef, value) {
|
|||
};
|
||||
|
||||
TestConnector.prototype.beginTransaction = function(isolationLevel, cb) {
|
||||
var name = 'tx_' + transactionId++;
|
||||
const name = 'tx_' + transactionId++;
|
||||
cb(null, new MockTransaction(this, name));
|
||||
};
|
||||
|
||||
|
@ -210,12 +210,12 @@ TestConnector.prototype.rollback = function(tx, cb) {
|
|||
};
|
||||
|
||||
TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
|
||||
var transaction = options.transaction;
|
||||
var model = options.model;
|
||||
var useTransaction = transaction && transaction.connector === this &&
|
||||
const transaction = options.transaction;
|
||||
const model = options.model;
|
||||
const useTransaction = transaction && transaction.connector === this &&
|
||||
transaction.connection;
|
||||
var data = useTransaction ? transaction.connection.data : this.data;
|
||||
var name = useTransaction ? transaction.connection.name : undefined;
|
||||
const data = useTransaction ? transaction.connection.data : this.data;
|
||||
const name = useTransaction ? transaction.connection.name : undefined;
|
||||
if (sql.indexOf('INSERT') === 0) {
|
||||
data[model] = data[model] || [];
|
||||
data[model].push({sql: sql, params: params});
|
||||
|
@ -223,8 +223,8 @@ TestConnector.prototype.executeSQL = function(sql, params, options, callback) {
|
|||
callback(null, 1);
|
||||
} else {
|
||||
debug('SELECT', data, sql, name);
|
||||
var instances = data[model] || [];
|
||||
var result = sql.indexOf('count(*) as "cnt"') !== -1 ?
|
||||
const instances = data[model] || [];
|
||||
const result = sql.indexOf('count(*) as "cnt"') !== -1 ?
|
||||
[{cnt: instances.length}] : instances;
|
||||
callback(null, result);
|
||||
}
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var JSONStringPacker = require('../lib/json-string-packer');
|
||||
var expect = require('chai').expect;
|
||||
const JSONStringPacker = require('../lib/json-string-packer');
|
||||
const expect = require('chai').expect;
|
||||
|
||||
describe('JSONStringPacker', function() {
|
||||
var packer;
|
||||
let packer;
|
||||
|
||||
beforeEach(function createPacker() {
|
||||
packer = new JSONStringPacker();
|
||||
|
@ -35,7 +35,7 @@ describe('JSONStringPacker', function() {
|
|||
});
|
||||
|
||||
describe('roundtrip', function() {
|
||||
var TEST_CASES = {
|
||||
const TEST_CASES = {
|
||||
String: 'a-value',
|
||||
Object: {a: 1, b: 2},
|
||||
Buffer: new Buffer([1, 2, 3]),
|
||||
|
@ -47,7 +47,7 @@ describe('JSONStringPacker', function() {
|
|||
|
||||
Object.keys(TEST_CASES).forEach(function(tc) {
|
||||
it('works for ' + tc + ' values', function() {
|
||||
var value = TEST_CASES[tc];
|
||||
const value = TEST_CASES[tc];
|
||||
return encodeAndDecode(value)
|
||||
.then(function(result) {
|
||||
expect(result).to.eql(value);
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var composer = require('../lib/model-key-composer');
|
||||
var expect = require('chai').expect;
|
||||
var Promise = require('bluebird');
|
||||
const composer = require('../lib/model-key-composer');
|
||||
const expect = require('chai').expect;
|
||||
const Promise = require('bluebird');
|
||||
|
||||
describe('ModelKeyComposer', function() {
|
||||
describe('compose()', function() {
|
||||
|
@ -66,7 +66,8 @@ describe('ModelKeyComposer', function() {
|
|||
},
|
||||
function onError(err) {
|
||||
expect(err).to.have.property('code', 'NO_MODEL_PREFIX');
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('supports invocation with a callback', function(done) {
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var assert = require('assert');
|
||||
var connector = require('../');
|
||||
const assert = require('assert');
|
||||
const connector = require('../');
|
||||
|
||||
describe('loopback-connector', function() {
|
||||
it('exports Connector', function() {
|
||||
|
|
132
test/sql.test.js
132
test/sql.test.js
|
@ -4,19 +4,19 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var expect = require('chai').expect;
|
||||
var SQLConnector = require('../lib/sql');
|
||||
var ParameterizedSQL = SQLConnector.ParameterizedSQL;
|
||||
var testConnector = require('./connectors/test-sql-connector');
|
||||
const expect = require('chai').expect;
|
||||
const SQLConnector = require('../lib/sql');
|
||||
const ParameterizedSQL = SQLConnector.ParameterizedSQL;
|
||||
const testConnector = require('./connectors/test-sql-connector');
|
||||
|
||||
var juggler = require('loopback-datasource-juggler');
|
||||
var ds = new juggler.DataSource({
|
||||
const juggler = require('loopback-datasource-juggler');
|
||||
const ds = new juggler.DataSource({
|
||||
connector: testConnector,
|
||||
debug: true,
|
||||
});
|
||||
/* eslint-disable one-var */
|
||||
var connector;
|
||||
var Customer;
|
||||
let connector;
|
||||
let Customer;
|
||||
/* eslint-enable one-var */
|
||||
|
||||
describe('sql connector', function() {
|
||||
|
@ -61,32 +61,32 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('should map table name', function() {
|
||||
var table = connector.table('customer');
|
||||
const table = connector.table('customer');
|
||||
expect(table).to.eql('CUSTOMER');
|
||||
});
|
||||
|
||||
it('should map column name', function() {
|
||||
var column = connector.column('customer', 'name');
|
||||
const column = connector.column('customer', 'name');
|
||||
expect(column).to.eql('NAME');
|
||||
});
|
||||
|
||||
it('should map column name from name attribute', function() {
|
||||
var column = connector.column('customer', 'primaryAddress');
|
||||
const column = connector.column('customer', 'primaryAddress');
|
||||
expect(column).to.eql('primary_address');
|
||||
});
|
||||
|
||||
it('prefers database-specific column name over property name', function() {
|
||||
var column = connector.column('customer', 'lastName');
|
||||
const column = connector.column('customer', 'lastName');
|
||||
expect(column).to.eql('LASTNAME');
|
||||
});
|
||||
|
||||
it('prefers property name when database is different', function() {
|
||||
var column = connector.column('customer', 'middleName');
|
||||
const column = connector.column('customer', 'middleName');
|
||||
expect(column).to.eql('middle_name');
|
||||
});
|
||||
|
||||
it('should find column metadata', function() {
|
||||
var column = connector.columnMetadata('customer', 'name');
|
||||
const column = connector.columnMetadata('customer', 'name');
|
||||
expect(column).to.eql({
|
||||
column: 'NAME',
|
||||
dataType: 'VARCHAR',
|
||||
|
@ -95,37 +95,37 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('should map property name', function() {
|
||||
var prop = connector.propertyName('customer', 'NAME');
|
||||
const prop = connector.propertyName('customer', 'NAME');
|
||||
expect(prop).to.eql('name');
|
||||
});
|
||||
|
||||
it('should map id column name', function() {
|
||||
var idCol = connector.idColumn('customer');
|
||||
const idCol = connector.idColumn('customer');
|
||||
expect(idCol).to.eql('NAME');
|
||||
});
|
||||
|
||||
it('should find escaped id column name', function() {
|
||||
var idCol = connector.idColumnEscaped('customer');
|
||||
const idCol = connector.idColumnEscaped('customer');
|
||||
expect(idCol).to.eql('`NAME`');
|
||||
});
|
||||
|
||||
it('should find escaped table name', function() {
|
||||
var table = connector.tableEscaped('customer');
|
||||
const table = connector.tableEscaped('customer');
|
||||
expect(table).to.eql('`CUSTOMER`');
|
||||
});
|
||||
|
||||
it('should find escaped column name', function() {
|
||||
var column = connector.columnEscaped('customer', 'vip');
|
||||
const column = connector.columnEscaped('customer', 'vip');
|
||||
expect(column).to.eql('`VIP`');
|
||||
});
|
||||
|
||||
it('should convert to escaped id column value', function() {
|
||||
var column = connector.idColumnValue('customer', 'John');
|
||||
const column = connector.idColumnValue('customer', 'John');
|
||||
expect(column).to.eql('John');
|
||||
});
|
||||
|
||||
it('builds where', function() {
|
||||
var where = connector.buildWhere('customer', {name: 'John'});
|
||||
const where = connector.buildWhere('customer', {name: 'John'});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE `NAME`=?',
|
||||
params: ['John'],
|
||||
|
@ -133,7 +133,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with null', function() {
|
||||
var where = connector.buildWhere('customer', {name: null});
|
||||
const where = connector.buildWhere('customer', {name: null});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE `NAME` IS NULL',
|
||||
params: [],
|
||||
|
@ -141,7 +141,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with inq', function() {
|
||||
var where = connector.buildWhere('customer', {name: {inq: ['John', 'Mary']}});
|
||||
const where = connector.buildWhere('customer', {name: {inq: ['John', 'Mary']}});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE `NAME` IN (?,?)',
|
||||
params: ['John', 'Mary'],
|
||||
|
@ -149,7 +149,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with or', function() {
|
||||
var where = connector.buildWhere('customer',
|
||||
const where = connector.buildWhere('customer',
|
||||
{or: [{name: 'John'}, {name: 'Mary'}]});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE (`NAME`=?) OR (`NAME`=?)',
|
||||
|
@ -158,7 +158,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with and', function() {
|
||||
var where = connector.buildWhere('customer',
|
||||
const where = connector.buildWhere('customer',
|
||||
{and: [{name: 'John'}, {vip: true}]});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE (`NAME`=?) AND (`VIP`=?)',
|
||||
|
@ -167,7 +167,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp string that does not have flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: '^J',
|
||||
},
|
||||
|
@ -179,7 +179,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp string that has flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: '^J/i',
|
||||
},
|
||||
|
@ -191,7 +191,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp literal that does not have flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: /^J/,
|
||||
},
|
||||
|
@ -203,7 +203,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp literal that has flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: /^J/i,
|
||||
},
|
||||
|
@ -215,7 +215,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp object that does not have flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: new RegExp(/^J/),
|
||||
},
|
||||
|
@ -227,7 +227,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with a regexp object that has flags', function() {
|
||||
var where = connector.buildWhere('customer', {
|
||||
const where = connector.buildWhere('customer', {
|
||||
name: {
|
||||
regexp: new RegExp(/^J/i),
|
||||
},
|
||||
|
@ -239,7 +239,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds where with nesting and/or', function() {
|
||||
var where = connector.buildWhere('customer',
|
||||
const where = connector.buildWhere('customer',
|
||||
{and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]});
|
||||
expect(where.toJSON()).to.eql({
|
||||
sql: 'WHERE (`NAME`=?) AND ((`VIP`=?) OR (`ADDRESS` IS NULL))',
|
||||
|
@ -248,32 +248,34 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds order by with one field', function() {
|
||||
var orderBy = connector.buildOrderBy('customer', 'name');
|
||||
const orderBy = connector.buildOrderBy('customer', 'name');
|
||||
expect(orderBy).to.eql('ORDER BY `NAME`');
|
||||
});
|
||||
|
||||
it('builds order by with two fields', function() {
|
||||
var orderBy = connector.buildOrderBy('customer', ['name', 'vip']);
|
||||
const orderBy = connector.buildOrderBy('customer', ['name', 'vip']);
|
||||
expect(orderBy).to.eql('ORDER BY `NAME`,`VIP`');
|
||||
});
|
||||
|
||||
it('builds order by with two fields and dirs', function() {
|
||||
var orderBy = connector.buildOrderBy('customer', ['name ASC', 'vip DESC']);
|
||||
const orderBy = connector.buildOrderBy('customer', ['name ASC', 'vip DESC']);
|
||||
expect(orderBy).to.eql('ORDER BY `NAME` ASC,`VIP` DESC');
|
||||
});
|
||||
|
||||
it('builds fields for columns', function() {
|
||||
var fields = connector.buildFields('customer',
|
||||
const fields = connector.buildFields('customer',
|
||||
{name: 'John', vip: true, unknown: 'Random'});
|
||||
expect(fields.names).to.eql(['`NAME`', '`VIP`']);
|
||||
expect(fields.columnValues[0].toJSON()).to.eql(
|
||||
{sql: '?', params: ['John']});
|
||||
{sql: '?', params: ['John']}
|
||||
);
|
||||
expect(fields.columnValues[1].toJSON()).to.eql(
|
||||
{sql: '?', params: [true]});
|
||||
{sql: '?', params: [true]}
|
||||
);
|
||||
});
|
||||
|
||||
it('builds fields for UPDATE without ids', function() {
|
||||
var fields = connector.buildFieldsForUpdate('customer',
|
||||
const fields = connector.buildFieldsForUpdate('customer',
|
||||
{name: 'John', vip: true});
|
||||
expect(fields.toJSON()).to.eql({
|
||||
sql: 'SET `VIP`=?',
|
||||
|
@ -282,7 +284,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds fields for UPDATE with ids', function() {
|
||||
var fields = connector.buildFieldsForUpdate('customer',
|
||||
const fields = connector.buildFieldsForUpdate('customer',
|
||||
{name: 'John', vip: true}, false);
|
||||
expect(fields.toJSON()).to.eql({
|
||||
sql: 'SET `NAME`=?,`VIP`=?',
|
||||
|
@ -291,18 +293,18 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds column names for SELECT', function() {
|
||||
var cols = connector.buildColumnNames('customer');
|
||||
const cols = connector.buildColumnNames('customer');
|
||||
expect(cols).to.eql('`NAME`,`middle_name`,`LASTNAME`,`VIP`,' +
|
||||
'`primary_address`,`ADDRESS`');
|
||||
});
|
||||
|
||||
it('builds column names with true fields filter for SELECT', function() {
|
||||
var cols = connector.buildColumnNames('customer', {fields: {name: true}});
|
||||
const cols = connector.buildColumnNames('customer', {fields: {name: true}});
|
||||
expect(cols).to.eql('`NAME`');
|
||||
});
|
||||
|
||||
it('builds column names with false fields filter for SELECT', function() {
|
||||
var cols = connector.buildColumnNames('customer', {
|
||||
const cols = connector.buildColumnNames('customer', {
|
||||
fields: {
|
||||
name: false,
|
||||
primaryAddress: false,
|
||||
|
@ -314,12 +316,12 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds column names with array fields filter for SELECT', function() {
|
||||
var cols = connector.buildColumnNames('customer', {fields: ['name']});
|
||||
const cols = connector.buildColumnNames('customer', {fields: ['name']});
|
||||
expect(cols).to.eql('`NAME`');
|
||||
});
|
||||
|
||||
it('builds DELETE', function() {
|
||||
var sql = connector.buildDelete('customer', {name: 'John'});
|
||||
const sql = connector.buildDelete('customer', {name: 'John'});
|
||||
expect(sql.toJSON()).to.eql({
|
||||
sql: 'DELETE FROM `CUSTOMER` WHERE `NAME`=$1',
|
||||
params: ['John'],
|
||||
|
@ -327,7 +329,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds UPDATE', function() {
|
||||
var sql = connector.buildUpdate('customer', {name: 'John'}, {vip: false});
|
||||
const sql = connector.buildUpdate('customer', {name: 'John'}, {vip: false});
|
||||
expect(sql.toJSON()).to.eql({
|
||||
sql: 'UPDATE `CUSTOMER` SET `VIP`=$1 WHERE `NAME`=$2',
|
||||
params: [false, 'John'],
|
||||
|
@ -335,7 +337,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds SELECT', function() {
|
||||
var sql = connector.buildSelect('customer',
|
||||
const sql = connector.buildSelect('customer',
|
||||
{order: 'name', limit: 5, where: {name: 'John'}});
|
||||
expect(sql.toJSON()).to.eql({
|
||||
sql: 'SELECT `NAME`,`middle_name`,`LASTNAME`,`VIP`,`primary_address`,`ADDRESS`' +
|
||||
|
@ -346,7 +348,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('builds INSERT', function() {
|
||||
var sql = connector.buildInsert('customer', {name: 'John', vip: true});
|
||||
const sql = connector.buildInsert('customer', {name: 'John', vip: true});
|
||||
expect(sql.toJSON()).to.eql({
|
||||
sql: 'INSERT INTO `CUSTOMER`(`NAME`,`VIP`) VALUES($1,$2)',
|
||||
params: ['John', true],
|
||||
|
@ -354,51 +356,53 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('normalizes a SQL statement from string', function() {
|
||||
var sql = 'SELECT * FROM `CUSTOMER`';
|
||||
var stmt = new ParameterizedSQL(sql);
|
||||
const sql = 'SELECT * FROM `CUSTOMER`';
|
||||
const stmt = new ParameterizedSQL(sql);
|
||||
expect(stmt.toJSON()).to.eql({sql: sql, params: []});
|
||||
});
|
||||
|
||||
it('normalizes a SQL statement from object without params', function() {
|
||||
var sql = {sql: 'SELECT * FROM `CUSTOMER`'};
|
||||
var stmt = new ParameterizedSQL(sql);
|
||||
const sql = {sql: 'SELECT * FROM `CUSTOMER`'};
|
||||
const stmt = new ParameterizedSQL(sql);
|
||||
expect(stmt.toJSON()).to.eql({sql: sql.sql, params: []});
|
||||
});
|
||||
|
||||
it('normalizes a SQL statement from object with params', function() {
|
||||
var sql =
|
||||
const sql =
|
||||
{sql: 'SELECT * FROM `CUSTOMER` WHERE `NAME`=?', params: ['John']};
|
||||
var stmt = new ParameterizedSQL(sql);
|
||||
const stmt = new ParameterizedSQL(sql);
|
||||
expect(stmt.toJSON()).to.eql({sql: sql.sql, params: ['John']});
|
||||
});
|
||||
|
||||
it('should throw if the statement is not a string or object', function() {
|
||||
expect(function() {
|
||||
/* jshint unused:false */
|
||||
var stmt = new ParameterizedSQL(true);
|
||||
const stmt = new ParameterizedSQL(true);
|
||||
}).to.throw('sql must be a string');
|
||||
});
|
||||
|
||||
it('concats SQL statements', function() {
|
||||
var stmt1 = {sql: 'SELECT * from `CUSTOMER`'};
|
||||
var where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
||||
let stmt1 = {sql: 'SELECT * from `CUSTOMER`'};
|
||||
const where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
||||
stmt1 = ParameterizedSQL.append(stmt1, where);
|
||||
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() {
|
||||
var stmt1 = 'SELECT * from `CUSTOMER`';
|
||||
var where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
||||
let stmt1 = 'SELECT * from `CUSTOMER`';
|
||||
const where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
||||
stmt1 = ParameterizedSQL.append(stmt1, where);
|
||||
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() {
|
||||
expect(function() {
|
||||
var stmt1 = 'SELECT * from `CUSTOMER`';
|
||||
var where = {sql: 'WHERE `NAME`=?', params: ['John', 'Mary']};
|
||||
let stmt1 = 'SELECT * from `CUSTOMER`';
|
||||
const where = {sql: 'WHERE `NAME`=?', params: ['John', 'Mary']};
|
||||
stmt1 = ParameterizedSQL.append(stmt1, where);
|
||||
}).to.throw('must match the number of params');
|
||||
});
|
||||
|
@ -438,7 +442,7 @@ describe('sql connector', function() {
|
|||
});
|
||||
|
||||
it('should invoke hooks', function(done) {
|
||||
var events = [];
|
||||
const events = [];
|
||||
connector.observe('before execute', function(ctx, next) {
|
||||
expect(ctx.req.sql).be.a('string');
|
||||
expect(ctx.req.params).be.a('array');
|
||||
|
|
|
@ -4,16 +4,16 @@
|
|||
// License text available at https://opensource.org/licenses/MIT
|
||||
|
||||
'use strict';
|
||||
var Transaction = require('../index').Transaction;
|
||||
const Transaction = require('../index').Transaction;
|
||||
|
||||
const chai = require('chai');
|
||||
chai.use(require('chai-as-promised'));
|
||||
const {expect} = chai;
|
||||
const chaiAsPromised = require('chai-as-promised');
|
||||
var testConnector = require('./connectors/test-sql-connector');
|
||||
const testConnector = require('./connectors/test-sql-connector');
|
||||
|
||||
var juggler = require('loopback-datasource-juggler');
|
||||
var db, Post, Review;
|
||||
const juggler = require('loopback-datasource-juggler');
|
||||
let db, Post, Review;
|
||||
describe('transactions', function() {
|
||||
before(function(done) {
|
||||
db = new juggler.DataSource({
|
||||
|
@ -34,8 +34,8 @@ describe('transactions', function() {
|
|||
});
|
||||
});
|
||||
|
||||
var currentTx;
|
||||
var hooks = [];
|
||||
let currentTx;
|
||||
let hooks = [];
|
||||
// Return an async function to start a transaction and create a post
|
||||
function createPostInTx(post, timeout) {
|
||||
return function(done) {
|
||||
|
@ -87,7 +87,7 @@ describe('transactions', function() {
|
|||
// records to equal to the count
|
||||
function expectToFindPosts(where, count, inTx) {
|
||||
return function(done) {
|
||||
var options = {model: 'Post'};
|
||||
const options = {model: 'Post'};
|
||||
if (inTx) {
|
||||
options.transaction = currentTx;
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ describe('transactions', function() {
|
|||
}
|
||||
|
||||
describe('commit', function() {
|
||||
var post = {title: 't1', content: 'c1'};
|
||||
const post = {title: 't1', content: 'c1'};
|
||||
before(createPostInTx(post));
|
||||
|
||||
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
|
||||
|
@ -150,7 +150,7 @@ describe('transactions', function() {
|
|||
db.connector.data = {};
|
||||
});
|
||||
|
||||
var post = {title: 't2', content: 'c2'};
|
||||
const post = {title: 't2', content: 'c2'};
|
||||
before(createPostInTx(post));
|
||||
|
||||
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
|
||||
|
@ -182,7 +182,7 @@ describe('transactions', function() {
|
|||
db.connector.data = {};
|
||||
});
|
||||
|
||||
var post = {title: 't3', content: 'c3'};
|
||||
const post = {title: 't3', content: 'c3'};
|
||||
beforeEach(createPostInTx(post, TIMEOUT));
|
||||
|
||||
it('should report timeout', function(done) {
|
||||
|
@ -219,7 +219,7 @@ describe('transactions', function() {
|
|||
TestTransaction.prototype.foo = true;
|
||||
function beginTransaction(isolationLevel, cb) {
|
||||
return cb(null, new TestTransaction(testConnector, {}));
|
||||
};
|
||||
}
|
||||
|
||||
it('should do nothing when transaction is like a Transaction', function(done) {
|
||||
testConnector.initialize(db, function(err, resultConnector) {
|
||||
|
@ -275,7 +275,8 @@ describe('transactions', function() {
|
|||
return cb(null, 'begun');
|
||||
};
|
||||
|
||||
return expect(Transaction.begin(connectorObject, '')
|
||||
return expect(
|
||||
Transaction.begin(connectorObject, ''),
|
||||
).to.eventually.be.instanceOf(Transaction);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue