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

问题描述 投票:0回答:6

在 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
6个回答
3
投票

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

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

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

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

注意: 将 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: [],
})


1
投票

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


1
投票

我也有类似的问题 打字版本: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 - 我用于迁移

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

但是我有一个问题:

yarn run typeorm migration:generate -n

谁解决了这个问题,如果你解决了这个问题,请告诉我

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


0
投票

(15/08/2022)嗨!我将 typeorm 更新到 0.3.7,并且 typeorm 正在使用 DataSource(在 typeorm 网页教程中称为 data-source.ts)而不是 ormconfig.ts...我意识到

cli:{ 实体目录:“src/实体”, 订阅者目录:“src/订阅者”, 迁移目录:“src/迁移” }

不起作用。但遵循以下步骤可能会对您有所帮助:

1-- 在 packege.json 的脚本中添加这些行

“脚本”:{ ... “typeorm”:“typeorm-ts-node-commonjs”, "typeorm_src": "typeorm-ts-node-commonjs -d src/data-source.ts" }

“src/data-source.ts”是您拥有数据库信息的地方...

  • (如果生成迁移) 在位于根目录的终端中,您必须编写此行

npm run typeorm_src 迁移:生成 src/migrations/nameOfMyMigration

  • (如果仅创建空文件的迁移) 在位于根目录的终端中,您必须编写此行

npm 运行 typeorm 迁移:创建 src/migrations/nameOfMyMigration

  • 在这两种情况下,这些行都会生成一个文件(在 src/migrations/nameOfMyMigration 中),其中包含要执行的迁移,名称为 {timestamp}-nameOfMyMigration.ts,创建该文件后,执行此行

npm 运行 typeorm_src 迁移:运行

它将在您先前在 {timestamp}-nameOfMyMigration.ts 中生成的数据库中进行迁移。

来源:https://typeorm.io/


0
投票

确保您的数据源选项中有

type
例如

 import { DataSource } from "typeorm"

const AppDataSource = new DataSource({

    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "",
    database: "test",
    logging: true,
    synchronize: false,
    entities: [],
    subscribers: [],
});

0
投票

yarn run typeorm migration:generate -n
的问题我通过添加带有名称的迁移路径来解决,如下所示:

yarn run typeorm migration:generate ./src/db/migrations/NameMyMigration
© www.soinside.com 2019 - 2024. All rights reserved.