mirror of
https://github.com/zadam/trilium.git
synced 2025-02-25 15:35:43 +08:00
wip
This commit is contained in:
parent
88348c560c
commit
6207203b35
8 changed files with 27 additions and 34 deletions
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -147,5 +147,4 @@ eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
|
|||
noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes());
|
||||
});
|
||||
|
||||
// FIXME
|
||||
// load();
|
||||
load();
|
||||
|
|
|
@ -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(`
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue