2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
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-12-03 19:18:33 -05:00
|
|
|
const fs = require('fs');
|
2017-11-28 17:04:47 -05:00
|
|
|
const sqlite = require('sqlite');
|
2017-12-10 12:56:59 -05:00
|
|
|
const app_info = require('./app_info');
|
2017-12-27 16:44:15 -05:00
|
|
|
const resource_dir = require('./resource_dir');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2017-11-28 17:04:47 -05:00
|
|
|
async function createConnection() {
|
|
|
|
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
|
|
|
}
|
2017-11-17 19:09:51 -05:00
|
|
|
|
2017-12-03 19:18:33 -05:00
|
|
|
const dbConnected = createConnection();
|
|
|
|
|
2017-12-03 22:29:23 -05:00
|
|
|
let dbReadyResolve = null;
|
2017-12-03 19:18:33 -05:00
|
|
|
const dbReady = new Promise((resolve, reject) => {
|
|
|
|
dbConnected.then(async db => {
|
2017-12-19 22:24:41 -05:00
|
|
|
await execute("PRAGMA foreign_keys = ON");
|
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
dbReadyResolve = () => {
|
|
|
|
log.info("DB ready.");
|
|
|
|
|
|
|
|
resolve(db);
|
|
|
|
};
|
2017-12-03 22:29:23 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
const tableResults = await getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
|
2017-12-03 19:18:33 -05:00
|
|
|
if (tableResults.length !== 1) {
|
2017-12-03 22:29:23 -05:00
|
|
|
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
|
|
|
|
|
2017-12-27 16:44:15 -05:00
|
|
|
const schema = fs.readFileSync(resource_dir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
|
|
|
|
const notesSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_notes.sql', 'UTF-8');
|
2018-01-28 19:38:05 -05:00
|
|
|
const notesTreeSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_note_tree.sql', 'UTF-8');
|
2018-01-07 23:52:32 -05:00
|
|
|
const imagesSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_images.sql', 'UTF-8');
|
2018-01-28 19:38:05 -05:00
|
|
|
const notesImageSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_note_images.sql', 'UTF-8');
|
2017-12-03 22:29:23 -05:00
|
|
|
|
|
|
|
await doInTransaction(async () => {
|
|
|
|
await executeScript(schema);
|
2017-12-23 13:55:13 -05:00
|
|
|
await executeScript(notesSql);
|
|
|
|
await executeScript(notesTreeSql);
|
2018-01-07 23:52:32 -05:00
|
|
|
await executeScript(imagesSql);
|
|
|
|
await executeScript(notesImageSql);
|
2017-12-03 22:29:23 -05:00
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
const startNoteId = await getValue("SELECT noteId FROM note_tree WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
|
2017-12-04 00:17:08 -05:00
|
|
|
|
2018-01-03 22:13:02 -05:00
|
|
|
await require('./options').initOptions(startNoteId);
|
2017-12-23 13:55:13 -05:00
|
|
|
await require('./sync_table').fillAllSyncRows();
|
2017-12-03 22:29:23 -05:00
|
|
|
});
|
|
|
|
|
2017-12-23 13:55:13 -05:00
|
|
|
log.info("Schema and initial content generated. Waiting for user to enter username/password to finish setup.");
|
|
|
|
|
2017-12-03 22:29:23 -05:00
|
|
|
// we don't resolve dbReady promise because user needs to setup the username and password to initialize
|
|
|
|
// the database
|
2017-12-03 19:18:33 -05:00
|
|
|
}
|
2017-12-03 22:29:23 -05:00
|
|
|
else {
|
2017-12-27 18:23:24 -05:00
|
|
|
if (!await isUserInitialized()) {
|
2017-12-10 12:56:59 -05:00
|
|
|
log.info("Login/password not initialized. DB not ready.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await isDbUpToDate()) {
|
|
|
|
return;
|
2017-12-03 22:29:23 -05:00
|
|
|
}
|
2017-12-10 12:56:59 -05:00
|
|
|
|
|
|
|
resolve(db);
|
2017-12-03 22:29:23 -05:00
|
|
|
}
|
2017-12-03 19:18:33 -05:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.log("Error connecting to DB.", e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
});
|
2017-11-17 19:09:51 -05:00
|
|
|
|
2017-12-03 22:29:23 -05:00
|
|
|
function setDbReadyAsResolved() {
|
|
|
|
dbReadyResolve();
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function insert(table_name, rec, replace = false) {
|
2017-10-25 22:39:21 -04:00
|
|
|
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-11-28 17:24:08 -05:00
|
|
|
const res = await execute(query, Object.values(rec));
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
return res.lastID;
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function replace(table_name, rec) {
|
|
|
|
return await insert(table_name, rec, true);
|
2017-10-29 22:22:30 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function beginTransaction() {
|
|
|
|
return await wrap(async db => db.run("BEGIN"));
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function commit() {
|
|
|
|
return await wrap(async db => db.run("COMMIT"));
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function rollback() {
|
|
|
|
return await wrap(async db => db.run("ROLLBACK"));
|
2017-10-25 22:39:21 -04:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
async function getRow(query, params = []) {
|
2017-11-28 17:24:08 -05:00
|
|
|
return await wrap(async db => db.get(query, ...params));
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
async function getRowOrNull(query, params = []) {
|
2017-11-28 17:24:08 -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;
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
async function getValue(query, params = []) {
|
|
|
|
const row = await getRowOrNull(query, params);
|
2017-10-29 22:22:30 -04:00
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return row[Object.keys(row)[0]];
|
2017-10-28 19:55:55 -04:00
|
|
|
}
|
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
async function getRows(query, params = []) {
|
2017-11-28 17:24:08 -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 = {};
|
2018-01-29 17:41:59 -05:00
|
|
|
const results = await getRows(query, params);
|
2017-11-02 22:55:22 -04:00
|
|
|
|
|
|
|
for (const row of results) {
|
|
|
|
const keys = Object.keys(row);
|
|
|
|
|
|
|
|
map[row[keys[0]]] = row[keys[1]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2018-01-29 17:41:59 -05:00
|
|
|
async function getColumn(query, params = []) {
|
2017-10-24 23:14:26 -04:00
|
|
|
const list = [];
|
2018-01-29 17:41:59 -05:00
|
|
|
const result = await getRows(query, params);
|
2017-10-24 23:14:26 -04:00
|
|
|
|
2017-12-14 22:16:26 -05:00
|
|
|
if (result.length === 0) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = Object.keys(result[0])[0];
|
|
|
|
|
2017-10-24 23:14:26 -04:00
|
|
|
for (const row of result) {
|
|
|
|
list.push(row[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function execute(query, params = []) {
|
|
|
|
return await wrap(async db => db.run(query, ...params));
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
async function executeScript(query) {
|
|
|
|
return await wrap(async db => db.exec(query));
|
2017-10-15 17:31:49 -04:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:31:44 -04:00
|
|
|
async function wrap(func) {
|
2017-11-21 22:11:27 -05:00
|
|
|
const thisError = new Error();
|
2017-12-03 19:18:33 -05:00
|
|
|
const db = await dbConnected;
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2017-11-01 20:31:44 -04:00
|
|
|
try {
|
2017-11-28 17:24:08 -05:00
|
|
|
return await func(db);
|
2017-11-01 20:31:44 -04:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-11-18 18:57:50 -05:00
|
|
|
log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
|
|
|
|
|
2017-12-19 22:33:44 -05:00
|
|
|
thisError.message = e.stack;
|
|
|
|
|
2017-11-18 18:57:50 -05:00
|
|
|
throw thisError;
|
2017-11-01 20:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 18:33:23 -05:00
|
|
|
let transactionActive = false;
|
2017-11-28 17:24:08 -05:00
|
|
|
let transactionPromise = null;
|
|
|
|
|
2017-10-29 18:50:28 -04:00
|
|
|
async function doInTransaction(func) {
|
2017-11-28 18:33:23 -05:00
|
|
|
while (transactionActive) {
|
2017-11-28 17:24:08 -05:00
|
|
|
await transactionPromise;
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:15:49 -04:00
|
|
|
const error = new Error(); // to capture correct stack trace in case of exception
|
|
|
|
|
2017-11-28 18:33:23 -05:00
|
|
|
transactionActive = true;
|
2017-11-28 17:24:08 -05:00
|
|
|
transactionPromise = new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
await beginTransaction();
|
2017-10-29 18:50:28 -04:00
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
await func();
|
2017-10-29 18:50:28 -04:00
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
await commit();
|
2017-10-29 18:50:28 -04:00
|
|
|
|
2017-11-28 18:33:23 -05:00
|
|
|
transactionActive = false;
|
2017-11-28 17:24:08 -05:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack);
|
2017-10-31 00:15:49 -04:00
|
|
|
|
2017-11-28 17:24:08 -05:00
|
|
|
await rollback();
|
|
|
|
|
2017-11-28 18:33:23 -05:00
|
|
|
transactionActive = false;
|
2017-11-28 17:24:08 -05:00
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
reject(e);
|
2017-11-28 17:24:08 -05:00
|
|
|
}
|
|
|
|
});
|
2017-11-28 18:33:23 -05:00
|
|
|
|
|
|
|
if (transactionActive) {
|
|
|
|
await transactionPromise;
|
|
|
|
}
|
2017-10-29 18:50:28 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 12:56:59 -05:00
|
|
|
async function isDbUpToDate() {
|
2018-01-28 20:52:05 -05:00
|
|
|
let dbVersion;
|
|
|
|
|
|
|
|
try {
|
2018-01-29 17:41:59 -05:00
|
|
|
dbVersion = parseInt(await getValue("SELECT value FROM options WHERE name = 'db_version'"));
|
2018-01-28 20:52:05 -05:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-01-29 17:41:59 -05:00
|
|
|
dbVersion = parseInt(await getValue("SELECT opt_value FROM options WHERE opt_name = 'db_version'"));
|
2018-01-28 20:52:05 -05:00
|
|
|
}
|
2017-12-10 12:56:59 -05:00
|
|
|
|
|
|
|
const upToDate = dbVersion >= app_info.db_version;
|
|
|
|
|
|
|
|
if (!upToDate) {
|
|
|
|
log.info("App db version is " + app_info.db_version + ", while db version is " + dbVersion + ". Migration needed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return upToDate;
|
|
|
|
}
|
|
|
|
|
2017-12-27 18:23:24 -05:00
|
|
|
async function isUserInitialized() {
|
2018-01-28 20:52:05 -05:00
|
|
|
let username;
|
|
|
|
|
|
|
|
try {
|
2018-01-29 17:41:59 -05:00
|
|
|
username = await getValue("SELECT value FROM options WHERE name = 'username'");
|
2018-01-28 20:52:05 -05:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-01-29 17:41:59 -05:00
|
|
|
username = await getValue("SELECT opt_value FROM options WHERE opt_name = 'username'");
|
2018-01-28 20:52:05 -05:00
|
|
|
}
|
2017-12-27 18:23:24 -05:00
|
|
|
|
|
|
|
return !!username;
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
module.exports = {
|
2017-11-16 21:50:00 -05:00
|
|
|
dbReady,
|
2017-12-27 18:23:24 -05:00
|
|
|
isUserInitialized,
|
2017-10-14 23:31:44 -04:00
|
|
|
insert,
|
2017-10-29 22:22:30 -04:00
|
|
|
replace,
|
2018-01-29 17:41:59 -05:00
|
|
|
getValue,
|
|
|
|
getRow,
|
|
|
|
getRowOrNull,
|
|
|
|
getRows,
|
2017-11-02 22:55:22 -04:00
|
|
|
getMap,
|
2018-01-29 17:41:59 -05:00
|
|
|
getColumn,
|
2017-10-14 23:31:44 -04:00
|
|
|
execute,
|
2017-10-15 17:31:49 -04:00
|
|
|
executeScript,
|
2017-12-03 22:29:23 -05:00
|
|
|
doInTransaction,
|
2017-12-10 12:56:59 -05:00
|
|
|
setDbReadyAsResolved,
|
|
|
|
isDbUpToDate
|
2017-10-14 23:31:44 -04:00
|
|
|
};
|