Notes/src/share/sql.js

35 lines
911 B
JavaScript
Raw Normal View History

2021-10-16 22:13:34 +02:00
"use strict";
const Database = require('better-sqlite3');
const dataDir = require('../services/data_dir');
2021-10-16 22:13:34 +02:00
const dbConnection = new Database(dataDir.DOCUMENT_PATH, { readonly: true });
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach(eventType => {
process.on(eventType, () => {
if (dbConnection) {
// closing connection is especially important to fold -wal file into the main DB file
// (see https://sqlite.org/tempfiles.html for details)
dbConnection.close();
}
});
});
function getRawRows(query, params = []) {
2021-12-23 12:54:21 +01:00
return dbConnection.prepare(query).raw().all(params);
2021-10-16 22:13:34 +02:00
}
2021-12-23 12:54:21 +01:00
function getRow(query, params = []) {
return dbConnection.prepare(query).get(params);
2021-10-16 22:13:34 +02:00
}
function getColumn(query, params = []) {
2021-12-23 12:54:21 +01:00
return dbConnection.prepare(query).pluck().all(params);
2021-10-16 22:13:34 +02:00
}
module.exports = {
getRawRows,
2021-12-23 12:54:21 +01:00
getRow,
2021-10-16 22:13:34 +02:00
getColumn
};