smaller refactorings

This commit is contained in:
azivner 2018-04-02 22:53:01 -04:00
parent a4e64350e9
commit 42dd8d4754
11 changed files with 32 additions and 69 deletions

View File

@ -18,6 +18,8 @@ class Entity {
async save() {
await repository.updateEntity(this);
return this;
}
}

View File

@ -19,15 +19,13 @@ async function cloneNoteToParent(req) {
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const branch = new Branch({
const branch = await new Branch({
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
notePosition: newNotePos,
isExpanded: 0
});
await branch.save();
}).save();
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
@ -53,14 +51,12 @@ async function cloneNoteAfter(req) {
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
const branch = new Branch({
const branch = await new Branch({
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
notePosition: afterNote.notePosition + 1,
isExpanded: 0
});
await branch.save();
}).save();
return { success: true };
}

View File

@ -24,14 +24,12 @@ async function addRecentNote(req) {
const branchId = req.params.branchId;
const notePath = req.params.notePath;
const recentNote = new RecentNote({
await new RecentNote({
branchId: branchId,
notePath: notePath,
dateAccessed: dateUtils.nowDate(),
isDeleted: 0
});
await recentNote.save();
}).save();
await optionService.setOption('startNotePath', notePath);

View File

@ -20,8 +20,9 @@ async function login(req) {
return [401, "Incorrect username/password"];
}
const apiToken = new ApiToken({ token: utils.randomSecureToken() });
await apiToken.save();
const apiToken = await new ApiToken({
token: utils.randomSecureToken()
}).save();
return {
token: apiToken.token

View File

@ -2,7 +2,6 @@
const sql = require('../../services/sql');
const optionService = require('../../services/options');
const config = require('../../services/config');
const protectedSessionService = require('../../services/protected_session');
async function getTree() {

View File

@ -10,7 +10,7 @@ async function anonymize() {
fs.mkdirSync(dataDir.ANONYMIZED_DB_DIR, 0o700);
}
const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "backup-" + dateUtils.getDateTimeForFile() + ".db";
const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "anonymized-" + dateUtils.getDateTimeForFile() + ".db";
fs.copySync(dataDir.DOCUMENT_PATH, anonymizedFile);

View File

@ -20,21 +20,17 @@ async function saveImage(file, noteId) {
const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext);
const image = new Image({
const image = await new Image({
format: imageFormat.ext,
name: fileName,
checksum: utils.hash(optimizedImage),
data: optimizedImage
});
}).save();
await image.save();
const noteImage = new NoteImage({
await new NoteImage({
noteId: noteId,
imageId: image.imageId
});
await noteImage.save();
}).save();
return {fileName, imageId: image.imageId};
}

View File

@ -15,10 +15,6 @@ const BUILTIN_LABELS = [
'appCss'
];
async function getNoteLabelMap(noteId) {
return await sql.getMap(`SELECT name, value FROM labels WHERE noteId = ? AND isDeleted = 0`, [noteId]);
}
async function getNoteIdWithLabel(name, value) {
return await sql.getValue(`SELECT notes.noteId FROM notes JOIN labels USING(noteId)
WHERE notes.isDeleted = 0
@ -54,19 +50,14 @@ async function getNoteIdsWithLabel(name) {
}
async function createLabel(noteId, name, value = "") {
const label = new Label({
return await new Label({
noteId: noteId,
name: name,
value: value
});
await label.save();
return label;
}).save();
}
module.exports = {
getNoteLabelMap,
getNoteIdWithLabel,
getNotesWithLabel,
getNoteWithLabel,

View File

@ -44,24 +44,20 @@ async function createNewNote(parentNoteId, noteData) {
noteData.mime = noteData.mime || parent.mime;
}
const note = new Note({
const note = await new Note({
title: noteData.title,
content: noteData.content || '',
isProtected: noteData.isProtected,
type: noteData.type || 'text',
mime: noteData.mime || 'text/html'
});
}).save();
await note.save();
const branch = new Branch({
const branch = await new Branch({
noteId: note.noteId,
parentNoteId: parentNoteId,
notePosition: newNotePos,
isExpanded: 0
});
await branch.save();
}).save();
return {
note,
@ -141,10 +137,10 @@ async function saveNoteImages(note) {
const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId);
if (!existingNoteImage) {
await (new NoteImage({
await new NoteImage({
noteId: note.noteId,
imageId: imageId
})).save();
}).save();
}
// else we don't need to do anything
@ -179,7 +175,7 @@ async function saveNoteRevision(note) {
&& !existingnoteRevisionId
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
await (new NoteRevision({
await new NoteRevision({
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
@ -187,7 +183,7 @@ async function saveNoteRevision(note) {
isProtected: 0, // will be fixed in the protectNoteRevisions() call
dateModifiedFrom: note.dateModified,
dateModifiedTo: dateUtils.nowDate()
})).save();
}).save();
}
}

View File

@ -4,12 +4,8 @@ const dateUtils = require('./date_utils');
const syncTableService = require('./sync_table');
const appInfo = require('./app_info');
async function getOptionOrNull(name) {
return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
}
async function getOption(name) {
const row = await getOptionOrNull(name);
const row = await await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
if (!row) {
throw new Error("Option " + name + " doesn't exist");
@ -69,8 +65,6 @@ async function initOptions(startNotePath) {
module.exports = {
getOption,
getOptionOrNull,
setOption,
initOptions,
createOption
initOptions
};

View File

@ -48,21 +48,11 @@ function ScriptApi(startNote, currentNote) {
this.getEntity = repository.getEntity;
this.getEntities = repository.getEntities;
this.getNotesWithLabel = async function (labelName, labelValue) {
return await labelService.getNotesWithLabel(labelName, labelValue);
};
this.getNoteWithLabel = async function (labelName, labelValue) {
const notes = await this.getNotesWithLabel(labelName, labelValue);
return notes.length > 0 ? notes[0] : null;
};
this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {
return await noteService.createNote(parentNoteId, title, content, extraOptions);
};
this.createLabel = labelService.createLabel;
this.getNotesWithLabel = labelService.getNotesWithLabel;
this.getNoteWithLabel = labelService.getNoteWithLabel;
this.createNote = noteService.createNote;
this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);