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): allow configuring aliasing naming strategy #2419

Merged
merged 1 commit into from
Nov 14, 2021

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Nov 14, 2021

Implements different table aliasing for select-in loading strategy and for QueryBuilder
and makes it configurable.

BREAKING CHANGE:
Previously with select-in strategy as well as with QueryBuilder, table aliases
were always the letter e followed by unique index. In v5, we use the same
method as with joined strategy - the letter is inferred from the entity name.

This can be breaking if you used the aliases somewhere, e.g. in custom SQL
fragments. We can restore to the old behaviour by implementing custom naming
strategy, overriding the aliasName method:

import { AbstractNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends AbstractNamingStrategy {
  aliasName(entityName: string, index: number) {
    return 'e' + index;
  }
}

Note that in v5 it is possible to use expr() helper to access the alias name
dynamically, e.g. expr(alias => `lower('${alias}.name')`), which should be
now preferred way instead of hardcoding the aliases.

Implements different table aliasing for select-in loading strategy and for QueryBuilder
and makes it configurable.

BREAKING CHANGE:
Previously with select-in strategy as well as with QueryBuilder, table aliases
were always the letter `e` followed by unique index. In v5, we use the same
method as with joined strategy - the letter is inferred from the entity name.

This can be breaking if you used the aliases somewhere, e.g. in custom SQL
fragments. We can restore to the old behaviour by implementing custom naming
strategy, overriding the `aliasName` method:

```ts
import { AbstractNamingStrategy } from '@mikro-orm/core';

class CustomNamingStrategy extends AbstractNamingStrategy {
  aliasName(entityName: string, index: number) {
    return 'e' + index;
  }
}
```

Note that in v5 it is possible to use `expr()` helper to access the alias name
dynamically, e.g. ``expr(alias => `lower('${alias}.name')`)``, which should be
now preferred way instead of hardcoding the aliases.
@B4nan B4nan mentioned this pull request Nov 14, 2021
48 tasks
@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2021

Codecov Report

Merging #2419 (06c8963) into master (706935e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2419   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          188       188           
  Lines        10419     10423    +4     
  Branches      2285      2288    +3     
=========================================
+ Hits         10419     10423    +4     
Impacted Files Coverage Δ
packages/core/src/drivers/DatabaseDriver.ts 100.00% <100.00%> (ø)
...core/src/naming-strategy/AbstractNamingStrategy.ts 100.00% <100.00%> (ø)
packages/knex/src/query/ObjectCriteriaNode.ts 100.00% <100.00%> (ø)
packages/knex/src/query/QueryBuilder.ts 100.00% <100.00%> (ø)
packages/knex/src/query/ScalarCriteriaNode.ts 100.00% <100.00%> (ø)

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 706935e...06c8963. Read the comment docs.

@B4nan B4nan merged commit 89d63b3 into master Nov 14, 2021
@B4nan B4nan deleted the alias-naming-strategy branch November 14, 2021 20:48
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.

None yet

2 participants