【问题标题】:TypeORM create connection globallyTypeORM 全局创建连接
【发布时间】:2018-11-12 09:25:05
【问题描述】:

我在我的节点 Js 应用程序中使用带有打字稿的typeorm。我试图找出对类中的所有函数使用单个数据库连接的方式。例如,我的类有两个函数,并且想对所有函数使用全局/单一连接,而不是在每个函数中创建一个连接,如下所示:

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 typescript typeorm


    【解决方案1】:

    您应该使用一个全局连接池,它将为您创建、保存和处理使用过的连接。我对 node.js 不熟悉,所以我不能给出这种第 3 方库的名称。但肯定有一些,因为连接池是一种被广泛接受的设计模式。

    【讨论】:

      【解决方案2】:

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

      正如the documentation 解释的那样,

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

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

      【讨论】:

        【解决方案3】:

        这个非常小的重构应该可以解决问题

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-30
          • 1970-01-01
          • 1970-01-01
          • 2020-08-30
          • 2014-09-21
          • 1970-01-01
          • 2016-11-14
          • 2017-07-24
          相关资源
          最近更新 更多