53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.DB = void 0;
|
|
require("dotenv/config");
|
|
const log_1 = require("../log");
|
|
const sqlite3_1 = require("sqlite3");
|
|
const env_1 = require("../env");
|
|
class DB {
|
|
constructor() {
|
|
this.log = (0, log_1.createLog)('db');
|
|
this.db = new sqlite3_1.Database((0, env_1.envString)('DB', 'data/site.db'));
|
|
this.db.run('PRAGMA journal_mode = WAL;');
|
|
}
|
|
async run(query, args = null) {
|
|
return new Promise((resolve, reject) => {
|
|
return this.db.run(query, args, (err, rows) => {
|
|
if (err)
|
|
return reject(err);
|
|
return resolve(true);
|
|
});
|
|
});
|
|
}
|
|
toBoolean(val) {
|
|
return val === 1 ? true : false;
|
|
}
|
|
fromBoolean(val) {
|
|
return val ? 1 : 0;
|
|
}
|
|
//CASE WHEN LOWER(active) = 'true' THEN 1 ELSE 0 END AS active_bool
|
|
async create(photo) {
|
|
const keys = Object.keys(photo);
|
|
const query = `INSERT INTO photos (${keys.join(',')}) VALUES (${keys.map(el => '?').join(',')});`;
|
|
const values = [];
|
|
for (let key of keys) {
|
|
if (typeof photo[key] === 'boolean') {
|
|
values.push(this.fromBoolean(photo[key]));
|
|
}
|
|
else {
|
|
values.push(photo[key]);
|
|
}
|
|
}
|
|
try {
|
|
await this.run(query, values);
|
|
this.log.info(`Inserted new photo`);
|
|
}
|
|
catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
exports.DB = DB;
|
|
module.exports = { DB };
|
|
//# sourceMappingURL=index.js.map
|