Synchronizing data from a relational database in Couch DB

I need to synchronize my relational database (Oracle or Mysql) with CouchDb. Does anyone know how this is possible. if possible, than we can notify CouchDb of any changes that have occurred in the relational database.

Thanks in advance.

+6
source share
1 answer

First of all, you need to change the way you model the database. Syncing with CouchDB is not just creating documents for all your tables and transferring them to Couch.

I use CouchDB for the site in production, I will describe what I did, maybe this will help you:

From the very beginning, we used MySQL as our main database. I had entities, including their relationships. In an attempt to speed up the work with the interface, I decided to use CouchDB as a content repository . The advantage was to have fully prepared documents containing all the relational data, so the data can be obtained with much lower overhead. Since documents may contain related objects β€” for example, a questionnaire containing all the answers β€” I first decided which top-level objects I would like to click on Couch. In my example, only questions will be placed on Couch, and these documents will contain answers and possible metadata such as tags, user information, etc. When requesting a question on the interface, I will only need to get one document in order to have all the information I need at this moment.

Now for your second question: how to notify CouchDB of changes. In our case, all changes to our data are performed using the CMS. I have one item in my code that invokes all editing actions. This is the place where I connected to the function that saved saving the object in CouchDB. The function determines whether this object should be stored (i.e., whether it is a top-level object), then creates a document of this object (thinks of some toArray function) and retrieves all its relationships recursively. Then the full document is transferred to CouchDB.

Now, in your case, the variables here can be completely different, but the main idea is the same: find out what documents you want to save, and how they look. Then write the function that composes these documents and make sure that it is called when you make changes to your relational database.

CouchDB Change Notification

CouchDB is very simple. Probably the easiest thing is to directly update an existing document. There are two ways to do this for this:

  • The easiest way is a regular CouchDB update: fetching the current document by id; change it; then send it back to Couch using HTTP PUT or POST.
  • If you have explicit changes for specific applications (for example, β€œthe value of views been increased”), then writing the _update function seems reasonable. The update function is very simple: they receive an HTTP request and a document; they modify the document; and then CouchDB saves the new version. You write update functions in Javascript and they run on the server. This is a great way to β€œcompress” common actions into simpler (and less) HTTP requests.
+6
source

Source: https://habr.com/ru/post/891622/


All Articles