首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在DataSourceOptions中TypeORM中添加cli参数

无法在DataSourceOptions中TypeORM中添加cli参数
EN

Stack Overflow用户
提问于 2022-04-04 16:22:44
回答 4查看 3.4K关注 0票数 4

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

代码语言:javascript
运行
复制
{
    cli: {
        entitiesDir: "src/entity",
        subscribersDir: "src/subscriber",
        migrationsDir: "src/migration"
    }
}

我在代码中这样做,如下所示:

代码语言:javascript
运行
复制
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)类型中。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-04-26 00:41:10

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

代码语言:javascript
运行
复制
typeorm migration:create -n UrlMigration -d src/migrations 

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

代码语言:javascript
运行
复制
"scripts": {
  ...
  "typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
}

NB :将ormconfig.ts替换为数据源文件名

请参阅TypeORM CLI帮助

注:实体、迁移和订阅者可以按折叠方式添加到数据源选项中:

代码语言:javascript
运行
复制
// 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: [],
})
票数 3
EN

Stack Overflow用户

发布于 2022-04-09 20:55:42

TypeORMTypeORM CLI0.3.0之后不能很好地工作。我也有同样的问题,所以我建议您降级为0.2

票数 1
EN

Stack Overflow用户

发布于 2022-07-10 23:12:18

另外,我还有一个类似的版本: 0.3.7

我所做的:

  1. 我在src/config/ormconfig migrations.ts中创建了
代码语言:javascript
运行
复制
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/omconfig.ts中

代码语言:javascript
运行
复制
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;

我已经分离了,因为在我的应用程序中,我使用了用于连接db的ormconfig,一个ormconfig grations.ts-l用于迁移。

代码语言:javascript
运行
复制
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中,我添加了:
代码语言:javascript
运行
复制
"scripts": {
     "typeorm": "typeorm-ts-node-commonjs -d src/config/ormconfig-migrations.ts",
     "db:drop": "yarn run typeorm schema:drop"
}
  1. 在终点站:
代码语言:javascript
运行
复制
yarn run db:drop

但我有个问题:

代码语言:javascript
运行
复制
yarn run typeorm migration:generate -n

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

我希望通过国家预防机制,其他人不会有问题,我希望这对某人有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71740574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档