๐ Overview:
Have you tried to implement getter/setter in your entity of TypeORM?
But Not found exact though use of AI.
๐ The Problem :
- I try to map a private _userType to a public userType getter/setter.
 - TypeORM still reads/writes the raw _userType field without calling get/set.
 - The conversion code inside get userType() / set userType() is never applied
 
โ Have you tried like below ?
And getting the
"Error: Missing column value in entity for: user_type
"
โ
@Entity()
export class User {
  @Column({ name: 'user_type', type: 'int' })
  private _userType: number;
  // These will NOT be used by TypeORM:
  get userType(): string {
    return this._userType === 1 ? 'ADMIN' : 'USER';
  }
  set userType(val: string) {
    this._userType = val === 'ADMIN' ? 1 : 0;
  }
}
โ The Fix: Use transformer
Works perfectly โ no more errors, and clean transformations. Instead of getters/setters, use TypeORMโs built-in transformer option on the column
@Entity()
export class User {
  @Column({
    name: 'user_type',
    type: 'int',
    transformer: {
     to(role: string): number {
       return role === 'ADMIN' ? 1 : 0;
     }
     from(value: number): string {
       return value === 1 ? 'ADMIN' : 'USER';
     }
   };
  })
}
              
    
Top comments (0)