script API changes

This commit is contained in:
azivner 2018-01-28 12:08:57 -05:00
parent 4ab763e295
commit 55d8ef7e81
3 changed files with 33 additions and 14 deletions

View File

@ -68,8 +68,6 @@ async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
script += await getSubTreeScripts(child.note_id, includedNoteIds, dataKey); script += await getSubTreeScripts(child.note_id, includedNoteIds, dataKey);
console.log('MIME: ', child.mime);
if (child.mime === 'application/javascript') { if (child.mime === 'application/javascript') {
child.note_text = '<script>' + child.note_text + '</script>'; child.note_text = '<script>' + child.note_text + '</script>';
} }

View File

@ -14,17 +14,29 @@ async function getNoteIdWithAttribute(name, value) {
WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
} }
async function getNoteWithAttribute(dataKey, name, value) { async function getNotesWithAttribute(dataKey, name, value) {
const note = await sql.getFirst(`SELECT notes.* FROM notes JOIN attributes USING(note_id) let notes;
WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
if (!note) { if (value !== undefined) {
return note; notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(note_id)
WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
}
else {
notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(note_id)
WHERE notes.is_deleted = 0 AND attributes.name = ?`, [name]);
} }
protected_session.decryptNote(dataKey, note); for (const note of notes) {
protected_session.decryptNote(dataKey, note);
}
return note; return notes;
}
async function getNoteWithAttribute(dataKey, name, value) {
const notes = getNotesWithAttribute(dataKey, name, value);
return notes.length > 0 ? notes[0] : null;
} }
async function getNoteIdsWithAttribute(name) { async function getNoteIdsWithAttribute(name) {
@ -51,6 +63,7 @@ async function createAttribute(noteId, name, value = null, sourceId = null) {
module.exports = { module.exports = {
getNoteAttributeMap, getNoteAttributeMap,
getNoteIdWithAttribute, getNoteIdWithAttribute,
getNotesWithAttribute,
getNoteWithAttribute, getNoteWithAttribute,
getNoteIdsWithAttribute, getNoteIdsWithAttribute,
createAttribute createAttribute

View File

@ -11,7 +11,7 @@ function ScriptContext(noteId, dataKey) {
this.dataKey = protected_session.getDataKey(dataKey); this.dataKey = protected_session.getDataKey(dataKey);
function deserializePayload(note) { function deserializePayload(note) {
if (note.type === "code" && note.mime === "application/json") { if (note && note.type === "code" && note.mime === "application/json") {
note.payload = JSON.parse(note.note_text); note.payload = JSON.parse(note.note_text);
} }
} }
@ -28,12 +28,20 @@ function ScriptContext(noteId, dataKey) {
return note; return note;
}; };
this.getNotesWithAttribute = async function (attrName, attrValue) {
const notes = await attributes.getNotesWithAttribute(this.dataKey, attrName, attrValue);
for (const note of notes) {
deserializePayload(note);
}
return notes;
};
this.getNoteWithAttribute = async function (attrName, attrValue) { this.getNoteWithAttribute = async function (attrName, attrValue) {
const note = await attributes.getNoteWithAttribute(this.dataKey, attrName, attrValue); const notes = this.getNotesWithAttribute(attrName, attrValue);
deserializePayload(note); return notes.length > 0 ? notes[0] : null;
return note;
}; };
this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) { this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) {