Alessandro Prudencio
Alessandro Prudencio

Reputation: 53

Typeorm transformer always return true

I'm using typeorm and trying to transform a column in the database to bollean instead of string.

The field in the bank is bit.

But I want to return as boolean, but when using or transforming it always returns true, what to do?

    export default class ColumnBooleanTransformer implements ValueTransformer {
      public from(value?: string | null): boolean | undefined {
        return Boolean(Number(value));
      }
    
      public to(value?: boolean | null): string | undefined {
        return value ? '1' : '0';
      }

My column:

  @Column({
    nullable: false,
    transformer: new ColumnBooleanTransformer(),
  })
  STAProvado: boolean;

Upvotes: 1

Views: 1806

Answers (1)

Alessandro Prudencio
Alessandro Prudencio

Reputation: 53

I found the solution, add type property to column, use the same type created in the bank

 @Column({
    type: 'bit',
    nullable: false,
    transformer: new ColumnBooleanTransformer(),
  })
  isAdmin: boolean;

Upvotes: 2

Related Questions