Skip to content

Instantly share code, notes, and snippets.

@Bnaya
Last active September 28, 2020 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bnaya/b228369df108379b95be468a30548f6d to your computer and use it in GitHub Desktop.
Save Bnaya/b228369df108379b95be468a30548f6d to your computer and use it in GitHub Desktop.
typeorm-idonnowhattodo-1
import {
Entity,
JoinColumn,
ManyToOne,
Column,
PrimaryGeneratedColumn,
ManyToMany,
JoinTable,
OneToMany,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@ManyToMany(type => Template, template => template.categories)
@JoinTable({
name: 'category_templates_template',
joinColumn: {
name: 'category_id',
referencedColumnName: 'id',
},
inverseJoinColumn: {
name: 'template_id',
referencedColumnName: 'id',
},
})
templates: Category[];
@OneToMany(
type => Category_templates_template,
category_templates_template => category_templates_template.category,
)
categoryTemplates: Category_templates_template[];
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt: Date;
}
@Entity()
export class Template {
@PrimaryGeneratedColumn()
id: number;
@Column({
length: 100,
})
name: string;
// https://github.com/typeorm/typeorm/issues/1224#issuecomment-506631201
@ManyToMany(type => Category, category => category.templates)
categories: Category[];
@OneToMany(
type => Category_templates_template,
category_templates_template => category_templates_template.template,
)
templateCategories: Category_templates_template[];
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date;
@UpdateDateColumn({ type: 'timestamp' })
updatedAt: Date;
}
// almost as
// https://github.com/typeorm/typeorm/issues/1224#issuecomment-506631201
@Entity('category_templates_template')
export class Category_templates_template {
// @PrimaryGeneratedColumn()
// id: number;
@JoinColumn({ name: 'template_id' })
@ManyToOne(type => Template, template => template.templateCategories, {
primary: true,
})
template: Template;
@JoinColumn({
name: 'category_id',
})
@ManyToOne(type => Category, category => category.categoryTemplates, {
primary: true,
})
category: Category;
@Column()
bla: number;
// @CreateDateColumn({ type: 'timestamp' })
// createdAt: Date;
// @UpdateDateColumn({ type: 'timestamp' })
// updatedAt: Date;
}
CREATE TABLE "category" ("id" SERIAL NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_9c4e4a89e3674fc9f382d733f03" PRIMARY KEY ("id"));
CREATE TABLE "template" ("id" SERIAL NOT NULL, "name" character varying(100) NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_fbae2ac36bd9b5e1e793b957b7f" PRIMARY KEY ("id"));
CREATE TABLE "category_templates_template" ("bla" integer NOT NULL, "template_id" integer NOT NULL, "category_id" integer NOT NULL, CONSTRAINT "PK_9930c914c598eb0ad93a4befaa6" PRIMARY KEY ("template_id", "category_id"));
ALTER TABLE "category_templates_template" DROP COLUMN "bla";
ALTER TABLE "category_templates_template" ADD "bla" integer NOT NULL;
CREATE INDEX "IDX_ad1ac1095859c4e9463ab52406" ON "category_templates_template" ("category_id");
CREATE INDEX "IDX_329a4d7da114e1ddf7db935204" ON "category_templates_template" ("template_id");
ALTER TABLE "category_templates_template" ADD CONSTRAINT "FK_329a4d7da114e1ddf7db9352049" FOREIGN KEY ("template_id") REFERENCES "template"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "category_templates_template" ADD CONSTRAINT "FK_ad1ac1095859c4e9463ab52406c" FOREIGN KEY ("category_id") REFERENCES "category"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment