trilium/src/services/options.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-11-03 08:48:02 +08:00
const sql = require('./sql');
const utils = require('./utils');
2018-04-03 08:46:46 +08:00
const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const appInfo = require('./app_info');
2017-11-03 08:48:02 +08:00
2018-01-29 08:30:14 +08:00
async function getOption(name) {
2018-04-03 10:53:01 +08:00
const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
2017-11-03 08:48:02 +08:00
if (!row) {
2018-01-29 08:30:14 +08:00
throw new Error("Option " + name + " doesn't exist");
2017-11-03 08:48:02 +08:00
}
return row.value;
2017-11-03 08:48:02 +08:00
}
async function setOption(name, value) {
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
if (!opt) {
2018-01-29 08:30:14 +08:00
throw new Error(`Option ${name} doesn't exist`);
}
2018-01-29 08:30:14 +08:00
if (opt.isSynced) {
await syncTableService.addOptionsSync(name);
2017-11-03 08:48:02 +08:00
}
await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",
2018-04-03 08:46:46 +08:00
[value, dateUtils.nowDate(), name]);
}
async function createOption(name, value, isSynced) {
await sql.insert("options", {
2018-01-29 08:30:14 +08:00
name: name,
value: value,
isSynced: isSynced,
2018-04-03 08:46:46 +08:00
dateModified: dateUtils.nowDate()
2017-12-04 11:29:23 +08:00
});
if (isSynced) {
await syncTableService.addOptionsSync(name);
}
2017-11-03 08:48:02 +08:00
}
async function initOptions(startNotePath) {
2018-04-03 09:47:46 +08:00
await createOption('documentId', utils.randomSecureToken(16), false);
await createOption('documentSecret', utils.randomSecureToken(16), false);
await createOption('username', '', true);
2018-04-03 09:47:46 +08:00
await createOption('passwordVerificationHash', '', true);
await createOption('passwordVerificationSalt', '', true);
await createOption('passwordDerivedKeySalt', '', true);
await createOption('encryptedDataKey', '', true);
2018-04-03 10:33:54 +08:00
await createOption('encryptedDataKeyIv', '', true);
2018-04-03 09:47:46 +08:00
await createOption('startNotePath', startNotePath, false);
await createOption('protectedSessionTimeout', 600, true);
await createOption('noteRevisionSnapshotTimeInterval', 600, true);
await createOption('lastBackupDate', dateUtils.nowDate(), false);
await createOption('dbVersion', appInfo.dbVersion, false);
2018-04-03 09:47:46 +08:00
await createOption('lastSyncedPull', appInfo.dbVersion, false);
await createOption('lastSyncedPush', 0, false);
2017-12-04 11:29:23 +08:00
}
2017-11-03 08:48:02 +08:00
module.exports = {
getOption,
setOption,
2018-04-03 10:53:01 +08:00
initOptions
2017-11-03 08:48:02 +08:00
};