This repository has been archived on 2025-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
noteApp/testDB.js
Mats van Reenen d7535b418c add db
2020-07-29 10:14:54 +02:00

57 lines
1.2 KiB
JavaScript

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)