(option: SequelizeOption, numRetries: number)
| 31 | const CONNECTION_SSL_ERROR_REGEX = 'not support SSL'; |
| 32 | |
| 33 | async function establishConnectionSequelize(option: SequelizeOption, numRetries: number): Promise<Sequelize> { |
| 34 | const uri = `postgresql://${option.username}:${option.password}@${option.host}:${option.port}/${option.database}`; |
| 35 | |
| 36 | const sequelize = new Sequelize(uri, option); |
| 37 | try { |
| 38 | await sequelize.authenticate(); |
| 39 | } catch (error: any) { |
| 40 | if (JSON.stringify(error.message).includes(CONNECTION_SSL_ERROR_REGEX)) { |
| 41 | logger.warn('Database does not support SSL connection, will try to connect without it'); |
| 42 | option.dialectOptions = undefined; |
| 43 | } |
| 44 | if (numRetries > 0) { |
| 45 | await delay(3); |
| 46 | return establishConnectionSequelize(option, numRetries - 1); |
| 47 | } else { |
| 48 | exitWithError(new Error(`Unable to connect to the database`, {cause: error}), logger); |
| 49 | } |
| 50 | } |
| 51 | return sequelize; |
| 52 | } |
| 53 | |
| 54 | const sequelizeFactory = (option: SequelizeOption) => async () => { |
| 55 | const numRetries = 5; |
no test coverage detected