Building a Todo App with DiskDB

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

DiskDB

Pol and couple of others reached out to me requesting a post on real time usage of DiskDB and here it goes.

I have written another article on how to build the same Todo App with M.E.A.N. stack : MEAN stack – A Hands on Tutorial

I would recommend checking out my MEAN Workshop too to get a better idea on the integration between different layers.

If you did not already know, DiskDB is a file system based database. The only reason I have create DiskDB is to avoid using the mighty MongoDB for managing small scale REST apps.

The Todo app we are going to build will look like

Screen Shot 2014-08-24 at 10.56.05 am Screen Shot 2014-08-24 at 10.56.15 am Screen Shot 2014-08-24 at 10.56.29 am

 

You can find a demo here. You can find the complete code here.

You can also check out Node Webkit powered Dashboard application and Creating a Sentiment Analysis Application Using Node.js, which use DiskDB.

Why DiskDB?

There are many application out there, which have a couple of collections in the DB and a few REST endpoints. All the app is doing is Creating, Reading, Updating and Deleting a few records across collections. Using MongoDB for apps like these is an overkill. Hence DiskDB.

DiskDB stores the data in a JSON format and has a MongoDB like API to interact with the data. DiskDB being a file based DB, you need not worry about using a DB service, when you move your app to a public facing environment, all you data will be saved in files inside the folder you have configured as your DB – on your hosting server (saving bucks!).

Enough rambling about DiskDB, let us get started with the app development.

Getting started with DiskDB

To get a basic idea, we will do a couple of examples. Create a new folder named myDiskDBApp and open a new terminal/prompt here. Run

npm init

to initialize a new node project. Fill it up as applicable.

Now, we will install diskDB as a dependency. Run

npm i --save diskdb

Create a new folder at the root of the project and name it mydb. Next, create a new file named app.js at the root of the project folder.

Open app.js in your favorite editor and add the below code

Line 1 – Requires DiskDB

Line 2 –  Connects to the mydb  folder and looks for a file named fruits. If the file is not present, it will create a new JSON file for you.

Now, let us add a fruit to the collection

Like MongoDB, you can access the collection using its name and calling the API methods. Run the app by executing

node app.js

You can add multiple collections at once

Simple right? You can also query the collection using the find()  or findOne() .

The app.js so far

And the output would beScreen Shot 2014-08-24 at 10.04.43 amYou can update one or many records in a collection by using the options 

 

 

Using this API, we can update the quantity for ‘apple’ like

And finally you can remove an item by simply running

You can always get the count of your collection by running

Building a Todo Application

Now, you have a basic idea of DiskDB. Let us use this to build a simple Todo application with DiskDB as a persistence layer.

I have written a post named MEAN stack – A Hands on Tutorial, where I show how to build a Todo application with MongoDB, Express, Angular and Node. We are going to clone the same project and then modify it to use DiskDB instead of MongoDB.

Before you continue, I would highly recommend completing the above post.

Clone/Download the repo from here. Open a new terminal/prompt inside the cloned/downloaded folder and run

npm install

This will install all the required dependencies. Next, we will uninstall mongojs node module from saved dependencies

npm uninstall mongojs --save

Next, install diskdb as a dependency to the project

npm i diskdb --save 

Now, we will create a new folder inside the server folder named db. This is where we will be storing the Todo collection.

Now, open server/routes/index.js and update it like

That is it!! Your backend is now powered with DiskDB.

When using DiskDB, the response for update function is a bit different. So, we need to update the /client/js/controllers.js. 

Replace line 34 and line 51

with

Save all the files and run

gulp

and navigate to  http://localhost:3000  to view the updated code.

You can also checkout /server/db/todos.json and find the JSON data updating!

Simple and Easy right!!


Thanks for reading! Do comment.
@arvindr21

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • Oliver Mensah

    Hi Sir, Please so can I create a desktop app using the diskdb, html, css, javascript and nwjs. Looking forward to hearing from you.

  • mansh

    hello arvind sir,how can i save data in offline mode when i click on save button……….

    • / Arvind Ravulavaru

      Hey mansh. Offline as in the browser?

  • kiran ps

    Hi Arvind,

    i was using diskdb for a project. one thing i didn’t liked about it was inoder to perform operations on database you have use something like this

    db.collectionName.save(object);

    the problem with this code is that you cannot specify the connection name dynamically, i made some changes in diskdb, now i use some thing like this

    db.save(collectionName, object);

    what do you think, should i use this way or is there any alternative method

    • kiran ps

      i saw in a commet that you can use

      db[filename].save(data);
      this thing is not mentioned in the documentation or there is a syntax error in the documentation

  • supriya kale

    Hi Arvind,

    This is really great tute to start with. I had another requirement where I need to retrieve just values of attribute irrespective of conditions. What would be the query in that case?

    var fruits = [{
    name: ‘pineapple’,
    qty: 5}, {
    name: ‘banana’,
    qty: 8
    }, {
    name: ‘pear’,
    qty: 1
    }];

    I just want to retrieve names of all the fruits here, could you please help me with the query?

    Thanks

    • / Arvind Ravulavaru

      Hello Supriya,

      Thanks. There is not straight forward way to do this. You can try the below approach

      Thanks.

  • kiran ps

    Hi,
    Is there any method to add diskdb in angular project + node webkit

    • / Arvind Ravulavaru

      Hello Kiran,

      Take a look at /node-webkit-powered-dashboard-app/

      Thanks.

      • kiran ps

        i was using angular for my dashboard application
        i wanted to where should i put require(‘diskdb’) in angular
        i used it as a service, working perfectly

        • / Arvind Ravulavaru

          Service seems to be an appropriate place for diskDB.

  • Kshitij Gugale

    Hi,

    Thanks for the tutorial.

    However, I am facing an issue while creating the “collection” based on GUID.

    I am storing the information based on user logged in. I am creating the GUID and giving the same name to collection.

    But I am getting error as: TypeError: Cannot call method ‘save’ of undefined.

    Snippet:

    var filename = generateGUID(); // User defined function which will give me the GUID.

    var db = require(‘diskdb’);

    db = db.connect(‘mydb’,[‘/’+filename+’/’]); //JSON file gets created of name GUID successfully.

    var data = /* Sample JSON data */

    db.filename.save(data); //Error is occurring at this point.

    I might be doing silly mistake on above line but unable to resolve.

    Kindly help and thanks in advance.

    • / Arvind Ravulavaru

      Hey Kshitij, Thanks!

      Update

      to

      Thanks.

      • Kshitij Gugale

        Thanks Arvind.

        Before come to see your reply, I did the same thing and that worked.
        But thank you for the same and quick reply.
        +1.

        Thanks,
        Kshitij

  • Andichamy

    Hi dude,

    Your tutorials are really awesome. All the posts are very helpful. I have a small doubt in this topic.
    How to install diskDB in windows ?

    Thanks for your support to the javascript community. Keep doing it great man :)

    • / Arvind Ravulavaru

      Thanks Andichamy!

      DiskDB is not an actual Database, it is a node module that makes managing file system based JSON documents easy. Since all the data is stored in the format of collections and documents, I thought having an API like MongoDB would make consuming the JSON files easy.

      To install diskdb in you local project, you can run

      Thanks.