在 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) 中
我也有同样的问题。要解决此问题,您只需从 Datasource 选项中删除 cli 键并在创建新迁移时指定路径
typeorm migration:create -n UrlMigration -d src/migrations
-d 选项用于指定将创建迁移的目录
"scripts": {
...
"typeorm": "typeorm-ts-node-commonjs -d ormconfig.ts"
}
注意: 将 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: [],
})
TypeORM 和 TypeORM CLI 在 0.3.0 之后无法完美运行。我也遇到了同样的问题,所以我建议你降级到版本0.2
我也有类似的问题 打字版本: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/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 {}
"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
谁解决了这个问题,如果你解决了这个问题,请告诉我
我希望使用 npm 其他人不会遇到问题 我希望它对某人有帮助
(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
npm 运行 typeorm_src 迁移:运行
它将在您先前在 {timestamp}-nameOfMyMigration.ts 中生成的数据库中进行迁移。
确保您的数据源选项中有
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: [],
});
yarn run typeorm migration:generate -n
的问题我通过添加带有名称的迁移路径来解决,如下所示:
yarn run typeorm migration:generate ./src/db/migrations/NameMyMigration