参考官网文档:命名策略
自定义命名策略:MyNamingStrategy.ts,如果想换成js格式的,请修改语法即可
import { DefaultNamingStrategy, NamingStrategyInterface, Table } from "typeorm"; export class MyNamingStrategy extends DefaultNamingStrategy { tableName(targetName: string, userSpecifiedName: string | undefined): string { if (userSpecifiedName) return userSpecifiedName; return parseName(targetName); } columnName(propertyName: string, customName: string, embeddedPrefixes: string[]): string { if (customName) return customName; return parseName(propertyName); } } function parseName(targetName: string): string { if(!targetName) return ""; let str : string = ""; for(let i = 0; i < targetName.length; ++i) { let code = targetName[i]; if (code >= "A" && code <= "Z") { if(i != 0 ) { str = str.concat("_"); } str = str.concat(code.toLocaleLowerCase()); }else { str = str.concat(code); } } return str; }
然后全局设置命名策略
import { MyNamingStrategy } from "./conf/MyNamingStrategy"; getConnectionOptions().then((connectionOptions) => { return createConnection(Object.assign(connectionOptions, { namingStrategy: new MyNamingStrategy() })) }).catch(err => console.error(err));
typeorm自定义表名,列名,大写别下划线,不用手动指定名称
原文:https://www.cnblogs.com/zhouquan-1992-04-06/p/14693153.html