2024-12-03 04:09:32 +00:00
|
|
|
"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() {
|
2024-12-09 23:24:25 +00:00
|
|
|
const databaseFile = (0, env_1.envString)('DB', 'data/site.db');
|
2024-12-03 04:09:32 +00:00
|
|
|
this.log = (0, log_1.createLog)('db');
|
2024-12-09 23:24:25 +00:00
|
|
|
this.db = new sqlite3_1.Database(databaseFile);
|
|
|
|
this.db.run('PRAGMA journal_mode = WAL;');
|
|
|
|
this.log.info(`DB: ${databaseFile}`);
|
2024-12-03 04:09:32 +00:00
|
|
|
}
|
2024-12-11 02:38:41 +00:00
|
|
|
async run(query, args = []) {
|
2024-12-03 04:09:32 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return this.db.run(query, args, (err, rows) => {
|
|
|
|
if (err)
|
|
|
|
return reject(err);
|
|
|
|
return resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-12-11 02:38:41 +00:00
|
|
|
async all(query, args = []) {
|
2024-12-09 20:15:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return this.db.all(query, args, (err, rows) => {
|
|
|
|
if (err)
|
|
|
|
return reject(err);
|
|
|
|
return resolve(rows);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-12-08 21:51:53 +00:00
|
|
|
toBoolean(val) {
|
|
|
|
return val === 1 ? true : false;
|
|
|
|
}
|
|
|
|
fromBoolean(val) {
|
|
|
|
return val ? 1 : 0;
|
|
|
|
}
|
2024-12-05 19:55:12 +00:00
|
|
|
//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') {
|
2024-12-08 21:51:53 +00:00
|
|
|
values.push(this.fromBoolean(photo[key]));
|
2024-12-05 19:55:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
values.push(photo[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.run(query, values);
|
2024-12-09 20:15:27 +00:00
|
|
|
this.log.info(`Inserted new photo ${photo.name}`);
|
2024-12-05 19:55:12 +00:00
|
|
|
}
|
|
|
|
catch (err) {
|
2024-12-08 21:51:53 +00:00
|
|
|
throw err;
|
2024-12-05 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-09 20:15:27 +00:00
|
|
|
async existsName(name) {
|
|
|
|
const query = `SELECT id FROM photos WHERE name = ? LIMIT 1;`;
|
|
|
|
let rows = [];
|
|
|
|
let exists = false;
|
|
|
|
try {
|
|
|
|
rows = await this.all(query, [name]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error(`Error finding photo by name ${name}`, err);
|
|
|
|
}
|
|
|
|
if (rows.length > 0) {
|
|
|
|
exists = true;
|
|
|
|
}
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
async existsHash(hash) {
|
|
|
|
const query = `SELECT id FROM photos WHERE hash = ? LIMIT 1;`;
|
|
|
|
let rows = [];
|
|
|
|
let exists = false;
|
|
|
|
try {
|
|
|
|
rows = await this.all(query, [hash]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error(`Error finding photo by hash ${hash}`, err);
|
|
|
|
}
|
|
|
|
if (rows.length > 0) {
|
|
|
|
exists = true;
|
|
|
|
}
|
|
|
|
return exists;
|
|
|
|
}
|
2024-12-09 23:24:25 +00:00
|
|
|
async getAll() {
|
2024-12-11 02:38:41 +00:00
|
|
|
const query = `SELECT * FROM photos ORDER BY score ASC, created DESC;`;
|
2024-12-09 23:24:25 +00:00
|
|
|
let rows = [];
|
|
|
|
try {
|
|
|
|
rows = await this.all(query);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error(`Error getting all photos`, err);
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
}
|
2024-12-11 07:03:46 +00:00
|
|
|
async getBsky() {
|
|
|
|
const query = `SELECT * FROM photos WHERE bsky = 0 ORDER BY score ASC, created DESC LIMIT 1;`;
|
|
|
|
let rows = [];
|
|
|
|
try {
|
|
|
|
rows = await this.all(query);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this.log.error(`Error getting all photos`, err);
|
|
|
|
}
|
|
|
|
if (rows.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return rows[0];
|
|
|
|
}
|
|
|
|
async confirmBsky(id) {
|
|
|
|
const query = `UPDATE photos SET bsky = 1 WHERE id = ?;`;
|
|
|
|
try {
|
|
|
|
await this.run(query, [id]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2024-12-09 20:15:27 +00:00
|
|
|
async cacheLocation(location, latlng) {
|
|
|
|
const query = `INSERT OR IGNORE INTO geocode (location, latitude, longitude) VALUES (?, ?, ?);`;
|
|
|
|
try {
|
|
|
|
await this.run(query, [location, latlng.latitude, latlng.longitude]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async getLocation(location) {
|
|
|
|
const query = `SELECT latitude, longitude FROM geocode WHERE location = ? LIMIT 1;`;
|
|
|
|
let rows = [];
|
|
|
|
let res = null;
|
|
|
|
try {
|
|
|
|
rows = await this.all(query, [location]);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//ignore
|
|
|
|
}
|
|
|
|
if (rows.length > 0) {
|
|
|
|
res = {
|
|
|
|
latitude: rows[0].latitude,
|
|
|
|
longitude: rows[0].longitude
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2024-12-03 04:09:32 +00:00
|
|
|
}
|
|
|
|
exports.DB = DB;
|
|
|
|
module.exports = { DB };
|
|
|
|
//# sourceMappingURL=index.js.map
|