/
ruscreator
/
Webed
Обзор
Документация
Войти
/
ruscreator
/
Webed
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
backend/src/config/database.js
68 строк
2 KB
Алексей Слепов
Initial commit
17 фев 2025, 15:15
17 фев 2025, 15:15
63a31d6
Код
Авторство
О чём код?
const { Sequelize } = require('sequelize'); const config = { development: { username: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, host: process.env.DB_HOST, port: process.env.DB_PORT, dialect: 'postgres', logging: console.log, dialectOptions: { ssl: false }, pool: { max: 5, min: 0, acquire: 30000, idle: 10000 } } }; const sequelize = new Sequelize( config.development.database, config.development.username, config.development.password, { host: config.development.host, port: config.development.port, dialect: 'postgres', logging: config.development.logging, retry: { max: 10, match: [ /SequelizeConnectionError/, /SequelizeConnectionRefusedError/, /SequelizeHostNotFoundError/, /SequelizeHostNotReachableError/, /SequelizeInvalidConnectionError/, /SequelizeConnectionTimedOutError/ ], backoffBase: 1000, backoffExponent: 1.5, } } ); const connectWithRetry = async () => { let retries = 5; while (retries) { try { await sequelize.authenticate(); console.log('База данных успешно подключена'); return true; } catch (error) { retries -= 1; console.log(`Попытка подключения к базе данных не удалась. Осталось попыток: ${retries}`); if (retries === 0) { console.error('Не удалось подключиться к базе данных после всех попыток'); throw error; } // Ждем перед следующей попыткой await new Promise(resolve => setTimeout(resolve, 5000)); } } }; module.exports = config[process.env.NODE_ENV || 'development'];