Repository navigation
Description
Issue type:
[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue
I'd like to implement some improvements for the Find Options and want to discuss them before I submit a PR.
1. Support nested where in combination with relations (See: #3902, #3890)
It seems to me that TypeORM only supports nested where statements in combination with embedded entities, or am I wrong?
It would be nice to have such a feature while dealing with relations.
I would suggest either:
userRepository.find({
relations: ["photo"],
where: {
photo: {
format: "png"
}
}
});or
userRepository.find({
relations: ["photo"],
where: {
"photo.format": "png"
}
});2. Support partial select for relations
Currently, when using relations: ["photo"], TypeORM uses leftJoinAndSelect() which selects all columns from photo.
Maybe we could do something like:
userRepository.find({
relations: [
{
name: "photo",
select: ["format", "createdAt"]
},
// Stay backwards compatible
"something.else"
]
});3. Add possibility to extend generated queries with QueryBuilder
That solution would be easy to implement:
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Stan", lastName: "Lee" }
],
query: (qb) => {
// Do some stuff with `QueryBuilder` that isn't supported by
// FindOptions
}
});Of course one could use QueryBuilder in the first place, but then you have to re-implement all those handy generations from find methods. The docs should make clear that something like qb.execute() or qb.getOne() shouldn't be used, although it makes no sense anyway.
4. Support custom find operators
Currently, the creation logic of all find operators is hard coded in FindOperator.
We could easily refactor this by passing a callback to FindOperator, e.g.:
export function CustomEqual<T>(value: T|FindOperator<T>) {
return new FindOperator(
(
connection: Connection,
aliasPath: string,
parameters: string[]
) => {
return `${aliasPath} = ${parameters[0]}`;
},
value
);
}Maybe FindOperator could be refactored a little bit more - for example connection is passed to toSql() but isn't used.
@pleerock I would appreciate if you'd find some time to give your input :)
Since you mentioned that you'll refactor QueryBuilder in the future, I would stay out of the QueryBuilder code and try to implement everything within the Find Options, e.g. FindOptionsUtils - also point 1.
Activity
Kononnable commentedon Jun 23, 2019
Make sure you take a look at @next implementation of
FindOptions, there is a lot changed there and I'm not sure everything would be possible outside ofQueryBuilder.As for the proposal - number 3 might be a tricky one. In @next one
.findmight results in multiple queries to db(whererelationsare passed). When 'old relations system'(which uses single query) will be reimplemented in @next user would be able to choose which approach he wants to use(both have some advantages and disadvantages).Anyhow, even with 'old relations' I'm not sure we should implement it like that. Wouldn't it be easier to just generate a query builder based on object passed in FindOptions and instead of returning entities return QueryBuilder, so user can do with it what he wants? Not sure how we could call such method.
haschu commentedon Jun 24, 2019
I wasn't aware of @next- thanks for pointing out. Should I implement those features on the @next branch then? And if so, when can we expect the changes to be merged into master? I guess not anytime soon, or? (over 300 commits behind master)
I also considered that solution - imho there are two options:
findOne()andfindOneAndGetQB()QueryBuilderinstead of entities (I don't like that)All other solution that come to my mind would introduce lots of breaking changes which should be avoided. What do you think?
haschu commentedon Jul 4, 2019
@pleerock, any thought on this? :)
Are those proposed features acceptable for a PR?
For me, point 1 & 2 are having the most priority.
Regarding point 1 - I think a few adjustments in
QueryBuilderwill be necessary, but not too much.lsmialowski commentedon Jul 9, 2019
Fingers crossed for points 1 and 2 :)
stellarbear commentedon Nov 12, 2019
Hope for nested where
Firaenix commentedon Mar 31, 2020
javierlinked commentedon Jun 23, 2020
It seems like nop.
Firas1993 commentedon Sep 14, 2020
it seems we are forced to use the queryBuilder here ?
haschu commentedon Oct 6, 2020
@imnotjames Just noticed the new "new feature" label. Does that mean the proposal has been accepted and is open for a PR?
imnotjames commentedon Oct 6, 2020
This means it's a feature request, nothing more. If you want to open a PR you always can
haschu commentedon Oct 6, 2020
@imnotjames Before I invest the time and effort, I'd like to make sure which of the proposed features are actually acceptable for the maintainers :)
That's why I opened this issue in the first place. I guess the most requested feature would be nested where statements.
imnotjames commentedon Oct 15, 2020
Ok - looking at this issue --
✔️ Already supported
I tested it and this seems to work, not sure where you're seeing it isn't. See #2707 & comment there if you believe it's not working.
❌ Not yet supported
This will break a lot of assumptions made throughout the codebase. Why make this change? It'd be easier to discuss in its own issue if you think it's a very important extension to the
FindOptions❌ Not yet supported
You should be able to use many of the find options within various parts of the query builder. Please use that instead as it simplifies the API. If you believe this should be brought in can you find examples of similar APIs we can build off of for this?
Because query builders have global namespaces, etc, it could break a lot of assumptions to let it be modified directly.
✔️ Already supported
You can create custom
FindOperators with theRawoperator in the next release, as it supports parameters.For example, a fancy shmancy JSON path comparator:
(ps that's off the top of my head, haven't tested it so don't just copy/ paste it directly.)
amadeus-torwell commentedon Nov 6, 2020
imnotjames commentedon Jul 4, 2021
It worked in some relations cases but not all.
pleerock commentedon Mar 27, 2022
I think everything proposed in this PR already supported in
0.3.0. I'll close this issue.