Hi, I’m Dung Huynh Duc . Nice to meet you.

About Me

I’m a full stack developer. I’m a fast learner and self-taught coder. I often take my time for researching and learning about hot and trending technology.

TypeORM
Database

#TIL 10 - Add User Define Function to TypeORM entity

blog_hero_#TIL 10 - Add User Define Function to TypeORM entity

This is a workaround that I found from the TypeORM issue. Simply, the solution would implement as below

Step 1: Add custom field to the class entity

        // add to User.ts entity
        @Column('int', { insert: false, readonly: true })
        public qty: number;

Step 2: query the custom field with query builder

        // need to mapping with the convention: table + _ + UDF column (user_qty)
        .addSelect('dbo.udfFindTotalQty(user.id)', 'user_qty')
        ...
        .printSql()
        .getManyAndCount();