Typegoose: Class-Transformer Collection Issue

Created on 3 Nov 2019  路  6Comments  路  Source: typegoose/typegoose

Currently class-transformer dosnt work on documents, because it is not an instance of the class, and without an function like anyToplain(class, input) (https://github.com/typestack/class-transformer/issues/227), it is currently not possible to use class-transformer (or class-validator) with typegoose

-> when using it with nestjs and returning it in an route, you should always call doc.toJSON() (or directly model.find().lean() to directly convert it to a POJO

Related Issues

  • #61
  • #96
    (probably - #105 too)
bug cant fix has repro script help wanted

Most helpful comment

i dont know if it will work now, but if anyone knows how to use class-transformer / class-validator, i would like to accept pr's that add tests for them

All 6 comments

Hi not sure if I'm confusing things -- do you mean if a key on a document has the value of another class/subcollection with ref? or just class-transformer not working with any use case?

I'm currently using class-transformer/class-validator with my classes but my data models do not need subcollections/subdocuments or use "ref" as part of a nestjs based project.

I haven't encountered any issues with it.

@sebastiangug in my testing (back in 5.x and 6.x times), class-transformer didnt want to translate any document correctly
-> i never tested it on 7.x, because from what i know, the code on how typegoose and mongoose handles this is the same

i dont know if it will work now, but if anyone knows how to use class-transformer / class-validator, i would like to accept pr's that add tests for them

As a side note class-validator is similar to class-transformer in the way that it can only work its magic after creating an instance of its annotated class. I'm not sure that showing its integration with typegoose is really relevant, unless you want to validate the result of a typegoose query, since usually it's used upfront to validate data arriving on your route endpoints.

@Roms1383 from what i know c-v is commonly used with type-graphql and nestjs

Yes for Nest.js as specified in the documentation validation usually happens on your route controller.

For incoming data send to the Controller route, ValidationPipe with option transform: true will both deserialize incoming plain payload with class-transformer into a class instance and validate it with class-validator altogether.

For outgoing data returned from Controller route, usually serialization from a class instance back to a plain object happens with ClassSerializerInterceptor specified on the Controller route.

e.g.

// user.controller.ts
@Controller()
export class UserController {
  constructor(
     private readonly users: UserService,
     private readonly mailer: MailerService) {}

  @UseInterceptors(ClassSerializerInterceptor) //serialize outgoing data
  @Post('register')
  register(
    @Body(new ValidationPipe({ transform: true })) // deserialize & validate incoming data
    payload: User) {
    return this.users.create(payload)
  }
}
// user.service.ts
@Injectable()
export class UserService {
  private model: any
  constructor() {
    this.model = getModelForClass(User)
  }
  async create(user: User): Promise<Document<User>|never> {
    const { _id: id } = await this.model.create(user)
    const newcomer = await this.model.findById(id).exec()
    return newcomer
  }
}



md5-43cd4d483255ea2ace1e8d638cba163a



```ts
// user.service.ts
@Injectable()
export class UserService {
  private model: any
  constructor() {
    this.model = getModelForClass(User)
  }
  async create(user: User): Promise<User|never> {
    const { _id: id } = await this.model.create(user)
    const newcomer = await this.model.findById(id).exec()
    return plainToClass(User, newcomer) // deserialize plain Document<User> back into a User instance
  }
}

I'm not familiar with GraphQL, so if somebody who does can step in and had more informations it would be nice 馃槂

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huming2207 picture huming2207  路  8Comments

hasezoey picture hasezoey  路  10Comments

akash-rajput picture akash-rajput  路  4Comments

dandv picture dandv  路  10Comments

AbderrazzakB picture AbderrazzakB  路  3Comments