JSON is somehow powerful. With Node JS and JSON you may build almost anything you can think of (if you have unlimited time and coffee/beer).
How about building a small console application with the following options:
Yesterday, I started to build a console application, similar to this one, but still not the same and in Java Script. The current one is running over a JSON file, making some editions with it. The program has the following features:
add enables you to add a new entry to the JSON
get gives information for the entry on a specific id
remove removes entry from the JSO
update updates entry at JSON
search searches in JSON and returns the data (the get searches only per IDs, this is more general)
load loads a new JSON variable from a file
nice prints the JSON file in a beautiful table, adjustable by the size of the variables. I have used hardcoding in the function promptNiceCommand();, which I am not proud of, but the result was quite satisfactory.
save saves the JSON variable into a file
quit quits the application
Indeed, a fully functioning application. Loading DB, editing it, searching it, displaying it and saving the results. Well, the interface is a little chabby indeed, but it works:
Here comes the code. Make sure you have “file.json” in the same dir, containing something like this to have the application running smoothly:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[{"id":"10", "name":"Peter", "email":"peter@bg.com" }, { "id":"20", "name":"Sasho", "email":"Sasho@bg.com" }, { "id":"30", "name":"Gosho", "email":"gosho3@bg.com"}] |
Here comes the code, enjoy it responsibly :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
var prompt = require('prompt'); prompt.start(); var jsonFile = [{ "id":"11000000000000", "name":"DuckTalesCharacter", "email":"setralalair1@bg.com" }, { "id":"222", "name":"DuckTalesCharacterDui", "email":"seir2@bg.com" }, { "id":"3000", "name":"Lui", "email":"seir3@bg.com" }] function promptSearchCommand(){ console.log("Please, write an element to search:") prompt.get(['searched'], function(err, result){ var sSearchedItem = result.searched; for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].id){ console.log(jsonFile[i].id); console.log(jsonFile[i].name); console.log(jsonFile[i].email); main(); } }; for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].name){ console.log(jsonFile[i].id); console.log(jsonFile[i].name); console.log(jsonFile[i].email); main(); } }; for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].MyNewName){ console.log(jsonFile[i].id); console.log(jsonFile[i].name); console.log(jsonFile[i].email); main(); } }; }) } function promptLoadCommand(){ var jf = require('jsonfile') var util = require('util') var file = 'file.json' jsonFile = jf.readFileSync(file) console.log("A file has been loaded!") main(); } function promptGetCommand(){ console.log("Please, write the id to search:") prompt.get(['id'], function(err, result){ var sSearchedItem = result.id for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].id){ console.log (jsonFile[i].id); console.log (jsonFile[i].name); console.log (jsonFile[i].email); } }; main(); }) } function promptRemoveCommand(){ console.log("Please, write the id to delete:") prompt.get(['id'], function(err, result){ var sSearchedItem = result.id for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].id){ jsonFile.splice(i,1) } }; main(); }) } function promptUpdateCommand(){ console.log("Please, write the id to update:") prompt.get(['id','MyNewName','MyNewEmail'], function(err, result){ var sSearchedItem = result.id; for (var i = jsonFile.length - 1; i >= 0; i--) { if (sSearchedItem == jsonFile[i].id){ jsonFile[i].name = result.MyNewName; jsonFile[i].email = result.MyNewEmail; main(); } }; console.log("Wrong ID. No update generated!") }) } function promptMenuCommand(){ console.log(jsonFile); main(); } function promptAddItem(){ prompt.get(['id', 'name', 'email'], function(err, result){ console.log('Data entry confirmed'); var newID = result.id; var newName = result.name; var newEmail = result.email; var objToAdd = { "id":newID, "name":newName, "email":newEmail } jsonFile[jsonFile.length]=objToAdd; main(); }) } function promptSaveCommand(){ var jf = require('jsonfile') var file = 'myData.json' jf.writeFileSync(file, jsonFile) console.log("A file has been saved!") main(); } String.prototype.repeat = function(length) { return Array(length + 1).join(this); }; function promptNiceCommand(){ var l1 = 0 var l2 = 0 var l3 = 0 var c1 = 0 var c2 = 0 var c3 = 0 for (var i = jsonFile.length - 1; i >= 0; i--) { if (jsonFile[i].id.length>l1){ l1 = jsonFile[i].id.length; } if (jsonFile[i].name.length>l2){ l2 = jsonFile[i].name.length; } if (jsonFile[i].email.length>l3){ l3 = jsonFile[i].email.length; } } c1 = "id".length c2 = "name".length c3 = "email".length console.log("%s", "_".repeat(l1+l2+l3+14)) //14 is the sum of the added spaces and vertical lines between names console.log("|| %s %s|| %s %s|| %s %s||", "id", " ".repeat(l1-c1), "name", " ".repeat(l2-c2),"email"," ".repeat(l3-c3)) console.log("%s", "_".repeat(l1+l2+l3+14)) for (var i = jsonFile.length - 1; i >= 0; i--) { c1 = jsonFile[i].id.length c2 = jsonFile[i].name.length c3 = jsonFile[i].email.length console.log("|| %s %s|| %s %s|| %s %s||", jsonFile[i].id, " ".repeat(l1-c1), jsonFile[i].name, " ".repeat(l2-c2), jsonFile[i].email, " ".repeat(l3-c3)) } console.log("%s", "_".repeat(l1+l2+l3+14)) main(); } function main(){ console.log("Enter a command: - add list nice get remove update search load save end") prompt.get(['command'], function(err, result){ if (result.command == "list") { promptMenuCommand(); }else if (result.command == "add"){ promptAddItem(); }else if (result.command == "get"){ promptGetCommand(); }else if (result.command == "remove"){ promptRemoveCommand(); }else if (result.command == "update"){ promptUpdateCommand(); }else if (result.command == "search"){ promptSearchCommand(); }else if (result.command == "load"){ promptLoadCommand(); }else if (result.command == "nice"){ promptNiceCommand(); }else if (result.command == "save"){ promptSaveCommand(); }else{ console.log("Thank you for using our program!"); console.log("Have a nice day!"); return; }; })o } main(); |
That’s all folks! The code in GitHub is here. Finally:
Probably the answer is “NO”, but I like the picture.