Skip to content

Commit

Permalink
Improve with more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yasmikash committed Apr 12, 2021
1 parent a414c7e commit f756963
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules/
db/
package-lock.json
app.js
db.json
.vscode
80 changes: 73 additions & 7 deletions src/dropdb.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
37 changes: 10 additions & 27 deletions src/util/file-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});
};
Expand All @@ -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,
};

0 comments on commit f756963

Please sign in to comment.