Skip to content

Commit

Permalink
Added find, filter and error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
ismailcankaratas committed Jun 1, 2022
1 parent 99cbc93 commit c128d0f
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 30 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ const dbcopycat = require('dbcopycat');
dbcopycat.update(arrayName, data);
```

## :blue_square: filter(arrayName, condition)
Filters data by condition

```
const dbcopycat = require('dbcopycat');
dbcopycat.filter("arrayName", x => x.id == 1)
```

## :blue_square: find(arrayName, condition)
Returns a single data suitable for the condition

```
const dbcopycat = require('dbcopycat');
dbcopycat.find("arrayName", x => x.categoryid == 1)
```
18 changes: 18 additions & 0 deletions helpers/dataControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');

function statPath(path) {
try {
return fs.statSync(path);
} catch (ex) { }
return false;
}

function dataControl() {
var exist = statPath('./data/db.json');
if (exist && exist.isFile()) {
return true
} else {
return false
}
}
module.exports = dataControl;
8 changes: 8 additions & 0 deletions helpers/dataFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getAllData = require("./getAllData")

function dataFilter(arrayName, condition) {
const dataAll = getAllData();
return dataAll[arrayName].filter(condition)
}

module.exports = dataFilter;
8 changes: 8 additions & 0 deletions helpers/dataFind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getAllData = require("./getAllData")

function dataFind(arrayName, condition) {
const dataAll = getAllData();
return dataAll[arrayName].find(condition)
}

module.exports = dataFind;
46 changes: 25 additions & 21 deletions helpers/initdb.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const fs = require('fs');
const path = require('path');
const log = require('../utils/log');
const dataControl = require('./dataControl');

function init() {
fs.mkdir('data', function (error) {
if (error) console.log(error);
console.log('Data folder created.');
});
if (!dataControl()) {
fs.mkdir('data', function (error) {
console.log('Data folder created.');
});

fs.writeFile('data/db.json', `{
"products": [
{
"id": 1,
"categoryId": 2,
"productName": "Chai",
"quantityPerUnit": "48 - 6 oz jars",
"unitPrice": "30",
"unitsInStock": 53
}
]
}`, function (error) {
if (error) console.log(error);
console.log("db.json created.");
})
}
fs.writeFile('data/db.json', `{
"products": [
{
"id": 1,
"categoryId": 2,
"productName": "Chai",
"quantityPerUnit": "48 - 6 oz jars",
"unitPrice": "30",
"unitsInStock": 53
}
]
}`, function (error) {
if (error) console.log(error);
console.log("db.json created.");
})
} else {
log("Data already exists", "Error", "error")
}

}
module.exports = init;
6 changes: 5 additions & 1 deletion local.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ const getById = require('./operations/getById');
const add = require('./operations/add');
const deleteById = require('./operations/deleteById');
const update = require('./operations/update');
const filter = require('./operations/filter');
const find = require('./operations/filter');

module.exports.getAll = getAll;
module.exports.getById = getById;
module.exports.add = add;
module.exports.deleteById = deleteById;
module.exports.update = update;
module.exports.update = update;
module.exports.filter = filter;
module.exports.find = find;
7 changes: 7 additions & 0 deletions operations/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const dataFilter = require('../helpers/dataFilter');

function filter(arrayName, condition) {
return dataFilter(arrayName, condition);
}

module.exports = filter;
7 changes: 7 additions & 0 deletions operations/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const dataFind = require('../helpers/dataFind');

function find(arrayName, condition) {
return dataFind(arrayName, condition);
}

module.exports = find;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dbcopycat",
"version": "0.2.1",
"version": "0.3.2",
"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",
Expand Down
12 changes: 5 additions & 7 deletions utils/log.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const alert = require('cli-alerts');

module.exports = info => {
module.exports = (msg, title, type, info) => {
alert({
type: `warning`,
name: `DEBUG LOG`,
msg: ``
type: type,
name: title,
msg: msg
});

console.log(info);
console.log();
if (info) console.log(info);
};

0 comments on commit c128d0f

Please sign in to comment.