[英]Connection “default” was not found with TypeORM when trying to request a repository
I'm building an API with Express+TypeORM.我正在使用 Express+TypeORM 构建 API。 This is my ormconfig.json:这是我的 ormconfig.json:
{
"type": "postgres",
"host": "localhost",
"port": "5432",
"username": "mdsp9070",
"password": "mdsp9070",
"database": "mesha",
"entities": ["./src/entities/*.ts"],
"migrations": ["./src/shared/infra/typeorm/migrations/*.ts"],
"cli": {
"migrationsDir": "./src/shared/infra/typeorm/migrations"
}
}
And my entity is defined as:我的实体定义为:
import {
Entity,
Column,
PrimaryGeneratedColumn,
} from "typeorm";
@Entity("users")
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column()
email: string;
@Column()
birthYear: string;
@Column()
phoneNumber: string;
@Column()
photo: string;
}
On my CreateUserUseCase, I call this repository like:在我的 CreateUserUseCase 上,我将此存储库称为:
...
private usersRepository: IUsersRepository;
// receives ormRepository, that's users repository
constructor() {
this.usersRepository = getCustomRepository(UsersRepository);
}
...
but I'm getting this error: [ERROR] 13:08:39 ConnectionNotFoundError: Connection "default" was not found.
但我收到此错误: [ERROR] 13:08:39 ConnectionNotFoundError: Connection "default" was not found.
No, connection is not being called after getCustomRepository as I call the file on the index of my server.不,在我调用服务器索引上的文件时,在 getCustomRepository 之后没有调用连接。
full github repo: https://github.com/Mdsp9070/mesha完整的 github 回购: https://github.com/Mdsp9070/mesha
Explanation: Express routes eas been called before connection being established.说明:在建立连接之前调用了快速路由。 So I chose to use dynamic imports.所以我选择使用动态导入。
My application main entry was changed to typeorm/index.ts as:我的应用程序主条目更改为 typeorm/index.ts 为:
import { createConnection } from "typeorm";
import { AppError } from "../../errors/AppError";
// finds ormconfig.json file and creates a connection. (magic!),
// it could be set up manually with parameters
// however an external file is the best practise
createConnection()
.then(() => {
console.log("Connected to the database")
import("../../http/server.ts")
})
.catch(() => new AppError("Unable to connect to the database"));
That way worked like a charm!这种方式就像一个魅力!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.