FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

Add custom select and map to property of entity

Fresh3 days ago
Mar 14, 20260 views
Confidence Score62%
62%

Problem

Issue type: [ ] question [ ] bug report [X ] feature request [ ] documentation issue Database system/driver: [X] `cordova` [X] `mongodb` [X] `mssql` [X] `mysql` / `mariadb` [X] `oracle` [X] `postgres` [X] `sqlite` [X] `sqljs` [X] `websql` TypeORM version: [ ] `latest` [ ] `@next` [ ] `0.x.x` (or put your version here) Steps to reproduce or a small repository showing the problem: What I to accomplish is to add a custom select that maps to a property of my Entity. I've found an work around but would like to know if there is an easier/cleaner way to do this. So I've my model: [code block] This entity has a ManyToMany relationship to User. The `JoinTable` `groups_users` has one extra column `active` this `active` indicates if the group that the user is linked to is visible. When I query the groups to get all the groups for one user (also the inactive groups) I do this: [code block] As you can see the `Group` entity has a property called active. As for now the `active` property is not seen by `typeorm` so what I did was the following: I added this to the active property: [code block] By setting `select` to `false` the other queries won't break (since active isn't a real column inside the `group` table). Now I added this to the query: [code block] By doing this the active property is filled for each user and the `active` is true or false according the 0 or 1 in `JoinTable`. To query I ended up with is: [code block] which leaves me with the correct output. Now thi

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Implement Custom Select for Active Property in Group Entity

Medium Risk

The 'active' property in the Group entity is not directly mapped to a column in the Group table, as it is derived from a ManyToMany relationship with the User entity through a JoinTable. This causes TypeORM to ignore the 'active' property during queries unless explicitly defined.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define Active Property in Group Entity

    Add the 'active' property to the Group entity with 'select' set to false to prevent it from being included in default queries.

    typescript
    active: { type: 'boolean', select: false }
  2. 2

    Modify Query to Include Active Property

    Update the query that retrieves groups for a user to explicitly select the 'active' property from the JoinTable, ensuring it is populated correctly.

    typescript
    const groups = await getRepository(Group)
      .createQueryBuilder('group')
      .leftJoinAndSelect('group.users', 'user')
      .addSelect('groups_users.active')
      .where('user.id = :userId', { userId })
      .getMany();
  3. 3

    Map Active Property to Group Instances

    After executing the query, map the 'active' values from the JoinTable to the corresponding Group instances to ensure each group has the correct 'active' status.

    typescript
    groups.forEach(group => {
      group.active = groups_users.active;
    });
  4. 4

    Test the Implementation

    Run tests to ensure that the 'active' property is correctly populated for each Group instance when querying by user. Confirm that inactive groups are also retrieved with the correct 'active' status.

    typescript
    const userGroups = await getUserGroups(userId);
    console.log(userGroups); // Check if active status is correct

Validation

Confirm the fix by running the query to fetch groups for a user and checking that each group instance has the correct 'active' property set based on the JoinTable data. Ensure that both active and inactive groups are returned with accurate statuses.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typeormormtypescript