mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 22:47:12 +03:00
86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { config } from "../config";
|
|
import { Sqlite } from "./Sqlite";
|
|
import { Mysql } from "./Mysql";
|
|
import { Postgres } from "./Postgres";
|
|
import { IDatabase } from "./IDatabase";
|
|
|
|
let db: IDatabase;
|
|
let privateDB: IDatabase;
|
|
if (config.mysql) {
|
|
db = new Mysql(config.mysql);
|
|
privateDB = new Mysql(config.privateMysql);
|
|
} else if (config.postgres) {
|
|
db = new Postgres({
|
|
dbSchemaFileName: config.dbSchema,
|
|
dbSchemaFolder: config.schemaFolder,
|
|
fileNamePrefix: "sponsorTimes",
|
|
readOnly: config.readOnly,
|
|
createDbIfNotExists: config.createDatabaseIfNotExist,
|
|
postgres: {
|
|
user: config.postgres?.user,
|
|
host: config.postgres?.host,
|
|
database: "sponsorTimes",
|
|
password: config.postgres?.password,
|
|
port: config.postgres?.port,
|
|
}
|
|
});
|
|
|
|
privateDB = new Postgres({
|
|
dbSchemaFileName: config.privateDBSchema,
|
|
dbSchemaFolder: config.schemaFolder,
|
|
fileNamePrefix: "private",
|
|
readOnly: config.readOnly,
|
|
createDbIfNotExists: config.createDatabaseIfNotExist,
|
|
postgres: {
|
|
user: config.postgres?.user,
|
|
host: config.postgres?.host,
|
|
database: "privateDB",
|
|
password: config.postgres?.password,
|
|
port: config.postgres?.port,
|
|
}
|
|
});
|
|
} else {
|
|
db = new Sqlite({
|
|
dbPath: config.db,
|
|
dbSchemaFileName: config.dbSchema,
|
|
dbSchemaFolder: config.schemaFolder,
|
|
fileNamePrefix: "sponsorTimes",
|
|
readOnly: config.readOnly,
|
|
createDbIfNotExists: config.createDatabaseIfNotExist,
|
|
enableWalCheckpointNumber: !config.readOnly && config.mode === "production"
|
|
});
|
|
privateDB = new Sqlite({
|
|
dbPath: config.privateDB,
|
|
dbSchemaFileName: config.privateDBSchema,
|
|
dbSchemaFolder: config.schemaFolder,
|
|
fileNamePrefix: "private",
|
|
readOnly: config.readOnly,
|
|
createDbIfNotExists: config.createDatabaseIfNotExist,
|
|
enableWalCheckpointNumber: false
|
|
});
|
|
}
|
|
async function initDb(): Promise<void> {
|
|
await db.init();
|
|
await privateDB.init();
|
|
|
|
if (db instanceof Sqlite) {
|
|
// Attach private db to main db
|
|
(db as Sqlite).attachDatabase(config.privateDB, "privateDB");
|
|
}
|
|
|
|
if (config.mode === "mirror" && db instanceof Postgres) {
|
|
const tables = config?.dumpDatabase?.tables ?? [];
|
|
const tableNames = tables.map(table => table.name);
|
|
for (const table of tableNames) {
|
|
const filePath = `${config?.dumpDatabase?.postgresExportPath}/${table}.csv`;
|
|
await db.prepare("run", `COPY "${table}" FROM '${filePath}' WITH (FORMAT CSV, HEADER true);`);
|
|
}
|
|
}
|
|
}
|
|
|
|
export {
|
|
db,
|
|
privateDB,
|
|
initDb,
|
|
};
|