trilium/services/attributes.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-01-14 07:02:41 +08:00
"use strict";
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
const protected_session = require('./protected_session');
async function getNoteAttributeMap(noteId) {
2018-01-29 08:30:14 +08:00
return await sql.getMap(`SELECT name, value FROM attributes WHERE noteId = ?`, [noteId]);
}
async function getNoteIdWithAttribute(name, value) {
2018-01-29 08:30:14 +08:00
return await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
2018-01-29 01:08:57 +08:00
async function getNotesWithAttribute(dataKey, name, value) {
let notes;
if (value !== undefined) {
2018-01-29 08:30:14 +08:00
notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
2018-01-29 01:08:57 +08:00
}
else {
2018-01-29 08:30:14 +08:00
notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]);
2018-01-29 01:08:57 +08:00
}
2018-01-29 01:08:57 +08:00
for (const note of notes) {
protected_session.decryptNote(dataKey, note);
}
2018-01-29 01:08:57 +08:00
return notes;
}
async function getNoteWithAttribute(dataKey, name, value) {
const notes = getNotesWithAttribute(dataKey, name, value);
2018-01-29 01:08:57 +08:00
return notes.length > 0 ? notes[0] : null;
}
async function getNoteIdsWithAttribute(name) {
2018-01-29 08:30:14 +08:00
return await sql.getFirstColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)
WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]);
}
async function createAttribute(noteId, name, value = null, sourceId = null) {
const now = utils.nowDate();
const attributeId = utils.newAttributeId();
await sql.insert("attributes", {
2018-01-29 08:30:14 +08:00
attributeId: attributeId,
noteId: noteId,
name: name,
value: value,
2018-01-29 08:30:14 +08:00
dateModified: now,
dateCreated: now
});
await sync_table.addAttributeSync(attributeId, sourceId);
}
module.exports = {
getNoteAttributeMap,
getNoteIdWithAttribute,
2018-01-29 01:08:57 +08:00
getNotesWithAttribute,
getNoteWithAttribute,
getNoteIdsWithAttribute,
createAttribute
};