db.js 794 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import knex from "knex";
  2. import {configGet, configHas} from "./lib/config.js";
  3. let instance = null;
  4. const generateDbConfig = () => {
  5. if (!configHas("database")) {
  6. throw new Error(
  7. "Database config does not exist! Please read the instructions: https://nginxproxymanager.com/setup/",
  8. );
  9. }
  10. const cfg = configGet("database");
  11. if (cfg.engine === "knex-native") {
  12. return cfg.knex;
  13. }
  14. return {
  15. client: cfg.engine,
  16. connection: {
  17. host: cfg.host,
  18. user: cfg.user,
  19. password: cfg.password,
  20. database: cfg.name,
  21. port: cfg.port,
  22. ...(cfg.ssl ? { ssl: cfg.ssl } : {})
  23. },
  24. migrations: {
  25. tableName: "migrations",
  26. },
  27. };
  28. };
  29. const getInstance = () => {
  30. if (!instance) {
  31. instance = knex(generateDbConfig());
  32. }
  33. return instance;
  34. }
  35. export default getInstance;