Convention for use of Pouchdb with Couchdb

Asked

Viewed 286 times

1

I am developing a simple application with pouchDB and couchDB and have the following questions:

  1. In a normal, relational database (mysql for example) I would have several tables. The equivalent structure in Pouch/Couch would be to create a database for each table? How would this be done.
  2. Continuing the above question, assuming that between these mysql tables I have FK relations, what is the best way to do this in Pouch/Couch?

2 answers

2


Hello, the comparison between Mysql and Pouchdb is complicated, we are talking about different paradigms between databases. Mysql is a relational database (Entity/Relationship) and Puchdb is non-relational (Nosql). One of the main characteristics of a Nosql database is to be atomic, since it has no transactional control.

More about Nosql

Comparative table between terms:

SQL concept     PouchDB concept
table           no equivalent
row             document
column          field
primary key     primary key (_id)
index           view

Let’s have your questions:

1) You would have several documents

2) You would have the relationship (PK) in the same document, for example an array (hobbies):

{
 "_id": "mittens",
 "name": "Mittens",
 "occupation": "kitten",
 "age": 3,
 "hobbies": [
   "playing with balls of yarn",
   "chasing laser pointers",
   "lookin' hella cute"
 ]
}

Complete documentation of Pouchdb

  • Thank you, assuming in mysql I have a table products, one requests and a connecting pedi_products, in the Pouchdb I created a new DB for each table, this escapes from the concepts of Pouchdb, I’m new to the subject, what would be the best way to create this Pouchdb

  • Creates only an order json and an array of products within the order. The relationship table pedido_produtos no need. Like the example above, between person and hobby

  • Got it, thanks, but in the case of the same product for multiple orders duplicates the product?

  • Yes, repeat.. This is one of the side effects of Nosql banks.. This may result in a lack of synchronization between equal products.. It’s up to you to write an application that controls this. If you need a database that you care about Normalização data instead of performance/data volume.. recommend using relational databases.. such as Mysql..

1

Answering your first question: for each table you can create a new document, which is much more practical than creating a database for each table.

As for the second question: noSQL database (couchDB, for example) does not have fk or primary key, but the 1:1, 1:N and N:N relationships are still the same as those applied to relational databases. Then your product table, order and ordering:

product:

    {
     "_id": "100",
     "type": "produto",
     "nome": "mamão",
     "preco": 5.4,
   }

request:

    {
     "_id": "200",
     "type": "pedido",
     "data": "12/02/2019",
    }

product:

    {
     "_id": "1",
     "type": "pedido_produto",
     "pedido_id": "200",
     "produto_id": "100"
   }

And in order for you to be able to fetch all this data in couchDB at once,:

    function (doc) {
      var pedidoID, produtoID;  
      if(doc.type == "pedido_produto"){
       pedidoID = doc.pedido_id;
       produtoID = doc.produto_id;
      }
      if(doc.type == "pedido"){
       emit(pedidoID, {pedido: doc});
      }
     if(doc.type == "produto"){
      emit(produtoID, {produto: doc});
     }
    }

After saving, so you can consume this view in your application, just make a request via ajax, in your javascript:

    function buscarView() {
       $.ajax({
        url: 'http://127.0.0.1:5984/nome_do_banco/_design/view/_view/view? 
        include_docs=true',
        type: 'GET',
        success: function (response) {
          //aqui você manipula o resultado
        },
        error: function (err) {
          var t = err.responseText;
          console.log("Erro de requisição: " + err.responseText);
        }
      });
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.