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

Add Alias in Filter/Entity #10588

Closed
1 task done
samonkoba opened this issue Nov 22, 2022 · 1 comment
Closed
1 task done

Add Alias in Filter/Entity #10588

samonkoba opened this issue Nov 22, 2022 · 1 comment
Labels
needs triage This issue has not been looked into type: enhancement 🐺

Comments

@samonkoba
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I have an issue when trying to filter nested columns in the @authorize decorator.

ERROR [ExceptionsHandler] ER_NONUNIQ_TABLE: Not unique table/alias
@Authorize({
    authorize: (context: UserContext) => ({
        or:[
            {apartment: {owner: {agents: {agentId: {eq: context.req.user.id}}}}},
            {owner: {agents: {agentId: {eq: context.req.user.id}}}},
        ]
    } as unknown)
})

and was wondering whether we could have alias possibilities in the Filter functionality to enable us use Authorize or any other fllter feature without deeper customizations

Describe the solution you'd like

A solution would be

@Authorize({
    authorize: (context: UserContext) => ({
        or:[
            {apartment: {owner: {agents: {agentId: {eq: context.req.user.id}}, alias:"a_agents"}, alias:"a_owner"}}},
            {owner: {agents: {agentId: {eq: context.req.user.id}}, alias:"h_agents"}, alias:"h_owner"},
        ]
    } as unknown)
})

OR

@Authorize({
    authorize: (context: UserContext) => ({
        or:[
            {apartment: {"owner as apartment_owner": {"agents as apartment_agents": {agentId: {eq: context.req.user.id}}}}},
            {"owner as house_owner": {"agents as house_agents": {agentId: {eq: context.req.user.id}}}},
        ]
    } as unknown)
})

OR

    @JoinColumn({alias:"a_owner"})
    owner?: OwnerEntity;

The result would be to generate an SQL statement with a custom alias and ideally prevent multiple aliases when filtering with same entities and help alleviate the "Alias" issue reducing the workload for revamping whole modules

Teachability, documentation, adoption, migration strategy

In cases one would need to use custom alias for all SQL statements generated through filter. You can specify the alias as part of the filter tags as below

@Authorize({
    authorize: (context: UserContext) => ({
        or:[
            {apartment: {owner: {agents: {agentId: {eq: context.req.user.id}}, alias:"a_agents"}, alias:"a_owner"}}},
            {owner: {agents: {agentId: {eq: context.req.user.id}}, alias:"h_agents"}, alias:"h_owner"},
        ]
    } as unknown)
})

OR
you can as well add alias in the JoinColumn for your entity

    @JoinColumn({alias:"a_owner"})
    owner?: OwnerEntity;

What is the motivation / use case for changing the behavior?

Having multiple entities that share a third entity. Example; Either a house or an apartment can have an owner, while trying to filter owner house relationship one faces the error below.
ERROR [ExceptionsHandler] ER_NONUNIQ_TABLE: Not unique table/alias: 'owner'

@samonkoba samonkoba added needs triage This issue has not been looked into type: enhancement 🐺 labels Nov 22, 2022
@samonkoba samonkoba changed the title Alias in Filter Add Alias in Filter/Entity Nov 22, 2022
@micalevisk
Copy link
Member

micalevisk commented Nov 22, 2022

@Authorize() is not maintained by @nestjs

run npm docs <package-name> to check out the right repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into type: enhancement 🐺
Projects
None yet
Development

No branches or pull requests

2 participants