-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Transparently ensure foreign key target field in inclusion resolvers #5592
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR! I think once it is landed it would filter.fields more straightforward 👍 Before that, I have several questions would like to discuss.
packages/repository/src/__tests__/integration/repositories/relation.factory.integration.ts
Outdated
Show resolved
Hide resolved
packages/repository/src/relations/belongs-to/belongs-to.inclusion-resolver.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like hear from @bajtos before landing. Except the normalizeFields
function, the PR looks good to me in general. It is more intuitive when handling fields
filter with nested relations.
Do you mind also update the bottom part of the docs https://loopback.io/doc/en/lb4/Fields-filter.html that excluding the source key for relations might cause errors? Thanks!
79ed612
to
e1a3114
Compare
That's not necessary anymore. The latest commit addresses source keys too. |
bc189e6
to
e290275
Compare
@raymondfeng @agnes512 @hacksparrow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay. Good progress 👍 I think the API docs could be more descriptive.
); | ||
return resolved[0]; | ||
return resolved[0] || null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if we should have || null
here. I think the empty value is handled before retuning here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, we can get an empty array and we guard against undefined here.
Thank you @InvictusMB for the pull request 👏 and @agnes512 for reviewing the changes 💪 I need to find out more time to build a better understanding of the wider context and asses the implications of the proposed changes. I'll try to do that early next week 🤞 Sorry for the delay! 🙈 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @InvictusMB for the pull request. I like the idea of improving inclusion resolves to include primary/foreign key properties in the fields
used to build the database query 👍
I have difficulties understanding your proposed implementation. You are changing a lot of files and there is no high-level description of the target design you are aiming for. Can you please explain the proposed change in English? In particular, I am confused about pruneFields
and why we need resolvers to expose relationMeta
. I would expect resolvers to be fully encapsulated and if they need to do some processing of the query filter, then such step should happen inside the resolvers (ideally).
@bajtos Sorry, the PR grew out of control. Initially it was only about targets of a relation.
I don't think we can fully encapsulate this behavior inside a resolver the way you wish. Very clever code async find(
filter?: Filter<T>,
options?: Options,
): Promise<(T & Relations)[]> {
const include = filter?.include;
const entityResolver = async (newFilter?: Filter<T>) => {
const models = await ensurePromise(
this.modelClass.find(this.normalizeFilter(newFilter), options),
);
return this.toEntities(models);
}
let amendFilter;
const entityPromise = new Promise(resolve => {
let count = 0;
amendFilter = (newFilter: Filter<T>) => {
if (count === include?.length) {
resolve(entityResolver(newFilter));
}
count++;
};
});
return this.includeRelatedModels(
entityPromise,
amendFilter,
include,
options,
);
} |
d88320c
to
404f83c
Compare
dc26131
to
f06acf3
Compare
Separated into #6495 |
FK source part split into #6496 |
@agnes512 @hacksparrow #6495 has to land first though. |
Signed-off-by: InvictusMB <[email protected]>
…n resolvers Signed-off-by: InvictusMB <[email protected]>
This pull request has been marked stale because it has not seen activity within two months. It will be closed within 14 days of being stale unless there is new activity. |
This pull request has been marked stale because it has not seen activity within two months. It will be closed within 14 days of being stale unless there is new activity. |
Not stale. I'll have time to refresh this soon. |
This pull request has been marked stale because it has not seen activity within two months. It will be closed within 14 days of being stale unless there is new activity. |
This pull request has been closed due to continued inactivity. If you are interested in finishing the proposed changes, then feel free to re-open this pull request or open a new one. |
findByForeignKeys
adds the foreign key to the filter fields if it's not there.Concerned inclusion resolvers prune the foreign key value from entities if it has not been requested in the original filter.
Fixes: #5314
Reasoning: comment
Obsoletes: #3455
Also addresses: comment
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈