This commit is contained in:
2026-01-10 18:39:55 +00:00
parent 0a0969b8af
commit 70ea1894d4
928 changed files with 5187 additions and 3121 deletions

View File

@@ -1,11 +1,55 @@
// knexfile.js
require('dotenv').config();
function must(name) {
const v = process.env[name];
if (!v) throw new Error(`${name} não definido no .env`);
return v;
}
const shared = {
client: 'pg',
migrations: {
directory: './src/db/migrations',
},
};
function buildConnectionFromEnv() {
// Se houver DATABASE_URL, usa-o.
// Se PGSSL=true, aplica ssl no formato esperado pelo driver pg (dentro de connection).
const ssl =
process.env.PGSSL === 'true' ? { rejectUnauthorized: false } : undefined;
if (process.env.DATABASE_URL) {
// knex aceita string, mas o ssl precisa estar no objeto:
return ssl
? { connectionString: process.env.DATABASE_URL, ssl }
: process.env.DATABASE_URL;
}
// fallback para vars soltas
return {
host: process.env.PGHOST || '127.0.0.1',
port: Number(process.env.PGPORT || 5432),
user: process.env.PGUSER || 'postgres',
password: process.env.PGPASSWORD || 'postgres',
database: process.env.PGDATABASE || 'evse',
...(ssl ? { ssl } : {}),
};
}
module.exports = {
development: {
client: 'pg',
connection: process.env.DATABASE_URL,
migrations: {
directory: './migrations',
},
...shared,
connection: buildConnectionFromEnv(),
},
production: {
...shared,
// Em produção normalmente queres obrigar DATABASE_URL (se for o teu caso):
// connection: must('DATABASE_URL'),
// Mas mantendo compatível com vars soltas:
connection: buildConnectionFromEnv(),
pool: { min: 2, max: 10 },
},
};