繁体   English   中英

TypeORM全局创建连接

[英]TypeORM create connection globally

我在节点Js应用程序中将typeormtypescript一起使用。 我试图弄清楚对类中的所有函数使用单个DB连接的方式。 例如,我的类中有两个函数,并且想对所有函数使用全局/单个连接,而不是在每个函数中创建一个连接,如下所示:

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);
        }
    });
}}

有人可以指出我正确的方向吗?

上面的代码没有有效地使用async..await因为诺言没有被链接,这导致差的错误处理和不正确的控制流。

文档所述

TypeORM的Connection并没有建立数据库连接,而是建立了一个连接池。 <...>一旦调用Connection的connect方法,便建立了连接池设置。 如果使用createConnection函数设置连接,则会自动调用connect方法。 调用close时,将断开连接(关闭池中的所有连接)。 通常,您必须在应用程序引导程序中仅创建一次连接,并在完全使用数据库后关闭它。

应该仅在应用程序初始化时调用一次createConnection 由于它是异步的,因此初始化例程应在使用TypeORM模型之前等待它。

如文档所示,可以使用getConnection()代替createConnection 由于目的是为默认连接获取存储库, getRepository可以使用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")
}

您应该使用一个全局连接池,该池将为您创建,保留和维护使用的连接。 我不熟悉node.js,因此无法给出此类第三方库的名称。 但是必须有一些,因为连接池是一种被广泛接受的设计模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM