PouchDB Jump Start PRO

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>
<html>
<head>
    <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) {
                    clearFields();
                    document.getElementById("message").innerHTML =
                            "the new book was successfully added";
                }
            });
        }
        function clearFields() {
            window.document.bookform.titlefield.value="";
            window.document.bookform.authorfield.value="";
            window.document.bookform.isbnfield.value="";
        }
        function showBooks() {
            db.allDocs( {include_docs: true, descending: true},
                        function(err, doc) {
                            showTableOfBooks(doc.rows);
                        } );
        }
        function showTableOfBooks(data) {
            var div = document.getElementById("message");
            var str = "<table border='1' aligh='left'><tr><th>isbn</th>"+
                    "<th>title</th><th>author</th></tr>";
            for(var i=0; i<data.length; i++)
            {
                str +=  "<tr><td>"+data[i].doc._id+
                        "</td><td>"+data[i].doc.title+
                        "</td><td>"+data[i].doc.author+"</td></tr>"
            }
            str += "</table>";
            div.innerHTML = str;
        }
    </script>
</head>
<body>
    <form name="bookform">
        book title <input type="text" name="titlefield" />
        <br/>
        book author <input type="text" name="authorfield" />
        <br/>
        book isbn <input type="text" name="isbnfield" />
        <br/>
        <input type="button" value="add book" onClick="addBook()" />
        <br/>
        <input type="button" value="clear fields" onClick="clearFields()" />
        <br/>
        <input type="button" value="show books" onClick="showBooks()" />
    </form>
    <div id="message"></div>
</body>
</html>

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.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences