CouchDB is my personal BaaS

My various small term and long-term projects need database. As of now I have used various kinds of RDBMS like Postgres, SQLite, NoSQLs like Mongo and Redis. All these four are great and have made me really productive. All four databases are open source and they cover all my requirements as of now. But recently I was in need of database with simple restful api, that can take or spit JSON. My immediate response was Eve+Mongo or sandman+rdbms. Both of them work well. I have used them before. But this time I needed much simpler tool, that would store data, give me restful API and easy interface to maintain. And that's how I ended up using CouchDB.

Apache CouchDB™ is a database that uses JSON for documents, JavaScript for MapReduce indexes, and regular HTTP for its API

The projects I am talking are my personal GPS logger which logs the locations while I am travelling. Couple of small IoT devices, a weather station, data I collect as part of quantified self etc. They are not very complex in terms of data structure. But they need a flexible, stable and simple system.

A simple Backend-as-a-Service platform is what I wanted. Something that was open source and can be hosted by me easily. So Firebase and other similar services got excluded easily. Open Source BaaS has many choices Meteor, Hook , Hoodie, etc. They are all good choices. Some of them are even built on the top of CouchDB. But I choose CouchDB over others for a bunch of reasons.

Futuon UI

Futuon UI. Screen shot by couchdb.org

  1. NoSQL- It's a simple versioned document store. You put the JSON and You get the JSON back. Just like in any other NoSQL its schema-less.
  2. Restful API You access database through restful API. Thats the primary way to get access to the database. Since its restful, you can interact with the database direct from browser or mobile.
  3. Replication synchronizes two copies of the same database and works really well.
  4. PouchDB is a FOSS JavaScript database inspired by CouchDB that runs within the browser. It enables applications to store data locally while offline, then synchronize it with CouchDB. It can sync the data bidirectional or one way. CouchDB/PouchDB together makes them a power combination to build offline first web apps or mobile apps.
  5. Simple JSONP is part of CouchDB. If you want to just access the data. You could use this.
  6. Changes API allows you to listen to database changes in JavaScript. So in a way you can subscribe to database changes over HTTP. You can use built-in API or you could use it with PouchDB. This is really powerful feature. You could do things like updating UI based on DB change.
  7. Futon UI is a native web-based interface built into CouchDB. Its like PHPMyAdmin but much better UI and it comes by default.
  8. Media Attachments can be stored on CouchDB. Even though it's not advised to use it as a file storage, the fact that it can store BLOBs makes it useful in certain situations.
  9. CouchDB has built-in support for Auth and Sessions. You can use the same RESTFUL api to create a web-session. Basically POST username and password to _session and you will receive a cookie. You can use this cookie for all the future transactions. You can also use simple https://username:password@mycouchdburl.com/mydatabase if that suits your taste.
  10. Users can write Validation functions on couchdb. This can be used to validate the documents while inserting or updating.
  11. All the views are MapReduce. You have to write MapReduce to query the database.
  12. All these features are one single installation. And everything happens over http (i prefer https). It makes the system simple, secure and stable.

As of now I use hosted service by IBM called Cloudant. I also have a locally installed Apache CouchDB. Local version gets synced with cloudant once in a while (one way). This works as backup.

In the next set of blog posts I will write about my use-cases and projects. In the mean time let me know what you think.

3 Responses

  1. Wesley Mostien says:

    Imagine this setup for Node/Express/Vue project :

    – desktop app (Electron) : writes to PouchDB (offline first) with a possibility to sync with remote db.
    – web app (online) : uses API server calls to write to CouchDB

    So :

    – PouchDB is on client
    – CouchDB is on server

    These two use the same data and I know I can sync (master-master db) them.

    I’m wondering how I would manage the code to achieve this.

    1. Is it possible to use the same controllers/models to interact with both databases?
    2. Or would I have to write controllers/models on the server to interact with CouchDB and also write other controllers/models on the client to interact with PouchDB?
    3. How would you structure a project like that ?

    Thank you to clarify this, as I can’t seem to find a clear answer for this and I’m a total noob.

  1. October 26, 2017

    […] I want to query. So sometime back I wrote a Python script to download the data every night from CouchDB to SQLite database. Then I would use that database to run my queries. Overtime I have had a set of […]

  2. January 31, 2018

    […] use pinboard API to pull the data and store it on my favourite JSON store CouchDB. You can either run a CouchDB instance on your machine or use a hosted service like Cloudant. I […]