是否可以像这里一样使用连接类provide
?
import { Connection, createConnection } from 'typeorm';
export const databaseProviders = [{
provide: Connection,
useFactory: async () => await createConnection({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'testo',
entities: [
__dirname + '/../**/*.entity{.ts,.js}',
],
logging: true,
synchronize: true,
}),
}];
使导入工作如下:
constructor(
private connection: Connection,
) {
this.repository = connection.getRepository(Project);
}
在那种情况下nestjs
找不到依赖关系。我猜问题出在typeorm
,它被编译为普通的 es5 函数。但也许有解决方案?
更新:
我找到了可接受的解决方案nestjs typeorm module,但不明白为什么我的Connection
课程不起作用,但它适用于字符串。希望@kamil-myśliwiec 有助于理解。
modules: [
TypeOrmModule.forRoot(
[
Build,
Project,
],
{
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'postgres',
database: 'testo',
entities: [
Build,
],
logging: true,
synchronize: true,
}),
],
// And the n inject like this by entity name
@InjectRepository(Build) private repository: Repository<Build>,