2018-01-14 07:02:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const sync_table = require('./sync_table');
|
2018-01-30 12:17:44 +08:00
|
|
|
const Repository = require('./repository');
|
2018-01-14 04:25:09 +08:00
|
|
|
|
2018-02-14 11:30:33 +08:00
|
|
|
const BUILTIN_ATTRIBUTES = [
|
|
|
|
'run_on_startup',
|
|
|
|
'disable_versioning',
|
|
|
|
'calendar_root',
|
|
|
|
'hide_in_autocomplete'
|
|
|
|
];
|
2018-02-05 08:27:27 +08:00
|
|
|
|
2018-01-24 11:11:03 +08:00
|
|
|
async function getNoteAttributeMap(noteId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
return await sql.getMap(`SELECT name, value FROM attributes WHERE noteId = ?`, [noteId]);
|
2018-01-24 11:11:03 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
async function getNoteIdWithAttribute(name, value) {
|
2018-01-30 06:41:59 +08:00
|
|
|
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)
|
2018-02-07 12:09:19 +08:00
|
|
|
WHERE notes.isDeleted = 0
|
|
|
|
AND attributes.isDeleted = 0
|
|
|
|
AND attributes.name = ?
|
|
|
|
AND attributes.value = ?`, [name, value]);
|
2018-01-14 04:25:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:08:57 +08:00
|
|
|
async function getNotesWithAttribute(dataKey, name, value) {
|
2018-01-30 12:17:44 +08:00
|
|
|
const repository = new Repository(dataKey);
|
|
|
|
|
2018-01-29 01:08:57 +08:00
|
|
|
let notes;
|
|
|
|
|
|
|
|
if (value !== undefined) {
|
2018-01-30 12:17:44 +08:00
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
|
2018-02-07 12:09:19 +08:00
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
|
2018-01-29 01:08:57 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-01-30 12:17:44 +08:00
|
|
|
notes = await repository.getEntities(`SELECT notes.* FROM notes JOIN attributes USING(noteId)
|
2018-02-07 12:09:19 +08:00
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ?`, [name]);
|
2018-01-29 01:08:57 +08:00
|
|
|
}
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-01-29 01:08:57 +08:00
|
|
|
return notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteWithAttribute(dataKey, name, value) {
|
|
|
|
const notes = getNotesWithAttribute(dataKey, name, value);
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-01-29 01:08:57 +08:00
|
|
|
return notes.length > 0 ? notes[0] : null;
|
2018-01-27 12:40:48 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
async function getNoteIdsWithAttribute(name) {
|
2018-01-30 06:41:59 +08:00
|
|
|
return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)
|
2018-02-07 12:09:19 +08:00
|
|
|
WHERE notes.isDeleted = 0 AND attributes.isDeleted = 0 AND attributes.name = ? AND attributes.isDeleted = 0`, [name]);
|
2018-01-26 12:49:03 +08:00
|
|
|
}
|
|
|
|
|
2018-01-14 04:25:09 +08:00
|
|
|
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,
|
2018-01-14 04:25:09 +08:00
|
|
|
name: name,
|
|
|
|
value: value,
|
2018-01-29 08:30:14 +08:00
|
|
|
dateModified: now,
|
2018-02-07 12:09:19 +08:00
|
|
|
dateCreated: now,
|
|
|
|
isDeleted: false
|
2018-01-14 04:25:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
await sync_table.addAttributeSync(attributeId, sourceId);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2018-01-24 11:11:03 +08:00
|
|
|
getNoteAttributeMap,
|
2018-01-14 04:25:09 +08:00
|
|
|
getNoteIdWithAttribute,
|
2018-01-29 01:08:57 +08:00
|
|
|
getNotesWithAttribute,
|
2018-01-27 12:40:48 +08:00
|
|
|
getNoteWithAttribute,
|
2018-01-26 12:49:03 +08:00
|
|
|
getNoteIdsWithAttribute,
|
2018-02-05 08:27:27 +08:00
|
|
|
createAttribute,
|
|
|
|
BUILTIN_ATTRIBUTES
|
2018-01-14 04:25:09 +08:00
|
|
|
};
|