diff --git a/helpers/dataFilter.js b/helpers/dataFilter.js index ede3a61..82eae31 100644 --- a/helpers/dataFilter.js +++ b/helpers/dataFilter.js @@ -1,7 +1,12 @@ +const log = require("../utils/log"); const getAllData = require("./getAllData") function dataFilter(arrayName, condition) { const dataAll = getAllData(); + if (dataAll[arrayName] == undefined) { + log(`${arrayName} array not found.`, "Error", "error"); + return undefined + } return dataAll[arrayName].filter(condition) } diff --git a/helpers/dataFind.js b/helpers/dataFind.js index 7246520..6083257 100644 --- a/helpers/dataFind.js +++ b/helpers/dataFind.js @@ -1,7 +1,12 @@ +const log = require("../utils/log"); const getAllData = require("./getAllData") function dataFind(arrayName, condition) { const dataAll = getAllData(); + if (dataAll[arrayName] == undefined) { + log(`${arrayName} array not found.`, "Error", "error"); + return undefined + } return dataAll[arrayName].find(condition) } diff --git a/helpers/removeData.js b/helpers/removeData.js index b0a2458..1993b8b 100644 --- a/helpers/removeData.js +++ b/helpers/removeData.js @@ -1,8 +1,13 @@ const getAllData = require("./getAllData"); const writeDataJson = require("./writeDataJson"); +const log = require("../utils/log"); function removeData(arrayName, dataId) { var dataAll = getAllData(); + if (dataAll[arrayName] == undefined) { + log(`${arrayName} array not found.`, "Error", "error"); + return undefined + } const index = dataAll[arrayName].findIndex(x => x.id == dataId); dataAll[arrayName].splice(index, 1); writeDataJson(JSON.stringify(dataAll)); diff --git a/helpers/updateData.js b/helpers/updateData.js index 222d855..4972ce8 100644 --- a/helpers/updateData.js +++ b/helpers/updateData.js @@ -1,16 +1,34 @@ const getAllData = require("./getAllData"); const writeDataJson = require("./writeDataJson"); +const log = require("../utils/log"); function updateData(arrayName, data) { var dataAll = getAllData(); + + if (dataAll[arrayName] == undefined) { + log(`${arrayName} array not found.`, "Error", "error"); + return undefined + } + + if (!data.id) { + log(`There is no id data!`, "Error", "error"); + return undefined + } + const index = dataAll[arrayName].findIndex(i => i.id === data.id); + + if (index == -1) { + log(`No data matching id found!`, "Error", "error"); + return undefined + } + Object.entries(dataAll[arrayName][index]).forEach(entry => { const [key, value] = entry; if (key != "id") { dataAll[arrayName][index][key] = data[key]; } }); - writeDataJson(JSON.stringify(dataAll)); + writeDataJson(JSON.stringify(dataAll, null, 4)); } module.exports = updateData; \ No newline at end of file diff --git a/operations/add.js b/operations/add.js index 6c2f3ec..875eb79 100644 --- a/operations/add.js +++ b/operations/add.js @@ -16,6 +16,7 @@ function add(arrayName, data) { newData[arrayName].push(data); } writeDataJson(JSON.stringify(newData, null, 4)); + return data; } module.exports = add; \ No newline at end of file diff --git a/operations/data/db.json b/operations/data/db.json new file mode 100644 index 0000000..aa0d905 --- /dev/null +++ b/operations/data/db.json @@ -0,0 +1,12 @@ +{ + "products": [ + { + "id": 1, + "categoryId": 2, + "productName": "Chai", + "quantityPerUnit": "48 - 6 oz jars", + "unitPrice": "30", + "unitsInStock": 53 + } + ] + } \ No newline at end of file diff --git a/operations/getById.js b/operations/getById.js index b6b8a3d..f6a7330 100644 --- a/operations/getById.js +++ b/operations/getById.js @@ -1,7 +1,12 @@ const getAll = require("./getAll"); +const log = require('../utils/log'); function getById(arrayName, dataId) { var dataAll = getAll(); + if (dataAll[arrayName] == undefined) { + log(`${arrayName} array not found.`, "Error", "error"); + return undefined + } return dataAll[arrayName].find(x => x.id == dataId) } diff --git a/package.json b/package.json index b7eb83c..1d02f01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dbcopycat", - "version": "0.4.1", + "version": "0.5.1", "description": "A JSON Database that saves your Json data in a file and makes it easy for you to perform CRUD operations.", "main": "local.js", "license": "ISC",