Search code examples
node.jstypescripttypeorm

TypeORM create connection globally


I am using typeorm with typescript in my node Js application. I am trying to figure out the way of using the single DB connection for all functions in the class. For example, I have two functions my class and want to use the global/single connection for all functions instead of creating a connection in every function as shown below:

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

Can someone point me in the right direction?


Solution

  • The code above doesn't efficiently use async..await because promises aren't chained, this results in poor error handling and improper control flow.

    As the documentation explains,

    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 is supposed to be called only once on application initialization. Since it's asynchronous, initialization routine should wait for it before using TypeORM models.

    As the documentation suggests, getConnection() can be used instead of createConnection. Since the purpose is to get a repository for default connection, getRepository can be used instead:

    It's:

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