In part 4 of the blogs, we learned how to use yargs npm package, which helps to get the user command from the terminal without using too much logic in our code. If you did not follow the previous blogs, I suggest you search for the Node.js Module system Part 1,2,3,4 before to dive in this one.
Last time we added the yargs command to add notes, but now in this blog, we are going to also practice other commands like remove, read and list and we will see how to use JSON format to save data and retrieve them from a file.
Remove command:
the remove command is similar to the add command, but instead of add, in the command property we should value of “remove”. We then have the describe property which is optional but describes what this command is for, and in the end, we have our handler function which at the moment print out a message but we going to give it more functionality later.
Now, after you know how to build the “add” and “remove” command, try to add a command to read a specific note and list all the notes. For the moment m make that these commands print out a message and nothing more.
Now is time to learn how to store the data for our note-app, this means that when we get this info, we are passing from the command line we should figure out what to save, access or modify them.
First, we need to use the file system or “fs” core module to save our notes in our file system. These data are going to be stored in a Jason data format. The goal is to create an array of objects where each object is a note, and each object will contain property like the title and body.
Let’s first explore how to work with JSON data and transfer them using the fs. JSON works better with array and objects in Javascript.
We start by creating a person object, feel free to use your name and gender:
const user = {
name: "Moustafa",
gender: "Male"
}
We now need to convert this object into a JSON format, and we going to use the method stringify(), this method takes Javascript value and return the JSON representation.
const userJSON = JSON.stringify(user)
we can now log out this in the terminal to see the results:
It looks similar to our JS object the difference is that it has double quotes in both the properties and the values.
After we saw how to convert object in JSON file, we also need to see the reverse method, which takes a JSON file and convert it into an object. Because we store our data in JSON format, but we cannot work with them or access them until the value is not converted back in Javascript format.
The method we are going to explore now is the “parse()”, which works exactly like the previous one but instead, it gives us back a Javascript object.
const parsedUser = JSON.parse(userJSON)
Now we should get back our original object, and you can test this by printing to the console both value the JSON format and the JS format:
Now we saw how we could store data in JSON format and call back this data by converting them in JS format. Next step is to save this data in a file system and call them back from that file.
We start by loading the fs module:
const fs = require("fs")
now we need to use the method “writeFileSync()” on the fs module which takes two arguments, the name of the file and the data to save.
fs.writeFileSync('users-json.json', userJSON)
By ruing our file now, we are going to create an entirely new file ‘users-json.json’. Inside this file, we are going to have our data stored:
We have built our first JSON file and stored data inside. Next step is to call this data from this file and convert them back in JS.
First, call out all the previous code because we created already our file. The method we are using now is readFileSync(). How the name suggests, this method read the file we are going to pass as an argument in this case “users-json.json, which is the file we just created to store our data.
However, this method does not return JSON data. Instead, we will get a buffer version of that data with bits and bytes. Let explore this in the console
const dataBuffer = fs.readFileSync('users-json.json')console.log(dataBuffer)
output:
To get data in the JSON format as we save them we need to call “toString()” :
console.log(dataBuffer.toString())
Now if we console.log() this info to the console, we can see the JSON representation of the data.
{"name":"Moustafa","gender":"Male"}
To simplify this process, I am going to concatenate the toString() method at the end of the readFuleSync() method:
const dataJSON = fs.readFileSync('users-json.json').toString()
now we need to use the parse() method we explored before to confer this info into a JS object back and be able to access the info:
const data = JSON.parse(dataJSON)
done now we have back our information, and we access “data.name” we can see the name saved:
console.log(data.name)
Output:
Moustafa
In this blog, we learned how to save and access JSON data. In the next blog, we are going to implement this knowledge to save the data coming from the user and access them back.