2018-03-28 23:41:22 -04:00
|
|
|
const clsHooked = require('cls-hooked');
|
|
|
|
const namespace = clsHooked.createNamespace("trilium");
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function init(callback) {
|
|
|
|
return namespace.runAndReturn(callback);
|
2018-03-28 23:41:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function wrap(callback) {
|
2020-09-01 00:05:19 +02:00
|
|
|
return () => {
|
|
|
|
try {
|
|
|
|
init(callback);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(`Error occurred: ${e.message}: ${e.stack}`);
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 23:41:22 -04:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:56:53 +02:00
|
|
|
function get(key) {
|
|
|
|
return namespace.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function set(key, value) {
|
|
|
|
namespace.set(key, value);
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:52:48 +01:00
|
|
|
function getHoistedNoteId() {
|
2020-12-14 23:59:05 +01:00
|
|
|
return namespace.get('hoistedNoteId') || 'root';
|
2020-11-23 22:52:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-09 20:16:39 +01:00
|
|
|
function getComponentId() {
|
|
|
|
return namespace.get('componentId');
|
2018-03-30 13:20:36 -04:00
|
|
|
}
|
|
|
|
|
2020-04-26 14:26:57 +02:00
|
|
|
function getLocalNowDateTime() {
|
|
|
|
return namespace.get('localNowDateTime');
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
function disableEntityEvents() {
|
|
|
|
namespace.set('disableEntityEvents', true);
|
|
|
|
}
|
|
|
|
|
2022-10-22 21:34:38 +02:00
|
|
|
function enableEntityEvents() {
|
|
|
|
namespace.set('disableEntityEvents', false);
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:27:57 +01:00
|
|
|
function isEntityEventsDisabled() {
|
|
|
|
return !!namespace.get('disableEntityEvents');
|
|
|
|
}
|
|
|
|
|
2024-01-21 20:50:38 +01:00
|
|
|
function setMigrationRunning(running) {
|
|
|
|
namespace.set('migrationRunning', !!running);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMigrationRunning() {
|
|
|
|
return !!namespace.get('migrationRunning');
|
|
|
|
}
|
|
|
|
|
2023-10-20 09:36:57 +02:00
|
|
|
function disableSlowQueryLogging(disable) {
|
|
|
|
namespace.set('disableSlowQueryLogging', disable);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSlowQueryLoggingDisabled() {
|
|
|
|
return !!namespace.get('disableSlowQueryLogging');
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
function getAndClearEntityChangeIds() {
|
|
|
|
const entityChangeIds = namespace.get('entityChangeIds') || [];
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2021-11-18 22:33:08 +01:00
|
|
|
namespace.set('entityChangeIds', []);
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
return entityChangeIds;
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
function putEntityChange(entityChange) {
|
2021-09-16 15:02:20 +02:00
|
|
|
if (namespace.get('ignoreEntityChangeIds')) {
|
2021-02-16 22:51:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
const entityChangeIds = namespace.get('entityChangeIds') || [];
|
2020-01-31 22:32:24 +01:00
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// store only ID since the record can be modified (e.g., in erase)
|
2021-09-16 15:02:20 +02:00
|
|
|
entityChangeIds.push(entityChange.id);
|
2020-01-31 22:32:24 +01:00
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
namespace.set('entityChangeIds', entityChangeIds);
|
2020-01-31 22:32:24 +01:00
|
|
|
}
|
|
|
|
|
2018-07-22 19:56:20 +02:00
|
|
|
function reset() {
|
|
|
|
clsHooked.reset();
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:02:20 +02:00
|
|
|
function ignoreEntityChangeIds() {
|
|
|
|
namespace.set('ignoreEntityChangeIds', true);
|
2020-04-06 20:59:04 +02:00
|
|
|
}
|
|
|
|
|
2018-03-28 23:41:22 -04:00
|
|
|
module.exports = {
|
|
|
|
init,
|
|
|
|
wrap,
|
2020-06-15 17:56:53 +02:00
|
|
|
get,
|
|
|
|
set,
|
2018-03-30 13:20:36 -04:00
|
|
|
namespace,
|
2020-11-23 22:52:48 +01:00
|
|
|
getHoistedNoteId,
|
2022-01-09 20:16:39 +01:00
|
|
|
getComponentId,
|
2020-04-26 14:26:57 +02:00
|
|
|
getLocalNowDateTime,
|
2018-11-26 22:27:57 +01:00
|
|
|
disableEntityEvents,
|
2022-10-22 21:34:38 +02:00
|
|
|
enableEntityEvents,
|
2018-11-26 22:27:57 +01:00
|
|
|
isEntityEventsDisabled,
|
2020-01-31 22:32:24 +01:00
|
|
|
reset,
|
2021-09-16 15:02:20 +02:00
|
|
|
getAndClearEntityChangeIds,
|
2023-07-29 23:25:02 +02:00
|
|
|
putEntityChange,
|
2023-02-01 21:07:23 +01:00
|
|
|
ignoreEntityChangeIds,
|
2023-10-20 09:36:57 +02:00
|
|
|
disableSlowQueryLogging,
|
2024-01-21 20:50:38 +01:00
|
|
|
isSlowQueryLoggingDisabled,
|
|
|
|
setMigrationRunning,
|
|
|
|
isMigrationRunning
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|