代码之家  ›  专栏  ›  技术社区  ›  Hussain Ali Akbar

如何指定ormconfig.ts公司为了打字?

  •  0
  • Hussain Ali Akbar  · 技术社区  · 6 年前

    我使用typeormcli创建了一个示例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
    

    这将在database/migrations文件夹中正确地创建迁移,并从中执行迁移。

    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公司在目标目录中生成迁移?

    谢谢!

    2 回复  |  直到 6 年前
        1
  •  10
  •   David Dehghan    5 年前

    ormconfig.json 无视 ormconfig.ts work in progress 尽管如此。

    "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"
    
        2
  •  15
  •   Adrien De Peretti    5 年前

    嘿,既然我能给你提个解决方案,我就把这段对话结束了。

    package.json 文件:

    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/database.ts",
    

    您的ts配置必须通过以下操作直接导出配置:

    export = { /* your config */ };
    

    如您所见,您还可以指定配置的路径。您的配置不需要位于项目的根级别。

    希望对你有帮助

        3
  •  14
  •   Siipe    5 年前

    只需移除 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 文件,因为数据库连接需要根据环境的不同而不同。 另外,不要忘记使用 ts-node 为了应付 typeorm cli 在里面 package.json :

    ...
    "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:down
    
        4
  •  0
  •   Jayant Malik    4 年前

    .
    ├── dist // Output Directory with javascript files
    │   ├── entities
    │   │   ├── User.js
    │   ├── index.js
    │   ├── ormconfig.js
    ├── src // Typescript files
    │   ├── entities
    │   │   └── User.ts
    │   └── ormconfig.ts
    ├── tsconfig.json
    ├── package.json
    
    

    问题

    ormconfig.ts 文件内部 project_dir/src/ormconfig.ts

    entities 文件中的目录路径

    import { ConnectionOptions } from "typeorm";
    
    export default {
      type: "mysql",
      database: "dbname",
      username: "test",
      password: "password",
      synchronize: true,
      logging: true,
      entities: ["entities/*.js"],
      migrations: ["migrations/*.js"],
    } as ConnectionOptions;
    

    我尝试将实体更改为:

    • ./entities/*.js
    • __dirname + ./entities/*.js
    • src/entities/*.js
    • __dirname + src/entities/*.js

    src 目录 dist

    解决方案

    ormconfig.ts公司 为我工作的文件:

    import { ConnectionOptions } from "typeorm";
    
    export default {
      type: "mysql",
      database: "dbname",
      username: "test",
      password: "password",
      synchronize: true,
      logging: true,
      entities: ["dist/entities/*.js"],
      migrations: ["dist/migrations/*.js"],
    } as ConnectionOptions;
    

    从解决方案来看,似乎 typeorm

        5
  •  0
  •   RJ grover    3 年前

    解决方案

    指定ormconfig.ts公司

    import { ConnectionOptions } from 'typeorm';
    
    // Check typeORM documentation for more information.
    const config: ConnectionOptions = {
        type: 'postgres',
        host: process.env.SQL_IP, // localhost
        port: process.env.SQL_PORT,// 5432
        username: process.env.SQL_USER, // databse login role username
        password: process.env.SQL_PASSWORD, // database login role password
        database: process.env.SQL_DATABASE, // db name
    
        // entities name should be **.entity.ts
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
    
        // We are using migrations, synchronize should be set to false.
        // synchronize: process.env.TYPEORM_SYNCHRONIZE
        //  ? process.env.TYPEORM_SYNCHRONIZE.toLowerCase() === 'true'
        //  : false,
        synchronize: false,
    
        // Run migrations automatically,
        // you can disable this if you prefer running migration manually.
        migrationsRun: false,
    
        logging: false,
        // logger: 'advanced-console',
    
        // Allow both start:prod and start:dev to use migrations
        // __dirname is either dist or src folder, meaning either
        // the compiled js in prod or the ts in dev.
        migrations: [__dirname + '/migrations/*{.ts,.js}'],
        cli: {
            // Location of migration should be inside src folder
            // to be compiled into dist/ folder.
            migrationsDir: 'src/database/migrations'
        }
    };
    
    export = config;
    
    
    

    "typeorm": "ts-node --files -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/database/config.ts"
    "db:migrate": "npm run typeorm migration:run",
    "db:create-migration": "npm run typeorm migration:create -- -n",