From 681ef1a3633f03a1b39fe500c5549aef94ad8168 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Mon, 7 Oct 2019 22:29:09 +0200 Subject: [PATCH] more tests passing --- src/Engine.ts | 41 ++++++++++------- src/entity.mst | 4 +- .../sample18-lazy-relations/entity/Author.ts | 13 ++++-- .../entity/Category.ts | 17 ++++--- .../sample18-lazy-relations/entity/Post.ts | 14 ++++-- .../sample3-many-to-one/entity/Post.ts | 26 ++++++----- .../sample4-many-to-many/entity/Post.ts | 34 ++++++++------ .../github-issues/71/entity/Post.ts | 44 +++++++++++-------- test/utils/EntityFileToJson.ts | 5 ++- 9 files changed, 125 insertions(+), 73 deletions(-) diff --git a/src/Engine.ts b/src/Engine.ts index e79e5d8..353e337 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -15,6 +15,7 @@ import SqliteDriver from "./drivers/SqliteDriver"; import NamingStrategy from "./NamingStrategy"; import AbstractNamingStrategy from "./AbstractNamingStrategy"; import { Entity } from "./models/Entity"; +import { Relation } from "./models/Relation"; import changeCase = require("change-case"); import fs = require("fs"); @@ -146,7 +147,7 @@ export function modelCustomizationPhase( let retVal = setRelationId(generationOptions, dbModel); // TODO: retVal = applyNamingStrategy(namingStrategy, retVal); - // retVal = addImportsAndGenerationOptions(retVal, generationOptions); + retVal = addImportsAndGenerationOptions(retVal, generationOptions); // retVal = removeColumnDefaultProperties(retVal, defaultValues); return retVal; } @@ -192,26 +193,21 @@ function removeColumnDefaultProperties( return dbModel; } function addImportsAndGenerationOptions( - dbModel: EntityInfo[], + dbModel: Entity[], generationOptions: IGenerationOptions ) { - dbModel.forEach(element => { - element.Imports = []; - element.Columns.forEach(column => { - column.relations.forEach(relation => { - if (element.tsEntityName !== relation.relatedTable) { - element.Imports.push(relation.relatedTable); + dbModel.forEach(entity => { + entity.relations.forEach(relation => { + if (generationOptions.lazy) { + if (!relation.relationOptions) { + relation.relationOptions = {}; } - }); - }); - element.GenerateConstructor = generationOptions.generateConstructor; - element.IsActiveRecord = generationOptions.activeRecord; - element.Imports.filter((elem, index, self) => { - return index === self.indexOf(elem); + relation.relationOptions.lazy = true; + } }); if (generationOptions.skipSchema) { - element.Schema = undefined; - element.Database = undefined; + entity.schema = undefined; + entity.database = undefined; } }); return dbModel; @@ -368,6 +364,19 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) { return retStr; }); Handlebars.registerHelper("toLowerCase", str => str.toLowerCase()); + Handlebars.registerHelper( + "toRelation", + (entityType: string, relationType: Relation["relationType"]) => { + let retVal = entityType; + if (relationType === "ManyToMany" || relationType === "OneToMany") { + retVal = `${retVal}[]`; + } + if (generationOptions.lazy) { + retVal = `Promise<${retVal}>`; + } + return retVal; + } + ); Handlebars.registerHelper("tolowerCaseFirst", str => changeCase.lowerCaseFirst(str) ); diff --git a/src/entity.mst b/src/entity.mst index 8efda4e..b196fea 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -10,10 +10,10 @@ import { {{toEntityName .}} } from './{{toFileName .}}' {{/inline}} {{#*inline "Relation"}} -@{{relationType}}(()=>{{toEntityName relatedTable}},{{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{toPropertyName relatedField}}{{#if joinColumnOptions}},{ {{json relationOptions}} }{{/if}}) +@{{relationType}}(()=>{{toEntityName relatedTable}},{{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{toPropertyName relatedField}}{{#if relationOptions}},{ {{json relationOptions}} }{{/if}}) {{#if joinColumnOptions}}@JoinColumn({{json joinColumnOptions}}){{/if}} {{#if joinTableOptions}}@JoinTable({ {{json joinTableOptions}} }){{/if}} -{{toPropertyName fieldName}}:{{toEntityName relatedTable}}{{#contains "ToMany" relationType}}[]{{/contains}}; +{{toPropertyName fieldName}}:{{toRelation (toEntityName relatedTable) relationType}}; {{/inline}} {{#*inline "Entity"}} diff --git a/test/integration/examples/sample18-lazy-relations/entity/Author.ts b/test/integration/examples/sample18-lazy-relations/entity/Author.ts index 0d3d178..7a2bd65 100644 --- a/test/integration/examples/sample18-lazy-relations/entity/Author.ts +++ b/test/integration/examples/sample18-lazy-relations/entity/Author.ts @@ -1,9 +1,14 @@ -import { Column, Entity, Index, PrimaryGeneratedColumn, OneToMany } from "typeorm" -import {Post} from "./Post"; +import { + Column, + Entity, + Index, + PrimaryGeneratedColumn, + OneToMany +} from "typeorm"; +import { Post } from "./Post"; @Entity("Author") export class Author { - @PrimaryGeneratedColumn() id: number; @@ -12,6 +17,7 @@ export class Author { @OneToMany(type => Post, post => post.author, { // cascade: true + lazy: true }) posts: Promise; @@ -21,5 +27,4 @@ export class Author { // asPromise() { // return Promise.resolve(this); // } - } diff --git a/test/integration/examples/sample18-lazy-relations/entity/Category.ts b/test/integration/examples/sample18-lazy-relations/entity/Category.ts index 410df9b..1f90c4f 100644 --- a/test/integration/examples/sample18-lazy-relations/entity/Category.ts +++ b/test/integration/examples/sample18-lazy-relations/entity/Category.ts @@ -1,16 +1,23 @@ -import { Column, Entity, Index, PrimaryGeneratedColumn, VersionColumn, ManyToMany } from "typeorm" -import {Post} from "./Post"; +import { + Column, + Entity, + Index, + PrimaryGeneratedColumn, + VersionColumn, + ManyToMany +} from "typeorm"; +import { Post } from "./Post"; @Entity("Category") export class Category { - @PrimaryGeneratedColumn() id: number; @Column() name: string; - @ManyToMany(type => Post, post => post.categorys) + @ManyToMany(type => Post, post => post.categorys, { + lazy: true + }) posts: Promise; - } diff --git a/test/integration/examples/sample18-lazy-relations/entity/Post.ts b/test/integration/examples/sample18-lazy-relations/entity/Post.ts index 6d74a76..2283783 100644 --- a/test/integration/examples/sample18-lazy-relations/entity/Post.ts +++ b/test/integration/examples/sample18-lazy-relations/entity/Post.ts @@ -1,10 +1,17 @@ -import { Column, Entity, Index, PrimaryGeneratedColumn, ManyToOne, ManyToMany, JoinTable } from "typeorm" +import { + Column, + Entity, + Index, + PrimaryGeneratedColumn, + ManyToOne, + ManyToMany, + JoinTable +} from "typeorm"; import { Author } from "./Author"; import { Category } from "./Category"; @Entity("Post") export class Post { - @PrimaryGeneratedColumn() id: number; @@ -15,6 +22,7 @@ export class Post { text: string; @ManyToOne(type => Author, author => author.posts, { + lazy: true, // cascade: ["insert"], onDelete: "SET NULL", onUpdate: "CASCADE" @@ -22,9 +30,9 @@ export class Post { author: Promise; @ManyToMany(type => Category, category => category.posts, { + lazy: true // cascade: true }) @JoinTable() categorys: Promise; - } diff --git a/test/integration/examples/sample3-many-to-one/entity/Post.ts b/test/integration/examples/sample3-many-to-one/entity/Post.ts index 4505194..01a5140 100644 --- a/test/integration/examples/sample3-many-to-one/entity/Post.ts +++ b/test/integration/examples/sample3-many-to-one/entity/Post.ts @@ -1,4 +1,12 @@ -import { PrimaryGeneratedColumn, Column, Entity, OneToOne, OneToMany, ManyToOne, JoinColumn } from "typeorm"; +import { + PrimaryGeneratedColumn, + Column, + Entity, + OneToOne, + OneToMany, + ManyToOne, + JoinColumn +} from "typeorm"; import { PostDetails } from "./PostDetails"; import { PostCategory } from "./PostCategory"; import { PostAuthor } from "./PostAuthor"; @@ -8,7 +16,6 @@ import { PostMetadata } from "./PostMetadata"; @Entity("Post") export class Post { - @PrimaryGeneratedColumn() id: number; @@ -20,41 +27,40 @@ export class Post { // post has relation with category, however inverse relation is not set (category does not have relation with post set) @ManyToOne(type => PostCategory, { - cascade: true, - onDelete: 'CASCADE' + // cascade: true, + onDelete: "CASCADE" }) category: PostCategory; // post has relation with details. cascade inserts here means if new PostDetails instance will be set to this // relation it will be inserted automatically to the db when you save this Post entity @ManyToOne(type => PostDetails, details => details.posts, { - cascade: true, + // cascade: true, }) details: PostDetails; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity @ManyToOne(type => PostImage, image => image.posts, { - cascade: true, + // cascade: true, }) image: PostImage; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity @ManyToOne(type => PostMetadata, metadata => metadata.posts, { - cascade: true, + // cascade: true, }) metadata: PostMetadata | null; // post has relation with details. full cascades here @ManyToOne(type => PostInformation, information => information.posts, { - cascade: true, - onDelete: 'CASCADE' + // cascade: true, + onDelete: "CASCADE" }) information: PostInformation; // post has relation with details. not cascades here. means cannot be persisted, updated or removed @ManyToOne(type => PostAuthor, author => author.posts) author: PostAuthor; - } diff --git a/test/integration/examples/sample4-many-to-many/entity/Post.ts b/test/integration/examples/sample4-many-to-many/entity/Post.ts index 83b80c0..5c2a680 100644 --- a/test/integration/examples/sample4-many-to-many/entity/Post.ts +++ b/test/integration/examples/sample4-many-to-many/entity/Post.ts @@ -1,14 +1,23 @@ -import { PrimaryGeneratedColumn, Column, Entity, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable } from "typeorm"; -import {PostDetail} from "./PostDetail"; -import {PostCategory} from "./PostCategory"; -import {PostAuthor} from "./PostAuthor"; -import {PostInformation} from "./PostInformation"; -import {PostImage} from "./PostImage"; -import {PostMetadata} from "./PostMetadata"; +import { + PrimaryGeneratedColumn, + Column, + Entity, + OneToOne, + OneToMany, + ManyToOne, + ManyToMany, + JoinColumn, + JoinTable +} from "typeorm"; +import { PostDetail } from "./PostDetail"; +import { PostCategory } from "./PostCategory"; +import { PostAuthor } from "./PostAuthor"; +import { PostInformation } from "./PostInformation"; +import { PostImage } from "./PostImage"; +import { PostMetadata } from "./PostMetadata"; @Entity("Post") export class Post { - @PrimaryGeneratedColumn() id: number; @@ -20,7 +29,7 @@ export class Post { // post has relation with category, however inverse relation is not set (category does not have relation with post set) @ManyToMany(type => PostCategory, { - cascade: true + // cascade: true }) @JoinTable() postCategorys: PostCategory[]; @@ -28,7 +37,7 @@ export class Post { // post has relation with details. cascade inserts here means if new PostDetails instance will be set to this // relation it will be inserted automatically to the db when you save this Post entity @ManyToMany(type => PostDetail, details => details.posts, { - cascade: true + // cascade: true }) @JoinTable() postDetails: PostDetail[]; @@ -36,7 +45,7 @@ export class Post { // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity @ManyToMany(type => PostImage, image => image.posts, { - cascade: true + // cascade: true }) @JoinTable() postImages: PostImage[]; @@ -49,7 +58,7 @@ export class Post { // post has relation with details. full cascades here @ManyToMany(type => PostInformation, information => information.posts, { - cascade: true + // cascade: true }) @JoinTable() postInformations: PostInformation[]; @@ -58,5 +67,4 @@ export class Post { @ManyToMany(type => PostAuthor, author => author.posts) @JoinTable() postAuthors: PostAuthor[]; - } diff --git a/test/integration/github-issues/71/entity/Post.ts b/test/integration/github-issues/71/entity/Post.ts index c9b85b6..92b7eba 100644 --- a/test/integration/github-issues/71/entity/Post.ts +++ b/test/integration/github-issues/71/entity/Post.ts @@ -1,41 +1,47 @@ -import { Index, Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable } from "typeorm"; +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, + // nullable: false, primary: true, name: "Id" }) Id: number; - @OneToOne(type => PostAuthor, PostAuthor => PostAuthor.Id, - { - // onDelete: "CASCADE", - // onUpdate: "CASCADE" - }) + @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" - }) + @OneToOne(type => PostCategory, PostCategory => PostCategory.Id, { + // onDelete: "RESTRICT", + // onUpdate: "RESTRICT" + }) postCategory: PostCategory; - @OneToOne(type => PostDetails, PostDetails => PostDetails.Id, - { - // onDelete: "SET NULL", - // onUpdate: "SET NULL" - }) + @OneToOne(type => PostDetails, PostDetails => PostDetails.Id, { + // onDelete: "SET NULL", + // onUpdate: "SET NULL" + }) postDetails: PostDetails; } diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts index 80b1e38..a6f9229 100644 --- a/test/utils/EntityFileToJson.ts +++ b/test/utils/EntityFileToJson.ts @@ -356,6 +356,7 @@ export default class EntityFileToJson { retVal.columns.push(column); column.relationType = "ManyToOne"; column.isOwnerOfRelation = true; + EntityFileToJson.getRelationOptions(trimmedLine, column); } return; } @@ -368,6 +369,7 @@ export default class EntityFileToJson { const column = new EntityColumn(); retVal.columns.push(column); column.relationType = "OneToMany"; + EntityFileToJson.getRelationOptions(trimmedLine, column); } return; } @@ -380,6 +382,7 @@ export default class EntityFileToJson { const column = new EntityColumn(); retVal.columns.push(column); column.relationType = "ManyToMany"; + EntityFileToJson.getRelationOptions(trimmedLine, column); } return; } @@ -452,7 +455,7 @@ export default class EntityFileToJson { colTypes = colTypes.substring(8, colTypes.length - 1); retVal.columns[ retVal.columns.length - 1 - ].columnOptions.isLazy = true; + ].columnOptions.isTypeLazy = true; } retVal.columns[ retVal.columns.length - 1