• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.05
/src/naming-strategy/DefaultNamingStrategy.ts
1
import { NamingStrategyInterface } from "./NamingStrategyInterface"
2
import { RandomGenerator } from "../util/RandomGenerator"
4✔
3
import { camelCase, snakeCase, titleCase } from "../util/StringUtils"
4✔
4
import { Table } from "../schema-builder/table/Table"
5

6
/**
7
 * Naming strategy that is used by default.
8
 */
9
export class DefaultNamingStrategy implements NamingStrategyInterface {
4✔
10
    protected getTableName(tableOrName: Table | string): string {
11
        if (typeof tableOrName !== "string") {
11,674✔
12
            tableOrName = tableOrName.name
5,182✔
13
        }
14

15
        return tableOrName.split(".").pop()!
11,674✔
16
    }
17
    /**
18
     * Normalizes table name.
19
     *
20
     * @param targetName Name of the target entity that can be used to generate a table name.
21
     * @param userSpecifiedName For example if user specified a table name in a decorator, e.g. @Entity("name")
22
     */
23
    tableName(
24
        targetName: string,
25
        userSpecifiedName: string | undefined,
26
    ): string {
27
        return userSpecifiedName ? userSpecifiedName : snakeCase(targetName)
4,709✔
28
    }
29

30
    /**
31
     * Creates a table name for a junction table of a closure table.
32
     *
33
     * @param originalClosureTableName Name of the closure table which owns this junction table.
34
     */
35
    closureJunctionTableName(originalClosureTableName: string): string {
36
        return originalClosureTableName + "_closure"
67✔
37
    }
38

39
    columnName(
40
        propertyName: string,
41
        customName: string,
42
        embeddedPrefixes: string[],
43
    ): string {
44
        const name = customName || propertyName
56,258✔
45

46
        if (embeddedPrefixes.length)
56,258✔
47
            return camelCase(embeddedPrefixes.join("_")) + titleCase(name)
1,824✔
48

49
        return name
54,434✔
50
    }
51

52
    relationName(propertyName: string): string {
53
        return propertyName
×
54
    }
55

56
    primaryKeyName(tableOrName: Table | string, columnNames: string[]): string {
57
        // sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
58
        const clonedColumnNames = [...columnNames]
4,803✔
59
        clonedColumnNames.sort()
4,803✔
60
        const tableName = this.getTableName(tableOrName)
4,803✔
61
        const replacedTableName = tableName.replace(".", "_")
4,803✔
62
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
4,803✔
63
        return "PK_" + RandomGenerator.sha1(key).substr(0, 27)
4,803✔
64
    }
65

66
    uniqueConstraintName(
67
        tableOrName: Table | string,
68
        columnNames: string[],
69
    ): string {
70
        // sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
71
        const clonedColumnNames = [...columnNames]
643✔
72
        clonedColumnNames.sort()
643✔
73
        const tableName = this.getTableName(tableOrName)
643✔
74
        const replacedTableName = tableName.replace(".", "_")
643✔
75
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
643✔
76
        return "UQ_" + RandomGenerator.sha1(key).substr(0, 27)
643✔
77
    }
78

79
    relationConstraintName(
80
        tableOrName: Table | string,
81
        columnNames: string[],
82
        where?: string,
83
    ): string {
84
        // sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
85
        const clonedColumnNames = [...columnNames]
317✔
86
        clonedColumnNames.sort()
317✔
87
        const tableName = this.getTableName(tableOrName)
317✔
88
        const replacedTableName = tableName.replace(".", "_")
317✔
89
        let key = `${replacedTableName}_${clonedColumnNames.join("_")}`
317✔
90
        if (where) key += `_${where}`
317!
91

92
        return "REL_" + RandomGenerator.sha1(key).substr(0, 26)
317✔
93
    }
94

95
    defaultConstraintName(
96
        tableOrName: Table | string,
97
        columnName: string,
98
    ): string {
99
        const tableName = this.getTableName(tableOrName)
×
100
        const replacedTableName = tableName.replace(".", "_")
×
101
        const key = `${replacedTableName}_${columnName}`
×
102
        return "DF_" + RandomGenerator.sha1(key).substr(0, 27)
×
103
    }
104

105
    foreignKeyName(
106
        tableOrName: Table | string,
107
        columnNames: string[],
108
        _referencedTablePath?: string,
109
        _referencedColumnNames?: string[],
110
    ): string {
111
        // sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
112
        const clonedColumnNames = [...columnNames]
4,123✔
113
        clonedColumnNames.sort()
4,123✔
114
        const tableName = this.getTableName(tableOrName)
4,123✔
115
        const replacedTableName = tableName.replace(".", "_")
4,123✔
116
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
4,123✔
117
        return "FK_" + RandomGenerator.sha1(key).substr(0, 27)
4,123✔
118
    }
119

120
    indexName(
121
        tableOrName: Table | string,
122
        columnNames: string[],
123
        where?: string,
124
    ): string {
125
        // sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
126
        const clonedColumnNames = [...columnNames]
1,636✔
127
        clonedColumnNames.sort()
1,636✔
128
        const tableName = this.getTableName(tableOrName)
1,636✔
129
        const replacedTableName = tableName.replace(".", "_")
1,636✔
130
        let key = `${replacedTableName}_${clonedColumnNames.join("_")}`
1,636✔
131
        if (where) key += `_${where}`
1,636✔
132

133
        return "IDX_" + RandomGenerator.sha1(key).substr(0, 26)
1,636✔
134
    }
135

136
    checkConstraintName(
137
        tableOrName: Table | string,
138
        expression: string,
139
        isEnum?: boolean,
140
    ): string {
141
        const tableName = this.getTableName(tableOrName)
152✔
142
        const replacedTableName = tableName.replace(".", "_")
152✔
143
        const key = `${replacedTableName}_${expression}`
152✔
144
        const name = "CHK_" + RandomGenerator.sha1(key).substr(0, 26)
152✔
145
        return isEnum ? `${name}_ENUM` : name
152!
146
    }
147

148
    exclusionConstraintName(
149
        tableOrName: Table | string,
150
        expression: string,
151
    ): string {
152
        const tableName = this.getTableName(tableOrName)
×
153
        const replacedTableName = tableName.replace(".", "_")
×
154
        const key = `${replacedTableName}_${expression}`
×
155
        return "XCL_" + RandomGenerator.sha1(key).substr(0, 26)
×
156
    }
157

158
    joinColumnName(relationName: string, referencedColumnName: string): string {
159
        return camelCase(relationName + "_" + referencedColumnName)
1,339✔
160
    }
161

162
    joinTableName(
163
        firstTableName: string,
164
        secondTableName: string,
165
        firstPropertyName: string,
166
        secondPropertyName: string,
167
    ): string {
168
        return snakeCase(
375✔
169
            firstTableName +
170
                "_" +
171
                firstPropertyName.replace(/\./gi, "_") +
172
                "_" +
173
                secondTableName,
174
        )
175
    }
176

177
    joinTableColumnDuplicationPrefix(
178
        columnName: string,
179
        index: number,
180
    ): string {
181
        return columnName + "_" + index
×
182
    }
183

184
    joinTableColumnName(
185
        tableName: string,
186
        propertyName: string,
187
        columnName?: string,
188
    ): string {
189
        return camelCase(
898✔
190
            tableName + "_" + (columnName ? columnName : propertyName),
898!
191
        )
192
    }
193

194
    joinTableInverseColumnName(
195
        tableName: string,
196
        propertyName: string,
197
        columnName?: string,
198
    ): string {
199
        return this.joinTableColumnName(tableName, propertyName, columnName)
443✔
200
    }
201

202
    /**
203
     * Adds globally set prefix to the table name.
204
     * This method is executed no matter if prefix was set or not.
205
     * Table name is either user's given table name, either name generated from entity target.
206
     * Note that table name comes here already normalized by #tableName method.
207
     */
208
    prefixTableName(prefix: string, tableName: string): string {
209
        return prefix + tableName
×
210
    }
211

212
    nestedSetColumnNames = { left: "nsleft", right: "nsright" }
1,863✔
213
    materializedPathColumnName = "mpath"
1,863✔
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc