Skip to content

Update master #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions dist/js-data-sql.js
Original file line number Diff line number Diff line change
@@ -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,21 +370,21 @@ module.exports =

var relationPath = [];

var _loop = function () {
var _loop = function _loop() {
var relationName = parts.shift();
var relationResourceConfig = resourceConfig.getResource(relationName);
relationPath.push(relationName);

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");
9 changes: 5 additions & 4 deletions mocha.start.js
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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: {}}
21 changes: 21 additions & 0 deletions test/findAll.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe('DSSqlAdapter#findAll', function () {
it('should not return relation columns on parent', function* () {
var profile1 = yield adapter.create(Profile, { email: '[email protected]' });
var user1 = yield adapter.create(User, {name: 'John', profileId: profile1.id});

var users = yield adapter.findAll(User, {'profile.email': '[email protected]'});
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: '[email protected]' });
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': '[email protected]'});
assert.equal(users.length, 1);
assert.equal(users[0].profileId, profile1.id);
});
});
19 changes: 11 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
module.exports = {
debug: true,
entry: './src/index.js',
output: {
filename: './dist/js-data-sql.js',
libraryTarget: 'commonjs2',
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']
}
}
]
}
};