trilium/src/routes/api/autocomplete.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-04-18 12:26:42 +08:00
"use strict";
2018-06-06 07:12:52 +08:00
const noteCacheService = require('../../services/note_cache');
const repository = require('../../services/repository');
2018-04-18 12:26:42 +08:00
async function getAutocomplete(req) {
const query = req.query.query;
let results;
if (query.trim().length === 0) {
results = await getRecentNotes();
}
else {
results = noteCacheService.findNotes(query);
}
2018-04-18 12:26:42 +08:00
return results.map(res => {
return {
value: res.path,
2018-06-06 10:47:47 +08:00
label: res.title
2018-04-18 12:26:42 +08:00
}
});
}
async function getRecentNotes() {
const recentNotes = await repository.getEntities(`
SELECT
recent_notes.*
FROM
recent_notes
JOIN branches USING(branchId)
WHERE
recent_notes.isDeleted = 0
AND branches.isDeleted = 0
ORDER BY
dateCreated DESC
LIMIT 200`);
return recentNotes.map(rn => {
return {
path: rn.notePath,
title: noteCacheService.getNoteTitleForPath(rn.notePath.split('/'))
};
});
}
2018-04-18 12:26:42 +08:00
module.exports = {
getAutocomplete
};