Skip to content

Avoid loading belongsTo relations with falsey keys attempt 3 #53

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 1 commit into from
Jan 19, 2016
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
40 changes: 23 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,33 @@ function loadWithRelations (items, resourceConfig, options) {
})
} else if (def.type === 'belongsTo' || (def.type === 'hasOne' && def.localKey)) {
if (instance) {
task = this.find(resourceConfig.getResource(relationName), DSUtils.get(instance, def.localKey), __options).then(relatedItem => {
instance[def.localField] = relatedItem
return relatedItem
})
let id = DSUtils.get(instance, def.localKey)
if (id) {
task = this.find(resourceConfig.getResource(relationName), DSUtils.get(instance, def.localKey), __options).then(relatedItem => {
instance[def.localField] = relatedItem
return relatedItem
})
}
} else {
task = this.findAll(resourceConfig.getResource(relationName), {
where: {
[relationDef.idAttribute]: {
'in': DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x)
}
}
}, __options).then(relatedItems => {
DSUtils.forEach(items, item => {
DSUtils.forEach(relatedItems, relatedItem => {
if (relatedItem[relationDef.idAttribute] === item[def.localKey]) {
item[def.localField] = relatedItem
let ids = DSUtils.filter(items.map(function (item) { return DSUtils.get(item, def.localKey) }), x => x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing arrow functions and non-arrow functions on the same line. Seems like we should be consistent

let ids = DSUtils.filter(items.map(item => DSUtils.get(item, def.localKey)), x => x)

This could also be even more concise by using ES5 functions instead of DSUtils/mout

let ids = items.map(item => DSUtils.get(item, def.localKey)).filter(x => x)

DSUtils.get() is still needed as it supports getting a nested property value (it uses mout under the hood)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shrugs
It's the same call that was there before. If you want consistency then there are a lot of other changes that will have to be made as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, my bad. I was just doing a quick review before merging and saw this. I have plans to remove most of our use of DSUtils as ES5 functions serve our needs. Just leave it as is and I'll clean it up (along with the rest of the code base) later.

if (ids.length) {
task = this.findAll(resourceConfig.getResource(relationName), {
where: {
[relationDef.idAttribute]: {
'in': ids
}
}
}, __options).then(relatedItems => {
DSUtils.forEach(items, item => {
DSUtils.forEach(relatedItems, relatedItem => {
if (relatedItem[relationDef.idAttribute] === item[def.localKey]) {
item[def.localField] = relatedItem
}
})
})
return relatedItems
})
return relatedItems
})
}
}
}

Expand Down