Comments (11)
Just patched via
patch-package
, this seems to work great! Is there a reason why this isn't already supported?
Partly because what you are saying above, this solution is not correctly tested at all so can have unexpected behaviour with other drivers, also there was no response on the ticket for a while 😅
from nestjs-query.
Thanks @TriPSs !
from nestjs-query.
Interesting, could it be because TypORM version is not the same? Do you use yarn by any change? If so could you try to add the following:
"resolutions": {
"typeorm": "0.3.11"
}
Then do yarn
again to reinstall deps and see if it works.
This is actually a quite awesome feature!
from nestjs-query.
Agreed, it's an awesome feature and also, I appreciate you taking over as maintainer of this project.
I use npm, but did something similar to pin the typeorm version
"overrides": {
"@ptc-org/nestjs-query-graphql": {
"typeorm": "0.3.11"
}
}
But no luck, still getting the same error on filter and sort.
I edited the main post with this, just FYI:
I am able to successfully query the Field of the VirtualColumn, but as soon as I try to filter or sort, it says the column doesn't exist (it really doesn't, but the repository should handle that).
So selecting the VirtualColumn field does return the appropriate calculated value, it just doesn't work on filter and sort.
from nestjs-query.
Okay thanks for the explanation, will try to check this out somewhere in the coming days.
from nestjs-query.
After some digging it's because the query services tries to filter / order on it as a normal database field, like tabel.field
but since it's not a existing field it does not work. When querying it it does work since TypeORM selects all fields as table.field as table_field
.
We could make a simple fix by checking if the column we are filtering/ordering on has the property isVirtualProperty
and if true
add the order/filter with the _
instead of a .
.
In the case of sorting it would look like:
public applySorting<T extends Sortable<Entity>>(qb: T, sorts?: SortField<Entity>[], alias?: string): T {
if (!sorts) {
return qb
}
return sorts.reduce((prevQb, { field, direction, nulls }) => {
const columnMetadata = this.repo.metadata.columns.find((col) => col.propertyName === field)
let col = alias ? `${alias}.${field as string}` : `${field as string}`
// If the column is virtual we need to use the actual selected field as it does not exist in the database
if (columnMetadata?.isVirtualProperty) {
col = `"${alias ? `${alias}_${field as string}` : `${field as string}`}"`
}
return prevQb.addOrderBy(col, direction, nulls)
}, qb)
}
from nestjs-query.
Any update on this? Just bumped versions in Typeorm and would love to take advantage of nestjs-query functionality on the VirtualColumns.
from nestjs-query.
After some digging it's because the query services tries to filter / order on it as a normal database field, like
tabel.field
but since it's not a existing field it does not work. When querying it it does work since TypeORM selects all fields astable.field as table_field
.We could make a simple fix by checking if the column we are filtering/ordering on has the property
isVirtualProperty
and iftrue
add the order/filter with the_
instead of a.
.In the case of sorting it would look like:
public applySorting<T extends Sortable<Entity>>(qb: T, sorts?: SortField<Entity>[], alias?: string): T { if (!sorts) { return qb } return sorts.reduce((prevQb, { field, direction, nulls }) => { const columnMetadata = this.repo.metadata.columns.find((col) => col.propertyName === field) let col = alias ? `${alias}.${field as string}` : `${field as string}` // If the column is virtual we need to use the actual selected field as it does not exist in the database if (columnMetadata?.isVirtualProperty) { col = `"${alias ? `${alias}_${field as string}` : `${field as string}`}"` } return prevQb.addOrderBy(col, direction, nulls) }, qb) }
Just patched via patch-package
, this seems to work great! Is there a reason why this isn't already supported?
from nestjs-query.
@MrSquaare, would you be able to make a PR?
from nestjs-query.
@MrSquaare, would you be able to make a PR?
@YeomansIII Unfortunately, after further testing, it doesn't seem to work with some databases (such as Postgres).
Also, I couldn't get it to work with the WHERE clause. Did you see somewhere that VirtualColumn should support the WHERE clause? (I've seen a few people mention that HAVING works with aliases, but I haven't tested it yet).
It seems that the only reliable way to get VirtualColumn to work with ORDER BY, WHERE, ... is via a sub query, which can have performance disadvantages. This remains a viable option.
from nestjs-query.
Any news about thaat issue ?
Having the same issue when using where contidional, that code fix the sort.
ERROR [ExceptionsHandler] Unknown column 'Quote.daysUntilDue' in 'where clause'
QueryFailedError: Unknown column 'Quote.daysUntilDue' in 'where clause'
from nestjs-query.
Related Issues (20)
- how to best import modules to leverage custom services? HOT 1
- Add Fuzzy Search Support using fuse.js HOT 1
- Custom Assembler: TypeORM module inconsistently calling async methods HOT 6
- Option to disable pluralization of relation name HOT 2
- Infinite depth in DTO but not in suscriptions HOT 3
- TypeORM - Simple-array doesn't work HOT 6
- Adding startsWith / endsWith / contains options in StringFieldComparisons
- Add support for `FULLTEXT` indexes
- Nest can't resolve dependencies of the MarkdownActivityAuthorizerInterceptor HOT 2
- Error: No fields found to create FilterType. HOT 2
- Will FilterableField decorator support array values? HOT 1
- Alias And Field Names In Generated SQL For Aggregate GroupBy Date Are Not Properly Escaped With Quotes HOT 1
- Specified relationName is not honoured for mutations and aggregations HOT 2
- Issue with Remove Relation/set relation to null in Many to One relation
- SetRelationOnEntity failing for many to one relationships HOT 2
- Returning Relay-connection style responses from custom resolvers HOT 5
- Filtering for OR in TypeORM and GraphQL results in AND HOT 3
- Implement schema based multi tenancy HOT 2
- One-to-many Relations is not visible in dto in other services via @ResolveField() HOT 2
- Relation fields in TypeOrm Embedded Entities are always null HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-query.