├── LICENSE ├── README.md ├── jsonDB.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ahmet Buğra Çakıcı 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # About Package 2 | 3 | JsonDB is a small and open source project which is usable for JS developers. You can store your DB in a json file easily with this package. 4 | 5 | # Usage 6 | 7 | ## Installation 8 | 9 | `npm i -g js-json-db` 10 | 11 | ## Require 12 | 13 | ```coffeescript 14 | const JsonDB = require("js-json-db"); 15 | const jDB = new JsonDB(); 16 | ``` 17 | 18 | Now you can access package's functions but firstly you should generate a db and this function will be run just one time. 19 | 20 | ## Database & Table Management 21 | 22 | ### Generate Database 23 | 24 | ```coffeescript 25 | const JsonDB = require("js-json-db"); 26 | const jDB = new JsonDB(); 27 | 28 | jDB.generatejDB("sample_db"); 29 | ``` 30 | 31 | We have a database anymore! We also should generate tables in this database. But never forget that you should always use jDB.usejDB function while working on your database. This function always should be active. 32 | 33 | ### Generate Table 34 | 35 | ```coffeescript 36 | jDB.usejDB("sample_db"); 37 | jDB.newTable("table_name",["column1","column2"...]); 38 | ``` 39 | 40 | ### Drop Table 41 | 42 | ```coffeescript 43 | jDB.usejDB("sample_db"); 44 | jDB.dropTable("table_name"); 45 | ``` 46 | 47 | You can specify column names in quotes as second parameter. 48 | 49 | ## Usage Basic Functions 50 | 51 | ### Insert 52 | 53 | ```coffeescript 54 | #jDB.generatejDB("sample_db"); 55 | jDB.usejDB("sample_db"); 56 | 57 | jDB.newTable("student",["student_no","student_name","student_department"]); 58 | jDB.insert("student",[1,"John","Computer Science Department"]); 59 | ``` 60 | 61 | ### Find && FindOne 62 | 63 | ```coffeescript 64 | jDB.usejDB("sample_db"); 65 | 66 | jDB.find("student",(data) => { 67 | console.log(data); 68 | }) 69 | 70 | #w/Filter : 71 | 72 | jDB.find("student",{"student_no":1,"student_name":"John"},(data) => { 73 | console.log(data); 74 | }) 75 | 76 | #Also you can findOne too with filter or without filter 77 | #You can use filter more on as below 78 | 79 | jDB.findOne("student", { "student_no": 1, "student_name": "John" }, (data) => { 80 | console.log(); 81 | }) 82 | ``` 83 | 84 | ### Update 85 | 86 | ```coffeescript 87 | jDB.usejDB("sample_db"); 88 | jDB.update("student", { "student_name": "John" });#This line changes every record student_name with John in student table because we did not use filter parameter as thirdy 89 | #w/Filter : 90 | jDB.update("student", { "student_name": "John" },{ "student_no": 1 });#student_name will change as John which student has number 1 91 | ``` 92 | 93 | ### Delete 94 | 95 | ```coffeescript 96 | jDB.usejDB("sample_db"); 97 | jDB.delete("student");#Pretty similar with update.This line delete all records in student table 98 | #w/Filter : 99 | jDB.delete("student",{"student_no":1}); 100 | ``` 101 | -------------------------------------------------------------------------------- /jsonDB.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | 4 | module.exports = class jsonDB { 5 | constructor() { 6 | if (!fs.existsSync("./jDB")) 7 | fs.mkdirSync("./jDB"); 8 | } 9 | 10 | generatejDB(db_name) { 11 | fs.writeFile(`./jDB/${db_name}.json`, JSON.stringify({}), (err) => { 12 | if (err) throw err; 13 | }); 14 | } 15 | 16 | usejDB(db_name) { 17 | this.db_name = db_name; 18 | this.db = require(`${path.dirname(require.main.filename)}/jDB/${db_name}.json`); 19 | } 20 | 21 | newTable(table, array) { 22 | this.db = {...this.db, 23 | [table]: [{ 24 | [array[0]]: null, 25 | [array[1]]: null, 26 | [array[2]]: null, 27 | [array[3]]: null, 28 | [array[4]]: null, 29 | [array[5]]: null, 30 | [array[6]]: null, 31 | [array[7]]: null, 32 | [array[8]]: null, 33 | [array[9]]: null 34 | }] 35 | } 36 | this.saveChanges(); 37 | } 38 | 39 | dropTable(table) { 40 | delete this.db[table]; 41 | this.saveChanges(); 42 | } 43 | 44 | insert(table, array) { 45 | let keynames = []; 46 | let counter = 0; 47 | this.db[table].slice(0, 1).map(docs => { 48 | Object.keys(docs).map(data => { 49 | keynames[counter] = data; 50 | counter++; 51 | }) 52 | }) 53 | 54 | try { 55 | this.db[table].push({ 56 | [keynames[0]]: array[0], 57 | [keynames[1]]: array[1], 58 | [keynames[2]]: array[2], 59 | [keynames[3]]: array[3], 60 | [keynames[4]]: array[4], 61 | [keynames[5]]: array[5], 62 | [keynames[6]]: array[6], 63 | [keynames[7]]: array[7], 64 | [keynames[8]]: array[8], 65 | [keynames[9]]: array[9], 66 | }) 67 | this.saveChanges(); 68 | } catch (e) { console.log(`${table} table not found`); } 69 | } 70 | 71 | find(table, filter, callback) { 72 | try { 73 | if (typeof filter == "function") { 74 | filter(this.db[table].slice(1)); //callback 75 | } else { 76 | this.db[table].map(data => { 77 | if (data[Object.keys(filter)[0]] == filter[Object.keys(filter)[0]] && data[Object.keys(filter)[1]] == filter[Object.keys(filter)[1]] && data[Object.keys(filter)[2]] == filter[Object.keys(filter)[2]] && data[Object.keys(filter)[3]] == filter[Object.keys(filter)[3]] && data[Object.keys(filter)[4]] == filter[Object.keys(filter)[4]] && data[Object.keys(filter)[5]] == filter[Object.keys(filter)[5]] && data[Object.keys(filter)[6]] == filter[Object.keys(filter)[6]] && data[Object.keys(filter)[7]] == filter[Object.keys(filter)[7]] && data[Object.keys(filter)[8]] == filter[Object.keys(filter)[8]] && data[Object.keys(filter)[9]] == filter[Object.keys(filter)[9]]) { 78 | if (typeof callback == "function") 79 | callback(data); 80 | } 81 | }) 82 | } 83 | } catch (e) { console.log(`${table} table not found`); } 84 | } 85 | 86 | findOne(table, filter, callback) { 87 | try { 88 | if (typeof filter == "function") { 89 | filter(this.db[table].slice(1, 2)); 90 | } else { 91 | this.db[table].some(data => { 92 | if (data[Object.keys(filter)[0]] == filter[Object.keys(filter)[0]] && data[Object.keys(filter)[1]] == filter[Object.keys(filter)[1]] && data[Object.keys(filter)[2]] == filter[Object.keys(filter)[2]] && data[Object.keys(filter)[3]] == filter[Object.keys(filter)[3]] && data[Object.keys(filter)[4]] == filter[Object.keys(filter)[4]] && data[Object.keys(filter)[5]] == filter[Object.keys(filter)[5]] && data[Object.keys(filter)[6]] == filter[Object.keys(filter)[6]] && data[Object.keys(filter)[7]] == filter[Object.keys(filter)[7]] && data[Object.keys(filter)[8]] == filter[Object.keys(filter)[8]] && data[Object.keys(filter)[9]] == filter[Object.keys(filter)[9]]) { 93 | if (typeof callback == "function") 94 | callback(data); 95 | return true; 96 | } 97 | }) 98 | } 99 | } catch (e) { console.log(`${table} table not found`); } 100 | } 101 | 102 | update(table, set, filter) { 103 | try { 104 | if (filter) { 105 | this.db[table].map(data => { 106 | if (data[Object.keys(filter)[0]] == filter[Object.keys(filter)[0]] && data[Object.keys(filter)[1]] == filter[Object.keys(filter)[1]] && data[Object.keys(filter)[2]] == filter[Object.keys(filter)[2]] && data[Object.keys(filter)[3]] == filter[Object.keys(filter)[3]] && data[Object.keys(filter)[4]] == filter[Object.keys(filter)[4]] && data[Object.keys(filter)[5]] == filter[Object.keys(filter)[5]] && data[Object.keys(filter)[6]] == filter[Object.keys(filter)[6]] && data[Object.keys(filter)[7]] == filter[Object.keys(filter)[7]] && data[Object.keys(filter)[8]] == filter[Object.keys(filter)[8]] && data[Object.keys(filter)[9]] == filter[Object.keys(filter)[9]]) 107 | data[Object.keys(set)[0]] = set[Object.keys(set)[0]]; 108 | }) 109 | } else { 110 | this.db[table].map(data => { 111 | data[Object.keys(set)[0]] = set[Object.keys(set)[0]]; 112 | }) 113 | } 114 | this.saveChanges(); 115 | } catch (e) { console.log(`${table} table not found`); } 116 | } 117 | 118 | delete(table, filter) { 119 | try { 120 | let counter = 1; 121 | 122 | if (filter) { 123 | counter = 0; 124 | this.db[table].map(data => { 125 | if (data[Object.keys(filter)[0]] == filter[Object.keys(filter)[0]]) 126 | delete this.db[table][counter]; 127 | counter++; 128 | }) 129 | } else { 130 | this.db[table].map(data => { 131 | delete this.db[table][counter]; 132 | counter++; 133 | }) 134 | } 135 | 136 | let temp_len = this.db[table].length; 137 | for (let i = 0; i < temp_len; i++) { 138 | for (let i = 0; i < this.db[table].length; i++) { 139 | if (!this.db[table][i]) { 140 | this.db[table].splice(this.db[table].indexOf(i), 1); 141 | } 142 | } 143 | } 144 | this.saveChanges(); 145 | } catch (e) { console.log(`${table} table not found`); } 146 | } 147 | 148 | saveChanges() { 149 | fs.writeFile(`./jDB/${this.db_name}.json`, JSON.stringify(this.db), function(err) { 150 | if (err) throw err; 151 | }); 152 | } 153 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-json-db", 3 | "version": "1.0.6", 4 | "description": "JsonDB is a small and open source project which is usable for JS developers. You can store your DB in a json file easily with this package.", 5 | "main": "jsonDB.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ahmetbcakici/js-json-db.git" 12 | }, 13 | "keywords": [ 14 | "js-json-db", 15 | "js-db", 16 | "json-db", 17 | "json-database" 18 | ], 19 | "author": "Ahmet Buğra Çakıcı", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/ahmetbcakici/js-json-db/issues" 23 | }, 24 | "homepage": "https://github.com/ahmetbcakici/js-json-db#readme" 25 | } --------------------------------------------------------------------------------