server-esm: Fix circular dependency between sql and sql_init

This commit is contained in:
Elian Doran 2024-07-19 00:47:09 +03:00
parent 87fbd4bce8
commit 1cd6670c55
No known key found for this signature in database
2 changed files with 29 additions and 22 deletions

View File

@ -13,6 +13,7 @@ import routes from "./routes/routes.js";
import custom from "./routes/custom.js";
import error_handlers from "./routes/error_handlers.js";
import { startScheduledCleanup } from "./services/erase.js";
import sql_init from "./services/sql_init.js";
await import('./services/handlers');
await import('./becca/becca_loader');
@ -21,6 +22,9 @@ const app = express();
const scriptDir = dirname(fileURLToPath(import.meta.url));
// Initialize DB
sql_init.initializeDb();
// view engine setup
app.set('views', path.join(scriptDir, 'views'));
app.set('view engine', 'ejs');

View File

@ -21,8 +21,6 @@ import backup from "./backup.js";
const dbReady = utils.deferred<void>();
cls.init(initDbConnection);
function schemaExists() {
return !!sql.getValue(`SELECT name FROM sqlite_master
WHERE type = 'table' AND name = 'options'`);
@ -160,6 +158,15 @@ function optimize() {
log.info(`Optimization finished in ${Date.now() - start}ms.`);
}
function getDbSize() {
return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
}
function initializeDb() {
cls.init(initDbConnection);
log.info(`DB size: ${getDbSize()} KB`);
dbReady.then(() => {
if (config.General && config.General.noBackup === true) {
log.info("Disabling scheduled backups.");
@ -177,13 +184,8 @@ dbReady.then(() => {
setInterval(() => optimize(), 10 * 60 * 60 * 1000);
});
function getDbSize() {
return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
}
log.info(`DB size: ${getDbSize()} KB`);
export default {
dbReady,
schemaExists,
@ -191,5 +193,6 @@ export default {
createInitialDatabase,
createDatabaseForSync,
setDbAsInitialized,
getDbSize
getDbSize,
initializeDb
};