mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	wip
This commit is contained in:
		| @@ -7,7 +7,7 @@ const scriptService = require('../../services/script'); | |||||||
| const searchService = require('../../services/search/search'); | const searchService = require('../../services/search/search'); | ||||||
|  |  | ||||||
| function searchNotes(req) { | function searchNotes(req) { | ||||||
|     const {count, results} = searchService.searchNotes(req.params.searchString); |     const {count, results} = searchService.searchTrimmedNotes(req.params.searchString); | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|         return { |         return { | ||||||
|   | |||||||
| @@ -89,14 +89,9 @@ function route(method, path, middleware, routeHandler, resultHandler, transactio | |||||||
|                 cls.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']); |                 cls.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']); | ||||||
|                 protectedSessionService.setProtectedSessionId(req); |                 protectedSessionService.setProtectedSessionId(req); | ||||||
|  |  | ||||||
|                 if (transactional) { |                 const cb = () => routeHandler(req, res, next); | ||||||
|                     return sql.transactional(() => { |  | ||||||
|                         return routeHandler(req, res, next); |                 return transactional ? sql.transactional(cb) : cb(); | ||||||
|                     }); |  | ||||||
|                 } |  | ||||||
|                 else { |  | ||||||
|                     return routeHandler(req, res, next); |  | ||||||
|                 } |  | ||||||
|             }); |             }); | ||||||
|  |  | ||||||
|             if (resultHandler) { |             if (resultHandler) { | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ const dayjs = require('dayjs'); | |||||||
| const cloningService = require('./cloning'); | const cloningService = require('./cloning'); | ||||||
| const ws = require('./ws.js'); | const ws = require('./ws.js'); | ||||||
| const appInfo = require('./app_info'); | 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. |  * 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 |      * @method | ||||||
|      * @param {string} searchString |      * @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.: |      * 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>} |      * @returns {Promise<Note|null>} | ||||||
|      */ |      */ | ||||||
|     this.searchForNote = searchString => { |     this.searchForNote = searchString => { | ||||||
|         const notes = searchService.searchForNotes(searchString); |         const notes = searchService.searchNoteEntities(searchString); | ||||||
|  |  | ||||||
|         return notes.length > 0 ? notes[0] : null; |         return notes.length > 0 ? notes[0] : null; | ||||||
|     }; |     }; | ||||||
|   | |||||||
| @@ -147,5 +147,4 @@ eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | |||||||
|     noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes()); |     noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes()); | ||||||
| }); | }); | ||||||
|  |  | ||||||
| // FIXME | load(); | ||||||
| // load(); |  | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| const scriptService = require('./script'); | const scriptService = require('./script'); | ||||||
| const repository = require('./repository'); | const repository = require('./repository'); | ||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
| const sqlInit = require('./sql_init'); |  | ||||||
|  |  | ||||||
| function runNotesWithLabel(runAttrValue) { | function runNotesWithLabel(runAttrValue) { | ||||||
|     const notes = repository.getEntities(` |     const notes = repository.getEntities(` | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ const repository = require('./repository'); | |||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
|  |  | ||||||
| function executeNote(note, apiParams) { | async function executeNote(note, apiParams) { | ||||||
|     if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) { |     if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) { | ||||||
|         log.info(`Cannot execute note ${note.noteId}`); |         log.info(`Cannot execute note ${note.noteId}`); | ||||||
|  |  | ||||||
| @@ -16,16 +16,16 @@ function executeNote(note, apiParams) { | |||||||
|     return executeBundle(bundle, apiParams); |     return executeBundle(bundle, apiParams); | ||||||
| } | } | ||||||
|  |  | ||||||
| function executeNoteNoException(note, apiParams) { | async function executeNoteNoException(note, apiParams) { | ||||||
|     try { |     try { | ||||||
|         executeNote(note, apiParams); |         await executeNote(note, apiParams); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         // just swallow, exception is logged already in executeNote |         // just swallow, exception is logged already in executeNote | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| function executeBundle(bundle, apiParams = {}) { | async function executeBundle(bundle, apiParams = {}) { | ||||||
|     if (!apiParams.startNote) { |     if (!apiParams.startNote) { | ||||||
|         // this is the default case, the only exception is when we want to preserve frontend startNote |         // this is the default case, the only exception is when we want to preserve frontend startNote | ||||||
|         apiParams.startNote = bundle.note; |         apiParams.startNote = bundle.note; | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ const ParsingContext = require("./parsing_context"); | |||||||
| const noteCache = require('../note_cache/note_cache'); | const noteCache = require('../note_cache/note_cache'); | ||||||
| const noteCacheService = require('../note_cache/note_cache_service'); | const noteCacheService = require('../note_cache/note_cache_service'); | ||||||
| const hoistedNoteService = require('../hoisted_note'); | const hoistedNoteService = require('../hoisted_note'); | ||||||
|  | const repository = require('../repository'); | ||||||
| const utils = require('../utils'); | const utils = require('../utils'); | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -87,7 +88,11 @@ function searchNotes(query) { | |||||||
|         fuzzyAttributeSearch: false |         fuzzyAttributeSearch: false | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     const allSearchResults = findNotesWithQuery(query, parsingContext); |     return findNotesWithQuery(query, parsingContext); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function searchTrimmedNotes(query) { | ||||||
|  |     const allSearchResults = searchNotes(query); | ||||||
|     const trimmedSearchResults = allSearchResults.slice(0, 200); |     const trimmedSearchResults = allSearchResults.slice(0, 200); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
| @@ -174,8 +179,15 @@ function formatAttribute(attr) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function searchNoteEntities(query) { | ||||||
|  |     return searchNotes(query) | ||||||
|  |         .map(res => repository.getNote(res.noteId)); | ||||||
|  | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     searchNotes, |     searchNotes, | ||||||
|  |     searchTrimmedNotes, | ||||||
|     searchNotesForAutocomplete, |     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() { | function startTransactionIfNecessary() { | ||||||
|     if (!cls.get('isTransactional') |     if (!cls.get('isTransactional') | ||||||
|         || cls.get('isInTransaction')) { |         || cls.get('isInTransaction')) { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // first set semaphore (atomic operation and only then start transaction |  | ||||||
|     transactionActive = true; |  | ||||||
|     transactionPromise = new Promise(res => transactionPromiseResolve = res); |  | ||||||
|     cls.set('isInTransaction', true); |     cls.set('isInTransaction', true); | ||||||
|  |  | ||||||
|     beginTransaction(); |     beginTransaction(); | ||||||
| @@ -276,10 +266,8 @@ function transactional(func) { | |||||||
|         cls.namespace.set('isTransactional', false); |         cls.namespace.set('isTransactional', false); | ||||||
|  |  | ||||||
|         if (cls.namespace.get('isInTransaction')) { |         if (cls.namespace.get('isInTransaction')) { | ||||||
|             transactionActive = false; |  | ||||||
|             cls.namespace.set('isInTransaction', false); |             cls.namespace.set('isInTransaction', false); | ||||||
|             // resolving even for rollback since this is just semaphore for allowing another write transaction to proceed |             // resolving even for rollback since this is just semaphore for allowing another write transaction to proceed | ||||||
|             transactionPromiseResolve(); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user