GithubHelp home page GithubHelp logo

pouchdb's Introduction

Semantic Web and Linked Data - Exercise 6

Introduction to PouchDB

In this exercise we will have a look at PouchDB, which is a CouchDB clone written in JavaScript. It is a document-oriented database system. We'll mainly follow the PouchDB Getting Started Guide in this exercise.

Exercises

  1. Download this repository and install pouchdb in the package folder.

    > npm install --save pouchdb
  2. In index.js, use require to include pouchdb in your code.

    var PouchDB = require('pouchdb');
  3. Create a new PouchDB database to store to-dos.

    var db = new PouchDB('todos');
  4. Add a function to create a to-do to the database.

    function addTodo(text) {
      
      var todo = {
    	_id: new Date().toISOString(),
    	title: text,
    	completed: false
      };
      
      db.put(todo, function callback(err, result) {
    	if (!err) {
    	  console.log('Successfully posted a todo!');
    	}
      });
    
    }
  5. Add a function to retreive all the to-dos in the database and print them on the console.

    function showTodos() {
      db.allDocs({include_docs: true, descending: true}, function(err, doc) {
    	console.log(doc.rows);
      });
    }
  6. Add a function to update a to-do. For now the only update we'll allow is to have the to-do marked as completed.

    function completeTodo(todo) {
      todo.completed = true;
      db.put(todo);
    }
  7. Add a function to delete a to-do

    function deleteTodo(todo) {
      db.remove(todo);
    }
  8. Tell PouchDB to show us the to-dos on the console every time we change the database.

    db.changes({
      since: 'now',
      live: true
    }).on('change', showTodos);
  9. Create three to-dos: "Clean dog", "Wash car", "Drink coffee".

    addTodo("Clean dog");
    ...
  10. List all the todos.

    showTodos();

Advanced exercises

  1. Have showTodos store all of the to-dos in an array.

  2. Update a to-do.

  3. Delete a to-do.

  4. Use express to create a todos HTTP API.

Notes

pouchdb's People

Contributors

ianmcloughlin avatar

Watchers

James Cloos avatar James Ngondo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.