diff --git a/server/database.js b/server/database.js index 1dc0664..df3cf77 100644 --- a/server/database.js +++ b/server/database.js @@ -309,7 +309,7 @@ class Database { log.info("db", `Database Type: ${dbConfig.type}`); if (dbConfig.type === "sqlite") { - if (!fs.existsSync(Database.sqlitePath)) { + if (!fs.existsSync(Database.sqlitePath) && fs.existsSync(Database.templatePath)) { log.info("server", "Copying Database"); fs.copyFileSync(Database.templatePath, Database.sqlitePath); } @@ -403,26 +403,28 @@ class Database { }, pool: mariadbPoolConfig, }; - } else if (dbConfig.type === "embedded-mariadb") { - let embeddedMariaDB = EmbeddedMariaDB.getInstance(); - await embeddedMariaDB.start(); - log.info("mariadb", "Embedded MariaDB started"); + } else if (dbConfig.type === "postgres" || dbConfig.type === "postgresql") { config = { - client: "mysql2", + client: "pg", connection: { - socketPath: embeddedMariaDB.socketPath, - user: embeddedMariaDB.username, - database: "kuma", - timezone: "Z", - typeCast: function (field, next) { - if (field.type === "DATETIME") { - // Do not perform timezone conversion - return field.string(); - } - return next(); - }, + host: dbConfig.hostname, + port: Number(dbConfig.port) || 5432, + user: dbConfig.username, + password: dbConfig.password, + database: dbConfig.dbName, + ...(dbConfig.ssl + ? { + ssl: { + rejectUnauthorized: false, + ...(dbConfig.ca && dbConfig.ca.trim() !== "" ? { ca: dbConfig.ca } : {}), + }, + } + : {}), + }, + pool: { + min: 0, + max: parsedMaxPoolConnections, }, - pool: mariadbPoolConfig, }; } else { throw new Error("Unknown Database type: " + dbConfig.type); @@ -450,7 +452,7 @@ class Database { log.debug("db", await R.getAll("PRAGMA cache_size")); log.debug("db", "SQLite Version: " + (await R.getCell("SELECT sqlite_version()"))); } - } else if (dbConfig.type.endsWith("mariadb")) { + } else if (dbConfig.type.endsWith("mariadb") || dbConfig.type === "postgres" || dbConfig.type === "postgresql") { await this.initMariaDB(); } @@ -555,7 +557,18 @@ class Database { */ static createAuthDatabase(dbConfig) { let database; - if (dbConfig.type.includes("mariadb")) { + if (dbConfig.type === "postgres" || dbConfig.type === "postgresql") { + const { Pool: PgPool } = require("pg"); + database = new PgPool({ + host: dbConfig.hostname, + port: Number(dbConfig.port) || 5432, + database: dbConfig.dbName, + user: dbConfig.username, + password: dbConfig.password, + ...(dbConfig.ssl ? { ssl: { rejectUnauthorized: false } } : {}), + }); + Database.authPostgres = database; + } else if (dbConfig.type.includes("mariadb")) { database = mysql.createPool({ host: dbConfig.hostname, port: Number(dbConfig.port),