Nestjs: Loading jsonb from CockroachDB with TypeOrm turns date into string

252 views Asked by At

I'm storing an entity in a CockroachDB, one of the columns is a DTO that is stored in a JSONB column. One of the properties in the DTO is a Date, but when I load it from the database it is a string.

Here my entity:

@Entity('Entity')
export class Entity {
     @Column('jsonb', { nullable: true })
     @IsOptional()
     @Type(() => StateDto)
     @ValidateNested()
     state: StateDto;

     ...
}

StateDto:

export class StateDto {

    @IsDate()
    @IsOptional()
    firstActivationDate: Date;

    ....
}

When I get the entity from the controller firstActivationDate is a Date, when I write the entity into the database it is a Date, inside the database it is a Date, when I load if from the Database with

this.repository.findOne()

it it a string.

I tried to get NestJs/TypeOrm to automatically transform it by using these annotations in the StateDto:

@Type(() => Date)
@Transform(value => new Date(value) ,{ toClassOnly: true })

But that changed nothing. Is there a way to get my Date back as a Date object instead of a string without manually transforming it in the controller/service everytime I load my entity?

0

There are 0 answers