From ba372c070567e1d8f3082bf0f29863fd1505e469 Mon Sep 17 00:00:00 2001 From: Adrien Chauve Date: Fri, 24 Nov 2023 09:49:16 +0100 Subject: [PATCH] fix: PG ssl (#306) * fix: use full db config in app rather than only for migrations * fix: reenable require ssl on prod and redisable on dev * fix: fix CI * fix ci * fix ci * fix ci --- src/db/config/config.js | 20 +++++++++++--------- src/db/models/index.ts | 23 ++++++++++++++++------- src/services/config.ts | 1 - 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/db/config/config.js b/src/db/config/config.js index e5317c03..2196dc25 100644 --- a/src/db/config/config.js +++ b/src/db/config/config.js @@ -3,21 +3,23 @@ dotenv.config(); module.exports = { development: { + username: process.env.PGUSER || "monpsysante", + password: process.env.PGPASSWORD || "monpsysante", + database: process.env.PGDATABASE || "monpsysante", + port: process.env.PGPORT, + host: process.env.PGHOST, dialect: "postgres", - url: process.env.DATABASE_URL, - dialectOptions: { - ssl: { - require: false, - rejectUnauthorized: false, - }, - }, }, production: { + username: process.env.PGUSER, + password: process.env.PGPASSWORD, + database: process.env.PGDATABASE, + port: process.env.PGPORT, + host: process.env.PGHOST, dialect: "postgres", - url: process.env.DATABASE_URL, dialectOptions: { ssl: { - require: false, + require: true, rejectUnauthorized: false, }, }, diff --git a/src/db/models/index.ts b/src/db/models/index.ts index 062489d0..0e255d22 100644 --- a/src/db/models/index.ts +++ b/src/db/models/index.ts @@ -1,15 +1,24 @@ import { Sequelize } from "sequelize"; import config from "../../services/config"; +import dbConfig from "../config/config"; import psychologist from "./psychologist"; -export const sequelize = new Sequelize(config.postgre.url, { - define: { - freezeTableName: true, - }, - dialect: "postgres", - logging: config.postgre.logging ? console.log : false, -}); +const env = process.env.NODE_ENV || "development"; +const currentDbConfig = dbConfig[env] || dbConfig["development"]; + +export const sequelize = new Sequelize( + currentDbConfig.database, + currentDbConfig.username, + currentDbConfig.password, + { + ...currentDbConfig, + define: { + freezeTableName: true, + }, + logging: config.postgre.logging ? console.log : false, + } +); export const models = { Psychologist: psychologist(sequelize), diff --git a/src/services/config.ts b/src/services/config.ts index 9b53a547..c2e88574 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -43,7 +43,6 @@ export default { minScoreAddress: parseFloat(process.env.MIN_SCORE_ADDRESS || "0.30"), postgre: { logging: parseBoolean(process.env.DB_LOGGING_ENABLE), - url: process.env.DATABASE_URL || "postgres://localhost:5432/monpsysante", }, supportMail: process.env.SUPPORT_MAIL, reportingMailRecipients: process.env.REPORTING_MAIL_RECIPIENTS,