proper column names pluralization

This commit is contained in:
Kononnable 2019-10-12 17:45:18 +02:00
parent 12c9fbae9b
commit 8f719f67bb
7 changed files with 37 additions and 12 deletions

11
package-lock.json generated
View File

@ -352,6 +352,12 @@
"moment": ">=2.14.0"
}
},
"@types/pluralize": {
"version": "0.0.29",
"resolved": "https://registry.npmjs.org/@types/pluralize/-/pluralize-0.0.29.tgz",
"integrity": "sha512-BYOID+l2Aco2nBik+iYS4SZX0Lf20KPILP5RGmM1IgzdwNdTs0eebiFriOPcej1sX9mLnSoiNte5zcFxssgpGA==",
"dev": true
},
"@types/prettier": {
"version": "1.18.3",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-1.18.3.tgz",
@ -4217,6 +4223,11 @@
"semver-compare": "^1.0.0"
}
},
"pluralize": {
"version": "8.0.0",
"resolved": "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz",
"integrity": "sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA=="
},
"postgres-array": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",

View File

@ -31,6 +31,7 @@
"mssql": "^5.1.0",
"mysql": "^2.17.1",
"pg": "^7.12.0",
"pluralize": "^8.0.0",
"prettier": "^1.18.2",
"reflect-metadata": "^0.1.13",
"sqlite3": "^4.0.9",
@ -52,6 +53,7 @@
"@types/node": "^12.6.9",
"@types/oracledb": "^3.1.3",
"@types/pg": "^7.4.14",
"@types/pluralize": "0.0.29",
"@types/prettier": "^1.18.3",
"@types/sinon": "^7.0.13",
"@types/sqlite3": "^3.1.5",

View File

@ -1,3 +1,4 @@
import { plural } from "pluralize";
import AbstractNamingStrategy from "./AbstractNamingStrategy";
import { Relation } from "./models/Relation";
import { RelationId } from "./models/RelationId";
@ -22,7 +23,9 @@ export default class NamingStrategy extends AbstractNamingStrategy {
if (!Number.isNaN(parseInt(columnName[columnName.length - 1], 10))) {
columnName = columnName.substring(0, columnName.length - 1);
}
columnName += isRelationToMany ? "s" : "";
if (isRelationToMany) {
columnName = plural(columnName);
}
return columnName;
}
@ -52,8 +55,9 @@ export default class NamingStrategy extends AbstractNamingStrategy {
if (!Number.isNaN(parseInt(columnName[columnName.length - 1], 10))) {
columnName = columnName.substring(0, columnName.length - 1);
}
columnName += isRelationToMany ? "s" : "";
if (isRelationToMany) {
columnName = plural(columnName);
}
return columnName;
}

View File

@ -16,7 +16,7 @@ export class Category {
@Column()
name: string;
@ManyToMany(type => Post, post => post.categorys, {
@ManyToMany(type => Post, post => post.categories, {
lazy: true
})
posts: Promise<Post[]>;

View File

@ -34,5 +34,5 @@ export class Post {
// cascade: true
})
@JoinTable()
categorys: Promise<Category[]>;
categories: Promise<Category[]>;
}

View File

@ -32,7 +32,7 @@ export class Post {
// cascade: true
})
@JoinTable()
postCategorys: PostCategory[];
postCategories: 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
@ -54,7 +54,7 @@ export class Post {
// it will be inserted automatically to the db when you save this Post entity
@ManyToMany(type => PostMetadata, metadata => metadata.posts)
@JoinTable()
postMetadatas: PostMetadata[];
postMetadata: PostMetadata[];
// post has relation with details. full cascades here
@ManyToMany(type => PostInformation, information => information.posts, {

View File

@ -1,16 +1,24 @@
import { PrimaryGeneratedColumn, Column, Entity, OneToOne, OneToMany, ManyToOne, ManyToMany, JoinColumn, JoinTable } from "typeorm";
import {Post} from "./Post";
import {
PrimaryGeneratedColumn,
Column,
Entity,
OneToOne,
OneToMany,
ManyToOne,
ManyToMany,
JoinColumn,
JoinTable
} from "typeorm";
import { Post } from "./Post";
@Entity("PostMetadata")
export class PostMetadata {
@PrimaryGeneratedColumn()
id: number;
@Column()
description: string;
@ManyToMany(type => Post, post => post.postMetadatas)
@ManyToMany(type => Post, post => post.postMetadata)
posts: Post[];
}