Skip to content

Commit 0be03ed

Browse files
committed
Add support for multiple parents. Fixes #33
1 parent 7dc42bb commit 0be03ed

File tree

2 files changed

+97
-30
lines changed

2 files changed

+97
-30
lines changed

src/index.js

+39-30
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,51 @@ class DSHttpAdapter {
5454
options = options || {}
5555
options.params = options.params || {}
5656

57-
let item
58-
let parentKey = resourceConfig.parentKey
5957
let endpoint = options.hasOwnProperty('endpoint') ? options.endpoint : resourceConfig.endpoint
60-
let parentField = resourceConfig.parentField
61-
let parentDef = resourceConfig.getResource(resourceConfig.parent)
62-
let parentId = options.params[parentKey]
63-
64-
if (parentId === false || !parentKey || !parentDef) {
65-
if (parentId === false) {
66-
delete options.params[parentKey]
58+
let parents = resourceConfig.parents || (resourceConfig.parent ? {
59+
[resourceConfig.parent]: {
60+
key: resourceConfig.parentKey,
61+
field: resourceConfig.parentField
6762
}
68-
return endpoint
69-
} else {
70-
delete options.params[parentKey]
63+
} : {})
64+
65+
DSUtils.forOwn(parents, function (parent, parentName) {
66+
let item
67+
let parentKey = parent.key
68+
let parentField = parent.field
69+
let parentDef = resourceConfig.getResource(parentName)
70+
let parentId = options.params[parentKey]
71+
72+
if (parentId === false || !parentKey || !parentDef) {
73+
if (parentId === false) {
74+
delete options.params[parentKey]
75+
}
76+
} else {
77+
delete options.params[parentKey]
7178

72-
if (DSUtils._sn(id)) {
73-
item = resourceConfig.get(id)
74-
} else if (DSUtils._o(id)) {
75-
item = id
76-
}
79+
if (DSUtils._sn(id)) {
80+
item = resourceConfig.get(id)
81+
} else if (DSUtils._o(id)) {
82+
item = id
83+
}
84+
console.log('item', item)
7785

78-
if (item) {
79-
parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null)
80-
}
86+
if (item) {
87+
parentId = parentId || item[parentKey] || (item[parentField] ? item[parentField][parentDef.idAttribute] : null)
88+
}
8189

82-
if (parentId) {
83-
delete options.endpoint
84-
let _options = {}
85-
DSUtils.forOwn(options, (value, key) => {
86-
_options[key] = value
87-
})
88-
return DSUtils.makePath(this.getEndpoint(parentDef, parentId, DSUtils._(parentDef, _options)), parentId, endpoint)
89-
} else {
90-
return endpoint
90+
if (parentId) {
91+
delete options.endpoint
92+
let _options = {}
93+
DSUtils.forOwn(options, (value, key) => {
94+
_options[key] = value
95+
})
96+
endpoint = DSUtils.makePath(this.getEndpoint(parentDef, parentId, DSUtils._(parentDef, _options)), parentId, endpoint)
97+
}
9198
}
92-
}
99+
}, this)
100+
101+
return endpoint
93102
}
94103

95104
getPath (method, resourceConfig, id, options) {

test/findAll.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,62 @@ describe('dsHttpAdapter.findAll(resourceConfig, params, options)', function () {
3232
assert.equal(queryTransform.callCount, 2, 'queryTransform should have been called');
3333
});
3434
});
35+
36+
it('should use one of many parents', function () {
37+
var _this = this;
38+
39+
var Thing = datastore.defineResource({
40+
name: 'thing',
41+
endpoint: 'things',
42+
relations: {
43+
belongsTo: {
44+
user: {
45+
localKey: 'userId',
46+
localField: 'user',
47+
parent: true
48+
},
49+
posts: {
50+
localKey: 'postId',
51+
localField: 'post',
52+
parent: true
53+
}
54+
}
55+
}
56+
});
57+
58+
if (!Thing.parents) {
59+
Thing.parents = {
60+
user: {
61+
key: 'userId',
62+
field: 'user'
63+
},
64+
posts: {
65+
key: 'postId',
66+
field: 'post'
67+
}
68+
}
69+
}
70+
71+
setTimeout(function () {
72+
assert.equal(1, _this.requests.length);
73+
assert.equal(_this.requests[0].url, 'user/1/things');
74+
assert.equal(_this.requests[0].method, 'GET');
75+
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify([{ id: 1 }]));
76+
}, 30);
77+
78+
return dsHttpAdapter.findAll(Thing, { userId: 1 }).then(function (data) {
79+
assert.deepEqual(data, [{ id: 1 }], 'user thing should have been found');
80+
81+
setTimeout(function () {
82+
assert.equal(2, _this.requests.length);
83+
assert.equal(_this.requests[1].url, 'posts/2/things');
84+
assert.equal(_this.requests[1].method, 'GET');
85+
_this.requests[1].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify([{ id: 2 }]));
86+
}, 30);
87+
88+
return dsHttpAdapter.findAll(Thing, { postId: 2 });
89+
}).then(function(data) {
90+
assert.deepEqual(data, [{ id: 2 }], 'post thing should have been found');
91+
});
92+
});
3593
});

0 commit comments

Comments
 (0)