-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: add entity mode virtual-property #11597
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
Conversation
WalkthroughAdds two new column options — Changes
Sequence Diagram(s)sequenceDiagram
participant Dev as Entity Schema (definition)
participant Transformer as EntitySchemaTransformer
participant Meta as ColumnMetadataArgs
participant DB as Runtime / Tests
Dev->>Transformer: Provide column with virtualProperty + query
Transformer->>Transformer: Evaluate mode checks (createDate/.../objectId/virtualProperty)
Transformer->>Meta: Emit ColumnMetadataArgs with mode="virtual-property" and options.query
Meta->>DB: Later used to generate mapping/SQL for virtual column in tests
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/entity-schema/EntitySchemaColumnOptions.ts(1 hunks)src/entity-schema/EntitySchemaTransformer.ts(1 hunks)
🔇 Additional comments (1)
src/entity-schema/EntitySchemaTransformer.ts (1)
101-101: Mode assignment integration verified
All checks confirm that"virtual-property"is a validColumnModeand is consistently handled alongside"virtual":
- ColumnMode union (src/metadata-args/types/ColumnMode.ts) includes both
"virtual"and"virtual-property".- Consumer logic (src/metadata/ColumnMetadata.ts) correctly sets
isVirtualfor"virtual"andisVirtualPropertyfor"virtual-property". No other code branches directly on these raw mode strings.- EntitySchemaTransformer (src/entity-schema/EntitySchemaTransformer.ts:101) follows the same independent-
ifpattern as existing overrides, so there’s no unintended collision.(Optional) You may extract mode resolution into a helper and add unit tests to cover:
{ virtualProperty: true }⇒"virtual-property"{ virtual: true }⇒"virtual"
8413121 to
0b7ee17
Compare
commit: |
|
Hi @wszgrcy, thanks for the contribution :) do you think you can add a functional test for this somewhere in |
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.
Actionable comments posted: 0
🧹 Nitpick comments (4)
src/decorator/options/ColumnOptions.ts (1)
191-196: Clarify JSDoc, fix grammar/casing, and scope the option to virtual columns
- “entities alias” → “entity alias”
- Use the standard “@see”
- This is evaluated at SELECT time (not “generating the relational db script”)
- Note it only applies to mode "virtual" or "virtual-property" and add a short security note
Apply this diff to tighten up the docs:
- /** - * Query to 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 either defined by the Entity Decorator or automatically - * @See https://typeorm.io/decorator-reference#virtualcolumn for more details. - */ + /** + * SQL fragment used to populate the value of a virtual column when reading data (SELECT). + * Only applies to columns with mode "virtual" or "virtual-property". + * The function receives the current entity alias (from QueryBuilder) to reference other columns. + * @see https://typeorm.io/decorator-reference#virtualcolumn + * + * Security note: never concatenate untrusted input here; this string is interpolated into the SQL. + */ query?: (alias: string) => stringtest/functional/entity-schema/columns/virtual-columns/virtual-columns.ts (1)
24-34: Make the test resilient to driver differences by not hard-coding id=0Some drivers or configurations may not accept an explicit 0 for an auto-increment primary key. Capture the generated id from save() and read back by that id to avoid flakiness and improve portability.
- await repo.save({ id: 0, k1: 1 }) - const result = (await repo.findOne({ where: { id: 0 } }))! + const created = await repo.save({ k1: 1 }) + const result = await repo.findOneByOrFail({ id: created.id }) expect(result.vK1).eq(result.k1) expect(result.vK1).eq(1)test/functional/entity-schema/columns/virtual-columns/entity/Activity.ts (1)
18-20: Avoid hard-coded identifier quoting to improve cross-driver portabilityDouble-quoted identifiers work for SQLite/Postgres but not MySQL. Since the test runs on better-sqlite3 today but the feature is cross-driver, prefer an unquoted expression to reduce driver-specific syntax. The alias is provided by TypeORM and should be used as-is.
- query: (alias) => - `SELECT k1 FROM "activities" WHERE "k1" = ${alias}."k1"`, + query: (alias) => + `SELECT k1 FROM activities WHERE k1 = ${alias}.k1`,src/entity-schema/EntitySchemaTransformer.ts (1)
101-101: Clarify precedence or enforce exclusivity when multiple special modes are set
virtualPropertyis evaluated last, overriding other modes likecreateDate,updateDate, etc. If a column mistakenly sets multiple flags, the last one “wins”, which can hide configuration errors. Consider throwing or logging whenvirtualPropertyis combined with other special modes, or document the intended precedence.If you want, I can add a guard to error on conflicting modes (virtualProperty + any of createDate/updateDate/deleteDate/version/treeChildrenCount/treeLevel/objectId) and a small test asserting the error.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (5)
src/decorator/options/ColumnOptions.ts(1 hunks)src/entity-schema/EntitySchemaColumnOptions.ts(2 hunks)src/entity-schema/EntitySchemaTransformer.ts(2 hunks)test/functional/entity-schema/columns/virtual-columns/entity/Activity.ts(1 hunks)test/functional/entity-schema/columns/virtual-columns/virtual-columns.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/entity-schema/EntitySchemaColumnOptions.ts
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/decorator/options/ColumnOptions.ts (1)
src/query-builder/QueryBuilder.ts (1)
alias(140-145)
test/functional/entity-schema/columns/virtual-columns/virtual-columns.ts (3)
src/connection/ConnectionManager.ts (1)
connections(16-18)test/utils/test-utils.ts (3)
createTestingConnections(388-482)reloadTestingDatabases(500-505)closeTestingConnections(487-495)test/functional/entity-schema/columns/virtual-columns/entity/Activity.ts (1)
Activity(3-22)
test/functional/entity-schema/columns/virtual-columns/entity/Activity.ts (2)
src/index.ts (1)
EntitySchema(182-182)src/query-builder/QueryBuilder.ts (1)
alias(140-145)
🔇 Additional comments (1)
src/entity-schema/EntitySchemaTransformer.ts (1)
139-140: Propagating query into ColumnMetadataArgs looks correctPassing
query: regularColumn.queryensures the metadata contains the virtual-column SQL. This aligns with the new ColumnOptions.query addition and matches how the VirtualColumn decorator behaves.
test case has added |
gioboa
left a comment
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.
Thanks for your help @wszgrcy 🎉👏
* feat: add entity mode virtual-property * test: virtual columns
Description of change
Implement VirtualColumn using EntitySchema
Pull-Request Checklist
masterbranchFixes #00000Summary by CodeRabbit
New Features
Tests