diff --git a/package-lock.json b/package-lock.json index 8afdab2..1c9d8b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 40e1ba1..718351f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/NamingStrategy.ts b/src/NamingStrategy.ts index 42bd127..38423a7 100644 --- a/src/NamingStrategy.ts +++ b/src/NamingStrategy.ts @@ -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; } diff --git a/test/integration/examples/sample18-lazy-relations/entity/Category.ts b/test/integration/examples/sample18-lazy-relations/entity/Category.ts index 1f90c4f..4b7632f 100644 --- a/test/integration/examples/sample18-lazy-relations/entity/Category.ts +++ b/test/integration/examples/sample18-lazy-relations/entity/Category.ts @@ -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; diff --git a/test/integration/examples/sample18-lazy-relations/entity/Post.ts b/test/integration/examples/sample18-lazy-relations/entity/Post.ts index e5418d6..0209d7e 100644 --- a/test/integration/examples/sample18-lazy-relations/entity/Post.ts +++ b/test/integration/examples/sample18-lazy-relations/entity/Post.ts @@ -34,5 +34,5 @@ export class Post { // cascade: true }) @JoinTable() - categorys: Promise; + categories: Promise; } 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 5c2a680..a3e219c 100644 --- a/test/integration/examples/sample4-many-to-many/entity/Post.ts +++ b/test/integration/examples/sample4-many-to-many/entity/Post.ts @@ -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, { diff --git a/test/integration/examples/sample4-many-to-many/entity/PostMetadata.ts b/test/integration/examples/sample4-many-to-many/entity/PostMetadata.ts index 9bb0f38..280ff53 100644 --- a/test/integration/examples/sample4-many-to-many/entity/PostMetadata.ts +++ b/test/integration/examples/sample4-many-to-many/entity/PostMetadata.ts @@ -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[]; - }