Skip to content

Flexible relation with condition should be added #11554

Open
@danganh97

Description

@danganh97

Feature Description

While implementing TypeOrm for production, I got some use cases that need to set a condition in the relation.
For example:
Case 1:
I have table events and banks with 1-n relation.
In the banks table, have a column named active, and order

I would like to define 2 more relations like:
event 1-n actived_banks (active=true)
event 1-1 latest_bank (order by order take1)
event 1-n deactivated_banks (active=false)

Case 2:
I have a users table 1-n with events
If the user is deactivated, the bank should not be retrieved successfully.

So I would like to define a relationship like:
@BelongsTo (users.active === true)
If no existing active user, the query should return null, but now every relation is in left-join mode, so it cannot go that way.

The Solution

We should implement a separate relationship that allows users to choose the way the query joins (left join or inner join via BelongsTo or HasOne)

Considered Alternatives

Or at least we allow users to query in relation like
this.eventRepo.findOne({
relations: {
user: {
active: true
}
}
})

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions