TypeORM 全局创建连接
TypeORM create connection globally
我在我的节点 Js 应用程序中使用 typeorm 和打字稿。我正在尝试找出对 class 中的所有函数使用单个数据库连接的方法。例如,我有两个函数 class,我想对所有函数使用 global/single 连接,而不是在每个函数中都创建一个连接,如下所示:
export class SQLDBService implements IDatabaseService{
private readonly logger = getLogger("SQLDBService");
private connection:Connection;
getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
createConnection(/*...*/).then(async connection => {
let dbObj = await connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj)
conversationEntity = dbObj;
});
return conversationEntity;
}
pushWrapUp(conversationId: string, wrapUp: string): void {
createConnection().then(async connection => {
let conversationEntity = await connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(conversationEntity){
conversationEntity.wrapUp = wrapUp;
conversationEntity.endTime = new Date();
await connection.manager.save(conversationEntity);
}
});
}}
有人能指出我正确的方向吗?
您应该使用全局连接池,它会为您创建、保存和管理使用的连接。我不熟悉node.js,所以我不能给出这种第3方库的名称。但是肯定有一些,因为连接池是一种被广泛接受的设计模式。
上面的代码没有有效地使用 async..await
因为 promises 没有链接,这导致错误处理不当和控制流不正确。
正如 the documentation 解释的那样,
TypeORM's Connection does not setup a database connection as it might seem, instead it setups a connection pool. <...> Connection pool setup is established once connect method of the Connection is called. connect method is called automatically if you setup your connection using createConnection function. Disconnection (closing all connections in the pool) is made when close is called. Generally, you must create connection only once in your application bootstrap, and close it after you completely finished working with the database.
createConnection
应该只在应用程序初始化时调用一次。由于它是异步的,初始化例程应该在使用 TypeORM 模型之前等待它。
如文档所示,可以使用 getConnection()
代替 createConnection
。由于目的是获取默认连接的存储库,因此可以使用 getRepository
代替:
是:
import {getRepository} from "typeorm";
...
async getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
let dbObj = getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj) conversationEntity = dbObj;
return conversationEntity;
}
这个非常简单的重构应该可以解决问题
export class SQLDBService implements IDatabaseService {
private readonly logger = getLogger("SQLDBService");
private connection:Connection;
init() {
this.connection = await createConnection(/*...*/)
}
getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
let dbObj = await this.connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj)
conversationEntity = dbObj;
return conversationEntity;
}
pushWrapUp(conversationId: string, wrapUp: string): void {
let conversationEntity = await this.connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(conversationEntity){
conversationEntity.wrapUp = wrapUp;
conversationEntity.endTime = new Date();
await this.connection.manager.save(conversationEntity);
}
}
}
const db = new SQLDBService()
try {
await db.init()
}
catch (error) {
console.error("db connection error")
console.error(error)
console.error("db connection error")
}
我在我的节点 Js 应用程序中使用 typeorm 和打字稿。我正在尝试找出对 class 中的所有函数使用单个数据库连接的方法。例如,我有两个函数 class,我想对所有函数使用 global/single 连接,而不是在每个函数中都创建一个连接,如下所示:
export class SQLDBService implements IDatabaseService{
private readonly logger = getLogger("SQLDBService");
private connection:Connection;
getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
createConnection(/*...*/).then(async connection => {
let dbObj = await connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj)
conversationEntity = dbObj;
});
return conversationEntity;
}
pushWrapUp(conversationId: string, wrapUp: string): void {
createConnection().then(async connection => {
let conversationEntity = await connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(conversationEntity){
conversationEntity.wrapUp = wrapUp;
conversationEntity.endTime = new Date();
await connection.manager.save(conversationEntity);
}
});
}}
有人能指出我正确的方向吗?
您应该使用全局连接池,它会为您创建、保存和管理使用的连接。我不熟悉node.js,所以我不能给出这种第3方库的名称。但是肯定有一些,因为连接池是一种被广泛接受的设计模式。
上面的代码没有有效地使用 async..await
因为 promises 没有链接,这导致错误处理不当和控制流不正确。
正如 the documentation 解释的那样,
TypeORM's Connection does not setup a database connection as it might seem, instead it setups a connection pool. <...> Connection pool setup is established once connect method of the Connection is called. connect method is called automatically if you setup your connection using createConnection function. Disconnection (closing all connections in the pool) is made when close is called. Generally, you must create connection only once in your application bootstrap, and close it after you completely finished working with the database.
createConnection
应该只在应用程序初始化时调用一次。由于它是异步的,初始化例程应该在使用 TypeORM 模型之前等待它。
如文档所示,可以使用 getConnection()
代替 createConnection
。由于目的是获取默认连接的存储库,因此可以使用 getRepository
代替:
是:
import {getRepository} from "typeorm";
...
async getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
let dbObj = getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj) conversationEntity = dbObj;
return conversationEntity;
}
这个非常简单的重构应该可以解决问题
export class SQLDBService implements IDatabaseService {
private readonly logger = getLogger("SQLDBService");
private connection:Connection;
init() {
this.connection = await createConnection(/*...*/)
}
getConversation(conversationId: string): ConversationEntity {
let conversationEntity = new ConversationEntity();
let dbObj = await this.connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(dbObj)
conversationEntity = dbObj;
return conversationEntity;
}
pushWrapUp(conversationId: string, wrapUp: string): void {
let conversationEntity = await this.connection.getRepository(ConversationEntity).findOne({
conversationId: Equal(conversationId)
});
if(conversationEntity){
conversationEntity.wrapUp = wrapUp;
conversationEntity.endTime = new Date();
await this.connection.manager.save(conversationEntity);
}
}
}
const db = new SQLDBService()
try {
await db.init()
}
catch (error) {
console.error("db connection error")
console.error(error)
console.error("db connection error")
}