Skip to content

Instantly share code, notes, and snippets.

@biern
Forked from recurrence/snake_naming.ts
Last active November 22, 2019 12:33
Show Gist options
  • Save biern/18e1042f45b68856d8b3c466148f43f3 to your computer and use it in GitHub Desktop.
Save biern/18e1042f45b68856d8b3c466148f43f3 to your computer and use it in GitHub Desktop.
TypeORM Snake Case Naming Strategy
import { NamingStrategyInterface, DefaultNamingStrategy } 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 {
const prefix = embeddedPrefixes.length
? embeddedPrefixes.join('_') + '_'
: '';
return (
snakeCase(prefix) + (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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment