2026-03-23 18:49:06 +02:00
|
|
|
import { type OptionRow } from "@triliumnext/commons";
|
|
|
|
|
import { sql_init as coreSqlInit } from "@triliumnext/core";
|
2024-07-18 21:37:45 +03:00
|
|
|
import fs from "fs";
|
2026-01-06 11:10:09 +02:00
|
|
|
|
|
|
|
|
import BBranch from "../becca/entities/bbranch.js";
|
|
|
|
|
import BNote from "../becca/entities/bnote.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import BOption from "../becca/entities/boption.js";
|
|
|
|
|
import cls from "./cls.js";
|
2024-07-18 23:45:17 +03:00
|
|
|
import password from "./encryption/password.js";
|
2025-09-15 19:07:23 +03:00
|
|
|
import hidden_subtree from "./hidden_subtree.js";
|
2026-01-06 11:10:09 +02:00
|
|
|
import zipImportService from "./import/zip.js";
|
|
|
|
|
import log from "./log.js";
|
|
|
|
|
import optionService from "./options.js";
|
|
|
|
|
import resourceDir from "./resource_dir.js";
|
|
|
|
|
import sql from "./sql.js";
|
|
|
|
|
import TaskContext from "./task_context.js";
|
2018-04-02 21:25:20 -04:00
|
|
|
|
2026-03-23 18:49:06 +02:00
|
|
|
const schemaExists = coreSqlInit.schemaExists;
|
|
|
|
|
const isDbInitialized = coreSqlInit.isDbInitialized;
|
|
|
|
|
const dbReady = coreSqlInit.dbReady;
|
|
|
|
|
const setDbAsInitialized = coreSqlInit.setDbAsInitialized;
|
|
|
|
|
const initDbConnection = coreSqlInit.initDbConnection;
|
|
|
|
|
const initializeDb = coreSqlInit.initializeDb;
|
|
|
|
|
export const getDbSize = coreSqlInit.getDbSize;
|
2018-04-02 21:25:20 -04:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
async function createDatabaseForSync(options: OptionRow[], syncServerHost = "", syncProxy = "") {
|
2018-07-24 20:35:03 +02:00
|
|
|
log.info("Creating database for sync");
|
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (isDbInitialized()) {
|
2018-07-24 20:35:03 +02:00
|
|
|
throw new Error("DB is already initialized");
|
|
|
|
|
}
|
2018-07-23 21:15:32 +02:00
|
|
|
|
2024-02-17 19:51:22 +02:00
|
|
|
const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf8");
|
2018-07-23 21:15:32 +02:00
|
|
|
|
2024-09-14 14:34:24 +03:00
|
|
|
// We have to import async since options init requires keyboard actions which require translations.
|
|
|
|
|
const optionsInitService = (await import("./options_init.js")).default;
|
2024-12-22 15:42:15 +02:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.transactional(() => {
|
|
|
|
|
sql.executeScript(schema);
|
2018-07-23 21:15:32 +02:00
|
|
|
|
2024-11-22 20:15:35 +02:00
|
|
|
optionsInitService.initNotSyncedOptions(false, { syncServerHost, syncProxy });
|
2018-07-24 20:35:03 +02:00
|
|
|
|
|
|
|
|
// document options required for sync to kick off
|
|
|
|
|
for (const opt of options) {
|
2023-01-03 13:52:37 +01:00
|
|
|
new BOption(opt).save();
|
2018-07-24 20:35:03 +02:00
|
|
|
}
|
2018-07-23 21:15:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
log.info("Schema and not synced options generated.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-04-02 21:25:20 -04:00
|
|
|
dbReady,
|
2018-07-24 08:12:36 +02:00
|
|
|
schemaExists,
|
2018-07-22 19:56:20 +02:00
|
|
|
isDbInitialized,
|
2018-07-23 21:15:32 +02:00
|
|
|
createInitialDatabase,
|
2018-07-24 20:35:03 +02:00
|
|
|
createDatabaseForSync,
|
2022-05-31 14:09:46 +02:00
|
|
|
setDbAsInitialized,
|
2024-07-19 00:47:09 +03:00
|
|
|
getDbSize,
|
|
|
|
|
initializeDb
|
2020-06-17 23:03:46 +02:00
|
|
|
};
|