PouchDB is a small JavaScript library that provides us with the functionality of a document oriented database. We can use it both on the server and on the client.

The following is the code that I was using in this video clip. Make sure you update the link to the PouchDB JavaScript file before you run it.

<!DOCTYPE html>
    <title>pouchdb sample</title>
    <script src="http://download.pouchdb.com/pouchdb-nightly.js"></script>
    <script type="text/javascript">
        var db = new PouchDB('books');
        function addBook() {
            var booktitle = window.document.bookform.titlefield.value;
            var bookisbn = window.document.bookform.isbnfield.value;
            var bookauthor = window.document.bookform.authorfield.value;
            var book = {
                _id: bookisbn,
                title: booktitle,
                author: bookauthor
            db.put(book, function callback(error, result) {
                if (!error) {
                    document.getElementById("message").innerHTML =
                            "the new book was successfully added";
        function clearFields() {
        function showBooks() {
            db.allDocs( {include_docs: true, descending: true},
                        function(err, doc) {
                        } );
        function showTableOfBooks(data) {
            var div = document.getElementById("message");
            var str = "<table border='1' aligh='left'><tr><th>isbn</th>"+
            for(var i=0; i<data.length; i++)
                str +=  "<tr><td>"+data[i].doc._id+
            str += "</table>";
            div.innerHTML = str;
    <form name="bookform">
        book title <input type="text" name="titlefield" />
        book author <input type="text" name="authorfield" />
        book isbn <input type="text" name="isbnfield" />
        <input type="button" value="add book" onClick="addBook()" />
        <input type="button" value="clear fields" onClick="clearFields()" />
        <input type="button" value="show books" onClick="showBooks()" />
    <div id="message"></div>

You can find the complete sample with all the other files it uses as well as slides and video clips for learning how to use this library in my new PouchDB Basics free course at http://abelski.lifemichael.com.

Leave a Reply

Your email address will not be published. Required fields are marked *