-
-
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
feat: implement new virtual decorator #9339
feat: implement new virtual decorator #9339
Conversation
This new feature change bahviour of typeorm to allow use new calculated decorator Closes typeorm#9323
"Calculated column" is term associated with PowerBI. "Computed column" refers to specific feature in multiple databases. Maybe call it "virtual column"? Generally, good job! |
Thank you. I really think this feature will add value to the community. If you look at the history of the issue that I posted I originally called the calculated column decorator a "VirtualColumn" decorator because I believe that this is a better name myself, but the entity column Metadata already had a property being used for IsVirtual, so I figured I should go with a new name. I can for sure update this to VirtualColumn especially since like you said it is a better name, and I can add a new property called isVirtualDecorator to the entity column Metadata. This feature has been renamed @Ginden back to to VirtualColumn |
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
How are we looking on this @Ginden ? It has been a week since our last discussion, and I want to ensure that this PR doesn't get lost in the backlog. Can this get merged in or is there something outstanding I need to do on my end? |
I'm merely helping with issues and reviews, to get them streamlined faster. At the end of day, decision, especially design decisions, belongs to @pleerock and other higher level maintainers. |
thank you for doing this, this would be an extremely useful feature for us! |
@pleerock Сan this be released as a standalone release if it doesn't make it into the main release soon? This is a very useful feature for our project ( i think for others too ) after migrating from mikro-orm to typeorm |
This new feature change behavior of typeorm to allow use of the new virtual column decorator Closes typeorm#9323
Thank you a lot for this contribution! Quite a solid implementation! |
@pleerock Was looking for something like this today and found it hasn't been released yet :(. Any ETA on when the next release will be cut with this PR included? |
you can actually start using it in the latest dev version (check npm versions) |
This PR introduced a very breaking change about default behavior for number columns without specific type added. class Foo {
@Column()
bar!: number;
} This is now always pushed through Was this change intended? Seems completely unrelated to the original function of this PR. cc @pleerock |
I'm not sure why these changes were needed, let's ask @CollinCashio But in what cases previously it worked? |
@pleerock Than's for your quick response. Here is the use where I found this issue.
This would not round/floor the value, entity would contain the full float on fetch. I am not sure to what extent should |
I would avoid complicating the report with migrations. I'm not sure where did you get the migration you wrote - if it's manually written, then it's not valid since it differs from the entity definition. The best if you always specify column type in |
@pleerock Thanks for the clarification, I wasn't aware that |
@PrimaryColumn("varchar", { length: 50 }) | ||
name: string; | ||
|
||
@VirtualColumn({ query: (alias) => `SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}.name` }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a question. Can't the query
be optional?.. I don't need to perform a subquery when I use a virtualColumn.
For context: https://pietrzakadrian.com/blog/virtual-column-solutions-for-typeorm#5-decorator-method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the virtualcolumn is not in the database at all but some other computed value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
background would be graphql since if you want a unique id field and have a composite key for a table you want to cramp that into a single key maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @Abobos and @sschneider-ihre-pvs ,
This functionality was designed specifically for subqueries. This solves the need of allowing your database engine to return queried result sets and put the data into your defined model (also allows you to use the DB engine to filter data from the model).
If you are just looking for a readonly property that has no need to pull database information and possibly pull from a third party API or some other data source, you could just use a regular property without any column decorator. IE your model could look like:
@Entity({ name: "timesheets" })
export default class TimeSheet extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
hours: number;
@AfterLoad()
afterLoad() {
// perform calculations here to set property of hours
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CollinCashio VIrtual Column suffices for my use-case.. but I was thinking you can just add the column name as some alias in a query without having to do a subquery.
Thanks
This PR created bug #10026 |
Description of change
Add a new feature that Fixes #9323
Pull-Request Checklist
master
branchnpm run format
to apply prettier formattingnpm run test
passes with this changeFixes #0000