Skip to content

Commit 9dbd048

Browse files
committed
Merge pull request #40 from js-data/develop
Update master
2 parents 1121614 + 99a92f8 commit 9dbd048

File tree

6 files changed

+80
-60
lines changed

6 files changed

+80
-60
lines changed

dist/js-data-sql.js

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ module.exports =
4545
/* 0 */
4646
/***/ function(module, exports, __webpack_require__) {
4747

48-
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'); } }; })();
48+
'use strict';
4949

50-
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; }; })();
50+
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"); } }; })();
5151

52-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
52+
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; }; })();
53+
54+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
5355

5456
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; }
5557

5658
var knex = __webpack_require__(1);
5759
var JSData = __webpack_require__(2);
58-
var map = __webpack_require__(3);
59-
var underscore = __webpack_require__(4);
60-
var unique = __webpack_require__(5);
61-
var toString = __webpack_require__(6);
60+
var underscore = __webpack_require__(3);
61+
var unique = __webpack_require__(4);
62+
var toString = __webpack_require__(5);
6263
var DSUtils = JSData.DSUtils;
6364

6465
var reserved = ['orderBy', 'sort', 'limit', 'offset', 'skip', 'where'];
@@ -78,9 +79,9 @@ module.exports =
7879
var relationDef = resourceConfig.getResource(relationName);
7980

8081
var containedName = null;
81-
if (DSUtils.contains(options['with'], relationName)) {
82+
if (DSUtils.contains(options.with, relationName)) {
8283
containedName = relationName;
83-
} else if (DSUtils.contains(options['with'], def.localField)) {
84+
} else if (DSUtils.contains(options.with, def.localField)) {
8485
containedName = def.localField;
8586
} else {
8687
return;
@@ -89,7 +90,7 @@ module.exports =
8990
var __options = DSUtils.deepMixIn({}, options.orig ? options.orig() : options);
9091

9192
// Filter to only properties under current relation
92-
__options['with'] = options['with'].filter(function (relation) {
93+
__options.with = options.with.filter(function (relation) {
9394
return relation !== containedName && relation.indexOf(containedName) === 0 && relation.length >= containedName.length && relation[containedName.length] === '.';
9495
}).map(function (relation) {
9596
return relation.substr(containedName.length + 1);
@@ -102,7 +103,7 @@ module.exports =
102103
if (instance) {
103104
foreignKeyFilter = { '==': instance[resourceConfig.idAttribute] };
104105
} else {
105-
foreignKeyFilter = { 'in': map(items, function (item) {
106+
foreignKeyFilter = { 'in': items.map(function (item) {
106107
return item[resourceConfig.idAttribute];
107108
}) };
108109
}
@@ -178,7 +179,7 @@ module.exports =
178179
} else {
179180
task = _this.findAll(resourceConfig.getResource(relationName), {
180181
where: _defineProperty({}, relationDef.idAttribute, {
181-
'in': DSUtils.filter(map(items, function (item) {
182+
'in': DSUtils.filter(items.map(function (item) {
182183
return DSUtils.get(item, def.localKey);
183184
}), function (x) {
184185
return x;
@@ -225,7 +226,7 @@ module.exports =
225226

226227
var instance = undefined;
227228
options = options || {};
228-
options['with'] = options['with'] || [];
229+
options.with = options.with || [];
229230
var query = options && options.transaction || this.query;
230231
return query.select('*').from(getTable(resourceConfig)).where(resourceConfig.idAttribute, toString(id)).then(function (rows) {
231232
if (!rows.length) {
@@ -245,7 +246,7 @@ module.exports =
245246

246247
var items = null;
247248
options = options || {};
248-
options['with'] = options['with'] || [];
249+
options.with = options.with || [];
249250
return this.filterQuery(resourceConfig, params, options).then(function (_items) {
250251
items = _items;
251252
return loadWithRelations.call(_this3, _items, resourceConfig, options);
@@ -288,7 +289,7 @@ module.exports =
288289

289290
attrs = DSUtils.removeCircular(DSUtils.omit(attrs, resourceConfig.relationFields || []));
290291
return this.filterQuery(resourceConfig, params, options).then(function (items) {
291-
return map(items, function (item) {
292+
return items.map(function (item) {
292293
return item[resourceConfig.idAttribute];
293294
});
294295
}).then(function (ids) {
@@ -369,21 +370,21 @@ module.exports =
369370

370371
var relationPath = [];
371372

372-
var _loop = function () {
373+
var _loop = function _loop() {
373374
var relationName = parts.shift();
374375
var relationResourceConfig = resourceConfig.getResource(relationName);
375376
relationPath.push(relationName);
376377

377378
if (!joinedTables.some(function (t) {
378379
return t === relationPath.join('.');
379380
})) {
380-
var _localResourceConfig$relationList$filter = localResourceConfig.relationList.filter(function (r) {
381+
var _localResourceConfig$ = localResourceConfig.relationList.filter(function (r) {
381382
return r.relation === relationName;
382383
});
383384

384-
var _localResourceConfig$relationList$filter2 = _slicedToArray(_localResourceConfig$relationList$filter, 1);
385+
var _localResourceConfig$2 = _slicedToArray(_localResourceConfig$, 1);
385386

386-
var relation = _localResourceConfig$relationList$filter2[0];
387+
var relation = _localResourceConfig$2[0];
387388

388389
if (relation) {
389390
var _table = getTable(localResourceConfig);
@@ -501,24 +502,18 @@ module.exports =
501502

502503
/***/ },
503504
/* 3 */
504-
/***/ function(module, exports) {
505-
506-
module.exports = require("mout/array/map");
507-
508-
/***/ },
509-
/* 4 */
510505
/***/ function(module, exports) {
511506

512507
module.exports = require("mout/string/underscore");
513508

514509
/***/ },
515-
/* 5 */
510+
/* 4 */
516511
/***/ function(module, exports) {
517512

518513
module.exports = require("mout/array/unique");
519514

520515
/***/ },
521-
/* 6 */
516+
/* 5 */
522517
/***/ function(module, exports) {
523518

524519
module.exports = require("mout/lang/toString");

mocha.start.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ TestRunner.init({
4444
});
4545

4646
beforeEach(function () {
47-
globals.DSUtils = global.DSUtils = this.$$DSUtils
48-
globals.DSErrors = global.DSErrors = this.$$DSErrors
49-
globals.adapter = global.adapter = this.$$adapter
50-
globals.User = global.User = this.$$User
47+
globals.DSUtils = global.DSUtils = this.$$DSUtils;
48+
globals.DSErrors = global.DSErrors = this.$$DSErrors;
49+
globals.adapter = global.adapter = this.$$adapter;
50+
globals.store = global.store = this.$$store;
51+
globals.User = global.User = this.$$User;
5152
globals.Profile = global.Profile = this.$$Profile;
5253
globals.Post = global.Post = this.$$Post;
5354
globals.Comment = global.Comment = this.$$Comment;

package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@
2525
"mariadb",
2626
"sqlite"
2727
],
28-
"devDependencies": {
29-
"babel-core": "5.8.25",
30-
"babel-eslint": "4.1.3",
31-
"babel-loader": "5.3.2",
32-
"bluebird": "2.10.2",
33-
"chai": "3.4.1",
34-
"co": "4.6.0",
35-
"co-mocha": "1.1.2",
36-
"codacy-coverage": "1.1.3",
37-
"coveralls": "2.11.4",
38-
"istanbul": "0.4.0",
39-
"js-data-adapter-tests": "~1.x",
40-
"mocha": "2.3.3",
41-
"standard": "5.3.1",
42-
"webpack": "1.12.2"
43-
},
4428
"scripts": {
4529
"lint": "standard src/index.js",
4630
"build": "webpack --config webpack.config.js --progress --colors",
@@ -53,10 +37,27 @@
5337
"parser": "babel-eslint"
5438
},
5539
"dependencies": {
56-
"mout": "0.11.0"
40+
"mout": "0.11.1"
5741
},
5842
"peerDependencies": {
5943
"js-data": "~2.x",
6044
"knex": ">=0.7.4"
45+
},
46+
"devDependencies": {
47+
"babel-core": "6.1.4",
48+
"babel-eslint": "4.1.5",
49+
"babel-loader": "6.1.0",
50+
"babel-preset-es2015": "6.1.2",
51+
"bluebird": "2.10.2",
52+
"chai": "3.4.1",
53+
"co": "4.6.0",
54+
"co-mocha": "1.1.2",
55+
"codacy-coverage": "1.1.3",
56+
"coveralls": "2.11.4",
57+
"istanbul": "0.4.0",
58+
"js-data-adapter-tests": "~1.x",
59+
"mocha": "2.3.3",
60+
"standard": "5.3.1",
61+
"webpack": "1.12.2"
6162
}
6263
}

src/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
let knex = require('knex')
22
let JSData = require('js-data')
3-
let map = require('mout/array/map')
43
let underscore = require('mout/string/underscore')
54
let unique = require('mout/array/unique')
65
let toString = require('mout/lang/toString')
@@ -53,7 +52,7 @@ function loadWithRelations (items, resourceConfig, options) {
5352
if (instance) {
5453
foreignKeyFilter = { '==': instance[resourceConfig.idAttribute] }
5554
} else {
56-
foreignKeyFilter = { 'in': map(items, item => item[resourceConfig.idAttribute]) }
55+
foreignKeyFilter = { 'in': items.map(function (item) { return item[resourceConfig.idAttribute] }) }
5756
}
5857
task = this.findAll(resourceConfig.getResource(relationName), {
5958
where: {
@@ -124,7 +123,7 @@ function loadWithRelations (items, resourceConfig, options) {
124123
task = this.findAll(resourceConfig.getResource(relationName), {
125124
where: {
126125
[relationDef.idAttribute]: {
127-
'in': DSUtils.filter(map(items, item => DSUtils.get(item, def.localKey)), x => x)
126+
'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x)
128127
}
129128
}
130129
}, __options).then(relatedItems => {
@@ -217,7 +216,7 @@ class DSSqlAdapter {
217216
updateAll (resourceConfig, attrs, params, options) {
218217
attrs = DSUtils.removeCircular(DSUtils.omit(attrs, resourceConfig.relationFields || []))
219218
return this.filterQuery(resourceConfig, params, options).then(items => {
220-
return map(items, item => item[resourceConfig.idAttribute])
219+
return items.map(function (item) { return item[resourceConfig.idAttribute] })
221220
}).then(ids => {
222221
return this.filterQuery(resourceConfig, params, options).update(attrs).then(() => {
223222
let _params = {where: {}}

test/findAll.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
describe('DSSqlAdapter#findAll', function () {
2+
it('should not return relation columns on parent', function* () {
3+
var profile1 = yield adapter.create(Profile, { email: '[email protected]' });
4+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id});
5+
6+
var users = yield adapter.findAll(User, {'profile.email': '[email protected]'});
7+
assert.equal(users.length, 1);
8+
assert.equal(users[0].profileId, profile1.id);
9+
assert.isUndefined(users[0].email);
10+
});
11+
12+
it('should filter when relations have same column if column is qualified', function* () {
13+
var profile1 = yield adapter.create(Profile, { email: '[email protected]' });
14+
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id});
15+
16+
// `id` column must be qualified with `user.`
17+
var users = yield adapter.findAll(User, {'user.id': user1.id, 'profile.email': '[email protected]'});
18+
assert.equal(users.length, 1);
19+
assert.equal(users[0].profileId, profile1.id);
20+
});
21+
});

webpack.config.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
module.exports = {
2-
debug: true,
32
entry: './src/index.js',
43
output: {
54
filename: './dist/js-data-sql.js',
65
libraryTarget: 'commonjs2',
76
library: 'js-data-sql'
87
},
98
externals: [
10-
'mout/array/map',
11-
'mout/lang/toString',
129
'mout/string/underscore',
10+
'mout/lang/toString',
1311
'mout/array/unique',
1412
'js-data',
1513
'knex'
1614
],
1715
module: {
18-
loaders: [{
19-
test: /(src)(.+)\.js$/,
20-
exclude: /node_modules/,
21-
loader: 'babel-loader?blacklist=useStrict'
22-
}]
16+
loaders: [
17+
{
18+
test: /(src)(.+)\.js$/,
19+
exclude: /node_modules/,
20+
loader: 'babel',
21+
query: {
22+
presets: ['es2015']
23+
}
24+
}
25+
]
2326
}
2427
};

0 commit comments

Comments
 (0)