more tests passing
This commit is contained in:
		
							parent
							
								
									4f45dc9961
								
							
						
					
					
						commit
						681ef1a363
					
				@ -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)
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
@ -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"}}
 | 
			
		||||
 | 
			
		||||
@ -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<Post[]>;
 | 
			
		||||
 | 
			
		||||
@ -21,5 +27,4 @@ export class Author {
 | 
			
		||||
    // asPromise() {
 | 
			
		||||
    //     return Promise.resolve(this);
 | 
			
		||||
    // }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<Post[]>;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<Author | null>;
 | 
			
		||||
 | 
			
		||||
    @ManyToMany(type => Category, category => category.posts, {
 | 
			
		||||
        lazy: true
 | 
			
		||||
        // cascade: true
 | 
			
		||||
    })
 | 
			
		||||
    @JoinTable()
 | 
			
		||||
    categorys: Promise<Category[]>;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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[];
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user