mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	smaller refactorings (mostly entitization)
This commit is contained in:
		
							
								
								
									
										19
									
								
								src/entities/image.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/entities/image.js
									
									
									
									
									
										Normal 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; | ||||||
| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| const Entity = require('./entity'); | const Entity = require('./entity'); | ||||||
| const repository = require('../services/repository'); | const repository = require('../services/repository'); | ||||||
|  | const utils = require('../services/utils'); | ||||||
|  |  | ||||||
| class NoteImage extends Entity { | class NoteImage extends Entity { | ||||||
|     static get tableName() { return "note_images"; } |     static get tableName() { return "note_images"; } | ||||||
| @@ -10,6 +11,18 @@ class NoteImage extends Entity { | |||||||
|     async getNote() { |     async getNote() { | ||||||
|         return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); |         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; | module.exports = NoteImage; | ||||||
| @@ -4,6 +4,7 @@ const sql = require('../../services/sql'); | |||||||
| const utils = require('../../services/utils'); | const utils = require('../../services/utils'); | ||||||
| const sync_table = require('../../services/sync_table'); | const sync_table = require('../../services/sync_table'); | ||||||
| const log = require('../../services/log'); | const log = require('../../services/log'); | ||||||
|  | const repository = require('../../services/repository'); | ||||||
|  |  | ||||||
| async function cleanupSoftDeletedItems() { | async function cleanupSoftDeletedItems() { | ||||||
|     const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1"); |     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("branches", "branchId"); | ||||||
|     await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId"); |     await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId"); | ||||||
|     await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId"); |     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); |     log.info("Following notes has been completely cleaned from database: " + noteIdsSql); | ||||||
| } | } | ||||||
| @@ -51,10 +55,10 @@ async function cleanupUnusedImages() { | |||||||
|     for (const imageId of unusedImageIds) { |     for (const imageId of unusedImageIds) { | ||||||
|         log.info(`Deleting unused image: ${imageId}`); |         log.info(`Deleting unused image: ${imageId}`); | ||||||
|  |  | ||||||
|         await sql.execute("UPDATE images SET isDeleted = 1, data = null, dateModified = ? WHERE imageId = ?", |         const image = await repository.getImage(imageId); | ||||||
|             [now, imageId]); |         image.isDeleted = true; | ||||||
|  |         image.data = null; | ||||||
|         await sync_table.addImageSync(imageId); |         await image.save(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ const sql = require('../../services/sql'); | |||||||
| const utils = require('../../services/utils'); | const utils = require('../../services/utils'); | ||||||
| const sync_table = require('../../services/sync_table'); | const sync_table = require('../../services/sync_table'); | ||||||
| const tree = require('../../services/tree'); | const tree = require('../../services/tree'); | ||||||
|  | const Branch = require('../../entities/branch'); | ||||||
|  |  | ||||||
| async function cloneNoteToParent(req) { | async function cloneNoteToParent(req) { | ||||||
|     const parentNoteId = req.params.parentNoteId; |     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 maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); | ||||||
|     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | ||||||
|  |  | ||||||
|     const branch = { |     const branch = new Branch({ | ||||||
|         branchId: utils.newBranchId(), |         branchId: utils.newBranchId(), | ||||||
|         noteId: childNoteId, |         noteId: childNoteId, | ||||||
|         parentNoteId: parentNoteId, |         parentNoteId: parentNoteId, | ||||||
| @@ -28,11 +29,9 @@ async function cloneNoteToParent(req) { | |||||||
|         isExpanded: 0, |         isExpanded: 0, | ||||||
|         dateModified: utils.nowDate(), |         dateModified: utils.nowDate(), | ||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }; |     }); | ||||||
|  |  | ||||||
|     await sql.replace("branches", branch); |     await branch.save(); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branch.branchId); |  | ||||||
|  |  | ||||||
|     await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]); |     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); |     await sync_table.addNoteReorderingSync(afterNote.parentNoteId); | ||||||
|  |  | ||||||
|     const branch = { |     const branch = new Branch({ | ||||||
|         branchId: utils.newBranchId(), |         branchId: utils.newBranchId(), | ||||||
|         noteId: noteId, |         noteId: noteId, | ||||||
|         parentNoteId: afterNote.parentNoteId, |         parentNoteId: afterNote.parentNoteId, | ||||||
| @@ -66,11 +65,9 @@ async function cloneNoteAfter(req) { | |||||||
|         isExpanded: 0, |         isExpanded: 0, | ||||||
|         dateModified: utils.nowDate(), |         dateModified: utils.nowDate(), | ||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }; |     }); | ||||||
|  |  | ||||||
|     await sql.replace("branches", branch); |     await branch.save(); | ||||||
|  |  | ||||||
|     await sync_table.addBranchSync(branch.branchId); |  | ||||||
|  |  | ||||||
|     return { success: true }; |     return { success: true }; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -12,9 +12,7 @@ async function deleteOld() { | |||||||
|     const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1"); |     const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1"); | ||||||
|  |  | ||||||
|     if (cutoffId) { |     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]); |  | ||||||
|         }); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -24,8 +24,8 @@ async function exportNote(req, res) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function exportNoteInner(branchId, directory, pack) { | async function exportNoteInner(branchId, directory, pack) { | ||||||
|     const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]); |     const branch = await repository.getBranch(branchId); | ||||||
|     const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]); |     const note = await branch.getNote(); | ||||||
|  |  | ||||||
|     if (note.isProtected) { |     if (note.isProtected) { | ||||||
|         return; |         return; | ||||||
| @@ -46,12 +46,8 @@ async function exportNoteInner(branchId, directory, pack) { | |||||||
|  |  | ||||||
|     pack.entry({ name: childFileName + ".dat", size: content.length }, content); |     pack.entry({ name: childFileName + ".dat", size: content.length }, content); | ||||||
|  |  | ||||||
|     const children = await sql.getRows("SELECT * FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); |     for (const child of await note.getChildBranches()) { | ||||||
|  |         await exportNoteInner(child.branchId, childFileName + "/", pack); | ||||||
|     if (children.length > 0) { |  | ||||||
|         for (const child of children) { |  | ||||||
|             await exportNoteInner(child.branchId, childFileName + "/", pack); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return childFileName; |     return childFileName; | ||||||
|   | |||||||
| @@ -1,14 +1,10 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
|  |  | ||||||
| const sql = require('../../services/sql'); | const repository = require('../../services/repository'); | ||||||
| const protected_session = require('../../services/protected_session'); |  | ||||||
|  |  | ||||||
| async function getNoteRevisions(req) { | async function getNoteRevisions(req) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const revisions = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); |     return await repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); | ||||||
|     protected_session.decryptNoteRevisions(revisions); |  | ||||||
|  |  | ||||||
|     return revisions; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -5,9 +5,9 @@ const options = require('../../services/options'); | |||||||
| const utils = require('../../services/utils'); | const utils = require('../../services/utils'); | ||||||
| const config = require('../../services/config'); | const config = require('../../services/config'); | ||||||
| const protected_session = require('../../services/protected_session'); | 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(` |     const branches = await sql.getRows(` | ||||||
|       SELECT  |       SELECT  | ||||||
|         branchId, |         branchId, | ||||||
| @@ -64,13 +64,9 @@ async function setPrefix(req) { | |||||||
|     const branchId = req.params.branchId; |     const branchId = req.params.branchId; | ||||||
|     const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; |     const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     const branch = await repository.getBranch(branchId); | ||||||
|         await sql.execute("UPDATE branches SET prefix = ?, dateModified = ? WHERE branchId = ?", [prefix, utils.nowDate(), branchId]); |     branch.prefix = prefix; | ||||||
|  |     await branch.save(); | ||||||
|         await sync_table.addBranchSync(branchId); |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     return {}; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -52,7 +52,7 @@ function apiResultHandler(res, result) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     else if (result === undefined) { |     else if (result === undefined) { | ||||||
|         res.status(200).send(); |         res.status(204).send(); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         res.status(200).send(result); |         res.status(200).send(result); | ||||||
|   | |||||||
| @@ -58,7 +58,7 @@ async function createNewNote(parentNoteId, noteOpts) { | |||||||
|         mime: noteOpts.mime ? noteOpts.mime : 'text/html' |         mime: noteOpts.mime ? noteOpts.mime : 'text/html' | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await repository.updateEntity(note); |     await note.save(); | ||||||
|  |  | ||||||
|     const branch = new Branch({ |     const branch = new Branch({ | ||||||
|         branchId: utils.newBranchId(), |         branchId: utils.newBranchId(), | ||||||
| @@ -69,12 +69,13 @@ async function createNewNote(parentNoteId, noteOpts) { | |||||||
|         isDeleted: 0 |         isDeleted: 0 | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     await repository.updateEntity(branch); |     await branch.save(); | ||||||
|  |  | ||||||
|     return { |     return { | ||||||
|         noteId: note.noteId, |         noteId: note.noteId, | ||||||
|  |         note, | ||||||
|         branchId: branch.branchId, |         branchId: branch.branchId, | ||||||
|         note |         branch | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,10 +27,6 @@ function getDataKey() { | |||||||
|     return dataKeyMap[protectedSessionId]; |     return dataKeyMap[protectedSessionId]; | ||||||
| } | } | ||||||
|  |  | ||||||
| function getDataKeyForProtectedSessionId(protectedSessionId) { |  | ||||||
|     return dataKeyMap[protectedSessionId]; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| function isProtectedSessionAvailable(req) { | function isProtectedSessionAvailable(req) { | ||||||
|     const protectedSessionId = getProtectedSessionId(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) { | function encryptNote(note) { | ||||||
|     const dataKey = getDataKey(); |     const dataKey = getDataKey(); | ||||||
|  |  | ||||||
| @@ -107,12 +97,10 @@ function encryptNoteRevision(revision) { | |||||||
| module.exports = { | module.exports = { | ||||||
|     setDataKey, |     setDataKey, | ||||||
|     getDataKey, |     getDataKey, | ||||||
|     getDataKeyForProtectedSessionId, |  | ||||||
|     isProtectedSessionAvailable, |     isProtectedSessionAvailable, | ||||||
|     decryptNote, |     decryptNote, | ||||||
|     decryptNotes, |     decryptNotes, | ||||||
|     decryptNoteRevision, |     decryptNoteRevision, | ||||||
|     decryptNoteRevisions, |  | ||||||
|     encryptNote, |     encryptNote, | ||||||
|     encryptNoteRevision, |     encryptNoteRevision, | ||||||
|     setProtectedSessionId |     setProtectedSessionId | ||||||
|   | |||||||
| @@ -33,6 +33,10 @@ async function getBranch(branchId) { | |||||||
|     return await getEntity("SELECT * FROM branches WHERE branchId = ?", [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) { | async function updateEntity(entity) { | ||||||
|     if (entity.beforeSaving) { |     if (entity.beforeSaving) { | ||||||
|         entity.beforeSaving(); |         entity.beforeSaving(); | ||||||
| @@ -54,6 +58,7 @@ module.exports = { | |||||||
|     getEntity, |     getEntity, | ||||||
|     getNote, |     getNote, | ||||||
|     getBranch, |     getBranch, | ||||||
|  |     getImage, | ||||||
|     updateEntity, |     updateEntity, | ||||||
|     setEntityConstructor |     setEntityConstructor | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user