TypeOrm 已删除ormconfig.json
版本支持0.3.0 https://typeorm.io/changelog#030httpsgithubcomtypeormtypeormpull8616-2022-03-17。您应该使用新语法 - 创建ormconfig.ts
并为您的数据库指定选项,例如:
export const connectionSource = new DataSource({
migrationsTableName: 'migrations',
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'user',
password: 'pass',
database: 'somehealthchecker',
logging: false,
synchronize: false,
name: 'default',
entities: ['src/**/**.entity{.ts,.js}'],
migrations: ['src/migrations/**/*{.ts,.js}'],
subscribers: ['src/subscriber/**/*{.ts,.js}'],
});
然后,运行连接后:
await connectionSource.initialize();
您可以通过以下方式获取实体:
const myRepo = connectionSource.getRepository(SomeEntity)
还有你的脚本package.json
应该看起来像这样:
"migration:generate": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:generate -d src/modules/config/ormconfig.ts",
"migration:up": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:run -d src/modules/config/ormconfig.ts",
"migration:down": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm migration:revert -d src/modules/config/ormconfig.ts",
命令后,只需在控制台中给出迁移名称,无需 -n 选项