typeorm createQueryBuilder Boolean type ๊ด๋ จ
๋ต๋ณ ์ ๋ง ๊ฐ์ฌํฉ๋๋ค. ๊ด๋ จ ํํธ๋ฅผ ์ฃผ์
์ ์์ฑ์ ํด๋ณด์๋๋ฐ๋ ์๋์ด ์ ๋์ง ์๊ณ ๊ตฌ๊ธ๋ง์๋ ์ฝ์ง๊ฐ ์๋ค์.. ํน์ ์ด์ ๋ฅผ ์ ์ ์์๊น์? findBy๋ก ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ true๋ก ๋์ค๋๋ฐ queryBuilder๋ 0 ๋๋ 1๋ง ๊ณ์ ๋์ค๋ค์.. //--- transformer import { ValueTransformer } from 'typeorm'; function isNullOrUndefined( obj: T | null | undefined, ): obj is null | undefined { return typeof obj === 'undefined' || obj === null; } export class BooleanTransformer implements ValueTransformer { public from(value?: number | null): boolean | undefined { if (isNullOrUndefined(value)) { return; } return value ? true : false; } public to(value?: boolean | null): number | undefined { if (isNullOrUndefined(value)) { return; } return value ? 1 : 0; } } //--- entity @Column('tinyint', { name: 'is_use', nullable: true, comment: '์ฌ์ฉ ์ฌ๋ถ', width: 1, transformer: new BooleanTransformer(), }) isUse: boolean | null; // ---service async selectUserForTest(): Promise[] > { const user = await this.userRepository .createQueryBuilder() .select('username') .addSelect('is_use') .getRawMany(); return user; }