Developing applications with couch Db has never been easier with couchdb-query-helper
What the hell is couch Db?
CouchDB is one of what many are calling NoSQL solutions. Specifically, CouchDB is a document-oriented database and within each document, fields are stored as key-value maps. Fields can be either a simple key/value pair, list, or map.
Creating a simple node application using couchdb-query-helper
Before we start off to create a new application, make sure that you have CouchDB and Node Js installed and running in your system.
Installing CouchDB on Ubuntu 16.04
Start by adding the CouchDB GPG key to your system using the following command:
curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | sudo apt-key add -echo "deb https://apache.bintray.com/couchdb-deb bionic main" | sudo tee -a /etc/apt/sources.list
Now that the repository is enabled update the packages list and install CouchDB:
sudo apt update
sudo apt install couchdb
During the installation, you will be asked whether you want to install CouchDB in a standalone or clustered mode. We will install the CouchDB in a single-server standalone mode.
Next, you’ll be given an option to set the IP address of the network interface on which the CouchDB will bind to. For single-server setup leave the default 127.0.0.0
To verify whether the installation has completed successfully run the following curl command which will print the CouchDB database information in JSON format:
curl http://127.0.0.1:5984/
Setting up a simple node application
open up the terminal and create a new folder called sample-app, change into that directory and type in the command.
npm init
Install the required node modules by running the command.
npm install express couchdb-query-helper body-parser --save
Create a new file called index.js and paste the following code.
while setting up CouchDB connection make sure you pass in the required configuration such as the username and password of an admin. If you have not yet created a user, visit your CouchDB admin console and create a new admin account.
creating a database
To create a database you need to call the createDatabase() method of the couchdb-query-helper module, the method expects one argument, the name of the database.
inserting document
After the creation of the database, you can easily insert documents into that database you just created by calling the insert method of couchdb-query-helper. Make sure that you have a unique id while inserting each document.
deleting documents
After successful insertion of the document, you can delete the recently inserted document by calling delete() of couchdb-query-helper. You will need the id of the document that you wanna delete.
selecting documents with condition
you can select documents with condition by passing in two arrays. array_of_key
represents the key that you are searching for andarray_of_values
represents the values that the key should have.
To learn more about couchdb-query-helper, visit the documentation.