Replies: 6 comments
-
Maybe try to describe more the use case for this, with concrete examples. But having wildcards like |
Beta Was this translation helpful? Give feedback.
-
I undestand /*
@Entity()
class Client {
//....
@Property()
code!: number;
@ManyToOne(entity: () => ClientType)
type_id: ClientType;
//....
}
@Entity()
class ClientType {
//...
@Property()
code!: number;
@Property()
name!: string;
//...
}
*/
const payload = '103';
const result1 = orm.em.find(
Client,
{
$or: {
[expr('cast(code as text)')]: {$re: `^${payload}`}, // A number starts with, bypass column check
type_id: {
[expr('cast(code as text)')]: {$re: `^${payload}`}
}
}
},
{
orderBy: {
type_id: {
name: 'asc'
}
}
}
);
/* Ambiguity problem
select
"e0".* from "Client" as "e0"
left join "ClientType" as "e1" on "e0"."type_id" = "e1"."id"
where
cast(code as text) ~ '^100' or (cast(code as text) ~ '^100'
order by "e1"."name" asc
*/
// Proposal:
const result2 = orm.em.find(
Client,
{
$or: {
[expr('cast($1 as text)', ['code'])]: {$re: `^${payload}`}, // Client.code < check on this property
type_id: {
[expr('cast($1 as text)'), ['code']]: {$re: `^${payload}`} // ClientType.code < check on this property
}
}
},
{
orderBy: {
type_id: {
name: 'asc'
}
}
}
);
/* Query:
select
"e0".* from "Client" as "e0"
left join "ClientType" as "e1" on "e0"."type_id" = "e1"."id"
where
cast(e0.code as text) ~ '^100' or cast(e1.code as text) ~ '^100'
order by "e1"."name" asc
*/ |
Beta Was this translation helpful? Give feedback.
-
Well but there is no context, you just call the |
Beta Was this translation helpful? Give feedback.
-
We have a generic api rest and we use filters to handle the result, so, as I see, the only solid solution would be assign an alias to the entitiy decorator and use it in |
Beta Was this translation helpful? Give feedback.
-
There is a method for getting alias for a join path on QB, but that all seems very verbose, as opposed to just passing the aliases here and there (which obviously won't work if you have too many moving parts, e.g. when things are too dynamic). Join path is a string like |
Beta Was this translation helpful? Give feedback.
-
I understand... and precisely the moving parts is my concern. I will use an optional "query builder" strategy and pass that query builder through all custom hooks to fulfil it and be executed at the end of the generic process. Thanks for your help and attention |
Beta Was this translation helpful? Give feedback.
-
I was developing a filter to use a "starts with" on an integer (using postgresql). The solution was to cast that integer via "expr" function and use a simple regular expression to filter those numbers.
My client side grid had the capability to sort by another entity which had the same name column as the main entity using the filter so I had a collision and had to handle alias by hand ("e0" in that case).
I think a good solution would be to enhance the expr function with $1, $2, $3.... params like this:
By this way it would be possible to check entity properties and assign alias references by Mikro itself.
Beta Was this translation helpful? Give feedback.
All reactions