Basic CRUD with CouchDB/PouchDB

In my previous post I discussed the basics of setting up CouchDB and PouchDB, in this article I'd like to walk you through some common operations we perform on any database: CRUD (Create-Read-Update-Delete) with CouchDB. I'd also add some SQL reference if you're coming from an SQL background.

Given that we already have our PouchDB database ready,

import PouchDB from 'pouchdb'

const db = PouchDB('my-offline-db')

Creating a Record

To create a database record, PouchDB gives us a ready to use interface with self-descriptive methods. Imagine we want to store peoples information based on their twitter handle, we could start by creating a JSON document and calling a post method to create.

const amustapha = {
   name: "Abdulhakeem",
   email: "abdulhakeemmustatpha@gmail.com",
   country: "NG",
   twitter: "https://twitter.com/_amustapha"
}
db.post(amustapha)

The snippet above is like writing the following in SQL

INSERT INTO profile (name, email, country, twitter) 
VALUE ('Abdulhakeem', ;abdulhakeemmustapha@gmail.com', 'NG', 'https://twitter.com/_amustapha')

When the post function is called, our document is automatically assigned an id, unlike SQL which is often auto-incremented, this is a UUID containing up to 32 characters, and the function also returns a promise.

Reading a Record

To read a database record, we also would refer to the packed in pouchdb helper function get. To run this function, we'll need to pass the id of an existing document.

db.get("unique-doc-id").then(doc => {
/* you can perform any operation on the document returned. */
    console.log(doc)
})

The snippet above is like writing the following in SQL

SELECT * FROM profile WHERE id = 'unique-doc-id'

Updating a Record

Once we have read a record from the database, we could modify and save that record. For example, imagine a user changing their email. A helper method for this is put

db.get("unique-doc-id").then(doc => {
    doc.email = "amustapha@hooli.ng"
    db.put(doc)
})

The snippet above is like writing the following in SQL

UPDATE profile SET email='amustapha@hooli.ng' WHERE id = 'unique-doc-id'

Delete a Record

To delete a database record, simply use the remove function

db.remove("unique-doc-id")

Hope you found this interesting, I'll be writing other articles to explore CouchDB queries in-depth and some really interesting plugins that would make life a lot easier for you. If you found it interesting, like it and share with friends.

Comments (6)

Mardone Dias's photo

Hello,

I have a little problem with couchdb, it's about pagination with startkey, endkey and limit. I can page to the next pages. But, I cannot understand how to keep the previous key to return. I use nodejs + express. If I could pass on some material with an example I would appreciate it. I've looked for a lot, a lot, but, there is nothing but the algorithm of the documentation itself. If you can help me I would appreciate it very much.

Thank you.

Show +3 replies
Mardone Dias's photo

Abdulhakeem Mustapha

Hello,

The documentation says that limit & skip performs poorly with a large amount of data. So I already want to avoid this problem in the future, treating it as efficiently as possible.

My question was how to return to the previous page. And the solution that stores the keys in the frontend will not add complexity to the project.

I appreciate the suggestion, I will do a deeper analysis to see the cost benefit.

Thank you.

Abdulhakeem Mustapha's photo

Mardone Dias

I see. Personally in my experience, I've not hit those performance bottlenecks yet (I've used CouchDB to handle a few thousands of documents). And I'd say, it's quite harder fetching by keys.

However, if that's the case, a workaround could be running an _all_docs to get a list of all documents (just rev and id, without the full document), save those in state/localstorage and use that as basis for performing queries using the ids for start & end key.