-
Notifications
You must be signed in to change notification settings - Fork 28
/
snake-naming.strategy.ts
68 lines (59 loc) · 1.75 KB
/
snake-naming.strategy.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
// Credits to @recurrence
// https://gist.github.com/recurrence/b6a4cb04a8ddf42eda4e4be520921bd2
import { DefaultNamingStrategy, NamingStrategyInterface } from 'typeorm';
import { snakeCase } from 'typeorm/util/StringUtils';
export class SnakeNamingStrategy
extends DefaultNamingStrategy
implements NamingStrategyInterface {
tableName(className: string, customName: string): string {
return customName ? customName : snakeCase(className);
}
columnName(
propertyName: string,
customName: string,
embeddedPrefixes: string[],
): string {
return (
snakeCase(embeddedPrefixes.concat('').join('_')) +
(customName ? customName : snakeCase(propertyName))
);
}
relationName(propertyName: string): string {
return snakeCase(propertyName);
}
joinColumnName(relationName: string, referencedColumnName: string): string {
return snakeCase(relationName + '_' + referencedColumnName);
}
joinTableName(
firstTableName: string,
secondTableName: string,
firstPropertyName: string,
secondPropertyName: string,
): string {
return snakeCase(
firstTableName +
'_' +
firstPropertyName.replace(/\./gi, '_') +
'_' +
secondTableName,
);
}
joinTableColumnName(
tableName: string,
propertyName: string,
columnName?: string,
): string {
return snakeCase(
tableName + '_' + (columnName ? columnName : propertyName),
);
}
classTableInheritanceParentColumnName(
parentTableName: any,
parentTableIdPropertyName: any,
): string {
return snakeCase(parentTableName + '_' + parentTableIdPropertyName);
}
eagerJoinRelationAlias(alias: string, propertyPath: string): string {
return alias + '__' + propertyPath.replace('.', '_');
}
}