Skip to content

Commit b12579b

Browse files
committed
Merge pull request #40 from tfoxy/master
Add support for multiple parents
2 parents e2f53c4 + 0be03ed commit b12579b

File tree

4 files changed

+127
-31
lines changed

4 files changed

+127
-31
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"codacy-coverage": "1.1.3",
3535
"coveralls": "2.11.4",
3636
"es6-promise": "3.0.2",
37-
"karma": "0.13.14",
37+
"karma": "0.13.19",
3838
"karma-browserstack-launcher": "0.1.6",
3939
"karma-chai": "0.1.0",
4040
"karma-chrome-launcher": "0.2.1",

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/find.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,33 @@ describe('DSHttpAdapter.find(resourceConfig, id, options)', function () {
124124
return otherAdapter.find(Post, 1);
125125
});
126126
});
127+
128+
it('should use parent', function () {
129+
var _this = this;
130+
131+
var Thing = datastore.defineResource({
132+
name: 'thing',
133+
endpoint: 'things',
134+
relations: {
135+
belongsTo: {
136+
user: {
137+
localKey: 'userId',
138+
localField: 'user',
139+
parent: true
140+
}
141+
}
142+
}
143+
});
144+
145+
setTimeout(function () {
146+
assert.equal(1, _this.requests.length);
147+
assert.equal(_this.requests[0].url, 'user/2/things/1');
148+
assert.equal(_this.requests[0].method, 'GET');
149+
_this.requests[0].respond(200, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 1 }));
150+
}, 30);
151+
152+
return dsHttpAdapter.find(Thing, 1, { params: { userId: 2 } }).then(function (data) {
153+
assert.deepEqual(data, { id: 1 }, 'thing should have been found');
154+
});
155+
});
127156
});

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)