Skip to content
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(core): add support for virtual entities #3351

Merged
merged 1 commit into from
Jul 31, 2022
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Jul 31, 2022

Virtual entities don't represent any database table. Instead, they dynamically resolve to an SQL query (or an aggregation in mongo), allowing to map any kind of results onto an entity. Such entities are mean for read purposes, they don't have a primary key and therefore cannot be tracked for changes. In a sense they are similar to (currently unsupported) database views.

To define a virtual entity, provide an expression, either as a string (SQL query):

@Entity({
  expression: 'select name, age, ' +
    '(select count(*) from book b where b.author_id = a.id) as total_books, ' +
    '(select group_concat(distinct t.name) from book b ' +
      'join tags_ordered bt on bt.book_id = b.id ' +
      'join book_tag t on t.id = bt.book_tag_id ' +
      'where b.author_id = a.id ' +
      'group by b.author_id) as used_tags ' +
    'from author a group by a.id',
})
export class BookWithAuthor {

  @Property()
  title!: string;

  @Property()
  authorName!: string;

  @Property()
  tags!: string[];

}

Closes #1104

@codecov-commenter
Copy link

codecov-commenter commented Jul 31, 2022

Codecov Report

Merging #3351 (91adf04) into master (eff463c) will decrease coverage by 0.00%.
The diff coverage is 98.43%.

@@             Coverage Diff             @@
##            master    #3351      +/-   ##
===========================================
- Coverage   100.00%   99.99%   -0.01%     
===========================================
  Files          203      205       +2     
  Lines        12456    12588     +132     
  Branches      2872     2905      +33     
===========================================
+ Hits         12456    12587     +131     
- Misses           0        1       +1     
Impacted Files Coverage Δ
packages/core/src/decorators/Entity.ts 100.00% <ø> (ø)
packages/core/src/drivers/DatabaseDriver.ts 100.00% <ø> (ø)
packages/core/src/drivers/IDatabaseDriver.ts 100.00% <ø> (ø)
...ges/entity-generator/src/EntitySchemaSourceFile.ts 100.00% <ø> (ø)
packages/knex/src/AbstractSqlDriver.ts 99.78% <95.45%> (-0.22%) ⬇️
packages/core/src/EntityManager.ts 100.00% <100.00%> (ø)
packages/core/src/entity/EntityFactory.ts 100.00% <100.00%> (ø)
packages/core/src/hydration/ObjectHydrator.ts 100.00% <100.00%> (ø)
packages/core/src/metadata/MetadataStorage.ts 100.00% <100.00%> (ø)
packages/core/src/metadata/MetadataValidator.ts 100.00% <100.00%> (ø)
... and 21 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eff463c...91adf04. Read the comment docs.

@B4nan B4nan force-pushed the virtual-entities branch 2 times, most recently from d5499fe to 2c4176b Compare July 31, 2022 20:54
@B4nan B4nan merged commit dcd62ac into master Jul 31, 2022
@B4nan B4nan deleted the virtual-entities branch July 31, 2022 22:06
@B4nan B4nan restored the virtual-entities branch January 10, 2023 19:12
@B4nan B4nan deleted the virtual-entities branch January 10, 2023 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom classes for partial (and maybe cross-table) selects
2 participants