Let's imagine you have a database where documents are created and deleted. But you need to keep a record of the documents that are deleted in an archive. How to setup such an archive with CouchDB? Well, by using the features of CouchDB, and more specifically the replication, it is actually pretty simple.
A CouchDB replication copies the new revisions of the documents from a source database to a target one. It also deletes the documents as they are deleted in the source. So if you do nothing, the target database will not be an archive but just a copy of the source database. The trick is to define a filter that will not propagate the deletion. Here is such as simple filter:
"filters": { "archiveFilter": "function(doc) {return !doc._deleted }" },Note that you can customize this filter so that you archive only specific types of documents.
You can run the replication on demand or continuously. It will be generally better in this case to setup a continuous replication so that you keep your archive up-to-date automatically. The replication document will then look like the following:
{ "_id": "myarchive", "source": { "url": "...source URL...", "headers": { "Authorization": "..token..." } }, "target": { "url": "...target URL...", "headers": { "Authorization": "...token..." } }, "continuous": true, "filter": "archive/archiveFilter" }
No comments:
Post a Comment