const low = require('lowdb'); const FileSync = require('lowdb/adapters/FileSync'); const paths = require('../data/paths.json'); // get our paths const settings = require('../data/settings.json'); // get the settings const defaults = settings.defaults || {}; // load the defaults // connect the db from the path const connect = (dir, opts) => { let options = opts || Object.assign({}, defaults, paths[dir]); // baseline the options let adapter = new FileSync(options.database); // init the db adapter let db = low(adapter); // connect the db return db; }; // find a file in the db const find = (db, file) => { return db .get('files') .find({ input: file, }) .value(); // does it already exist? }; // set a file in the db const set = async (db, file, payload) => { if (!payload && typeof file === 'object') { db.get('files').push(file).write(); return file; } return db .get('files') .find({ input: file, }) .assign(payload) .write(); }; // remove a file from the db const remove = async (db, file) => { return db .get('files') .remove({ input: file, }) .write(); // remove file from database }; module.exports = { connect, find, remove, set };