Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformer in Entity is ignored #422

Closed
joerndigital opened this issue Feb 13, 2020 · 2 comments
Closed

Transformer in Entity is ignored #422

joerndigital opened this issue Feb 13, 2020 · 2 comments

Comments

@joerndigital
Copy link

Hi,

I'm trying to work with a transformer in one of my entities to map a uuid to a binary number.

@Entity('object')
export class ObjectEntity {

    @PrimaryColumn('binary', {
        primary: true,
        transformer: new UuidTransformer(),
    })
    @ApiProperty({
        description: 'The uuid of the object',
        type: 'string',
        format: 'uuid'
    })
    id: string
}

The controller

@ApiTags('Object')
@Crud({
    model: {
        type: ObjectEntity,
    },
    routes: {
        only: ['getManyBase', 'getOneBase', 'createOneBase'],
    },
    params: {
        object_id: {
            type: 'uuid',
            primary: true,
            field: 'id',
        }
    },
})
@Controller()
export class ObjectController {
    constructor(public service: ObjectService) {
    }
}

But in my controller the transformer function is not triggered by the getOneBase method. Only after I overwrite getOneBase with my own custom method the transformer runs.

    @Override('getOneBase')
    _getOneBase( @Param('object_id') id) {
        return this.service.findOne(id);
    }

Where Clause without overriding:

 WHERE (`ObjectEntity`.`id` = ?) -- PARAMETERS: ["3fa85f64-5717-7562-b3fc-2c963f66afa6"]

Where Clause with overriding:

WHERE `ObjectEntity`.`id` = ? LIMIT 1 -- PARAMETERS: [{"type":"Buffer","data":[63,168,95,100,87,23,117,98,179,252,44,150,63,102,175,166]}]

Do I miss something to get the transformer working without overriding?

Best regards

@imamik
Copy link

imamik commented Feb 13, 2020

+ 1

@Diluka
Copy link
Contributor

Diluka commented Feb 28, 2020

by default, nestjs doesn't transform plain to class.

you can implement your own pipe:

@Injectable()
export class ParserPipe implements PipeTransform {

  transform(value: any, metadata: ArgumentMetadata): any {
    if (metadata.type !== 'custom' && value && metadata.data && metadata.metatype) {
      return plainToClass(metadata.metatype, value);
    }
    return value;
  }
}

or you can config ValidationPipe to do so (only body)
https://docs.nestjs.com/techniques/validation#transform-payload-objects

@Diluka Diluka closed this as completed Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants