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

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

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

51.28
/src/naming-strategy/DefaultNamingStrategy.ts
1
import { NamingStrategyInterface } from "./NamingStrategyInterface"
2
import { RandomGenerator } from "../util/RandomGenerator"
1✔
3
import { camelCase, snakeCase, titleCase } from "../util/StringUtils"
1✔
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 {
1✔
10
    protected getTableName(tableOrName: Table | string): string {
11
        if (typeof tableOrName !== "string") {
47!
UNCOV
12
            tableOrName = tableOrName.name
×
13
        }
14

15
        return tableOrName.split(".").pop()!
47✔
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)
51✔
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 {
UNCOV
36
        return originalClosureTableName + "_closure"
×
37
    }
38

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

46
        if (embeddedPrefixes.length)
540!
UNCOV
47
            return camelCase(embeddedPrefixes.join("_")) + titleCase(name)
×
48

49
        return name
540✔
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
UNCOV
58
        const clonedColumnNames = [...columnNames]
×
UNCOV
59
        clonedColumnNames.sort()
×
UNCOV
60
        const tableName = this.getTableName(tableOrName)
×
UNCOV
61
        const replacedTableName = tableName.replace(".", "_")
×
UNCOV
62
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
×
UNCOV
63
        return "PK_" + RandomGenerator.sha1(key).substr(0, 27)
×
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
UNCOV
71
        const clonedColumnNames = [...columnNames]
×
UNCOV
72
        clonedColumnNames.sort()
×
UNCOV
73
        const tableName = this.getTableName(tableOrName)
×
UNCOV
74
        const replacedTableName = tableName.replace(".", "_")
×
UNCOV
75
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
×
UNCOV
76
        return "UQ_" + RandomGenerator.sha1(key).substr(0, 27)
×
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]
7✔
86
        clonedColumnNames.sort()
7✔
87
        const tableName = this.getTableName(tableOrName)
7✔
88
        const replacedTableName = tableName.replace(".", "_")
7✔
89
        let key = `${replacedTableName}_${clonedColumnNames.join("_")}`
7✔
90
        if (where) key += `_${where}`
7!
91

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

95
    defaultConstraintName(
96
        tableOrName: Table | string,
97
        columnName: string,
98
    ): string {
UNCOV
99
        const tableName = this.getTableName(tableOrName)
×
UNCOV
100
        const replacedTableName = tableName.replace(".", "_")
×
UNCOV
101
        const key = `${replacedTableName}_${columnName}`
×
UNCOV
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]
31✔
113
        clonedColumnNames.sort()
31✔
114
        const tableName = this.getTableName(tableOrName)
31✔
115
        const replacedTableName = tableName.replace(".", "_")
31✔
116
        const key = `${replacedTableName}_${clonedColumnNames.join("_")}`
31✔
117
        return "FK_" + RandomGenerator.sha1(key).substr(0, 27)
31✔
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]
9✔
127
        clonedColumnNames.sort()
9✔
128
        const tableName = this.getTableName(tableOrName)
9✔
129
        const replacedTableName = tableName.replace(".", "_")
9✔
130
        let key = `${replacedTableName}_${clonedColumnNames.join("_")}`
9✔
131
        if (where) key += `_${where}`
9!
132

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

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

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

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

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

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

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

194
    joinTableInverseColumnName(
195
        tableName: string,
196
        propertyName: string,
197
        columnName?: string,
198
    ): string {
199
        return this.joinTableColumnName(tableName, propertyName, columnName)
2✔
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" }
48✔
213
    materializedPathColumnName = "mpath"
48✔
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