diff --git a/package.json b/package.json index a61835c..3acf623 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "codacy-coverage": "1.1.3", "coveralls": "2.11.4", "es6-promise": "3.0.2", - "karma": "0.13.14", + "karma": "0.13.19", "karma-browserstack-launcher": "0.1.6", "karma-chai": "0.1.0", "karma-chrome-launcher": "0.2.1", diff --git a/src/index.js b/src/index.js index 930b358..b018883 100644 --- a/src/index.js +++ b/src/index.js @@ -54,42 +54,51 @@ class DSHttpAdapter { options = options || {} options.params = options.params || {} - let item - let parentKey = resourceConfig.parentKey let endpoint = options.hasOwnProperty('endpoint') ? options.endpoint : resourceConfig.endpoint - let parentField = resourceConfig.parentField - let parentDef = resourceConfig.getResource(resourceConfig.parent) - let parentId = options.params[parentKey] - - if (parentId === false || !parentKey || !parentDef) { - if (parentId === false) { - delete options.params[parentKey] + let parents = resourceConfig.parents || (resourceConfig.parent ? { + [resourceConfig.parent]: { + key: resourceConfig.parentKey, + field: resourceConfig.parentField } - return endpoint - } else { - delete options.params[parentKey] + } : {}) + + DSUtils.forOwn(parents, function (parent, parentName) { + let item + let parentKey = parent.key + let parentField = parent.field + let parentDef = resourceConfig.getResource(parentName) + let parentId = options.params[parentKey] + + if (parentId === false || !parentKey || !parentDef) { + if (parentId === false) { + delete options.params[parentKey] + } + } else { + delete options.params[parentKey] - if (DSUtils._sn(id)) { - item = resourceConfig.get(id) - } else if (DSUtils._o(id)) { - item = id - } + if (DSUtils._sn(id)) { + item = resourceConfig.get(id) + } else if (DSUtils._o(id)) { + item = id + } + console.log('item', item) - if (item) { - parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null) - } + if (item) { + parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null) + } - if (parentId) { - delete options.endpoint - let _options = {} - DSUtils.forOwn(options, (value, key) => { - _options[key] = value - }) - return DSUtils.makePath(this.getEndpoint(parentDef, parentId, DSUtils._(parentDef, _options)), parentId, endpoint) - } else { - return endpoint + if (parentId) { + delete options.endpoint + let _options = {} + DSUtils.forOwn(options, (value, key) => { + _options[key] = value + }) + endpoint = DSUtils.makePath(this.getEndpoint(parentDef, parentId, DSUtils._(parentDef, _options)), parentId, endpoint) + } } - } + }, this) + + return endpoint } getPath (method, resourceConfig, id, options) { diff --git a/test/find.spec.js b/test/find.spec.js index 70573cc..c6e3ec1 100644 --- a/test/find.spec.js +++ b/test/find.spec.js @@ -124,4 +124,33 @@ describe('DSHttpAdapter.find(resourceConfig, id, options)', function () { return otherAdapter.find(Post, 1); }); }); + + it('should use parent', function () { + var _this = this; + + var Thing = datastore.defineResource({ + name: 'thing', + endpoint: 'things', + relations: { + belongsTo: { + user: { + localKey: 'userId', + localField: 'user', + parent: true + } + } + } + }); + + setTimeout(function () { + assert.equal(1, _this.requests.length); + assert.equal(_this.requests[0].url, 'user/2/things/1'); + assert.equal(_this.requests[0].method, 'GET'); + _this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 1 })); + }, 30); + + return dsHttpAdapter.find(Thing, 1, { params: { userId: 2 } }).then(function (data) { + assert.deepEqual(data, { id: 1 }, 'thing should have been found'); + }); + }); }); diff --git a/test/findAll.spec.js b/test/findAll.spec.js index 2644b26..06f6165 100644 --- a/test/findAll.spec.js +++ b/test/findAll.spec.js @@ -32,4 +32,62 @@ describe('dsHttpAdapter.findAll(resourceConfig, params, options)', function () { assert.equal(queryTransform.callCount, 2, 'queryTransform should have been called'); }); }); + + it('should use one of many parents', function () { + var _this = this; + + var Thing = datastore.defineResource({ + name: 'thing', + endpoint: 'things', + relations: { + belongsTo: { + user: { + localKey: 'userId', + localField: 'user', + parent: true + }, + posts: { + localKey: 'postId', + localField: 'post', + parent: true + } + } + } + }); + + if (!Thing.parents) { + Thing.parents = { + user: { + key: 'userId', + field: 'user' + }, + posts: { + key: 'postId', + field: 'post' + } + } + } + + setTimeout(function () { + assert.equal(1, _this.requests.length); + assert.equal(_this.requests[0].url, 'user/1/things'); + assert.equal(_this.requests[0].method, 'GET'); + _this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify([{ id: 1 }])); + }, 30); + + return dsHttpAdapter.findAll(Thing, { userId: 1 }).then(function (data) { + assert.deepEqual(data, [{ id: 1 }], 'user thing should have been found'); + + setTimeout(function () { + assert.equal(2, _this.requests.length); + assert.equal(_this.requests[1].url, 'posts/2/things'); + assert.equal(_this.requests[1].method, 'GET'); + _this.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify([{ id: 2 }])); + }, 30); + + return dsHttpAdapter.findAll(Thing, { postId: 2 }); + }).then(function(data) { + assert.deepEqual(data, [{ id: 2 }], 'post thing should have been found'); + }); + }); });