Last active
May 9, 2019 02:49
-
-
Save eddieajau/cc46507bb9d91e6ba129c2eb97770aff to your computer and use it in GitHub Desktop.
Example of automatic translation of a JSON property in a TypeORM model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrimaryGeneratedColumn, Entity, Column } from 'typeorm'; | |
@Entity('foo') | |
export class Foo { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column({ default: '' }) | |
public get link(): string { | |
let value = this._link; | |
if (typeof value === 'string' && value.length > 0) { | |
value = JSON.parse(value); | |
} | |
return value; | |
} | |
public set link(link: string) { | |
this._link = JSON.stringify(link); | |
} | |
private _link: string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment