This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 282
/
NamingStrategy.ts
79 lines (67 loc) · 2.48 KB
/
NamingStrategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable @typescript-eslint/no-unused-vars */
import { plural } from "pluralize";
import * as changeCase from "change-case";
import { Relation } from "./models/Relation";
import { RelationId } from "./models/RelationId";
import { Entity } from "./models/Entity";
import { Column } from "./models/Column";
let pluralize: boolean;
export function enablePluralization(value: boolean) {
pluralize = value;
}
export function relationIdName(
relationId: RelationId,
relation: Relation,
owner?: Entity
): string {
const columnOldName = relationId.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let newColumnName = columnOldName.replace(/[0-9]$/, "");
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (isRelationToMany && pluralize) {
newColumnName = plural(newColumnName);
}
return newColumnName;
}
export function relationName(relation: Relation, owner?: Entity): string {
const columnOldName = relation.fieldName;
const isRelationToMany =
relation.relationType === "OneToMany" ||
relation.relationType === "ManyToMany";
let newColumnName = columnOldName.replace(/[0-9]$/, "");
if (
newColumnName.toLowerCase().endsWith("id") &&
!newColumnName.toLowerCase().endsWith("guid")
) {
newColumnName = newColumnName.substring(
0,
newColumnName.toLowerCase().lastIndexOf("id")
);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
}
if (isRelationToMany && pluralize) {
newColumnName = plural(newColumnName);
}
return newColumnName;
}
export function entityName(oldEntityName: string, entity?: Entity): string {
return oldEntityName;
}
export function columnName(oldColumnName: string, column?: Column): string {
return oldColumnName;
}
export function fileName(oldFileName: string): string {
return oldFileName;
}