docmenager
Version:
a local documents menager
102 lines (91 loc) • 4.31 kB
JavaScript
const fs = require("fs");
class DataBase {
constructor(dir = new String) {
this.directory = dir;
this.docs = new Array(new Object)
}
async init() {
if (!this.directory.endsWith("/")) this.directory += "/";
if (this.directory.startsWith("./")) this.directory = this.directory.slice(2, this.directory.length);
if (this.directory.startsWith("/")) this.directory = this.directory.slice(1, this.directory.length);
fs.mkdirSync(`${__dirname}/json/${this.directory}`, { recursive: true });
fs.readdirSync(`${__dirname}/json/${this.directory}`).forEach(doc => {
this.docs.push(require(`./json/${this.directory}${doc}`));
})
return this.docs;
}
async createDoc(document) {
let getId = Math.random().toString(36).substring(2, 9);
if (typeof (document) != "object") throw new Error("argument was not an object")
let i = 0;
let _$docInfos = {
properties: Object.keys(document).length,
types: []
}
Object.values(document).forEach(value => {
_$docInfos.types.push(typeof value);
})
document['_$docId'] = getId;
document['_$docInfos'] = _$docInfos
fs.writeFileSync(`${__dirname}/json/${this.directory}${getId}.json`, JSON.stringify(document));
this.docs.push(require(`${__dirname}/json/${this.directory}${getId}.json`))
return document;
}
async findOne(document = new Object) {
if (typeof (document) != "object") throw new Error("argument was not an object")
let isThere = false;
let doc;
this.docs.forEach(async (toSearch = new Object) => {
if (!isThere) {
Object.keys(document).forEach(key => {
if (toSearch[key] == document[key]) isThere = true;
else {
isThere = false;
return;
}
})
if (isThere) doc = toSearch
}
})
if (doc) return doc;
else return undefined;
return new Object
}
async deleteDoc(document = new Object) {
if (typeof (document) != "object") throw new Error("argument was not an object")
await this.findOne(document).then(async doc => {
if (!doc) throw new Error("cannot find document");
fs.unlinkSync(`${__dirname}/json/${this.directory}${doc._$docId}.json`)
this.docs.splice(this.docs.indexOf(doc))
})
}
async updateOne(document = new Object, toUpdate = new Object, options = { newItem: new Boolean, newDoc: new Boolean }) {
if (typeof (document) != "object" && typeof (toUpdate) != "object" && typeof (options) != "object") throw new Error("argument was not an object")
return await this.findOne(document).then(async doc => {
if (!doc) throw new Error("cannot find document");
Object.keys(toUpdate).forEach(key => {
if (key in doc && key != "_$docId" && !Object.values(options)[Object.keys(options).indexOf("newItem")]) {
doc[key] = toUpdate[key]
} else if (Object.values(options)[Object.keys(options).indexOf("newItem")] && key != "_$docId") {
doc[key] = toUpdate[key]
}
})
if (Object.values(options)[Object.keys(options).indexOf("newDoc")]) {
let rand = Math.random().toString(36);
doc["_$docId"] = rand;
fs.writeFileSync(`${__dirname}/json/${this.directory}${rand}.json`, JSON.stringify(doc));
}
let _$docInfos = {
properties: Object.keys(doc).length - 2,
types: []
}
Object.keys(doc).forEach(key => {
if(key != "_$docId" && key != "_$docInfos") _$docInfos.types.push(typeof doc[key]);
})
doc['_$docInfos'] = _$docInfos;
fs.writeFileSync(`${__dirname}/json/${this.directory}${doc._$docId}.json`, JSON.stringify(doc))
return doc
})
}
}
module.exports = DataBase