Monday, March 20, 2017

Introduction to Node.js

Node.js is asynchronous just like JavaScript because of its Non-Blocking I/O nature, that is, the I/O operations are done in separate threads in the C++ thread pool upon which Node.js and JavaScript is built, while the main thread continues to execute the rest of the program. Since the main thread is not blocked for I/O operations, it is said to have a Non-Blocking I/O nature. The I/O operations will finish and inform the main thread, but the main thread will switch to the tasks in the completed queue only when it is free.

Let's start learning Node.js by printing "Hello World" in the console, just like when we started learning other programming and scripting languages.

To print "Hello World" in the console, type the simple command:

console.log("Hello World!");

We can run the file in the command prompt by giving the command: 
node <filename with extension as js> 
For example, if the filename is HelloFirst, then the command should be like: node HelloFirst.js

How to Read from a File and Write to a File

Firstly, we have to import the fs module in order to do the reading, and writing to a file. We can do so by using the require command, and storing the returned value in a variable. For example:

const fs=require('fs');

Keyword "const" makes the variable final, that is, the value stored in such a variable cannot be changed later.

Since we need a file to read from, we can create a text file in the same folder which contains our Node.js file with a sentence in it. For example, we can create a text file named firstfile.txt and add a sentence like "I love coding" and save it. Later on, we will be reading this file using Node,js.

After creating the file, we have to give the location of the file which the reading is supposed to be done from. The system variable __dirname is used to get the directory name of the directory in which the Node.js file is, and using this, we can give the location of the file. We can use this variable since the file we are intending to read from is also located in the same folder as the Node.js file. The following code achieves this part:

const readFromFile = __dirname + '/firstfile.txt';

We also have to give the location where the new file to which we are writing whatever read from the other file is to be stored. We can make use of the variable __dirname for this too if we are planning to save the new file in the same folder as the other two files(Node.js file and the text file). This particular part is achieved by the code below:

const writeToFile = __dirname + '/copiedfile.txt';

As the next step, we create a read stream-which allows you to read data from a source-to the file we are reading from, which is the source file:

const streamForReading = fs.createReadStream(readFromFile);

Since we need a stream to use in order to write whatever read, we will create a write stream-which allows you to write data to a destination-to the file we are writing to, which is the destination file:

const streamForWriting = fs.createWriteStream(writeToFile);

As the final step, we will pipe the read stream to the write stream. Piping simply means that we are passing or feeding the output of one method (reading in this case) as the input to another method (writing in this case), so it is like creating a pipe connection between two methods where the end of one pipe connects to the beginning of another pipe. The code for pipe-lining:

streamForReading.pipe(streamForWriting);

If we go and check the folder with the Node.js file and the text file now, we'll be able to find the new file (named copiedfile.txt in this case) created there with the same content as the original text file we had.







Tuesday, March 14, 2017

Basics of MongoDB

MongoDB is a NoSQL database, which means it is not like a relational database where data is handled using relations. It can be used to handle unstructured data like big data.

In MongoDB, a collection acts more like a table, while a document is more like a record or tuple.

Basic Commands
The following are some of the basic commands used in MongoDB:
  • The use command can be used to create and access a database. For example,
              use companyDB;

The above command will create a database if there is no database existing with the name companyDB. If there is a database already existing with the name companyDB, it can be accessed using this command. If such a database already exists, a new database will not be created from that name.

  • The insert command can be used to insert a document to a collection.
                          db.employee.insert({
                       "name" : "Alice",
                       "dept" : ["Accounting", "Marketing"]
            });

The above command will create a collection named employee, and insert the document with the above data to it, if such a collection is not already existing, but in the case where such a collection already exists(collection named employee in this case), then, the document is inserted into the already existing collection.

Each document is uniquely identified using _id. We can provide _id while inserting the document. If we do not provide it while inserting the document, the MongoDB will assign a value to _id by itself, and that will be the case for the above example; the _id will assigned by the MongoDB since we have not provided it.

If we try to add the same document with the same details again(for example, if we try to add the above one again), the addition will be successful since the MongoDB will provide the new document with a new _id value and add it to the database.

  • The find command can be used to get the details of a document.
                             db.employee.find({"name":"Alice"})

This command returns all the fields in the document with their values. Any field can be used with the find command to retrieve documents with a matching field and value. _id field can also be used with the find command, but for that, we have to know the _id. If there is more than one document with matching values for the search field, then all those documents will be returned.

  • The update command can be used to update data in a document.
     db.employee.update({"name":"Alice"}, {$push:{"dept":"Research"}})

Using the first parameter passed, the MongoDB will search for documents with a matching value for that particular field provided, and using the second parameter, it will update the relevant field with the value to be updated. $push is used to insert a value to an array. In this example, "dept" is an array if we take a look back at the insert command. Therefore, it is used in updating the existing array.


  • The remove command can be used to remove or delete a document.
          db.employee.remove({"name":"Alice"})

This command will remove any number of documents with a matching field and value as the one provided. In this case, any document having the field "name" with its value as "Alice" will be deleted. 





Friday, March 10, 2017

Second Year Project at SLIIT 2016

As our second year project, we developed a Business Management System for the hand loom company in Sri Lanka named Suntex Weaving Industries (PVT) Ltd. It is a stand-alone application implemented using Java.

The need for the development of this software arose when the company decided to computerize their whole system, as most of their tasks were carried out manually, and their computer at that moment handled only basic financial details, and some of the special orders.

We, as a team of eight, divided the scope of the project among us, and handled eight different functionalities, namely:
  • Employee Management
  • Material Management
  • Distribution Management
  • Production Management
  • Order Management
  • Services Management
  • Sales Management
  • Financial Management
Out of these eight functionalities, the one I handled and implemented was Sales Management.

Sales Management
Areas I worked on:
  • Details of products sold by the company in their branches(Includes the current quantity in stock and reorder levels as well).
  • Details of daily sales.
  • Details of all payments(Includes only Full Payments since Half Payments can be done only with Orders). 
  • Generation of Invoice.
  • Calculation of Total Sales for the day or month or year provided by the user.
  • Generation of Records.
Add, Update, Search, and Delete was done for all Products, Sales, and Payments.

In addition to handling Sales Management, I handled the logging in to the system, and the logged in details, and I also worked as the team leader in this project, trying my best to ensure that the time is well-managed by everyone in the team, and putting in my best effort make this a successfully completed project.

Benefits of the system we developed:
There are lots of benefits of using the software developed by us over continuing the manual process in the company, as the system we developed is faster, efficient, and it reduces paperwork and redundancy of data. It is easy to handle data and records using this software as it is less complicated, and is less time-consuming compared to the manual handling of data and records.

Technologies we used:
  • Langauge - Java
  • IDE - NetBeans 8.0
  • Database Management System - MySQL
  • Server - Wamp Server
  • Operating System- Microsoft Windows 10 / 8