无法将 cli 参数添加到 TypeORM 中的 DataSourceOptions

sev*_*sev 7 javascript typescript typeorm

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

{
    cli: {
        entitiesDir: "src/entity",
        subscribersDir: "src/subscriber",
        migrationsDir: "src/migration"
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的代码中尝试了这一点,如下所示:

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"
        }
  })
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误: 类型的参数'{ 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)

小智 3

我有同样的问题。要解决此问题,您只需从数据源选项中删除cli键并在创建新迁移时指定路径即可

typeorm migration:create -n UrlMigration -d src/migrations 
Run Code Online (Sandbox Code Playgroud)

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

"scripts": {
  ...
  "typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
}
Run Code Online (Sandbox Code Playgroud)

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

请参阅TypeORM CLI 帮助

注意:实体、迁移和订阅者可以通过以下方式添加到数据源选项中:

// ormconfig.ts

export const datasource = new DataSource({
  type: "postgres",
  host: "localhost",
  port: 5432,
  database: "database",
  username: "username",
  password: "password",
  entities: [EntityA, EntityB, EntityC],
  migrations: [__dirname + "/migrations/*{.js,.ts}"],
  subscribers: [],
})

Run Code Online (Sandbox Code Playgroud)