Skip to content

Instantly share code, notes, and snippets.

@eddieajau
Last active May 9, 2019 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieajau/cc46507bb9d91e6ba129c2eb97770aff to your computer and use it in GitHub Desktop.
Save eddieajau/cc46507bb9d91e6ba129c2eb97770aff to your computer and use it in GitHub Desktop.
Example of automatic translation of a JSON property in a TypeORM model.
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