All files / config sequelize.ts

72.22% Statements 13/18
64.71% Branches 11/17
75% Functions 3/4
73.33% Lines 11/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4111x 11x 11x   11x   11x                     182x             11x 11x 11x 11x                       11x  
import { Sequelize } from "sequelize";
import pino from "pino";
import { defaultLoggerOptions } from "./pino";
 
const logger = pino(defaultLoggerOptions);
 
const sequelize = new Sequelize(
    process.env.DB_NAME || "dumpster",
    process.env.DB_USER || "root",
    process.env.DB_PASSWORD || "password",
    {
        host: process.env.DB_HOST || "localhost",
        // @ts-ignore
        port: process.env.DB_PORT || 3306,
        // @ts-ignore
        dialect: process.env.DB_DIALECT || "mariadb",
        logging: (sql, timing) =>
            logger.info(sql, timing ? `Elapsed time: ${timing} ms` : ""),
        define: {
            timestamps: false,
        },
    },
);
 
export const connectToDatabase = async (tries = 10) => {
    try {
        await sequelize.authenticate();
        logger.info("Database connection established!");
    } catch (e) {
        if (tries <= 0)
            throw new Error(
                "Could not connect to the database after 10 tries, aborting.",
            );
        logger.info(e, "Could not connect to the database, retrying...");
        // Retry after a few seconds
        setTimeout(() => connectToDatabase(tries - 1), 3000);
    }
};
 
export default sequelize;