trilium/services/sql.js

169 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const utils = require('./utils');
2017-10-25 10:17:48 +08:00
const log = require('./log');
const dataDir = require('./data_dir');
const dbReady = (() => {
const db = require('sqlite');
return db.open(dataDir.DOCUMENT_PATH, {Promise}).then(() => db);
})();
2017-10-26 10:39:21 +08: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-26 10:39:21 +08:00
const res = await execute(query, Object.values(rec));
return res.lastID;
}
2017-10-30 10:22:30 +08:00
async function replace(table_name, rec) {
return await insert(table_name, rec, true);
}
async function beginTransaction() {
return await wrap(async db => db.run("BEGIN"));
}
async function commit() {
return await wrap(async db => db.run("COMMIT"));
}
2017-10-26 10:39:21 +08:00
async function rollback() {
return await wrap(async db => db.run("ROLLBACK"));
2017-10-26 10:39:21 +08:00
}
async function getSingleResult(query, params = []) {
return await wrap(async db => db.get(query, ...params));
}
async function getSingleResultOrNull(query, params = []) {
const all = await wrap(async db => db.all(query, ...params));
2017-10-30 10:22:30 +08: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]];
}
async function getResults(query, params = []) {
return await wrap(async db => db.all(query, ...params));
}
2017-11-03 10:55:22 +08: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-25 11:14:26 +08: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;
}
async function execute(query, params = []) {
return await wrap(async db => db.run(query, ...params));
}
2017-10-16 05:31:49 +08:00
async function executeScript(query) {
return await wrap(async db => db.exec(query));
2017-10-16 05:31:49 +08:00
}
async function remove(tableName, noteId) {
return await execute("DELETE FROM " + tableName + " WHERE note_id = ?", [noteId]);
}
async function wrap(func) {
2017-11-22 11:11:27 +08:00
const thisError = new Error();
const db = await dbReady;
try {
return await func(db);
}
catch (e) {
log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
throw thisError;
}
}
async function doInTransaction(func) {
const error = new Error(); // to capture correct stack trace in case of exception
try {
await beginTransaction();
await func();
await commit();
}
catch (e) {
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack);
await rollback();
throw e;
}
}
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);
});
module.exports = {
dbReady,
insert,
2017-10-30 10:22:30 +08:00
replace,
getSingleValue,
getSingleResult,
2017-10-30 10:22:30 +08:00
getSingleResultOrNull,
getResults,
2017-11-03 10:55:22 +08:00
getMap,
2017-10-25 11:14:26 +08:00
getFlattenedResults,
execute,
2017-10-16 05:31:49 +08:00
executeScript,
remove,
doInTransaction
};