我已经使用TypeORM CLI创建了一个示例TypeORM项目,默认情况下,它具有ormconfig.json:
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "test",
"synchronize": false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"database/migrations/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "database/migrations",
"subscribersDir": "src/subscriber"
}
}这是目录结构:
-database
-migrations
-src
-entity
-ormconfig.json这将在数据库/迁移文件夹中正确创建迁移,并从该文件夹执行迁移。
我用下面的ormconfig.ts替换了ormconfig.json:
export default {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'test',
synchronize: false,
"entities": [
"src/entity/**/*.ts"
],
"migrations": [
"database/migrations/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "database/migrations",
"subscribersDir": "src/subscriber"
}
};但是,这会在根目录中创建迁移,而不是在数据库/迁移中。
有没有人可以帮我找出这里缺少什么,以及如何使用ormconfig.ts在目标目录中生成迁移?
发布于 2018-09-10 08:10:32
在撰写本文时,TypeORM只查找ormconfig.json,而忽略ormconfig.ts。不过,还是有work in progress支持它。
除了拥有ormconfig.json之外,你还需要在你的package.json中使用这些命令。
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm",
"migration:generate": "npm run typeorm -- migration:generate --config src/config/ormconfig.json --connection --name ",
"migration:run": "npm run typeorm -- migration:run"发布于 2019-08-18 01:50:15
嘿,既然我可以给你一个解决方案,我就开始这次谈话。
您可以将以下行放入package.json文件中:
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/database.ts",并且您的ts配置必须通过执行以下操作直接导出配置:
export = { /* your config */ };如您所见,您还可以指定配置的路径。您的配置不需要位于项目的根级别。
希望这会对你有所帮助
发布于 2019-07-05 10:44:21
只需在导出时删除default即可。你的ormconfig.ts应该是这样的:
import env from './src/env';
export = {
host: env.DB_CONFIG.host,
type: 'mysql',
port: env.DB_CONFIG.port,
username: env.DB_CONFIG.username,
password: env.DB_CONFIG.password,
database: env.DB_CONFIG.database,
entities: [
'src/**/**.entity{.ts,.js}',
],
migrations: [
'src/database/migrations/*.ts',
],
cli: {
migrationsDir: 'src/database/migrations',
},
synchronize: false,
};在我的例子中,我使用了一个主env.ts文件,因为数据库连接需要根据环境的不同而有所不同。另外,不要忘记在package.json中使用ts-node来处理typeorm cli
...
"scripts": {
...
"migrate:create": "ts-node ./node_modules/typeorm/cli.js migration:create -n",
"migrate:up": "ts-node ./node_modules/typeorm/cli.js migration:run",
"migrate:down": "ts-node ./node_modules/typeorm/cli.js migration:revert"
...
}
...因此,创建、运行或回滚迁移应该如下所示:
npm run migrate:create FileName
npm run migrate:up
npm run migrate:downhttps://stackoverflow.com/questions/52187328
复制相似问题