在typeORM文档中,可以根据DataSourceOptions
将cli参数添加到https://github.com/typeorm/typeorm/blob/master/docs/data-source-options.md中。我在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)类型中。
发布于 2022-04-26 00:41:10
我也有同样的问题。要解决此问题,只需从Datasource选项中移除cli键,并在创建新迁移时指定路径。
typeorm migration:create -n UrlMigration -d src/migrations
-d选项用于指定要创建迁移的目录。
"scripts": {
...
"typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
}
NB :将ormconfig.ts替换为数据源文件名
注:实体、迁移和订阅者可以按折叠方式添加到数据源选项中:
// 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: [],
})
发布于 2022-04-09 20:55:42
TypeORM和TypeORM CLI在0.3.0之后不能很好地工作。我也有同样的问题,所以我建议您降级为0.2版
发布于 2022-07-10 23:12:18
另外,我还有一个类似的版本: 0.3.7
我所做的:
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中
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用于迁移。
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 {}
"scripts": {
"typeorm": "typeorm-ts-node-commonjs -d src/config/ormconfig-migrations.ts",
"db:drop": "yarn run typeorm schema:drop"
}
yarn run db:drop
但我有个问题:
yarn run typeorm migration:generate -n
是谁解决的?如果你解决了这个问题,请告诉我
我希望通过国家预防机制,其他人不会有问题,我希望这对某人有帮助。
https://stackoverflow.com/questions/71740574
复制相似问题