-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
VirtualColumn Decorator #9323
Comments
This new feature change bahviour of typeorm to allow use new calculated decorator Closes typeorm#9323
This new feature change bahviour of typeorm to allow use new virtual decorator Closes typeorm#9323
This new feature change bahviour of typeorm to allow use new virtual decorator Closes typeorm#9323
This new feature change bahviour of typeorm to allow use new calculated decorator Closes typeorm#9323
This new feature change behavior of typeorm to allow use of the new virtual column decorator Closes typeorm#9323
* feat: implement new calculated decorator This new feature change bahviour of typeorm to allow use new calculated decorator Closes #9323 * feat: implement new virtual decorator This new feature change bahviour of typeorm to allow use new virtual decorator Closes #9323 * feat: Implement new virtual decorator This new feature change bahviour of typeorm to allow use new virtual decorator Closes #9323 * feat: implement new virtual decorator This new feature change bahviour of typeorm to allow use new calculated decorator Closes #9323 * feat: implement new virtual decorator This new feature change behavior of typeorm to allow use of the new virtual column decorator Closes #9323
does someone check performance between VirtualColumn and simple querybuilder? i have an issue with it, i have checked and found out that using VirtualColumn is slower x3 than querybuilder with same data |
Can some more documentation be added on how this works? Can we pass current record values in query that is defined by virtual column? Can we use only primary column in the query or other values can be passed as well? |
I think you can find the docs here: https://orkhan.gitbook.io/typeorm/docs/decorator-reference#virtualcolumn |
Feature Description
I would like to add a new feature to TypeORM so that I can consolidate my code and simply run a subSelect with every get request on the requested entity. This would improve performance and allow me to utilize the full potential of the supported relational databases PostgresQL, MySql, and MSSQL.
Below is what I am planning on implementing and tying to this Issue once it is completed (I will be coding this up tonight and think that it would bring value to this project overall)
The Problem
When querying data against any relational database there is often a need to return some query (a sub-select, count, etc.) that returns with a model. For example, if we create an entity of type Company that has a one-to-many relationship on employees, it may make sense to have a totalEmployeesCount column ("Virtual Column") that calculates this on every request or even better when the column is selected in the base entity query. This is what I am wanting to implement in TypeORM. This is common in most ORM solutions and I would love to see it here.
The Solution
Let's use the same example above: we have entity of type Company that has a one-to-many relationship on an employee entity (employees), it may make sense to have a totalEmployeesCount column that could be re-calculated on every fetch of the entity:
Some notes on the VirtualColumn Decorator:
The "query" property will be used to populate the column data. This query is used when generating the relational db script. The query function is called with the current entities alias automatically generated by TypeORM.
A virtual column is not an editable column. It is a record that should be considered as readonly and it is ignored when the save entity function is run.
Considered Alternatives
One workaround would be to create a query builder but in doing so, this would add complexity, and it doesn't solve the overall need: code redundancy, and consistent entity result sets.
The second workaround would to be to add an @AfterLoad() decorator and perform a query there, but this is a poor solution since it would not allow us to utilize the relational databases query optimization engine when performing a query leading to worse performance. This proposed solution would be faster and avoid several unnecessary database hits.
Relevant Database Driver(s)
This solution would work for all relational databases (it is possible to add non-relational databases to this as well but the immediate need is for relational databases.
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: