Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB automatic archiving

I have an angular application using local storage (PouchDB) which automatically sync with a CouchDB database on a remote server.

As I would like to limit my application size, I would like to be able to archive automatically from CouchDB based on a condition.

Here is what I've imagined:

  • I save a file in my localDB which sync with my CouchDB
  • After few days, I tell my localDB to archive this file
  • My localDB delete the file and CouchDB automatically copy the file into another internal database (not sync'ed with the localDB) before removing it.

You could say that I can do this within my app but the problem is when the app is used offline, it cannot directly access the remote CouchDB to copy into another database.

So my question is, can CouchDB automatically copy a doc into another database (within same CouchDB) based on a value in the doc (archived = true for example)? If not, what would you suggest?

like image 207
ncohen Avatar asked Dec 04 '25 14:12

ncohen


1 Answers

To preserve the last non-deleted version of each document, you could set up a continuous replication between your "data" and "backup" couchdb databases with a filter function that prevents _deleted docs from replicating, e.g:

function(doc, req){
    return !doc._deleted
}

To exclusively preserve docs with an archive attribute set, you need to adjust your filter function:

function(doc, req){
    return !doc._deleted && doc.archive
}

However, archiving a document now consists of two steps:

  1. saving it with the archive attribute set to your localDB, triggering its replication to your "data" database (and thus triggering its replication to the "backup" database)
  2. actually deleting the document from the localDB, propagating the delete to your "data" database (with the filter function preventing the delete to propagate to the "backup" database) after confirming replication to the "data" database due to possible fast-forwards in couchdb's replication protocol.
like image 61
skiqh Avatar answered Dec 07 '25 03:12

skiqh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!