diff --git a/dist/js-data-sql.js b/dist/js-data-sql.js index 257543f..f2d846e 100644 --- a/dist/js-data-sql.js +++ b/dist/js-data-sql.js @@ -45,20 +45,21 @@ module.exports = /* 0 */ /***/ function(module, exports, __webpack_require__) { - var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })(); + 'use strict'; - var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); + var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); - function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } + var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); + + function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var knex = __webpack_require__(1); var JSData = __webpack_require__(2); - var map = __webpack_require__(3); - var underscore = __webpack_require__(4); - var unique = __webpack_require__(5); - var toString = __webpack_require__(6); + var underscore = __webpack_require__(3); + var unique = __webpack_require__(4); + var toString = __webpack_require__(5); var DSUtils = JSData.DSUtils; var reserved = ['orderBy', 'sort', 'limit', 'offset', 'skip', 'where']; @@ -78,9 +79,9 @@ module.exports = var relationDef = resourceConfig.getResource(relationName); var containedName = null; - if (DSUtils.contains(options['with'], relationName)) { + if (DSUtils.contains(options.with, relationName)) { containedName = relationName; - } else if (DSUtils.contains(options['with'], def.localField)) { + } else if (DSUtils.contains(options.with, def.localField)) { containedName = def.localField; } else { return; @@ -89,7 +90,7 @@ module.exports = var __options = DSUtils.deepMixIn({}, options.orig ? options.orig() : options); // Filter to only properties under current relation - __options['with'] = options['with'].filter(function (relation) { + __options.with = options.with.filter(function (relation) { return relation !== containedName && relation.indexOf(containedName) === 0 && relation.length >= containedName.length && relation[containedName.length] === '.'; }).map(function (relation) { return relation.substr(containedName.length + 1); @@ -102,7 +103,7 @@ module.exports = if (instance) { foreignKeyFilter = { '==': instance[resourceConfig.idAttribute] }; } else { - foreignKeyFilter = { 'in': map(items, function (item) { + foreignKeyFilter = { 'in': items.map(function (item) { return item[resourceConfig.idAttribute]; }) }; } @@ -178,7 +179,7 @@ module.exports = } else { task = _this.findAll(resourceConfig.getResource(relationName), { where: _defineProperty({}, relationDef.idAttribute, { - 'in': DSUtils.filter(map(items, function (item) { + 'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey); }), function (x) { return x; @@ -225,7 +226,7 @@ module.exports = var instance = undefined; options = options || {}; - options['with'] = options['with'] || []; + options.with = options.with || []; var query = options && options.transaction || this.query; return query.select('*').from(getTable(resourceConfig)).where(resourceConfig.idAttribute, toString(id)).then(function (rows) { if (!rows.length) { @@ -245,7 +246,7 @@ module.exports = var items = null; options = options || {}; - options['with'] = options['with'] || []; + options.with = options.with || []; return this.filterQuery(resourceConfig, params, options).then(function (_items) { items = _items; return loadWithRelations.call(_this3, _items, resourceConfig, options); @@ -288,7 +289,7 @@ module.exports = attrs = DSUtils.removeCircular(DSUtils.omit(attrs, resourceConfig.relationFields || [])); return this.filterQuery(resourceConfig, params, options).then(function (items) { - return map(items, function (item) { + return items.map(function (item) { return item[resourceConfig.idAttribute]; }); }).then(function (ids) { @@ -369,7 +370,7 @@ module.exports = var relationPath = []; - var _loop = function () { + var _loop = function _loop() { var relationName = parts.shift(); var relationResourceConfig = resourceConfig.getResource(relationName); relationPath.push(relationName); @@ -377,13 +378,13 @@ module.exports = if (!joinedTables.some(function (t) { return t === relationPath.join('.'); })) { - var _localResourceConfig$relationList$filter = localResourceConfig.relationList.filter(function (r) { + var _localResourceConfig$ = localResourceConfig.relationList.filter(function (r) { return r.relation === relationName; }); - var _localResourceConfig$relationList$filter2 = _slicedToArray(_localResourceConfig$relationList$filter, 1); + var _localResourceConfig$2 = _slicedToArray(_localResourceConfig$, 1); - var relation = _localResourceConfig$relationList$filter2[0]; + var relation = _localResourceConfig$2[0]; if (relation) { var _table = getTable(localResourceConfig); @@ -501,24 +502,18 @@ module.exports = /***/ }, /* 3 */ -/***/ function(module, exports) { - - module.exports = require("mout/array/map"); - -/***/ }, -/* 4 */ /***/ function(module, exports) { module.exports = require("mout/string/underscore"); /***/ }, -/* 5 */ +/* 4 */ /***/ function(module, exports) { module.exports = require("mout/array/unique"); /***/ }, -/* 6 */ +/* 5 */ /***/ function(module, exports) { module.exports = require("mout/lang/toString"); diff --git a/mocha.start.js b/mocha.start.js index 9954e70..362df27 100644 --- a/mocha.start.js +++ b/mocha.start.js @@ -44,10 +44,11 @@ TestRunner.init({ }); beforeEach(function () { - globals.DSUtils = global.DSUtils = this.$$DSUtils - globals.DSErrors = global.DSErrors = this.$$DSErrors - globals.adapter = global.adapter = this.$$adapter - globals.User = global.User = this.$$User + globals.DSUtils = global.DSUtils = this.$$DSUtils; + globals.DSErrors = global.DSErrors = this.$$DSErrors; + globals.adapter = global.adapter = this.$$adapter; + globals.store = global.store = this.$$store; + globals.User = global.User = this.$$User; globals.Profile = global.Profile = this.$$Profile; globals.Post = global.Post = this.$$Post; globals.Comment = global.Comment = this.$$Comment; diff --git a/package.json b/package.json index eee2978..3bdbb22 100644 --- a/package.json +++ b/package.json @@ -25,22 +25,6 @@ "mariadb", "sqlite" ], - "devDependencies": { - "babel-core": "5.8.25", - "babel-eslint": "4.1.3", - "babel-loader": "5.3.2", - "bluebird": "2.10.2", - "chai": "3.4.1", - "co": "4.6.0", - "co-mocha": "1.1.2", - "codacy-coverage": "1.1.3", - "coveralls": "2.11.4", - "istanbul": "0.4.0", - "js-data-adapter-tests": "~1.x", - "mocha": "2.3.3", - "standard": "5.3.1", - "webpack": "1.12.2" - }, "scripts": { "lint": "standard src/index.js", "build": "webpack --config webpack.config.js --progress --colors", @@ -53,10 +37,27 @@ "parser": "babel-eslint" }, "dependencies": { - "mout": "0.11.0" + "mout": "0.11.1" }, "peerDependencies": { "js-data": "~2.x", "knex": ">=0.7.4" + }, + "devDependencies": { + "babel-core": "6.1.4", + "babel-eslint": "4.1.5", + "babel-loader": "6.1.0", + "babel-preset-es2015": "6.1.2", + "bluebird": "2.10.2", + "chai": "3.4.1", + "co": "4.6.0", + "co-mocha": "1.1.2", + "codacy-coverage": "1.1.3", + "coveralls": "2.11.4", + "istanbul": "0.4.0", + "js-data-adapter-tests": "~1.x", + "mocha": "2.3.3", + "standard": "5.3.1", + "webpack": "1.12.2" } } diff --git a/src/index.js b/src/index.js index da03cc9..37df697 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,5 @@ let knex = require('knex') let JSData = require('js-data') -let map = require('mout/array/map') let underscore = require('mout/string/underscore') let unique = require('mout/array/unique') let toString = require('mout/lang/toString') @@ -53,7 +52,7 @@ function loadWithRelations (items, resourceConfig, options) { if (instance) { foreignKeyFilter = { '==': instance[resourceConfig.idAttribute] } } else { - foreignKeyFilter = { 'in': map(items, item => item[resourceConfig.idAttribute]) } + foreignKeyFilter = { 'in': items.map(function (item) { return item[resourceConfig.idAttribute] }) } } task = this.findAll(resourceConfig.getResource(relationName), { where: { @@ -124,7 +123,7 @@ function loadWithRelations (items, resourceConfig, options) { task = this.findAll(resourceConfig.getResource(relationName), { where: { [relationDef.idAttribute]: { - 'in': DSUtils.filter(map(items, item => DSUtils.get(item, def.localKey)), x => x) + 'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x) } } }, __options).then(relatedItems => { @@ -217,7 +216,7 @@ class DSSqlAdapter { updateAll (resourceConfig, attrs, params, options) { attrs = DSUtils.removeCircular(DSUtils.omit(attrs, resourceConfig.relationFields || [])) return this.filterQuery(resourceConfig, params, options).then(items => { - return map(items, item => item[resourceConfig.idAttribute]) + return items.map(function (item) { return item[resourceConfig.idAttribute] }) }).then(ids => { return this.filterQuery(resourceConfig, params, options).update(attrs).then(() => { let _params = {where: {}} diff --git a/test/findAll.spec.js b/test/findAll.spec.js new file mode 100644 index 0000000..58882a1 --- /dev/null +++ b/test/findAll.spec.js @@ -0,0 +1,21 @@ +describe('DSSqlAdapter#findAll', function () { + it('should not return relation columns on parent', function* () { + var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' }); + var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id}); + + var users = yield adapter.findAll(User, {'profile.email': 'foo@test.com'}); + assert.equal(users.length, 1); + assert.equal(users[0].profileId, profile1.id); + assert.isUndefined(users[0].email); + }); + + it('should filter when relations have same column if column is qualified', function* () { + var profile1 = yield adapter.create(Profile, { email: 'foo@test.com' }); + var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id}); + + // `id` column must be qualified with `user.` + var users = yield adapter.findAll(User, {'user.id': user1.id, 'profile.email': 'foo@test.com'}); + assert.equal(users.length, 1); + assert.equal(users[0].profileId, profile1.id); + }); +}); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 0391ee0..7c89b6d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,5 +1,4 @@ module.exports = { - debug: true, entry: './src/index.js', output: { filename: './dist/js-data-sql.js', @@ -7,18 +6,22 @@ module.exports = { library: 'js-data-sql' }, externals: [ - 'mout/array/map', - 'mout/lang/toString', 'mout/string/underscore', + 'mout/lang/toString', 'mout/array/unique', 'js-data', 'knex' ], module: { - loaders: [{ - test: /(src)(.+)\.js$/, - exclude: /node_modules/, - loader: 'babel-loader?blacklist=useStrict' - }] + loaders: [ + { + test: /(src)(.+)\.js$/, + exclude: /node_modules/, + loader: 'babel', + query: { + presets: ['es2015'] + } + } + ] } };