mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	repository is now stateless
This commit is contained in:
		| @@ -3,10 +3,8 @@ | |||||||
| const utils = require('../services/utils'); | const utils = require('../services/utils'); | ||||||
|  |  | ||||||
| class Entity { | class Entity { | ||||||
|     constructor(repository, row) { |     constructor(row) { | ||||||
|         utils.assertArguments(repository, row); |         utils.assertArguments(row); | ||||||
|  |  | ||||||
|         this.repository = repository; |  | ||||||
|  |  | ||||||
|         for (const key in row) { |         for (const key in row) { | ||||||
|             this[key] = row[key]; |             this[key] = row[key]; | ||||||
|   | |||||||
| @@ -4,17 +4,16 @@ const sql = require('../../services/sql'); | |||||||
| const html = require('html'); | const html = require('html'); | ||||||
| const tar = require('tar-stream'); | const tar = require('tar-stream'); | ||||||
| const sanitize = require("sanitize-filename"); | const sanitize = require("sanitize-filename"); | ||||||
| const Repository = require("../../services/repository"); | const repository = require("../../services/repository"); | ||||||
|  |  | ||||||
| async function exportNote(req, res) { | async function exportNote(req, res) { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const repo = new Repository(req); |  | ||||||
|  |  | ||||||
|     const branchId = await sql.getValue('SELECT branchId FROM branches WHERE noteId = ?', [noteId]); |     const branchId = await sql.getValue('SELECT branchId FROM branches WHERE noteId = ?', [noteId]); | ||||||
|  |  | ||||||
|     const pack = tar.pack(); |     const pack = tar.pack(); | ||||||
|  |  | ||||||
|     const name = await exportNoteInner(branchId, '', pack, repo); |     const name = await exportNoteInner(branchId, '', pack); | ||||||
|  |  | ||||||
|     pack.finalize(); |     pack.finalize(); | ||||||
|  |  | ||||||
| @@ -24,9 +23,9 @@ async function exportNote(req, res) { | |||||||
|     pack.pipe(res); |     pack.pipe(res); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function exportNoteInner(branchId, directory, pack, repo) { | async function exportNoteInner(branchId, directory, pack) { | ||||||
|     const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]); |     const branch = await sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]); | ||||||
|     const note = await repo.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]); |     const note = await repository.getEntity("SELECT notes.* FROM notes WHERE noteId = ?", [branch.noteId]); | ||||||
|  |  | ||||||
|     if (note.isProtected) { |     if (note.isProtected) { | ||||||
|         return; |         return; | ||||||
| @@ -51,7 +50,7 @@ async function exportNoteInner(branchId, directory, pack, repo) { | |||||||
|  |  | ||||||
|     if (children.length > 0) { |     if (children.length > 0) { | ||||||
|         for (const child of children) { |         for (const child of children) { | ||||||
|             await exportNoteInner(child.branchId, childFileName + "/", pack, repo); |             await exportNoteInner(child.branchId, childFileName + "/", pack); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
|  |  | ||||||
| const labels = require('../../services/labels'); | const labels = require('../../services/labels'); | ||||||
| const script = require('../../services/script'); | const script = require('../../services/script'); | ||||||
| const Repository = require('../../services/repository'); | const repository = require('../../services/repository'); | ||||||
|  |  | ||||||
| async function exec(req) { | async function exec(req) { | ||||||
|     const ret = await script.executeScript(req, req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId); |     const ret = await script.executeScript(req, req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId); | ||||||
| @@ -13,7 +13,6 @@ async function exec(req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function run(req) { | async function run(req) { | ||||||
|     const repository = new Repository(req); |  | ||||||
|     const note = await repository.getNote(req.params.noteId); |     const note = await repository.getNote(req.params.noteId); | ||||||
|  |  | ||||||
|     const ret = await script.executeNote(req, note); |     const ret = await script.executeNote(req, note); | ||||||
| @@ -24,8 +23,7 @@ async function run(req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getStartupBundles(req) { | async function getStartupBundles(req) { | ||||||
|     const repository = new Repository(req); |     const notes = await labels.getNotesWithLabel("run", "frontend_startup"); | ||||||
|     const notes = await labels.getNotesWithLabel(repository, "run", "frontend_startup"); |  | ||||||
|  |  | ||||||
|     const bundles = []; |     const bundles = []; | ||||||
|  |  | ||||||
| @@ -41,11 +39,8 @@ async function getStartupBundles(req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getBundle(req) { | async function getBundle(req) { | ||||||
|     const repository = new Repository(req); |  | ||||||
|     const note = await repository.getNote(req.params.noteId); |     const note = await repository.getNote(req.params.noteId); | ||||||
|     const bundle = await script.getScriptBundle(note); |     return await script.getScriptBundle(note); | ||||||
|  |  | ||||||
|     return bundle; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
| @@ -2,22 +2,20 @@ | |||||||
|  |  | ||||||
| const source_id = require('../services/source_id'); | const source_id = require('../services/source_id'); | ||||||
| const sql = require('../services/sql'); | const sql = require('../services/sql'); | ||||||
| const Repository = require('../services/repository'); | const repository = require('../services/repository'); | ||||||
| const labels = require('../services/labels'); | const labels = require('../services/labels'); | ||||||
|  |  | ||||||
| async function index(req, res) { | async function index(req, res) { | ||||||
|     const repository = new Repository(req); |  | ||||||
|  |  | ||||||
|     res.render('index', { |     res.render('index', { | ||||||
|         sourceId: await source_id.generateSourceId(), |         sourceId: await source_id.generateSourceId(), | ||||||
|         maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"), |         maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"), | ||||||
|         appCss: await getAppCss(repository) |         appCss: await getAppCss() | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getAppCss(repository) { | async function getAppCss() { | ||||||
|     let css = ''; |     let css = ''; | ||||||
|     const notes = labels.getNotesWithLabel(repository, 'app_css'); |     const notes = labels.getNotesWithLabel('app_css'); | ||||||
|  |  | ||||||
|     for (const note of await notes) { |     for (const note of await notes) { | ||||||
|         css += `/* ${note.noteId} */ |         css += `/* ${note.noteId} */ | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ | |||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
| const sync_table = require('./sync_table'); | const sync_table = require('./sync_table'); | ||||||
|  | const repository = require('./repository'); | ||||||
|  |  | ||||||
| const BUILTIN_LABELS = [ | const BUILTIN_LABELS = [ | ||||||
|     'frontend_startup', |     'frontend_startup', | ||||||
| @@ -29,7 +30,7 @@ async function getNoteIdWithLabel(name, value) { | |||||||
|                 AND labels.value = ?`, [name, value]); |                 AND labels.value = ?`, [name, value]); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getNotesWithLabel(repository, name, value) { | async function getNotesWithLabel(name, value) { | ||||||
|     let notes; |     let notes; | ||||||
|  |  | ||||||
|     if (value !== undefined) { |     if (value !== undefined) { | ||||||
| @@ -44,8 +45,8 @@ async function getNotesWithLabel(repository, name, value) { | |||||||
|     return notes; |     return notes; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getNoteWithLabel(repository, name, value) { | async function getNoteWithLabel(name, value) { | ||||||
|     const notes = getNotesWithLabel(repository, name, value); |     const notes = getNotesWithLabel(name, value); | ||||||
|  |  | ||||||
|     return notes.length > 0 ? notes[0] : null; |     return notes.length > 0 ? notes[0] : null; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,19 +1,19 @@ | |||||||
|  | "use strict"; | ||||||
|  |  | ||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const protected_session = require('./protected_session'); |  | ||||||
| const Note = require('../entities/note'); | const Note = require('../entities/note'); | ||||||
| const NoteRevision = require('../entities/note_revision'); | const NoteRevision = require('../entities/note_revision'); | ||||||
| const Branch = require('../entities/branch'); | const Branch = require('../entities/branch'); | ||||||
| const Label = require('../entities/label'); | const Label = require('../entities/label'); | ||||||
| const sync_table = require('../services/sync_table'); | const sync_table = require('../services/sync_table'); | ||||||
|  |  | ||||||
| class Repository { | async function getEntities(query, params = []) { | ||||||
|     async getEntities(query, params = []) { |  | ||||||
|     const rows = await sql.getRows(query, params); |     const rows = await sql.getRows(query, params); | ||||||
|  |  | ||||||
|     return rows.map(row => this.createEntityFromRow(row)); |     return rows.map(row => this.createEntityFromRow(row)); | ||||||
| } | } | ||||||
|  |  | ||||||
|     async getEntity(query, params = []) { | async function getEntity(query, params = []) { | ||||||
|     const row = await sql.getRowOrNull(query, params); |     const row = await sql.getRowOrNull(query, params); | ||||||
|  |  | ||||||
|     if (!row) { |     if (!row) { | ||||||
| @@ -23,24 +23,24 @@ class Repository { | |||||||
|     return this.createEntityFromRow(row); |     return this.createEntityFromRow(row); | ||||||
| } | } | ||||||
|  |  | ||||||
|     async getNote(noteId) { | async function getNote(noteId) { | ||||||
|     return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     return await this.getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
| } | } | ||||||
|  |  | ||||||
|     createEntityFromRow(row) { | function createEntityFromRow(row) { | ||||||
|     let entity; |     let entity; | ||||||
|  |  | ||||||
|     if (row.labelId) { |     if (row.labelId) { | ||||||
|             entity = new Label(this, row); |         entity = new Label(row); | ||||||
|     } |     } | ||||||
|     else if (row.noteRevisionId) { |     else if (row.noteRevisionId) { | ||||||
|             entity = new NoteRevision(this, row); |         entity = new NoteRevision(row); | ||||||
|     } |     } | ||||||
|     else if (row.branchId) { |     else if (row.branchId) { | ||||||
|             entity = new Branch(this, row); |         entity = new Branch(row); | ||||||
|     } |     } | ||||||
|     else if (row.noteId) { |     else if (row.noteId) { | ||||||
|             entity = new Note(this, row); |         entity = new Note(row); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         throw new Error('Unknown entity type for row: ' + JSON.stringify(row)); |         throw new Error('Unknown entity type for row: ' + JSON.stringify(row)); | ||||||
| @@ -49,7 +49,7 @@ class Repository { | |||||||
|     return entity; |     return entity; | ||||||
| } | } | ||||||
|  |  | ||||||
|     async updateEntity(entity) { | async function updateEntity(entity) { | ||||||
|     if (entity.beforeSaving) { |     if (entity.beforeSaving) { | ||||||
|         entity.beforeSaving(); |         entity.beforeSaving(); | ||||||
|     } |     } | ||||||
| @@ -57,7 +57,6 @@ class Repository { | |||||||
|     const clone = Object.assign({}, entity); |     const clone = Object.assign({}, entity); | ||||||
|  |  | ||||||
|     delete clone.jsonContent; |     delete clone.jsonContent; | ||||||
|         delete clone.repository; |  | ||||||
|  |  | ||||||
|     await sql.replace(entity.constructor.tableName, clone); |     await sql.replace(entity.constructor.tableName, clone); | ||||||
|  |  | ||||||
| @@ -65,6 +64,10 @@ class Repository { | |||||||
|  |  | ||||||
|     await sync_table.addEntitySync(entity.constructor.tableName, primaryKey); |     await sync_table.addEntitySync(entity.constructor.tableName, primaryKey); | ||||||
| } | } | ||||||
| } |  | ||||||
|  |  | ||||||
| module.exports = Repository; | module.exports = { | ||||||
|  |     getEntities, | ||||||
|  |     getEntity, | ||||||
|  |     getNote, | ||||||
|  |     updateEntity | ||||||
|  | }; | ||||||
| @@ -1,11 +1,9 @@ | |||||||
| const script = require('./script'); | const script = require('./script'); | ||||||
| const Repository = require('./repository'); | const repository = require('./repository'); | ||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
|  |  | ||||||
| const repo = new Repository(); |  | ||||||
|  |  | ||||||
| async function runNotesWithLabel(runAttrValue) { | async function runNotesWithLabel(runAttrValue) { | ||||||
|     const notes = await repo.getEntities(` |     const notes = await repository.getEntities(` | ||||||
|         SELECT notes.*  |         SELECT notes.*  | ||||||
|         FROM notes  |         FROM notes  | ||||||
|           JOIN labels ON labels.noteId = notes.noteId |           JOIN labels ON labels.noteId = notes.noteId | ||||||
| @@ -17,7 +15,7 @@ async function runNotesWithLabel(runAttrValue) { | |||||||
|           AND notes.isDeleted = 0`, [runAttrValue]); |           AND notes.isDeleted = 0`, [runAttrValue]); | ||||||
|  |  | ||||||
|     for (const note of notes) { |     for (const note of notes) { | ||||||
|         script.executeNote(null, note); |         script.executeNote(note); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const ScriptContext = require('./script_context'); | const ScriptContext = require('./script_context'); | ||||||
| const Repository = require('./repository'); | const repository = require('./repository'); | ||||||
|  |  | ||||||
| async function executeNote(note) { | async function executeNote(note) { | ||||||
|     if (!note.isJavaScript()) { |     if (!note.isJavaScript()) { | ||||||
| @@ -36,7 +36,6 @@ async function executeBundle(bundle, startNote) { | |||||||
|  * bundle's startNote. |  * bundle's startNote. | ||||||
|  */ |  */ | ||||||
| async function executeScript(script, params, startNoteId, currentNoteId) { | async function executeScript(script, params, startNoteId, currentNoteId) { | ||||||
|     const repository = new Repository(); |  | ||||||
|     const startNote = await repository.getNote(startNoteId); |     const startNote = await repository.getNote(startNoteId); | ||||||
|     const currentNote = await repository.getNote(currentNoteId); |     const currentNote = await repository.getNote(currentNoteId); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,12 +1,11 @@ | |||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const protected_session = require('./protected_session'); |  | ||||||
| const notes = require('./notes'); | const notes = require('./notes'); | ||||||
| const sql = require('./sql'); | const sql = require('./sql'); | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
| const labels = require('./labels'); | const labels = require('./labels'); | ||||||
| const date_notes = require('./date_notes'); | const date_notes = require('./date_notes'); | ||||||
| const config = require('./config'); | const config = require('./config'); | ||||||
| const Repository = require('./repository'); | const repository = require('./repository'); | ||||||
| const axios = require('axios'); | const axios = require('axios'); | ||||||
|  |  | ||||||
| function ScriptContext(startNote, allNotes) { | function ScriptContext(startNote, allNotes) { | ||||||
| @@ -28,7 +27,6 @@ function ScriptContext(startNote, allNotes) { | |||||||
| } | } | ||||||
|  |  | ||||||
| function ScriptApi(startNote, currentNote) { | function ScriptApi(startNote, currentNote) { | ||||||
|     const repository = new Repository(); |  | ||||||
|     this.startNote = startNote; |     this.startNote = startNote; | ||||||
|     this.currentNote = currentNote; |     this.currentNote = currentNote; | ||||||
|  |  | ||||||
| @@ -47,7 +45,7 @@ function ScriptApi(startNote, currentNote) { | |||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     this.getNotesWithLabel = async function (attrName, attrValue) { |     this.getNotesWithLabel = async function (attrName, attrValue) { | ||||||
|         return await labels.getNotesWithLabel(repository, attrName, attrValue); |         return await labels.getNotesWithLabel(attrName, attrValue); | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     this.getNoteWithLabel = async function (attrName, attrValue) { |     this.getNoteWithLabel = async function (attrName, attrValue) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user