smaller refactorings (mostly entitization)

This commit is contained in:
azivner 2018-04-01 11:05:09 -04:00
parent c9d73c6115
commit fad0ec757b
12 changed files with 69 additions and 56 deletions

19
src/entities/image.js Normal file
View File

@ -0,0 +1,19 @@
"use strict";
const Entity = require('./entity');
const utils = require('../services/utils');
class Image extends Entity {
static get tableName() { return "images"; }
static get primaryKeyName() { return "imageId"; }
beforeSaving() {
if (!this.dateCreated) {
this.dateCreated = utils.nowDate();
}
this.dateModified = utils.nowDate();
}
}
module.exports = Image;

View File

@ -2,6 +2,7 @@
const Entity = require('./entity');
const repository = require('../services/repository');
const utils = require('../services/utils');
class NoteImage extends Entity {
static get tableName() { return "note_images"; }
@ -10,6 +11,18 @@ class NoteImage extends Entity {
async getNote() {
return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
}
async getImage() {
return await repository.getEntity("SELECT * FROM images WHERE imageId = ?", [this.imageId]);
}
beforeSaving() {
if (!this.dateCreated) {
this.dateCreated = utils.nowDate();
}
this.dateModified = utils.nowDate();
}
}
module.exports = NoteImage;

View File

@ -4,6 +4,7 @@ const sql = require('../../services/sql');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const log = require('../../services/log');
const repository = require('../../services/repository');
async function cleanupSoftDeletedItems() {
const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1");
@ -33,6 +34,9 @@ async function cleanupSoftDeletedItems() {
await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");
await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");
await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");
await sync_table.cleanupSyncRowsForMissingEntities("images", "imageId");
await sync_table.cleanupSyncRowsForMissingEntities("note_images", "noteImageId");
await sync_table.cleanupSyncRowsForMissingEntities("labels", "labelId");
log.info("Following notes has been completely cleaned from database: " + noteIdsSql);
}
@ -51,10 +55,10 @@ async function cleanupUnusedImages() {
for (const imageId of unusedImageIds) {
log.info(`Deleting unused image: ${imageId}`);
await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?",
[now, imageId]);
await sync_table.addImageSync(imageId);
const image = await repository.getImage(imageId);
image.isDeleted = true;
image.data = null;
await image.save();
}
}

View File

@ -4,6 +4,7 @@ const sql = require('../../services/sql');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const tree = require('../../services/tree');
const Branch = require('../../entities/branch');
async function cloneNoteToParent(req) {
const parentNoteId = req.params.parentNoteId;
@ -19,7 +20,7 @@ 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 = {
const branch = new Branch({
branchId: utils.newBranchId(),
noteId: childNoteId,
parentNoteId: parentNoteId,
@ -28,11 +29,9 @@ async function cloneNoteToParent(req) {
isExpanded: 0,
dateModified: utils.nowDate(),
isDeleted: 0
};
});
await sql.replace("branches", branch);
await sync_table.addBranchSync(branch.branchId);
await branch.save();
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
@ -58,7 +57,7 @@ async function cloneNoteAfter(req) {
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
const branch = {
const branch = new Branch({
branchId: utils.newBranchId(),
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
@ -66,11 +65,9 @@ async function cloneNoteAfter(req) {
isExpanded: 0,
dateModified: utils.nowDate(),
isDeleted: 0
};
});
await sql.replace("branches", branch);
await sync_table.addBranchSync(branch.branchId);
await branch.save();
return { success: true };
}

View File

@ -12,9 +12,7 @@ async function deleteOld() {
const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1");
if (cutoffId) {
await sql.doInTransaction(async () => {
await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]);
});
await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]);
}
}

View File

@ -24,8 +24,8 @@ async function exportNote(req, res) {
}
async function exportNoteInner(branchId, directory, pack) {
const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]);
const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]);
const branch = await repository.getBranch(branchId);
const note = await branch.getNote();
if (note.isProtected) {
return;
@ -46,12 +46,8 @@ async function exportNoteInner(branchId, directory, pack) {
pack.entry({ name: childFileName + ".dat", size: content.length }, content);
const children = await sql.getRows("SELECT * FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]);
if (children.length > 0) {
for (const child of children) {
await exportNoteInner(child.branchId, childFileName + "/", pack);
}
for (const child of await note.getChildBranches()) {
await exportNoteInner(child.branchId, childFileName + "/", pack);
}
return childFileName;

View File

@ -1,14 +1,10 @@
"use strict";
const sql = require('../../services/sql');
const protected_session = require('../../services/protected_session');
const repository = require('../../services/repository');
async function getNoteRevisions(req) {
const noteId = req.params.noteId;
const revisions = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);
protected_session.decryptNoteRevisions(revisions);
return revisions;
return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]);
}
module.exports = {

View File

@ -5,9 +5,9 @@ const options = require('../../services/options');
const utils = require('../../services/utils');
const config = require('../../services/config');
const protected_session = require('../../services/protected_session');
const sync_table = require('../../services/sync_table');
const repository = require('../../services/repository');
async function getTree(req) {
async function getTree() {
const branches = await sql.getRows(`
SELECT
branchId,
@ -64,13 +64,9 @@ async function setPrefix(req) {
const branchId = req.params.branchId;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
await sql.doInTransaction(async () => {
await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]);
await sync_table.addBranchSync(branchId);
});
return {};
const branch = await repository.getBranch(branchId);
branch.prefix = prefix;
await branch.save();
}
module.exports = {

View File

@ -52,7 +52,7 @@ function apiResultHandler(res, result) {
}
}
else if (result === undefined) {
res.status(200).send();
res.status(204).send();
}
else {
res.status(200).send(result);

View File

@ -58,7 +58,7 @@ async function createNewNote(parentNoteId, noteOpts) {
mime: noteOpts.mime ? noteOpts.mime : 'text/html'
});
await repository.updateEntity(note);
await note.save();
const branch = new Branch({
branchId: utils.newBranchId(),
@ -69,12 +69,13 @@ async function createNewNote(parentNoteId, noteOpts) {
isDeleted: 0
});
await repository.updateEntity(branch);
await branch.save();
return {
noteId: note.noteId,
note,
branchId: branch.branchId,
note
branch
};
}

View File

@ -27,10 +27,6 @@ function getDataKey() {
return dataKeyMap[protectedSessionId];
}
function getDataKeyForProtectedSessionId(protectedSessionId) {
return dataKeyMap[protectedSessionId];
}
function isProtectedSessionAvailable(req) {
const protectedSessionId = getProtectedSessionId(req);
@ -84,12 +80,6 @@ function decryptNoteRevision(hist) {
}
}
function decryptNoteRevisions(noteRevisions) {
for (const revision of noteRevisions) {
decryptNoteRevision(revision);
}
}
function encryptNote(note) {
const dataKey = getDataKey();
@ -107,12 +97,10 @@ function encryptNoteRevision(revision) {
module.exports = {
setDataKey,
getDataKey,
getDataKeyForProtectedSessionId,
isProtectedSessionAvailable,
decryptNote,
decryptNotes,
decryptNoteRevision,
decryptNoteRevisions,
encryptNote,
encryptNoteRevision,
setProtectedSessionId

View File

@ -33,6 +33,10 @@ async function getBranch(branchId) {
return await getEntity("SELECT * FROM branches WHERE branchId = ?", [branchId]);
}
async function getImage(imageId) {
return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
}
async function updateEntity(entity) {
if (entity.beforeSaving) {
entity.beforeSaving();
@ -54,6 +58,7 @@ module.exports = {
getEntity,
getNote,
getBranch,
getImage,
updateEntity,
setEntityConstructor
};