【问题标题】:Cannot add cli parameters to DataSourceOptions in TypeORM无法将 cli 参数添加到 TypeORM 中的 DataSourceOptions
【发布时间】:2022-07-11 07:12:18
【问题描述】:

在 typeORM 文档中,可以根据 https://github.com/typeorm/typeorm/blob/master/docs/data-source-options.md 将 cli 参数添加到 DataSourceOptions。我在https://typeorm.io/using-cli 上看到的例子是

{
    cli: {
        entitiesDir: "src/entity",
        subscribersDir: "src/subscriber",
        migrationsDir: "src/migration"
    }
}

我在我的代码中尝试如下:

let dataSource = new DataSource(
  {
        type: 'postgres',
        host: 'localhost',
        port: 5432,
        database: 'website',
        username: 'test',
        password: 'test',
        logging: true,
        synchronize: false,
        entities: [User, Posts],
        cli: {
            entitiesDir: "src/entity",
            subscribersDir: "src/subscriber",
            migrationsDir: "src/migration"
        }
  })

但是我收到以下错误: '{ type: "postgres"; host: string; port: number; database: string; username: string; password: string; logging: true; synchronize: false; entities: (typeof User | typeof Wallet)[]; cli: { entitiesDir: string; subscribersDir: string; migrationsDir: string; }; }' 类型的参数不能分配给 'DataSourceOptions' 类型的参数。 对象字面量只能指定已知属性,'cli' 类型中不存在'PostgresConnectionOptions'.ts(2345)

【问题讨论】:

    标签: javascript typescript typeorm


    【解决方案1】:

    我遇到了同样的问题。要解决此问题,您只需从 Datasource 选项中删除 cli 键并在创建新迁移时指定路径

    typeorm migration:create -n UrlMigration -d src/migrations 
    

    -d 选项用于指定创建迁移的目录

    "scripts": {
      ...
      "typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
    }
    

    注意:将 ormconfig.ts 替换为您的数据源文件名

    参考TypeORM CLI help

    【讨论】:

    • 谢谢!这方面的文档至少可以说是不透明的。
    【解决方案2】:

    TypeORMTypeORM CLI0.3.0 之后无法完美运行。我有同样的问题,所以我建议你降级到版本 0.2

    【讨论】:

    • 同样的问题。切换到 0.2 版,一切正常
    【解决方案3】:

    我也有类似的问题 typeorm 版本:0.3.7

    我做了什么:

    1. 我在 src/config/ormconfig-migrations.ts 中创建
    import { DataSource } from 'typeorm';
    
    const configMigration = new DataSource({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'mediumclone',
      password: 'qwerty',
      database: 'mediumclone',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: false,
      migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
    });
    
    export default configMigration;
    
    

    在 src/comfig/ormconfig.ts 中

    import { DataSourceOptions } from 'typeorm';
    
    const config: DataSourceOptions = {
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'mediumclone',
      password: 'qwerty',
      database: 'mediumclone',
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: false,
      migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
    };
    
    export default config;
    

    我已经分开了,因为我使用 ormconfig 在我的应用程序上连接数据库,一个 ormconfig-migrations.ts - l 用于迁移

    import { Module } from '@nestjs/common';
    import { AppController } from '@app/app.controller';
    import { AppService } from '@app/app.service';
    import { TagModule } from '@app/tag/tag.module';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import ormconfig from '@app/config/ormconfig';
    
    @Module({
      imports: [TypeOrmModule.forRoot(ormconfig), TagModule],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
    
    1. 在 package.json 我添加了:
    "scripts": {
         "typeorm": "typeorm-ts-node-commonjs -d src/config/ormconfig-migrations.ts",
         "db:drop": "yarn run typeorm schema:drop"
    }
    
    1. 在终端中:
    yarn run db:drop
    

    我希望使用 npm 其他人不会有问题 我希望它对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 2017-11-19
      • 2022-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      相关资源
      最近更新 更多