mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	renaming of sql methods to fit getRows/getEntities model
This commit is contained in:
		| @@ -17,7 +17,7 @@ module.exports = async () => { | |||||||
|     const password = await question("Enter password: "); |     const password = await question("Enter password: "); | ||||||
|     const dataKey = await password_encryption.getDecryptedDataKey(password); |     const dataKey = await password_encryption.getDecryptedDataKey(password); | ||||||
|  |  | ||||||
|     const protectedNotes = await sql.getAll("SELECT * FROM notes WHERE is_protected = 1"); |     const protectedNotes = await sql.getRows("SELECT * FROM notes WHERE is_protected = 1"); | ||||||
|  |  | ||||||
|     for (const note of protectedNotes) { |     for (const note of protectedNotes) { | ||||||
|         const decryptedTitle = data_encryption.decrypt(dataKey, note.note_title); |         const decryptedTitle = data_encryption.decrypt(dataKey, note.note_title); | ||||||
| @@ -30,7 +30,7 @@ module.exports = async () => { | |||||||
|         await sql.execute("UPDATE notes SET note_title = ?, note_text = ? WHERE note_id = ?", [note.note_title, note.note_text, note.note_id]); |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ? WHERE note_id = ?", [note.note_title, note.note_text, note.note_id]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const protectedNotesHistory = await sql.getAll("SELECT * FROM notes_history WHERE is_protected = 1"); |     const protectedNotesHistory = await sql.getRows("SELECT * FROM notes_history WHERE is_protected = 1"); | ||||||
|  |  | ||||||
|     for (const noteHistory of protectedNotesHistory) { |     for (const noteHistory of protectedNotesHistory) { | ||||||
|         const decryptedTitle = data_encryption.decrypt(dataKey, noteHistory.note_title); |         const decryptedTitle = data_encryption.decrypt(dataKey, noteHistory.note_title); | ||||||
|   | |||||||
| @@ -10,7 +10,11 @@ class Note { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     async attributes() { |     async attributes() { | ||||||
|         return this.sql.getAll("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]); |         return this.sql.getRows("SELECT * FROM attributes WHERE noteId = ?", [this.noteId]); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async revisions() { | ||||||
|  |         return this.sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
| router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|     res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId])); |     res.send(await sql.getRows("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { | router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
| @@ -42,7 +42,7 @@ router.put('/:noteId/attributes', auth.checkApiAuth, wrap(async (req, res, next) | |||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     res.send(await sql.getAll("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId])); |     res.send(await sql.getRows("SELECT * FROM attributes WHERE noteId = ? ORDER BY dateCreated", [noteId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| module.exports = router; | module.exports = router; | ||||||
| @@ -11,7 +11,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
|  |  | ||||||
| router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => { | router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         const noteIdsToDelete = await sql.getFirstColumn("SELECT noteId FROM notes WHERE isDeleted = 1"); |         const noteIdsToDelete = await sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1"); | ||||||
|         const noteIdsSql = noteIdsToDelete |         const noteIdsSql = noteIdsToDelete | ||||||
|             .map(noteId => "'" + utils.sanitizeSql(noteId) + "'") |             .map(noteId => "'" + utils.sanitizeSql(noteId) + "'") | ||||||
|             .join(', '); |             .join(', '); | ||||||
| @@ -49,7 +49,7 @@ router.post('/cleanup-unused-images', auth.checkApiAuth, wrap(async (req, res, n | |||||||
|     const sourceId = req.headers.source_id; |     const sourceId = req.headers.source_id; | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         const unusedImageIds = await sql.getFirstColumn(` |         const unusedImageIds = await sql.getColumn(` | ||||||
|           SELECT images.imageId  |           SELECT images.imageId  | ||||||
|           FROM images  |           FROM images  | ||||||
|             LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0 |             LEFT JOIN note_images ON note_images.imageId = images.imageId AND note_images.isDeleted = 0 | ||||||
|   | |||||||
| @@ -19,7 +19,7 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async | |||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); |     const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); | ||||||
|     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|   | |||||||
| @@ -9,13 +9,13 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
| router.get('', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     await deleteOld(); |     await deleteOld(); | ||||||
|  |  | ||||||
|     const result = await sql.getAll("SELECT * FROM event_log ORDER BY dateAdded DESC"); |     const result = await sql.getRows("SELECT * FROM event_log ORDER BY dateAdded DESC"); | ||||||
|  |  | ||||||
|     res.send(result); |     res.send(result); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| async function deleteOld() { | async function deleteOld() { | ||||||
|     const cutoffId = await sql.getFirstValue("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.doInTransaction(async () => { | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, ne | |||||||
|  |  | ||||||
|     fs.mkdirSync(completeExportDir); |     fs.mkdirSync(completeExportDir); | ||||||
|  |  | ||||||
|     const noteTreeId = await sql.getFirstValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]); |     const noteTreeId = await sql.getValue('SELECT noteTreeId FROM note_tree WHERE noteId = ?', [noteId]); | ||||||
|  |  | ||||||
|     await exportNote(noteTreeId, completeExportDir); |     await exportNote(noteTreeId, completeExportDir); | ||||||
|  |  | ||||||
| @@ -34,14 +34,14 @@ router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, ne | |||||||
| })); | })); | ||||||
|  |  | ||||||
| async function exportNote(noteTreeId, dir) { | async function exportNote(noteTreeId, dir) { | ||||||
|     const noteTree = await sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); |     const noteTree = await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | ||||||
|     const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]); |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteTree.noteId]); | ||||||
|  |  | ||||||
|     const pos = (noteTree.notePosition + '').padStart(4, '0'); |     const pos = (noteTree.notePosition + '').padStart(4, '0'); | ||||||
|  |  | ||||||
|     fs.writeFileSync(dir + '/' + pos + '-' + note.title + '.html', html.prettyPrint(note.content, {indent_size: 2})); |     fs.writeFileSync(dir + '/' + pos + '-' + note.title + '.html', html.prettyPrint(note.content, {indent_size: 2})); | ||||||
|  |  | ||||||
|     const children = await sql.getAll("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); |     const children = await sql.getRows("SELECT * FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [note.noteId]); | ||||||
|  |  | ||||||
|     if (children.length > 0) { |     if (children.length > 0) { | ||||||
|         const childrenDir = dir + '/' + pos + '-' + note.title; |         const childrenDir = dir + '/' + pos + '-' + note.title; | ||||||
|   | |||||||
| @@ -19,7 +19,7 @@ const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | |||||||
| const fs = require('fs'); | const fs = require('fs'); | ||||||
|  |  | ||||||
| router.get('/:imageId/:filename', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => { | router.get('/:imageId/:filename', auth.checkApiAuthOrElectron, wrap(async (req, res, next) => { | ||||||
|     const image = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]); |     const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]); | ||||||
|  |  | ||||||
|     if (!image) { |     if (!image) { | ||||||
|         return res.status(404).send({}); |         return res.status(404).send({}); | ||||||
| @@ -39,7 +39,7 @@ router.post('', auth.checkApiAuthOrElectron, multer.single('upload'), wrap(async | |||||||
|     const noteId = req.query.noteId; |     const noteId = req.query.noteId; | ||||||
|     const file = req.file; |     const file = req.file; | ||||||
|  |  | ||||||
|     const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     if (!note) { |     if (!note) { | ||||||
|         return res.status(404).send(`Note ${noteId} doesn't exist.`); |         return res.status(404).send(`Note ${noteId} doesn't exist.`); | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, wrap(async (req, r | |||||||
| })); | })); | ||||||
|  |  | ||||||
| async function importNotes(dir, parentNoteId) { | async function importNotes(dir, parentNoteId) { | ||||||
|     const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); |     const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); | ||||||
|  |  | ||||||
|     if (!parent) { |     if (!parent) { | ||||||
|         return; |         return; | ||||||
| @@ -52,7 +52,7 @@ async function importNotes(dir, parentNoteId) { | |||||||
|             noteTitle = match[2]; |             noteTitle = match[2]; | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             let maxPos = await sql.getFirstValue("SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]); |             let maxPos = await sql.getValue("SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]); | ||||||
|             if (maxPos) { |             if (maxPos) { | ||||||
|                 notePos = maxPos + 1; |                 notePos = maxPos + 1; | ||||||
|             } |             } | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
|  |  | ||||||
| router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|     const history = await sql.getAll("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); |     const history = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? order by dateModifiedTo desc", [noteId]); | ||||||
|     protected_session.decryptNoteHistoryRows(req, history); |     protected_session.decryptNoteHistoryRows(req, history); | ||||||
|  |  | ||||||
|     res.send(history); |     res.send(history); | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
| router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|     const detail = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const detail = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     if (!detail) { |     if (!detail) { | ||||||
|         log.info("Note " + noteId + " has not been found."); |         log.info("Note " + noteId + " has not been found."); | ||||||
| @@ -61,7 +61,7 @@ router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { | |||||||
|     const search = '%' + utils.sanitizeSql(req.query.search) + '%'; |     const search = '%' + utils.sanitizeSql(req.query.search) + '%'; | ||||||
|  |  | ||||||
|     // searching in protected notes is pointless because of encryption |     // searching in protected notes is pointless because of encryption | ||||||
|     const noteIds = await sql.getFirstColumn(`SELECT noteId FROM notes  |     const noteIds = await sql.getColumn(`SELECT noteId FROM notes  | ||||||
|               WHERE isDeleted = 0 AND isProtected = 0 AND (title LIKE ? OR content LIKE ?)`, [search, search]); |               WHERE isDeleted = 0 AND isProtected = 0 AND (title LIKE ? OR content LIKE ?)`, [search, search]); | ||||||
|  |  | ||||||
|     res.send(noteIds); |     res.send(noteIds); | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ const auth = require('../../services/auth'); | |||||||
| const wrap = require('express-promise-wrap').wrap; | const wrap = require('express-promise-wrap').wrap; | ||||||
|  |  | ||||||
| router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const recentChanges = await sql.getAll( |     const recentChanges = await sql.getRows( | ||||||
|         `SELECT  |         `SELECT  | ||||||
|             notes.isDeleted AS current_isDeleted, |             notes.isDeleted AS current_isDeleted, | ||||||
|             notes.title AS current_title, |             notes.title AS current_title, | ||||||
|   | |||||||
| @@ -35,7 +35,7 @@ router.put('/:noteTreeId/:notePath', auth.checkApiAuth, wrap(async (req, res, ne | |||||||
| })); | })); | ||||||
|  |  | ||||||
| async function getRecentNotes() { | async function getRecentNotes() { | ||||||
|     return await sql.getAll(` |     return await sql.getRows(` | ||||||
|       SELECT  |       SELECT  | ||||||
|         recent_notes.*  |         recent_notes.*  | ||||||
|       FROM  |       FROM  | ||||||
|   | |||||||
| @@ -49,7 +49,7 @@ async function getNoteWithSubtreeScript(noteId, req) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getSubTreeScripts(parentId, includedNoteIds, dataKey) { | async function getSubTreeScripts(parentId, includedNoteIds, dataKey) { | ||||||
|     const children = await sql.getAll(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime  |     const children = await sql.getRows(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime  | ||||||
|                                      FROM notes JOIN note_tree USING(noteId) |                                      FROM notes JOIN note_tree USING(noteId) | ||||||
|                                      WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0 |                                      WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0 | ||||||
|                                            AND note_tree.parentNoteId = ? AND notes.type = 'code' |                                            AND note_tree.parentNoteId = ? AND notes.type = 'code' | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ router.post('/execute', auth.checkApiAuth, wrap(async (req, res, next) => { | |||||||
|     try { |     try { | ||||||
|         res.send({ |         res.send({ | ||||||
|             success: true, |             success: true, | ||||||
|             rows: await sql.getAll(query) |             rows: await sql.getRows(query) | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
| router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     res.send({ |     res.send({ | ||||||
|         'hashes': await content_hash.getHashes(), |         'hashes': await content_hash.getHashes(), | ||||||
|         'max_sync_id': await sql.getFirstValue('SELECT MAX(id) FROM sync') |         'max_sync_id': await sql.getValue('SELECT MAX(id) FROM sync') | ||||||
|     }); |     }); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| @@ -53,12 +53,12 @@ router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res, | |||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         await sync_table.addNoteSync(noteId); |         await sync_table.addNoteSync(noteId); | ||||||
|  |  | ||||||
|         for (const noteTreeId of await sql.getFirstColumn("SELECT noteTreeId FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [noteId])) { |         for (const noteTreeId of await sql.getColumn("SELECT noteTreeId FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [noteId])) { | ||||||
|             await sync_table.addNoteTreeSync(noteTreeId); |             await sync_table.addNoteTreeSync(noteTreeId); | ||||||
|             await sync_table.addRecentNoteSync(noteTreeId); |             await sync_table.addRecentNoteSync(noteTreeId); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         for (const noteRevisionId of await sql.getFirstColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) { |         for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) { | ||||||
|             await sync_table.addNoteHistorySync(noteRevisionId); |             await sync_table.addNoteHistorySync(noteRevisionId); | ||||||
|         } |         } | ||||||
|     }); |     }); | ||||||
| @@ -74,32 +74,32 @@ router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res, | |||||||
| router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const lastSyncId = parseInt(req.query.lastSyncId); |     const lastSyncId = parseInt(req.query.lastSyncId); | ||||||
|  |  | ||||||
|     res.send(await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSyncId])); |     res.send(await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSyncId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteId = req.params.noteId; |     const noteId = req.params.noteId; | ||||||
|  |  | ||||||
|     res.send({ |     res.send({ | ||||||
|         entity: await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]) |         entity: await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]) | ||||||
|     }); |     }); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/note_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/note_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteTreeId = req.params.noteTreeId; |     const noteTreeId = req.params.noteTreeId; | ||||||
|  |  | ||||||
|     res.send(await sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId])); |     res.send(await sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/note_revisions/:noteRevisionId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/note_revisions/:noteRevisionId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteRevisionId = req.params.noteRevisionId; |     const noteRevisionId = req.params.noteRevisionId; | ||||||
|  |  | ||||||
|     res.send(await sql.getFirst("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId])); |     res.send(await sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/options/:name', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/options/:name', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const name = req.params.name; |     const name = req.params.name; | ||||||
|     const opt = await sql.getFirst("SELECT * FROM options WHERE name = ?", [name]); |     const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]); | ||||||
|  |  | ||||||
|     if (!opt.isSynced) { |     if (!opt.isSynced) { | ||||||
|         res.send("This option can't be synced."); |         res.send("This option can't be synced."); | ||||||
| @@ -121,12 +121,12 @@ router.get('/note_reordering/:parentNoteId', auth.checkApiAuth, wrap(async (req, | |||||||
| router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteTreeId = req.params.noteTreeId; |     const noteTreeId = req.params.noteTreeId; | ||||||
|  |  | ||||||
|     res.send(await sql.getFirst("SELECT * FROM recent_notes WHERE noteTreeId = ?", [noteTreeId])); |     res.send(await sql.getRow("SELECT * FROM recent_notes WHERE noteTreeId = ?", [noteTreeId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const imageId = req.params.imageId; |     const imageId = req.params.imageId; | ||||||
|     const entity = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [imageId]); |     const entity = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [imageId]); | ||||||
|  |  | ||||||
|     if (entity && entity.data !== null) { |     if (entity && entity.data !== null) { | ||||||
|         entity.data = entity.data.toString('base64'); |         entity.data = entity.data.toString('base64'); | ||||||
| @@ -138,13 +138,13 @@ router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => | |||||||
| router.get('/note_images/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/note_images/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const noteImageId = req.params.noteImageId; |     const noteImageId = req.params.noteImageId; | ||||||
|  |  | ||||||
|     res.send(await sql.getFirst("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId])); |     res.send(await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [noteImageId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const attributeId = req.params.attributeId; |     const attributeId = req.params.attributeId; | ||||||
|  |  | ||||||
|     res.send(await sql.getFirst("SELECT * FROM attributes WHERE attributeId = ?", [attributeId])); |     res.send(await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [attributeId])); | ||||||
| })); | })); | ||||||
|  |  | ||||||
| router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => { | router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ const sync_table = require('../../services/sync_table'); | |||||||
| const wrap = require('express-promise-wrap').wrap; | const wrap = require('express-promise-wrap').wrap; | ||||||
|  |  | ||||||
| router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { | router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { | ||||||
|     const notes = await sql.getAll(` |     const notes = await sql.getRows(` | ||||||
|       SELECT  |       SELECT  | ||||||
|         note_tree.*,  |         note_tree.*,  | ||||||
|         notes.title,  |         notes.title,  | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async ( | |||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); |     const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); | ||||||
|     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; |     const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | ||||||
|  |  | ||||||
|     const now = utils.nowDate(); |     const now = utils.nowDate(); | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ const wrap = require('express-promise-wrap').wrap; | |||||||
| router.get('', auth.checkAuth, wrap(async (req, res, next) => { | router.get('', auth.checkAuth, wrap(async (req, res, next) => { | ||||||
|     res.render('index', { |     res.render('index', { | ||||||
|         sourceId: await source_id.generateSourceId(), |         sourceId: await source_id.generateSourceId(), | ||||||
|         maxSyncIdAtLoad: await sql.getFirstValue("SELECT MAX(id) FROM sync") |         maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync") | ||||||
|     }); |     }); | ||||||
| })); | })); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -10,7 +10,7 @@ async function getNoteAttributeMap(noteId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getNoteIdWithAttribute(name, value) { | async function getNoteIdWithAttribute(name, value) { | ||||||
|     return await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)  |     return await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); |           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -18,11 +18,11 @@ async function getNotesWithAttribute(dataKey, name, value) { | |||||||
|     let notes; |     let notes; | ||||||
|  |  | ||||||
|     if (value !== undefined) { |     if (value !== undefined) { | ||||||
|         notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  |         notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); |           WHERE notes.isDeleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  |         notes = await sql.getRows(`SELECT notes.* FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); |           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -40,7 +40,7 @@ async function getNoteWithAttribute(dataKey, name, value) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getNoteIdsWithAttribute(name) { | async function getNoteIdsWithAttribute(name) { | ||||||
|     return await sql.getFirstColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)  |     return await sql.getColumn(`SELECT DISTINCT notes.noteId FROM notes JOIN attributes USING(noteId)  | ||||||
|           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); |           WHERE notes.isDeleted = 0 AND attributes.name = ?`, [name]); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,7 +9,7 @@ const utils = require('./utils'); | |||||||
| async function runCheck(query, errorText, errorList) { | async function runCheck(query, errorText, errorList) { | ||||||
|     utils.assertArguments(query, errorText, errorList); |     utils.assertArguments(query, errorText, errorList); | ||||||
|  |  | ||||||
|     const result = await sql.getFirstColumn(query); |     const result = await sql.getColumn(query); | ||||||
|  |  | ||||||
|     if (result.length > 0) { |     if (result.length > 0) { | ||||||
|         const resultText = result.map(val => "'" + val + "'").join(', '); |         const resultText = result.map(val => "'" + val + "'").join(', '); | ||||||
| @@ -23,7 +23,7 @@ async function runCheck(query, errorText, errorList) { | |||||||
|  |  | ||||||
| async function checkTreeCycles(errorList) { | async function checkTreeCycles(errorList) { | ||||||
|     const childToParents = {}; |     const childToParents = {}; | ||||||
|     const rows = await sql.getAll("SELECT noteId, parentNoteId FROM note_tree WHERE isDeleted = 0"); |     const rows = await sql.getRows("SELECT noteId, parentNoteId FROM note_tree WHERE isDeleted = 0"); | ||||||
|  |  | ||||||
|     for (const row of rows) { |     for (const row of rows) { | ||||||
|         const childNoteId = row.noteId; |         const childNoteId = row.noteId; | ||||||
|   | |||||||
| @@ -17,7 +17,7 @@ async function getHashes() { | |||||||
|     const startTime = new Date(); |     const startTime = new Date(); | ||||||
|  |  | ||||||
|     const hashes = { |     const hashes = { | ||||||
|         notes: getHash(await sql.getAll(` |         notes: getHash(await sql.getRows(` | ||||||
|             SELECT |             SELECT | ||||||
|               noteId, |               noteId, | ||||||
|               title, |               title, | ||||||
| @@ -29,7 +29,7 @@ async function getHashes() { | |||||||
|             FROM notes |             FROM notes | ||||||
|             ORDER BY noteId`)), |             ORDER BY noteId`)), | ||||||
|  |  | ||||||
|         note_tree: getHash(await sql.getAll(` |         note_tree: getHash(await sql.getRows(` | ||||||
|             SELECT |             SELECT | ||||||
|                noteTreeId, |                noteTreeId, | ||||||
|                noteId, |                noteId, | ||||||
| @@ -41,7 +41,7 @@ async function getHashes() { | |||||||
|              FROM note_tree |              FROM note_tree | ||||||
|              ORDER BY noteTreeId`)), |              ORDER BY noteTreeId`)), | ||||||
|  |  | ||||||
|         note_revisions: getHash(await sql.getAll(` |         note_revisions: getHash(await sql.getRows(` | ||||||
|             SELECT |             SELECT | ||||||
|               noteRevisionId, |               noteRevisionId, | ||||||
|               noteId, |               noteId, | ||||||
| @@ -52,7 +52,7 @@ async function getHashes() { | |||||||
|             FROM note_revisions |             FROM note_revisions | ||||||
|             ORDER BY noteRevisionId`)), |             ORDER BY noteRevisionId`)), | ||||||
|  |  | ||||||
|         recent_notes: getHash(await sql.getAll(` |         recent_notes: getHash(await sql.getRows(` | ||||||
|            SELECT |            SELECT | ||||||
|              noteTreeId, |              noteTreeId, | ||||||
|              notePath, |              notePath, | ||||||
| @@ -61,7 +61,7 @@ async function getHashes() { | |||||||
|            FROM recent_notes |            FROM recent_notes | ||||||
|            ORDER BY notePath`)), |            ORDER BY notePath`)), | ||||||
|  |  | ||||||
|         options: getHash(await sql.getAll(` |         options: getHash(await sql.getRows(` | ||||||
|            SELECT  |            SELECT  | ||||||
|              name, |              name, | ||||||
|              value  |              value  | ||||||
| @@ -71,7 +71,7 @@ async function getHashes() { | |||||||
|  |  | ||||||
|         // we don't include image data on purpose because they are quite large, checksum is good enough |         // we don't include image data on purpose because they are quite large, checksum is good enough | ||||||
|         // to represent the data anyway |         // to represent the data anyway | ||||||
|         images: getHash(await sql.getAll(` |         images: getHash(await sql.getRows(` | ||||||
|           SELECT  |           SELECT  | ||||||
|             imageId, |             imageId, | ||||||
|             format, |             format, | ||||||
| @@ -83,7 +83,7 @@ async function getHashes() { | |||||||
|           FROM images   |           FROM images   | ||||||
|           ORDER BY imageId`)), |           ORDER BY imageId`)), | ||||||
|  |  | ||||||
|         attributes: getHash(await sql.getAll(` |         attributes: getHash(await sql.getRows(` | ||||||
|           SELECT  |           SELECT  | ||||||
|             attributeId, |             attributeId, | ||||||
|             noteId |             noteId | ||||||
|   | |||||||
| @@ -19,14 +19,14 @@ async function createNote(parentNoteId, noteTitle, noteText) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getNoteStartingWith(parentNoteId, startsWith) { | async function getNoteStartingWith(parentNoteId, startsWith) { | ||||||
|     return await sql.getFirstValue(`SELECT noteId FROM notes JOIN note_tree USING(noteId)  |     return await sql.getValue(`SELECT noteId FROM notes JOIN note_tree USING(noteId)  | ||||||
|                                     WHERE parentNoteId = ? AND title LIKE '${startsWith}%' |                                     WHERE parentNoteId = ? AND title LIKE '${startsWith}%' | ||||||
|                                     AND notes.isDeleted = 0 AND isProtected = 0  |                                     AND notes.isDeleted = 0 AND isProtected = 0  | ||||||
|                                     AND note_tree.isDeleted = 0`, [parentNoteId]); |                                     AND note_tree.isDeleted = 0`, [parentNoteId]); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getRootNoteId() { | async function getRootNoteId() { | ||||||
|     let rootNoteId = await sql.getFirstValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)  |     let rootNoteId = await sql.getValue(`SELECT notes.noteId FROM notes JOIN attributes USING(noteId)  | ||||||
|               WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`); |               WHERE attributes.name = '${CALENDAR_ROOT_ATTRIBUTE}' AND notes.isDeleted = 0`); | ||||||
|  |  | ||||||
|     if (!rootNoteId) { |     if (!rootNoteId) { | ||||||
|   | |||||||
| @@ -64,11 +64,11 @@ async function sendMessageToAllClients(message) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function sendPing(client, lastSentSyncId) { | async function sendPing(client, lastSentSyncId) { | ||||||
|     const syncData = await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]); |     const syncData = await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]); | ||||||
|  |  | ||||||
|     const lastSyncedPush = await options.getOption('last_synced_push'); |     const lastSyncedPush = await options.getOption('last_synced_push'); | ||||||
|  |  | ||||||
|     const changesToPushCount = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); |     const changesToPushCount = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); | ||||||
|  |  | ||||||
|     await sendMessage(client, { |     await sendMessage(client, { | ||||||
|         type: 'sync', |         type: 'sync', | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ const attributes = require('./attributes'); | |||||||
| const protected_session = require('./protected_session'); | const protected_session = require('./protected_session'); | ||||||
|  |  | ||||||
| async function getNoteById(noteId, dataKey) { | async function getNoteById(noteId, dataKey) { | ||||||
|     const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     protected_session.decryptNote(dataKey, note); |     protected_session.decryptNote(dataKey, note); | ||||||
|  |  | ||||||
| @@ -53,12 +53,12 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
|     let newNotePos = 0; |     let newNotePos = 0; | ||||||
|  |  | ||||||
|     if (noteOpts.target === 'into') { |     if (noteOpts.target === 'into') { | ||||||
|         const maxNotePos = await sql.getFirstValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); |         const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]); | ||||||
|  |  | ||||||
|         newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; |         newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | ||||||
|     } |     } | ||||||
|     else if (noteOpts.target === 'after') { |     else if (noteOpts.target === 'after') { | ||||||
|         const afterNote = await sql.getFirst('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]); |         const afterNote = await sql.getRow('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]); | ||||||
|  |  | ||||||
|         newNotePos = afterNote.notePosition + 1; |         newNotePos = afterNote.notePosition + 1; | ||||||
|  |  | ||||||
| @@ -73,7 +73,7 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (parentNoteId !== 'root') { |     if (parentNoteId !== 'root') { | ||||||
|         const parent = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); |         const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]); | ||||||
|  |  | ||||||
|         if (!noteOpts.type) { |         if (!noteOpts.type) { | ||||||
|             noteOpts.type = parent.type; |             noteOpts.type = parent.type; | ||||||
| @@ -125,11 +125,11 @@ async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | ||||||
|     const note = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     await protectNote(note, dataKey, protect, sourceId); |     await protectNote(note, dataKey, protect, sourceId); | ||||||
|  |  | ||||||
|     const children = await sql.getFirstColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); |     const children = await sql.getColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | ||||||
|  |  | ||||||
|     for (const childNoteId of children) { |     for (const childNoteId of children) { | ||||||
|         await protectNoteRecursively(childNoteId, dataKey, protect, sourceId); |         await protectNoteRecursively(childNoteId, dataKey, protect, sourceId); | ||||||
| @@ -165,7 +165,7 @@ async function protectNote(note, dataKey, protect, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | ||||||
|     const historyToChange = await sql.getAll("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]); |     const historyToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [noteId, protect]); | ||||||
|  |  | ||||||
|     for (const history of historyToChange) { |     for (const history of historyToChange) { | ||||||
|         if (protect) { |         if (protect) { | ||||||
| @@ -187,7 +187,7 @@ async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | ||||||
|     const oldNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [noteId]); |     const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | ||||||
|  |  | ||||||
|     if (oldNote.isProtected) { |     if (oldNote.isProtected) { | ||||||
|         protected_session.decryptNote(dataKey, oldNote); |         protected_session.decryptNote(dataKey, oldNote); | ||||||
| @@ -212,7 +212,7 @@ async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function saveNoteImages(noteId, noteText, sourceId) { | async function saveNoteImages(noteId, noteText, sourceId) { | ||||||
|     const existingNoteImages = await sql.getAll("SELECT * FROM note_images WHERE noteId = ?", [noteId]); |     const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]); | ||||||
|     const foundImageIds = []; |     const foundImageIds = []; | ||||||
|     const now = utils.nowDate(); |     const now = utils.nowDate(); | ||||||
|     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; |     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; | ||||||
| @@ -272,7 +272,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { | |||||||
|  |  | ||||||
|     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); |     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); | ||||||
|  |  | ||||||
|     const existingnoteRevisionId = await sql.getFirstValue( |     const existingnoteRevisionId = await sql.getValue( | ||||||
|         "SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]); |         "SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]); | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
| @@ -301,7 +301,7 @@ async function updateNote(noteId, newNote, dataKey, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function deleteNote(noteTreeId, sourceId) { | async function deleteNote(noteTreeId, sourceId) { | ||||||
|     const noteTree = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); |     const noteTree = await sql.getRowOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | ||||||
|  |  | ||||||
|     if (!noteTree || noteTree.isDeleted === 1) { |     if (!noteTree || noteTree.isDeleted === 1) { | ||||||
|         return; |         return; | ||||||
| @@ -312,15 +312,15 @@ async function deleteNote(noteTreeId, sourceId) { | |||||||
|     await sql.execute("UPDATE note_tree SET isDeleted = 1, dateModified = ? WHERE noteTreeId = ?", [now, noteTreeId]); |     await sql.execute("UPDATE note_tree SET isDeleted = 1, dateModified = ? WHERE noteTreeId = ?", [now, noteTreeId]); | ||||||
|     await sync_table.addNoteTreeSync(noteTreeId, sourceId); |     await sync_table.addNoteTreeSync(noteTreeId, sourceId); | ||||||
|  |  | ||||||
|     const noteId = await sql.getFirstValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); |     const noteId = await sql.getValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | ||||||
|  |  | ||||||
|     const notDeletedNoteTreesCount = await sql.getFirstValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]); |     const notDeletedNoteTreesCount = await sql.getValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]); | ||||||
|  |  | ||||||
|     if (!notDeletedNoteTreesCount) { |     if (!notDeletedNoteTreesCount) { | ||||||
|         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); |         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); | ||||||
|         await sync_table.addNoteSync(noteId, sourceId); |         await sync_table.addNoteSync(noteId, sourceId); | ||||||
|  |  | ||||||
|         const children = await sql.getAll("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); |         const children = await sql.getRows("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | ||||||
|  |  | ||||||
|         for (const child of children) { |         for (const child of children) { | ||||||
|             await deleteNote(child.noteTreeId, sourceId); |             await deleteNote(child.noteTreeId, sourceId); | ||||||
|   | |||||||
| @@ -5,10 +5,10 @@ const app_info = require('./app_info'); | |||||||
|  |  | ||||||
| async function getOptionOrNull(name) { | async function getOptionOrNull(name) { | ||||||
|     try { |     try { | ||||||
|         return await sql.getFirstOrNull("SELECT value FROM options WHERE name = ?", [name]); |         return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         return await sql.getFirstOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [name]); |         return await sql.getRowOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [name]); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -26,10 +26,10 @@ async function setOption(name, value, sourceId = null) { | |||||||
|     let opt; |     let opt; | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|         opt = await sql.getFirst("SELECT * FROM options WHERE name = ?", [name]); |         opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         opt = await sql.getFirst("SELECT * FROM options WHERE opt_name = ?", [name]); |         opt = await sql.getRow("SELECT * FROM options WHERE opt_name = ?", [name]); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (!opt) { |     if (!opt) { | ||||||
|   | |||||||
| @@ -29,7 +29,7 @@ async function generateSourceId() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function refreshSourceIds() { | async function refreshSourceIds() { | ||||||
|     allSourceIds = await sql.getFirstColumn("SELECT sourceId FROM source_ids ORDER BY dateCreated DESC"); |     allSourceIds = await sql.getColumn("SELECT sourceId FROM source_ids ORDER BY dateCreated DESC"); | ||||||
| } | } | ||||||
|  |  | ||||||
| let allSourceIds = []; | let allSourceIds = []; | ||||||
|   | |||||||
| @@ -25,7 +25,7 @@ const dbReady = new Promise((resolve, reject) => { | |||||||
|             resolve(db); |             resolve(db); | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         const tableResults = await getAll("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'"); |         const tableResults = await getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'"); | ||||||
|         if (tableResults.length !== 1) { |         if (tableResults.length !== 1) { | ||||||
|             log.info("Connected to db, but schema doesn't exist. Initializing schema ..."); |             log.info("Connected to db, but schema doesn't exist. Initializing schema ..."); | ||||||
|  |  | ||||||
| @@ -42,7 +42,7 @@ const dbReady = new Promise((resolve, reject) => { | |||||||
|                 await executeScript(imagesSql); |                 await executeScript(imagesSql); | ||||||
|                 await executeScript(notesImageSql); |                 await executeScript(notesImageSql); | ||||||
|  |  | ||||||
|                 const startNoteId = await getFirstValue("SELECT noteId FROM note_tree WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition"); |                 const startNoteId = await getValue("SELECT noteId FROM note_tree WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition"); | ||||||
|  |  | ||||||
|                 await require('./options').initOptions(startNoteId); |                 await require('./options').initOptions(startNoteId); | ||||||
|                 await require('./sync_table').fillAllSyncRows(); |                 await require('./sync_table').fillAllSyncRows(); | ||||||
| @@ -110,18 +110,18 @@ async function rollback() { | |||||||
|     return await wrap(async db => db.run("ROLLBACK")); |     return await wrap(async db => db.run("ROLLBACK")); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getFirst(query, params = []) { | async function getRow(query, params = []) { | ||||||
|     return await wrap(async db => db.get(query, ...params)); |     return await wrap(async db => db.get(query, ...params)); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getFirstOrNull(query, params = []) { | async function getRowOrNull(query, params = []) { | ||||||
|     const all = await wrap(async db => db.all(query, ...params)); |     const all = await wrap(async db => db.all(query, ...params)); | ||||||
|  |  | ||||||
|     return all.length > 0 ? all[0] : null; |     return all.length > 0 ? all[0] : null; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getFirstValue(query, params = []) { | async function getValue(query, params = []) { | ||||||
|     const row = await getFirstOrNull(query, params); |     const row = await getRowOrNull(query, params); | ||||||
|  |  | ||||||
|     if (!row) { |     if (!row) { | ||||||
|         return null; |         return null; | ||||||
| @@ -130,19 +130,25 @@ async function getFirstValue(query, params = []) { | |||||||
|     return row[Object.keys(row)[0]]; |     return row[Object.keys(row)[0]]; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getAll(query, params = []) { | async function getRows(query, params = []) { | ||||||
|     return await wrap(async db => db.all(query, ...params)); |     return await wrap(async db => db.all(query, ...params)); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getAllEntities(query, params = []) { | async function getEntities(query, params = []) { | ||||||
|     const rows = await getAll(query, params); |     const rows = await getRows(query, params); | ||||||
|  |  | ||||||
|  |     return rows.map(row => new Note(module.exports, row)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function getEntity(query, params = []) { | ||||||
|  |     const rows = await getRows(query, params); | ||||||
|  |  | ||||||
|     return rows.map(row => new Note(module.exports, row)); |     return rows.map(row => new Note(module.exports, row)); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getMap(query, params = []) { | async function getMap(query, params = []) { | ||||||
|     const map = {}; |     const map = {}; | ||||||
|     const results = await getAll(query, params); |     const results = await getRows(query, params); | ||||||
|  |  | ||||||
|     for (const row of results) { |     for (const row of results) { | ||||||
|         const keys = Object.keys(row); |         const keys = Object.keys(row); | ||||||
| @@ -153,9 +159,9 @@ async function getMap(query, params = []) { | |||||||
|     return map; |     return map; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getFirstColumn(query, params = []) { | async function getColumn(query, params = []) { | ||||||
|     const list = []; |     const list = []; | ||||||
|     const result = await getAll(query, params); |     const result = await getRows(query, params); | ||||||
|  |  | ||||||
|     if (result.length === 0) { |     if (result.length === 0) { | ||||||
|         return list; |         return list; | ||||||
| @@ -236,10 +242,10 @@ async function isDbUpToDate() { | |||||||
|     let dbVersion; |     let dbVersion; | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|         dbVersion = parseInt(await getFirstValue("SELECT value FROM options WHERE name = 'db_version'")); |         dbVersion = parseInt(await getValue("SELECT value FROM options WHERE name = 'db_version'")); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         dbVersion = parseInt(await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'db_version'")); |         dbVersion = parseInt(await getValue("SELECT opt_value FROM options WHERE opt_name = 'db_version'")); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const upToDate = dbVersion >= app_info.db_version; |     const upToDate = dbVersion >= app_info.db_version; | ||||||
| @@ -255,10 +261,10 @@ async function isUserInitialized() { | |||||||
|     let username; |     let username; | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|         username = await getFirstValue("SELECT value FROM options WHERE name = 'username'"); |         username = await getValue("SELECT value FROM options WHERE name = 'username'"); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         username = await getFirstValue("SELECT opt_value FROM options WHERE opt_name = 'username'"); |         username = await getValue("SELECT opt_value FROM options WHERE opt_name = 'username'"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return !!username; |     return !!username; | ||||||
| @@ -269,13 +275,14 @@ module.exports = { | |||||||
|     isUserInitialized, |     isUserInitialized, | ||||||
|     insert, |     insert, | ||||||
|     replace, |     replace, | ||||||
|     getFirstValue, |     getValue, | ||||||
|     getFirst, |     getRow, | ||||||
|     getFirstOrNull, |     getRowOrNull, | ||||||
|     getAll, |     getRows, | ||||||
|     getAllEntities, |     getEntities, | ||||||
|  |     getEntity, | ||||||
|     getMap, |     getMap, | ||||||
|     getFirstColumn, |     getColumn, | ||||||
|     execute, |     execute, | ||||||
|     executeScript, |     executeScript, | ||||||
|     doInTransaction, |     doInTransaction, | ||||||
|   | |||||||
| @@ -173,7 +173,7 @@ async function pushSync(syncContext) { | |||||||
|     let lastSyncedPush = await getLastSyncedPush(); |     let lastSyncedPush = await getLastSyncedPush(); | ||||||
|  |  | ||||||
|     while (true) { |     while (true) { | ||||||
|         const sync = await sql.getFirstOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]); |         const sync = await sql.getRowOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]); | ||||||
|  |  | ||||||
|         if (sync === null) { |         if (sync === null) { | ||||||
|             // nothing to sync |             // nothing to sync | ||||||
| @@ -200,13 +200,13 @@ async function pushEntity(sync, syncContext) { | |||||||
|     let entity; |     let entity; | ||||||
|  |  | ||||||
|     if (sync.entityName === 'notes') { |     if (sync.entityName === 'notes') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM notes WHERE noteId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM notes WHERE noteId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'note_tree') { |     else if (sync.entityName === 'note_tree') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM note_tree WHERE noteTreeId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM note_tree WHERE noteTreeId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'note_revisions') { |     else if (sync.entityName === 'note_revisions') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM note_revisions WHERE noteRevisionId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM note_revisions WHERE noteRevisionId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'note_reordering') { |     else if (sync.entityName === 'note_reordering') { | ||||||
|         entity = { |         entity = { | ||||||
| @@ -215,23 +215,23 @@ async function pushEntity(sync, syncContext) { | |||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'options') { |     else if (sync.entityName === 'options') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM options WHERE name = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM options WHERE name = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'recent_notes') { |     else if (sync.entityName === 'recent_notes') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM recent_notes WHERE noteTreeId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM recent_notes WHERE noteTreeId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'images') { |     else if (sync.entityName === 'images') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM images WHERE imageId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM images WHERE imageId = ?', [sync.entityId]); | ||||||
|  |  | ||||||
|         if (entity.data !== null) { |         if (entity.data !== null) { | ||||||
|             entity.data = entity.data.toString('base64'); |             entity.data = entity.data.toString('base64'); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'note_images') { |     else if (sync.entityName === 'note_images') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM note_images WHERE noteImageId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM note_images WHERE noteImageId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else if (sync.entityName === 'attributes') { |     else if (sync.entityName === 'attributes') { | ||||||
|         entity = await sql.getFirst('SELECT * FROM attributes WHERE attributeId = ?', [sync.entityId]); |         entity = await sql.getRow('SELECT * FROM attributes WHERE attributeId = ?', [sync.entityId]); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         throw new Error(`Unrecognized entity type ${sync.entityName} in sync #${sync.id}`); |         throw new Error(`Unrecognized entity type ${sync.entityName} in sync #${sync.id}`); | ||||||
| @@ -262,7 +262,7 @@ async function checkContentHash(syncContext) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     const lastSyncedPush = await getLastSyncedPush(); |     const lastSyncedPush = await getLastSyncedPush(); | ||||||
|     const notPushedSyncs = await sql.getFirstValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); |     const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); | ||||||
|  |  | ||||||
|     if (notPushedSyncs > 0) { |     if (notPushedSyncs > 0) { | ||||||
|         log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check."); |         log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check."); | ||||||
|   | |||||||
| @@ -66,10 +66,10 @@ async function cleanupSyncRowsForMissingEntities(entityName, entityKey) { | |||||||
| async function fillSyncRows(entityName, entityKey) { | async function fillSyncRows(entityName, entityKey) { | ||||||
|     await cleanupSyncRowsForMissingEntities(entityName, entityKey); |     await cleanupSyncRowsForMissingEntities(entityName, entityKey); | ||||||
|  |  | ||||||
|     const entityIds = await sql.getFirstColumn(`SELECT ${entityKey} FROM ${entityName}`); |     const entityIds = await sql.getColumn(`SELECT ${entityKey} FROM ${entityName}`); | ||||||
|  |  | ||||||
|     for (const entityId of entityIds) { |     for (const entityId of entityIds) { | ||||||
|         const existingRows = await sql.getFirstValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]); |         const existingRows = await sql.getValue("SELECT COUNT(id) FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]); | ||||||
|  |  | ||||||
|         // we don't want to replace existing entities (which would effectively cause full resync) |         // we don't want to replace existing entities (which would effectively cause full resync) | ||||||
|         if (existingRows === 0) { |         if (existingRows === 0) { | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ const notes = require('./notes'); | |||||||
| const sync_table = require('./sync_table'); | const sync_table = require('./sync_table'); | ||||||
|  |  | ||||||
| async function updateNote(entity, sourceId) { | async function updateNote(entity, sourceId) { | ||||||
|     const origNote = await sql.getFirst("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]); |     const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]); | ||||||
|  |  | ||||||
|     if (!origNote || origNote.dateModified <= entity.dateModified) { |     if (!origNote || origNote.dateModified <= entity.dateModified) { | ||||||
|         await sql.doInTransaction(async () => { |         await sql.doInTransaction(async () => { | ||||||
| @@ -20,7 +20,7 @@ async function updateNote(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateNoteTree(entity, sourceId) { | async function updateNoteTree(entity, sourceId) { | ||||||
|     const orig = await sql.getFirstOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [entity.noteTreeId]); |     const orig = await sql.getRowOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [entity.noteTreeId]); | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         if (orig === null || orig.dateModified < entity.dateModified) { |         if (orig === null || orig.dateModified < entity.dateModified) { | ||||||
| @@ -36,7 +36,7 @@ async function updateNoteTree(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateNoteHistory(entity, sourceId) { | async function updateNoteHistory(entity, sourceId) { | ||||||
|     const orig = await sql.getFirstOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]); |     const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]); | ||||||
|  |  | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         // we update note history even if date modified to is the same because the only thing which might have changed |         // we update note history even if date modified to is the same because the only thing which might have changed | ||||||
| @@ -62,7 +62,7 @@ async function updateNoteReordering(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateOptions(entity, sourceId) { | async function updateOptions(entity, sourceId) { | ||||||
|     const orig = await sql.getFirstOrNull("SELECT * FROM options WHERE name = ?", [entity.name]); |     const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]); | ||||||
|  |  | ||||||
|     if (!orig.isSynced) { |     if (!orig.isSynced) { | ||||||
|         return; |         return; | ||||||
| @@ -80,7 +80,7 @@ async function updateOptions(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateRecentNotes(entity, sourceId) { | async function updateRecentNotes(entity, sourceId) { | ||||||
|     const orig = await sql.getFirstOrNull("SELECT * FROM recent_notes WHERE noteTreeId = ?", [entity.noteTreeId]); |     const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteTreeId = ?", [entity.noteTreeId]); | ||||||
|  |  | ||||||
|     if (orig === null || orig.dateAccessed < entity.dateAccessed) { |     if (orig === null || orig.dateAccessed < entity.dateAccessed) { | ||||||
|         await sql.doInTransaction(async () => { |         await sql.doInTransaction(async () => { | ||||||
| @@ -96,7 +96,7 @@ async function updateImage(entity, sourceId) { | |||||||
|         entity.data = Buffer.from(entity.data, 'base64'); |         entity.data = Buffer.from(entity.data, 'base64'); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const origImage = await sql.getFirst("SELECT * FROM images WHERE imageId = ?", [entity.imageId]); |     const origImage = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [entity.imageId]); | ||||||
|  |  | ||||||
|     if (!origImage || origImage.dateModified <= entity.dateModified) { |     if (!origImage || origImage.dateModified <= entity.dateModified) { | ||||||
|         await sql.doInTransaction(async () => { |         await sql.doInTransaction(async () => { | ||||||
| @@ -110,7 +110,7 @@ async function updateImage(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateNoteImage(entity, sourceId) { | async function updateNoteImage(entity, sourceId) { | ||||||
|     const origNoteImage = await sql.getFirst("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]); |     const origNoteImage = await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]); | ||||||
|  |  | ||||||
|     if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) { |     if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) { | ||||||
|         await sql.doInTransaction(async () => { |         await sql.doInTransaction(async () => { | ||||||
| @@ -124,7 +124,7 @@ async function updateNoteImage(entity, sourceId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function updateAttribute(entity, sourceId) { | async function updateAttribute(entity, sourceId) { | ||||||
|     const origAttribute = await sql.getFirst("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]); |     const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]); | ||||||
|  |  | ||||||
|     if (!origAttribute || origAttribute.dateModified <= entity.dateModified) { |     if (!origAttribute || origAttribute.dateModified <= entity.dateModified) { | ||||||
|         await sql.doInTransaction(async () => { |         await sql.doInTransaction(async () => { | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ async function validateParentChild(res, parentNoteId, childNoteId, noteTreeId = | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getExistingNoteTree(parentNoteId, childNoteId) { | async function getExistingNoteTree(parentNoteId, childNoteId) { | ||||||
|     return await sql.getFirst('SELECT * FROM note_tree WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]); |     return await sql.getRow('SELECT * FROM note_tree WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]); | ||||||
| } | } | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -51,7 +51,7 @@ async function checkTreeCycle(parentNoteId, childNoteId) { | |||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         const parentNoteIds = await sql.getFirstColumn("SELECT DISTINCT parentNoteId FROM note_tree WHERE noteId = ? AND isDeleted = 0", [parentNoteId]); |         const parentNoteIds = await sql.getColumn("SELECT DISTINCT parentNoteId FROM note_tree WHERE noteId = ? AND isDeleted = 0", [parentNoteId]); | ||||||
|  |  | ||||||
|         for (const pid of parentNoteIds) { |         for (const pid of parentNoteIds) { | ||||||
|             if (!await checkTreeCycleInner(pid)) { |             if (!await checkTreeCycleInner(pid)) { | ||||||
| @@ -66,13 +66,13 @@ async function checkTreeCycle(parentNoteId, childNoteId) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getNoteTree(noteTreeId) { | async function getNoteTree(noteTreeId) { | ||||||
|     return sql.getFirst("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); |     return sql.getRow("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) { | async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) { | ||||||
|     subTreeNoteIds.push(parentNoteId); |     subTreeNoteIds.push(parentNoteId); | ||||||
|  |  | ||||||
|     const children = await sql.getFirstColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]); |     const children = await sql.getColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]); | ||||||
|  |  | ||||||
|     for (const childNoteId of children) { |     for (const childNoteId of children) { | ||||||
|         await loadSubTreeNoteIds(childNoteId, subTreeNoteIds); |         await loadSubTreeNoteIds(childNoteId, subTreeNoteIds); | ||||||
| @@ -81,7 +81,7 @@ async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) { | |||||||
|  |  | ||||||
| async function sortNotesAlphabetically(parentNoteId, req, sourceId) { | async function sortNotesAlphabetically(parentNoteId, req, sourceId) { | ||||||
|     await sql.doInTransaction(async () => { |     await sql.doInTransaction(async () => { | ||||||
|         const notes = await sql.getAll(`SELECT noteTreeId, noteId, title, isProtected  |         const notes = await sql.getRows(`SELECT noteTreeId, noteId, title, isProtected  | ||||||
|                                        FROM notes JOIN note_tree USING(noteId)  |                                        FROM notes JOIN note_tree USING(noteId)  | ||||||
|                                        WHERE note_tree.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]); |                                        WHERE note_tree.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user