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
Provide a matches
utility or document EntityComparator
#2392
Comments
I don't really understand what you are asking for here, could you provide more verbose example? Like runtime filtering that would work the same way as SQL where query? I am not interested in doing that, it's the job of database, not ORM. I also don't understand the reference to // given two User entities, u1 and u2
const comparator = em.getComparator();
const e1 = comparator.prepareEntity(u1);
const e2 = comparator.prepareEntity(u2);
const diff = comparator.diffEntities('User', e1, e2); |
Use Case: I have a invoice modification service, where I pass in a partial invoice line and I want the service to either:
The above is normally named something in the service layer like, Right now, I have to write this kind of function to support this: for (const [key, value] of Object.entries(criteria)) {
if (key === 'description') {
continue;
}
const references = ['action', 'event', 'jobField', 'field', 'provider', 'job', 'invoice'];
if (references.includes(key)) {
if (value === null) {
if (line[key]) {
return false;
}
} else {
if (line[key] === null || line[key] === undefined) {
return false;
}
if (typeof value === 'string') {
if (line[key].id !== value) {
return false;
}
} else if (value instanceof Reference) {
if (value['id'] !== line[key].id) {
return false;
}
} else if (value !== line[key].get()) {
return false;
}
}
} else {
if (value === null && !!line[key]) {
return false;
}
if (value != null && line[key] !== value) {
return false;
}
}
} That logic above is effectively similar to how |
|
FYI the comparator has nowadays a https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/utils/EntityComparator.ts#L43 The downside of using this is that it is built for comparing the database values, so the entity snapshots - the main difference being the custom types, which are handled on hydration. I guess we could introduce a comparator for runtime values (so for entity instances instead of snapshots) too without too much effort. |
Is your feature request related to a problem? Please describe.
I often have service methods which accept partials (eg:
EntityData<Entity>
orPartial<Entity>
) where it may then filter a collection of those entities and run some logic. At the moment, there is no way to use the logic similar to assign (or match?), where it can auto-correct between references, ids, and whole objects. To manually build acollection.filter()
command which could adequately support readingEntityData
would be very verbose and may not align with how Mikro matches internally.Describe the solution you'd like
if not using a collection (say an array or other JS collection type), EM could contain a filter method
The text was updated successfully, but these errors were encountered: