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

class-transformer-shim: support metadata reflection #4219

Closed
haschu opened this issue Jun 1, 2019 · 3 comments · Fixed by #4222
Closed

class-transformer-shim: support metadata reflection #4219

haschu opened this issue Jun 1, 2019 · 3 comments · Fixed by #4222

Comments

@haschu
Copy link
Contributor

haschu commented Jun 1, 2019

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)


TypeORM provides a class-transformer-shim, which is awesome for browser/fronend application usage.

However, the transformation of Date types for example - or any other type, that isn't passed via a decorator and a typeFunction - requires additional class-transformer code:

@Entity()
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    url: string;
    
    // Works fine => class-transformer creates User objects :)
    @ManyToOne(type => User, user => user.photos)
    user: User;
    
    // No Date object, class-transformer leaves that as string :(
    @CreateDateColumn()
    createdAt: Date;

}

To make it work, we need to write:

import { Type } from 'class-transformer';

//...

@Type(() => Date)
@CreateDateColumn()
createdAt: Date;

which I find annoying, especially when sharing entities between backend and frontend. One would also have to import the class-transformer library into his backend, but actually it's only used on the frontend side. Plus of course, it saves us lots of code lines.

We could solve this, if typeorm-class-transformer-shim.js would support metadata reflection.
If the client imports "reflect-metadata", we could use design:type and automatically pass it to the @Type decorator of class-transformer, as we already do with typeFunctions.

PR is on the way.

@haschu haschu changed the title class-transformer-shim: add metadata reflection class-transformer-shim: support metadata reflection Jun 1, 2019
haschu added a commit to haschu/typeorm that referenced this issue Jun 1, 2019
@Michael-Dawkins
Copy link

Huge thanks for bringing this up, I am currently "reviving" all dates individually and manually on the front end and forgetting this is often a source of bugs for us.

I am not familiar with the use of reflect-metadata, have you managed to implement a bridge from typeORM's date annotation to class-transformer ?

@haschu
Copy link
Contributor Author

haschu commented Jun 6, 2019

@Michael-Dawkins yes, it's already included in the latest release :)

You have to include typeorm-class-transformer-shim.js into your tsconfig.json path section:

    "paths": {
      "typeorm": [ "node_modules/typeorm/extra/typeorm-class-transformer-shim.js" ]
    }

@Michael-Dawkins
Copy link

You just made my day.

I've been thinking for months it would be nice to simply use the annotations already present from using TypeORM or heck, even typescript.
Now I can remove so much low value code from our codebase .

By reading class-transformer's doc again today, I also discovered "enableImplicitConversion"'s new option of class-transformer which is a nice addition.

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.

2 participants