diff --git a/testDB.js b/testDB.js new file mode 100644 index 0000000..a0d9079 --- /dev/null +++ b/testDB.js @@ -0,0 +1,56 @@ +const settings = require('./settings'); +const Path = __dirname + settings.dbDir +var api = {} + + +const fs = require('fs').promises; + + +const notFount = 'Error: ENOENT: no such file or directory' + +function print(files){ + console.log(files) +} + +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) +} + +api.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] + 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) + }) +} + +api.get('test', 'documents.json', console.log) +setTimeout(api.get, 1000, 'test2', 'documents.json', console.log)