| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const dataDir = require('./data_dir'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  | const dbReady = (() => { | 
					
						
							|  |  |  |     const db = require('sqlite'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return db.open(dataDir.DOCUMENT_PATH, {Promise}).then(() => db); | 
					
						
							|  |  |  | })(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | async function insert(table_name, rec, replace = false) { | 
					
						
							|  |  |  |     const keys = Object.keys(rec); | 
					
						
							|  |  |  |     if (keys.length === 0) { | 
					
						
							|  |  |  |         log.error("Can't insert empty object into table " + table_name); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const columns = keys.join(", "); | 
					
						
							|  |  |  |     const questionMarks = keys.map(p => "?").join(", "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const query = "INSERT " + (replace ? "OR REPLACE" : "") + " INTO " + table_name + "(" + columns + ") VALUES (" + questionMarks + ")"; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     const res = await execute(query, Object.values(rec)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return res.lastID; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | async function replace(table_name, rec) { | 
					
						
							|  |  |  |     return await insert(table_name, rec, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function beginTransaction() { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.run("BEGIN")); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function commit() { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.run("COMMIT")); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | async function rollback() { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.run("ROLLBACK")); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function getSingleResult(query, params = []) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.get(query, ...params)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | async function getSingleResultOrNull(query, params = []) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     const all = await wrap(async db => db.all(query, ...params)); | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     return all.length > 0 ? all[0] : null; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getSingleValue(query, params = []) { | 
					
						
							|  |  |  |     const row = await getSingleResultOrNull(query, params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!row) { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return row[Object.keys(row)[0]]; | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function getResults(query, params = []) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.all(query, ...params)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  | async function getMap(query, params = []) { | 
					
						
							|  |  |  |     const map = {}; | 
					
						
							|  |  |  |     const results = await getResults(query, params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const row of results) { | 
					
						
							|  |  |  |         const keys = Object.keys(row); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         map[row[keys[0]]] = row[keys[1]]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return map; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 23:14:26 -04:00
										 |  |  | async function getFlattenedResults(key, query, params = []) { | 
					
						
							|  |  |  |     const list = []; | 
					
						
							|  |  |  |     const result = await getResults(query, params); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const row of result) { | 
					
						
							|  |  |  |         list.push(row[key]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function execute(query, params = []) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.run(query, ...params)); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 17:31:49 -04:00
										 |  |  | async function executeScript(query) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     return await wrap(async db => db.exec(query)); | 
					
						
							| 
									
										
										
										
											2017-10-15 17:31:49 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function remove(tableName, noteId) { | 
					
						
							|  |  |  |     return await execute("DELETE FROM " + tableName + " WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | async function addAudit(category, browserId=null, noteId=null, changeFrom=null, changeTo=null, comment=null) { | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |     const now = utils.nowTimestamp(); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  |     log.info("audit: " + category + ", browserId=" + browserId + ", noteId=" + noteId + ", from=" + changeFrom | 
					
						
							|  |  |  |         + ", to=" + changeTo + ", comment=" + comment); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 12:12:20 -04:00
										 |  |  |     const id = utils.randomString(14); | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-20 23:51:28 -05:00
										 |  |  |     await insert("audit_log", { | 
					
						
							|  |  |  |         id: id, | 
					
						
							|  |  |  |         date_modified: now, | 
					
						
							|  |  |  |         category: category, | 
					
						
							|  |  |  |         browser_id: browserId, | 
					
						
							|  |  |  |         note_id: noteId, | 
					
						
							|  |  |  |         change_from: changeFrom, | 
					
						
							|  |  |  |         change_to: changeTo, | 
					
						
							|  |  |  |         comment: comment | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | async function deleteRecentAudits(category, browserId, noteId) { | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     const deleteCutoff = utils.nowTimestamp() - 10 * 60; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await execute("DELETE FROM audit_log WHERE category = ? AND browser_id = ? AND note_id = ? AND date_modified > ?", | 
					
						
							|  |  |  |             [category, browserId, noteId, deleteCutoff]) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  | async function wrap(func) { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |     const db = await dbReady; | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-11-17 19:09:51 -05:00
										 |  |  |         return await func(db); | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-11-18 18:57:50 -05:00
										 |  |  |         const thisError = new Error(); | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 18:57:50 -05:00
										 |  |  |         log.error("Error executing query. Inner exception: " + e.stack + thisError.stack); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         throw thisError; | 
					
						
							| 
									
										
										
										
											2017-11-01 20:31:44 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  | async function doInTransaction(func) { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |     const error = new Error(); // to capture correct stack trace in case of exception
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await beginTransaction(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await func(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await commit(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await rollback(); | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         throw e; | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | dbReady | 
					
						
							|  |  |  |     .then(async () => { | 
					
						
							|  |  |  |         const tableResults = await getResults("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (tableResults.length !== 1) { | 
					
						
							|  |  |  |             console.log("No connection to initialized DB."); | 
					
						
							|  |  |  |             process.exit(1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .catch(e => { | 
					
						
							|  |  |  |         console.log("Error connecting to DB.", e); | 
					
						
							|  |  |  |         process.exit(1); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     dbReady, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     insert, | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     replace, | 
					
						
							|  |  |  |     getSingleValue, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     getSingleResult, | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     getSingleResultOrNull, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     getResults, | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |     getMap, | 
					
						
							| 
									
										
										
										
											2017-10-24 23:14:26 -04:00
										 |  |  |     getFlattenedResults, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     execute, | 
					
						
							| 
									
										
										
										
											2017-10-15 17:31:49 -04:00
										 |  |  |     executeScript, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     addAudit, | 
					
						
							|  |  |  |     deleteRecentAudits, | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     remove, | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     doInTransaction | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | }; |