routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

use classTransformer only for input

Open jselesan opened this issue 9 years ago • 18 comments

Hi there. A time ago, I had to disable classTransformer (in useExpressServer) because it didn't work fine with mongoose models, but now, I would like to take advantage of classTransformer to transform the query params to an object, so it would be nice to have the ability to enable classTransformer only for the input and keep it disabled for output. Is it posible?

jselesan avatar Jun 09 '17 17:06 jselesan

no I don't think its possible right now. What problem do you have with mongooose models? Maybe @Exclude can help you in your mongoose models?

pleerock avatar Jun 10 '17 05:06 pleerock

It would be nice to have a option to disable transform on response. Return a sequelize instance or mongoose instance both throw error.

I now doing this to disable transform on response:

import { KoaDriver } from 'routing-controllers/driver/koa/KoaDriver'

const originHandleSuccess = KoaDriver.prototype.handleSuccess
KoaDriver.prototype.handleSuccess = function () {
  this.useClassTransformer = false
  return originHandleSuccess.apply(this, arguments)
}

ruiming avatar Jun 18 '17 13:06 ruiming

thats why you should use typeorm instead of sequelize or mongoose 😝 . But seriously it should be easily fixed if you do something like this in your models:

@Exclude()
_id: string;

@Expose()
get id() {
    return this._id.toString(); // or some other method that normalizes that id
}

pleerock avatar Jun 27 '17 14:06 pleerock

Closing as a duplicate in favor of #226. Please subscribe there to be notified when the required changes land.

MichalLytek avatar Aug 08 '17 14:08 MichalLytek

@ruiming Nice workaround, thanks!

@pleerock I really appreciate the work you've done with routing controllers so far, it's great! But I believe a library like this should be agnostic of any data mappers being used rather than being developed to suit only TypeORM.

nathanosdev avatar Aug 23 '17 23:08 nathanosdev

It does not "suit typeorm". class-transformer is generic serializer and does work with object. In mongodb you have complex MongoID object and it simply tries to serialize it as well. The issue with complexity of mongoid object, not with class-transformer.

pleerock avatar Sep 06 '17 04:09 pleerock

thats why you should use typeorm instead of sequelize or mongoose

I was referring to your comment, which implies you shouldn't be using anything other TypeOrm with routing-controllers.

nathanosdev avatar Sep 06 '17 10:09 nathanosdev

I didnt say that routing-controllers cannot be used with anything else except typeorm. It was simple advice and a bit of joke as you can see from "😝" smile I used.

pleerock avatar Sep 06 '17 10:09 pleerock

Okay that's my bad 😆 sorry for misunderstanding!

nathanosdev avatar Sep 06 '17 11:09 nathanosdev

BTW you should really try TypeORM 😝

pleerock avatar Sep 06 '17 11:09 pleerock

If TypeORM had full support for Mongoose then I would gladly, but unfortunately it doesn't 😢 I'm working on a similar project to TypeORM specifically for Mongoose myself at the moment to compensate 😉

nathanosdev avatar Sep 06 '17 11:09 nathanosdev

I'm working on a similar project to TypeORM specifically for Mongoose myself at the moment to compensate

+1 to that!

jselesan avatar Sep 06 '17 11:09 jselesan

It does not have all features mongoose has, but it does have all required features to make really good applications using mongodb.

pleerock avatar Sep 06 '17 11:09 pleerock

The last I checked, it had only "basic" MongoDB support. Which is great, I'm glad to see it going in that direction and I mean no offense or to put down TypeORM in any way because I think it's an absolutely fantastic project. But Mongoose is extremely advanced and has a lot of features which personally I could not feel like I could do without, I am very heavily invested in that particular technology in a lot of projects. My TypeORM-like project will just be a simple enough tool that converts classes into Mongoose compatible schemas and leave mostly everything else up to Mongoose. All hail Mongoose 🙏 📿

nathanosdev avatar Sep 06 '17 14:09 nathanosdev

Those using Sequelize: you can return the result of your model instance's toJSON method to the action method in order for class-transformer's serialization to work properly

smithdalec avatar Sep 19 '17 17:09 smithdalec

Any update about this?

rochapablo avatar Oct 30 '18 22:10 rochapablo

This is my workaround:

// Disable classToPlain class-transformer
require('class-transformer')['classToPlain'] = function (obj: object)  {
    return JSON.parse(JSON.stringify(obj))
}

sabereen avatar Apr 14 '19 10:04 sabereen

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 11 '20 00:12 github-actions[bot]