Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
69 changes: 39 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions test/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
58 changes: 58 additions & 0 deletions test/findAll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});