From f756963dbc53d58103fc936e05730d0d65cc46cf Mon Sep 17 00:00:00 2001 From: Yasmika Saubhagya Date: Mon, 12 Apr 2021 20:51:11 +0530 Subject: [PATCH] Improve with more functionality --- .gitignore | 2 + src/dropdb.js | 80 ++++++++++++++++++++++++++++++++++++---- src/util/file-process.js | 37 +++++-------------- 3 files changed, 85 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 69d9865..dcdb40b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ node_modules/ db/ package-lock.json app.js +db.json +.vscode diff --git a/src/dropdb.js b/src/dropdb.js index db83b43..10d9901 100644 --- a/src/dropdb.js +++ b/src/dropdb.js @@ -1,14 +1,12 @@ const { v4 } = require("uuid"); const osPath = require("path"); +const EventEmitter = require("events"); -const { - readFromFile, - readFromFileCb, - writeToFile, -} = require("./util/file-process"); +const { readFromFile, writeToFile } = require("./util/file-process"); -class DropDB { +class DropDB extends EventEmitter { constructor(path) { + super(); if (path) { if (typeof path !== "string") throw new TypeError("DROPDB: Path should be a string value"); @@ -17,8 +15,21 @@ class DropDB { this._path = osPath.join(__dirname, "/db.json"); } + const initDB = async () => { + try { + try { + await readFromFile(this._path); + } catch (err) { + if (err.code === "ENOENT") await writeToFile(this._path, []); + } + } catch (err) { + throw new Error("DROPDB: Init database failed"); + } + this.emit("ready"); + }; + // Clear the file with [] if no data is available - readFromFileCb(this._path, () => {}); + initDB(); } set path(path) { @@ -157,6 +168,61 @@ class DropDB { } } + async deleteById(path, id, cb) { + if (cb) { + if (typeof cb !== "function") + throw new TypeError("DROPDB: Callback should be a function"); + } + + try { + if (typeof path !== "string") + throw new TypeError("DROPDB: Path should be string value"); + if (typeof id !== "string") + throw new TypeError("DROPDB: Id should be passed as data"); + + const dataArray = await readFromFile(this._path); + + let pathData = null; + let pathDataIndex = 0; + for (let item of dataArray) { + if (item[path]) { + pathData = item[path]; + break; + } + pathDataIndex++; + } + + if (pathData !== null) { + let dataItem = null; + let dataItemIndex = 0; + for (let item of pathData) { + if (item.id === id) { + dataItem = item; + break; + } + dataItemIndex++; + } + + // Ready to be removed the data from db + if (dataItem) { + dataArray[pathDataIndex][path].splice( + dataItemIndex, + dataItemIndex + 1 + ); + + await writeToFile(this._path, dataArray); + if (cb) cb(null, dataItem); + } else { + if (cb) cb(null, null); + } + } else { + if (cb) cb(null, null); + } + } catch (err) { + if (cb) cb(err); + } + } + async getPathData(path, cb) { if (typeof cb !== "function") throw new TypeError("DROPDB: Callback should be a function"); diff --git a/src/util/file-process.js b/src/util/file-process.js index 2873e8e..7bfcce1 100644 --- a/src/util/file-process.js +++ b/src/util/file-process.js @@ -3,8 +3,15 @@ const fs = require("fs"); const readFromFile = (path) => { return new Promise((resolve, reject) => { fs.readFile(path, (err, data) => { - if (err) reject(new Error("DROPDB: Could not retrieve data")); - resolve(JSON.parse(data)); + if (err) { + if (err.code === "ENOENT") { + const error = new Error("DROPDB: Could not read data"); + error.code = "ENOENT"; + reject(error); + } else { + reject(new Error("DROPDB: Could not read data")); + } + } else resolve(JSON.parse(data)); }); }); }; @@ -13,36 +20,12 @@ const writeToFile = (path, data) => { return new Promise((resolve, reject) => { fs.writeFile(path, JSON.stringify(data), (err) => { if (err) reject(new Error("DROPDB: Could not write data")); - resolve(true); + else resolve(true); }); }); }; -const readFromFileCb = (path, cb) => { - fs.readFile(path, (err, data) => { - if (err) { - // Write with empty [] if no file exists - if (err.code === "ENOENT") { - writeToFileCb(path, [], () => readFromFileCb(path, cb)); - } else { - throw new Error("DROPDB: Could not read data"); - } - } else { - cb(JSON.parse(data)); - } - }); -}; - -const writeToFileCb = (path, data, cb) => { - fs.writeFile(path, JSON.stringify(data), (err) => { - if (err) throw new Error("DROPDB: Could not write data"); - else cb(); - }); -}; - module.exports = { readFromFile, writeToFile, - readFromFileCb, - writeToFileCb, };