| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const db = require('sqlite'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:17:48 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function beginTransaction() { | 
					
						
							|  |  |  |     return await db.run("BEGIN"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function commit() { | 
					
						
							|  |  |  |     return await db.run("COMMIT"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | async function rollback() { | 
					
						
							|  |  |  |     return await db.run("ROLLBACK"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function getOption(optName) { | 
					
						
							|  |  |  |     const row = await getSingleResult("SELECT opt_value FROM options WHERE opt_name = ?", [optName]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return row['opt_value']; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function setOption(optName, optValue) { | 
					
						
							|  |  |  |     await execute("UPDATE options SET opt_value = ? WHERE opt_name = ?", [optValue, optName]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getSingleResult(query, params = []) { | 
					
						
							|  |  |  |     return await db.get(query, ...params); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getResults(query, params = []) { | 
					
						
							|  |  |  |     return await db.all(query, ...params); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 = []) { | 
					
						
							|  |  |  |     return await db.run(query, ...params); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-15 17:31:49 -04:00
										 |  |  | async function executeScript(query) { | 
					
						
							|  |  |  |     return await db.exec(query); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | async function remove(tableName, noteId) { | 
					
						
							|  |  |  |     return await execute("DELETE FROM " + tableName + " WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function addAudit(category, req=null, noteId=null, changeFrom=null, changeTo=null, comment=null) { | 
					
						
							|  |  |  |     const now = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const browserId = req == null ? null : req.get('x-browser-id'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |     await execute("INSERT INTO audit_log (id, date_modified, category, browser_id, note_id, change_from, change_to, comment)" | 
					
						
							|  |  |  |            + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [id, now, category, browserId, noteId, changeFrom, changeTo, comment]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function deleteRecentAudits(category, req, noteId) { | 
					
						
							|  |  |  |     const browserId = req.get('x-browser-id'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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]) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     insert, | 
					
						
							|  |  |  |     getSingleResult, | 
					
						
							|  |  |  |     getResults, | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     getOption, | 
					
						
							|  |  |  |     setOption, | 
					
						
							|  |  |  |     beginTransaction, | 
					
						
							|  |  |  |     commit, | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     rollback, | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  |     addAudit, | 
					
						
							|  |  |  |     deleteRecentAudits, | 
					
						
							|  |  |  |     remove | 
					
						
							|  |  |  | }; |