From f3a79c2e381596be8a93bdea9d181279a0c8f92f Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 26 Aug 2018 01:19:28 +0200 Subject: [PATCH] #71 relation onUpdate --- src/entity.mst | 2 +- src/models/RelationInfo.ts | 10 ++++- src/models/RelationTempInfo.ts | 10 ++++- .../github-issues/71/entity/Post.ts | 41 +++++++++++++++++++ .../github-issues/71/entity/PostAuthor.ts | 24 +++++++++++ .../github-issues/71/entity/PostCategory.ts | 25 +++++++++++ .../github-issues/71/entity/PostDetails.ts | 23 +++++++++++ .../github-issues/71/entity/PostReader.ts | 19 +++++++++ test/integration/githubIssues.test.ts | 3 ++ 9 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 test/integration/github-issues/71/entity/Post.ts create mode 100644 test/integration/github-issues/71/entity/PostAuthor.ts create mode 100644 test/integration/github-issues/71/entity/PostCategory.ts create mode 100644 test/integration/github-issues/71/entity/PostDetails.ts create mode 100644 test/integration/github-issues/71/entity/PostReader.ts diff --git a/src/entity.mst b/src/entity.mst index 1cba3ed..35ccf37 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -24,7 +24,7 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Man }) {{toPropertyName tsName}}:{{ts_type}}{{#is_nullable}} | null{{/is_nullable}}; {{/relations}}{{#relations}} - @{{relationType}}(type=>{{toEntityName relatedTable}}, {{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../isPrimary}}primary:true,{{/../isPrimary}}{{^../is_nullable}} nullable:false,{{/../is_nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#actionOnDelete}},{ onDelete: '{{.}}' }{{/actionOnDelete}}{{/if}}){{#isOwner}} + @{{relationType}}(type=>{{toEntityName relatedTable}}, {{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../isPrimary}}primary:true,{{/../isPrimary}}{{^../is_nullable}} nullable:false,{{/../is_nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }}{{#actionOnDelete}},{ onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}} {{#if isManyToMany}}@JoinTable(){{else}}@JoinColumn({ name:'{{ ../sqlName}}'}){{/if}}{{/isOwner}} {{#if (or isOneToMany isManyToMany)}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) "[]")}}; {{else}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) ' | null')}}; diff --git a/src/models/RelationInfo.ts b/src/models/RelationInfo.ts index 58fc7fe..10b9824 100644 --- a/src/models/RelationInfo.ts +++ b/src/models/RelationInfo.ts @@ -5,8 +5,14 @@ export class RelationInfo { relatedColumn: string; ownerTable: string; ownerColumn: string; - actionOnDelete: "RESTRICT" | "CASCADE" | "SET NULL" | null; - actionOnUpdate: "RESTRICT" | "CASCADE" | "SET NULL" | null; + actionOnDelete: + | "RESTRICT" + | "CASCADE" + | "SET NULL" + | "DEFAULT" + | "NO ACTION" + | null; + actionOnUpdate: "RESTRICT" | "CASCADE" | "SET NULL" | "DEFAULT" | null; relationIdField: boolean = false; get isOneToMany(): boolean { diff --git a/src/models/RelationTempInfo.ts b/src/models/RelationTempInfo.ts index 1e1063f..0081955 100644 --- a/src/models/RelationTempInfo.ts +++ b/src/models/RelationTempInfo.ts @@ -3,7 +3,13 @@ interface RelationTempInfo { ownerColumnsNames: string[]; referencedTable: string; referencedColumnsNames: string[]; - actionOnDelete: "RESTRICT" | "CASCADE" | "SET NULL" | null; - actionOnUpdate: "RESTRICT" | "CASCADE" | "SET NULL" | null; + actionOnDelete: + | "RESTRICT" + | "CASCADE" + | "SET NULL" + | "DEFAULT" + | "NO ACTION" + | null; + actionOnUpdate: "RESTRICT" | "CASCADE" | "SET NULL" | "DEFAULT" | null; object_id: number | string; } diff --git a/test/integration/github-issues/71/entity/Post.ts b/test/integration/github-issues/71/entity/Post.ts new file mode 100644 index 0000000..c9b85b6 --- /dev/null +++ b/test/integration/github-issues/71/entity/Post.ts @@ -0,0 +1,41 @@ +import { Index, Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable } from "typeorm"; +import { PostReader } from "./PostReader"; +import { PostAuthor } from "./PostAuthor"; +import { PostCategory } from "./PostCategory"; +import { PostDetails } from "./PostDetails"; + + +@Entity("Post") +export class Post { + + @Column("int", { + nullable: false, + primary: true, + name: "Id" + }) + Id: number; + + @OneToOne(type => PostAuthor, PostAuthor => PostAuthor.Id, + { + // onDelete: "CASCADE", + // onUpdate: "CASCADE" + }) + postAuthor: PostAuthor; + + @OneToOne(type => PostReader, PostReader => PostReader.Id) + postReader: PostReader; + + @OneToOne(type => PostCategory, PostCategory => PostCategory.Id, + { + // onDelete: "RESTRICT", + // onUpdate: "RESTRICT" + }) + postCategory: PostCategory; + + @OneToOne(type => PostDetails, PostDetails => PostDetails.Id, + { + // onDelete: "SET NULL", + // onUpdate: "SET NULL" + }) + postDetails: PostDetails; +} diff --git a/test/integration/github-issues/71/entity/PostAuthor.ts b/test/integration/github-issues/71/entity/PostAuthor.ts new file mode 100644 index 0000000..70fcfc7 --- /dev/null +++ b/test/integration/github-issues/71/entity/PostAuthor.ts @@ -0,0 +1,24 @@ +import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm"; +import { Post } from "./Post"; + + +@Entity("PostAuthor") +export class PostAuthor { + + @Column("int",{ + nullable:false, + primary:true, + name:"Id" + }) + Id:number; + + + + @OneToOne(type => Post, Post => Post.Id,{ + onDelete: "CASCADE", + // onUpdate: "CASCADE" + }) + @JoinColumn() + post:Post; + +} diff --git a/test/integration/github-issues/71/entity/PostCategory.ts b/test/integration/github-issues/71/entity/PostCategory.ts new file mode 100644 index 0000000..688d343 --- /dev/null +++ b/test/integration/github-issues/71/entity/PostCategory.ts @@ -0,0 +1,25 @@ +import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm"; +import { Post } from "./Post"; + + +@Entity("PostCategory") +export class PostCategory { + + @Column("int",{ + nullable:false, + primary:true, + name:"Id" + }) + Id:number; + + + + @OneToOne(type => Post, Post => Post.Id, + { + // onDelete: "RESTRICT", + // onUpdate: "RESTRICT" + }) + @JoinColumn() + post:Post; + +} diff --git a/test/integration/github-issues/71/entity/PostDetails.ts b/test/integration/github-issues/71/entity/PostDetails.ts new file mode 100644 index 0000000..7935433 --- /dev/null +++ b/test/integration/github-issues/71/entity/PostDetails.ts @@ -0,0 +1,23 @@ +import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm"; +import { Post } from "./Post"; + + +@Entity("PostDetails") +export class PostDetails { + + @Column("int",{ + nullable:false, + primary:true, + name:"Id" + }) + Id:number; + + @OneToOne(type => Post, Post => Post.Id, + { + onDelete: "SET NULL", + // onUpdate: "SET NULL" + }) + @JoinColumn() + post:Post; + +} diff --git a/test/integration/github-issues/71/entity/PostReader.ts b/test/integration/github-issues/71/entity/PostReader.ts new file mode 100644 index 0000000..9e91689 --- /dev/null +++ b/test/integration/github-issues/71/entity/PostReader.ts @@ -0,0 +1,19 @@ +import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable, RelationId} from "typeorm"; +import { Post } from "./Post"; + + +@Entity("PostReader") +export class PostReader { + + @Column("int",{ + nullable:false, + primary:true, + name:"Id" + }) + Id:number; + + @OneToOne(type => Post, Post => Post.Id) + @JoinColumn() + post:Post; + +} diff --git a/test/integration/githubIssues.test.ts b/test/integration/githubIssues.test.ts index 1c348ed..2cb6f8d 100644 --- a/test/integration/githubIssues.test.ts +++ b/test/integration/githubIssues.test.ts @@ -41,6 +41,9 @@ describe("GitHub issues", async function () { if (dbDriver == 'mysql' || dbDriver == 'mariadb' || dbDriver == 'oracle' || dbDriver == 'sqlite') continue; break; + // case '71': + // xit(dbDriver); + // continue; default: break; }