'TypeORM Select Specific Column Only

I'm using TypeORM. Via Repository/Entity pattern and findOneOrFail, I want to select a specific column (isBusiness) only for my results.

this.userProfile.findOneOrFail({
            join: {
                innerJoin: {
                    user: "usr.user",
                    preference: "usr.preference"
                },
                alias: "usr"
            },
            where: {
                user: {
                    userId: userId
                }
            },
            select: ['id', 'preference'],
            relations: ['preference']

This will return:

  1. Id of an user profile.
  2. The preference row related to the user profile.

The example JSON payload:

  {
    id: 99,
    preference: { 
        id : 912,
        theme: 'dark'
        isBusiness: true
        }
}

I need to select only the isBusiness column so that I would have

{
    id: 99,
    preference: { 
        isBusiness: true
        }
}

Unfortunately, I couldn't use preference.isBusiness only to the select object as it only accepts a keyof the userProfile schema like this:

select: ['id', 'preference.isBusiness'],

Thank you for your help as I couldn't find any solution for the repository/entity way. For the query builder approach, it's all good as there's a bunch of documentation/references for it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source