Files

43 lines
794 B
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import knex from "knex";
import {configGet, configHas} from "./lib/config.js";
2020-02-19 15:55:06 +11:00
let instance = null;
2025-09-02 21:43:00 +10:00
const generateDbConfig = () => {
if (!configHas("database")) {
throw new Error(
"Database config does not exist! Please read the instructions: https://nginxproxymanager.com/setup/",
);
}
const cfg = configGet("database");
2020-02-19 15:55:06 +11:00
2025-09-02 21:43:00 +10:00
if (cfg.engine === "knex-native") {
2023-03-21 16:53:39 +10:00
return cfg.knex;
}
2025-09-02 21:43:00 +10:00
2023-03-21 16:53:39 +10:00
return {
2025-09-02 21:43:00 +10:00
client: cfg.engine,
2023-03-21 16:53:39 +10:00
connection: {
2025-09-02 21:43:00 +10:00
host: cfg.host,
user: cfg.user,
2023-03-21 16:53:39 +10:00
password: cfg.password,
database: cfg.name,
2025-10-08 10:57:58 +11:00
port: cfg.port,
...(cfg.ssl ? { ssl: cfg.ssl } : {})
2023-03-21 16:53:39 +10:00
},
migrations: {
2025-09-02 21:43:00 +10:00
tableName: "migrations",
},
2023-03-21 16:53:39 +10:00
};
2025-09-02 21:43:00 +10:00
};
2020-07-19 16:43:01 +02:00
const getInstance = () => {
if (!instance) {
instance = knex(generateDbConfig());
}
return instance;
}
export default getInstance;