const fs = require('fs').promises const settings = require('./settings') //TODO: use a propper file type and search method //TODO: allow encryption const Path = __dirname + settings.dbDir + '/' module.exports = {} function checkCollection(path, callback, args){ const files = fs.readdir(path) function fn(files){ callback(files, args) } files.catch(function(err){ console.log('catch') if(err.toString().substr(0, notFount.length) == notFount){ p = fs.mkdir(path) p.catch((err) => { throw err }) p.then(() => { checkCollection(path, callback, args) }) }else{ throw err } }) files.then(fn) } module.exports.get = function(coll, file, fn){ const path = Path + '/' + coll checkCollection(path, get, [path, file, fn]) } function get(files, args){ const path = args[0], file = args[1], fn = args[2] delete args if(files.indexOf(file) == -1){ return fn('file not found', null) } data = fs.readFile(path + '/' + file) data.catch((err) => { fn('error reading file', null) }) data.then((content) => { fn(false, content) }) } module.exports.write = function(coll, file, data, fn){ const path = Path + '/' + coll checkCollection(path, write, [path, file, data, fn]) } function write(files, args){ const path = args[0], file = args[1], data = args[2], fn = args[3] delete args data = fs.writeFile(path + '/' + file, data) data.catch((err) => { fn('error write file') }) data.then(() => { fn(false) }) }