1

I'm trying to use nextjs and sequelize together. It works but I have a problem with each request from the database it opens a connection for DB. I have a Db class and export an instance from this class then use it wherever I need.

import { FindOptions, Model, ModelCtor, Sequelize } from 'sequelize'
import { initModels, Models } from './models';

class Db {
    connection: Sequelize | null = null;
    constructor() {
        this.connection = this.connectDb() 
        initModels(this.connection)
    }

    retrieveAll(model: ModelCtor<Model<any, any>>, options: FindOptions<any> = {}) {
        return model?.findAll(options);
    }

    retrieve(model: ModelCtor<Model<any, any>>, options: FindOptions<any> = {}) {
        return model?.findOne(options);
    }

    connectDb() {
        const dbName = process.env.DB_NAME
        const dbHost = process.env.DB_HOST
        const dbUser = process.env.DB_USER as string       
        const dbPassword = process.env.DB_PASSWORD
        
        const sequelize = new Sequelize({
            dialect: 'postgres',
            host: dbHost,
            port: 5432,
            database: dbName,
            username: dbUser,
            password: dbPassword,
            pool: {
                max: 5,
                idle: 86400000,
            }
        });
        
        try {
            sequelize.authenticate();
            console.log('Connection has been established successfully.');
            return sequelize;
        } catch (error) {
            console.error('Unable to connect to the database:', error);
            return null;
        }
        
    }

    closeConnectionDb() {
        this.connection?.close()
    }
}

export const db = new Db();

For example, I make a query database for login then on homepage I get merchant list from the database. I see two times "Connection has been established successfully.". How can I solve this problem? By the way, it works very well in the production server. I think it's about module caching but I'm not sure.

10
  • Can you please share code from the components that are instantiating this class? And if you place a console.log('Disconnected') log inside the closeConnectionDb function, do you see that two times? Commented Feb 17, 2023 at 14:29
  • I don't yet invoke the closeConnectionDb function. I instantiate this class last line of the code I shared. I use it by importing wherever I need. Commented Feb 17, 2023 at 14:59
  • When you say it works well in production server, are you saying this only happens to you in development? Or that everything still works fine despite the double-connection? Commented Feb 17, 2023 at 15:26
  • Everything work well both production and development but in development I see double connection. Commented Feb 17, 2023 at 19:16
  • 1
    I saw now your last answer thank you for the answer Commented Feb 18, 2023 at 20:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.