Skip to content

Build your personal knowledge base with Trilium Notes

wip

zadam6207203b3539
diff --git a/src/routes/api/search.js b/src/routes/api/search.js
index e5ecac212c..c5ed5a0951 100644
--- a/src/routes/api/search.js
+++ b/src/routes/api/search.js
@@ -7,7 +7,7 @@ const scriptService = require('../../services/script');
const searchService = require('../../services/search/search');
function searchNotes(req) {
- const {count, results} = searchService.searchNotes(req.params.searchString);
+ const {count, results} = searchService.searchTrimmedNotes(req.params.searchString);
try {
return {
diff --git a/src/routes/routes.js b/src/routes/routes.js
index d4fce87929..89fb68775a 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -89,14 +89,9 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio
cls.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
protectedSessionService.setProtectedSessionId(req);
- if (transactional) {
- return sql.transactional(() => {
- return routeHandler(req, res, next);
- });
- }
- else {
- return routeHandler(req, res, next);
- }
+ const cb = () => routeHandler(req, res, next);
+
+ return transactional ? sql.transactional(cb) : cb();
});
if (resultHandler) {
diff --git a/src/services/backend_script_api.js b/src/services/backend_script_api.js
index dfa9fe4be3..1824952067 100644
--- a/src/services/backend_script_api.js
+++ b/src/services/backend_script_api.js
@@ -12,7 +12,7 @@ const dayjs = require('dayjs');
const cloningService = require('./cloning');
const ws = require('./ws.js');
const appInfo = require('./app_info');
-const searchService = require('./search');
+const searchService = require('./search/search');
/**
* This is the main backend API interface for scripts. It's published in the local "api" object.
@@ -99,9 +99,9 @@ function BackendScriptApi(currentNote, apiParams) {
*
* @method
* @param {string} searchString
- * @returns {Promise<Note[]>}
+ * @returns {Note[]}
*/
- this.searchForNotes = searchService.searchForNotes;
+ this.searchForNotes = searchService.searchNoteEntities;
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
@@ -112,7 +112,7 @@ function BackendScriptApi(currentNote, apiParams) {
* @returns {Promise<Note|null>}
*/
this.searchForNote = searchString => {
- const notes = searchService.searchForNotes(searchString);
+ const notes = searchService.searchNoteEntities(searchString);
return notes.length > 0 ? notes[0] : null;
};
diff --git a/src/services/note_cache/note_cache_loader.js b/src/services/note_cache/note_cache_loader.js
index 6a19b441cb..96ae506823 100644
--- a/src/services/note_cache/note_cache_loader.js
+++ b/src/services/note_cache/note_cache_loader.js
@@ -147,5 +147,4 @@ eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes());
});
-// FIXME
-// load();
+load();
diff --git a/src/services/scheduler.js b/src/services/scheduler.js
index d060515db6..d5a6d46b73 100644
--- a/src/services/scheduler.js
+++ b/src/services/scheduler.js
@@ -1,7 +1,6 @@
const scriptService = require('./script');
const repository = require('./repository');
const cls = require('./cls');
-const sqlInit = require('./sql_init');
function runNotesWithLabel(runAttrValue) {
const notes = repository.getEntities(`
diff --git a/src/services/script.js b/src/services/script.js
index 949d898a49..96a1d1da2f 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -4,7 +4,7 @@ const repository = require('./repository');
const cls = require('./cls');
const log = require('./log');
-function executeNote(note, apiParams) {
+async function executeNote(note, apiParams) {
if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) {
log.info(`Cannot execute note ${note.noteId}`);
@@ -16,16 +16,16 @@ function executeNote(note, apiParams) {
return executeBundle(bundle, apiParams);
}
-function executeNoteNoException(note, apiParams) {
+async function executeNoteNoException(note, apiParams) {
try {
- executeNote(note, apiParams);
+ await executeNote(note, apiParams);
}
catch (e) {
// just swallow, exception is logged already in executeNote
}
}
-function executeBundle(bundle, apiParams = {}) {
+async function executeBundle(bundle, apiParams = {}) {
if (!apiParams.startNote) {
// this is the default case, the only exception is when we want to preserve frontend startNote
apiParams.startNote = bundle.note;
diff --git a/src/services/search/search.js b/src/services/search/search.js
index c684e4bd9a..415df5ed6d 100644
--- a/src/services/search/search.js
+++ b/src/services/search/search.js
@@ -9,6 +9,7 @@ const ParsingContext = require("./parsing_context");
const noteCache = require('../note_cache/note_cache');
const noteCacheService = require('../note_cache/note_cache_service');
const hoistedNoteService = require('../hoisted_note');
+const repository = require('../repository');
const utils = require('../utils');
/**
@@ -87,7 +88,11 @@ function searchNotes(query) {
fuzzyAttributeSearch: false
});
- const allSearchResults = findNotesWithQuery(query, parsingContext);
+ return findNotesWithQuery(query, parsingContext);
+}
+
+function searchTrimmedNotes(query) {
+ const allSearchResults = searchNotes(query);
const trimmedSearchResults = allSearchResults.slice(0, 200);
return {
@@ -174,8 +179,15 @@ function formatAttribute(attr) {
}
}
+function searchNoteEntities(query) {
+ return searchNotes(query)
+ .map(res => repository.getNote(res.noteId));
+}
+
module.exports = {
searchNotes,
+ searchTrimmedNotes,
searchNotesForAutocomplete,
- findNotesWithQuery
+ findNotesWithQuery,
+ searchNoteEntities
};
diff --git a/src/services/sql.js b/src/services/sql.js
index c8ef0acc23..b0c4f51d4e 100644
--- a/src/services/sql.js
+++ b/src/services/sql.js
@@ -224,22 +224,12 @@ function wrap(func, query) {
}
}
-// true if transaction is active globally.
-// cls.namespace.get('isTransactional') OTOH indicates active transaction in active CLS
-let transactionActive = false;
-// resolves when current transaction ends with either COMMIT or ROLLBACK
-let transactionPromise = null;
-let transactionPromiseResolve = null;
-
function startTransactionIfNecessary() {
if (!cls.get('isTransactional')
|| cls.get('isInTransaction')) {
return;
}
- // first set semaphore (atomic operation and only then start transaction
- transactionActive = true;
- transactionPromise = new Promise(res => transactionPromiseResolve = res);
cls.set('isInTransaction', true);
beginTransaction();
@@ -276,10 +266,8 @@ function transactional(func) {
cls.namespace.set('isTransactional', false);
if (cls.namespace.get('isInTransaction')) {
- transactionActive = false;
cls.namespace.set('isInTransaction', false);
// resolving even for rollback since this is just semaphore for allowing another write transaction to proceed
- transactionPromiseResolve();
}
}
}