Problem
I have a database of recordings recordingsDB
sent from a client application which uses pouchDB. Each recording is it's own document. e.g.
{
"_id": "2019-10-20T04:51:22.744Z",
"_rev": "1-53e73be51ea76f96e3cfde2de1502439",
"locale": "en",
"_attachments": {
"recordings.webm": {
"content_type": "audio/webm; codecs=opus",
...
}
}
}
The client application has a single user clientUser
(so anyone accessing the website becomes clientUser
). The client user can therefor write to recordingsDB
but also read from recordingsDB
which is not desirable because they can read any recordings from recordingsDB
.
I went down "write only" database road and came across this answer from 2011. I'm wondering how to implement that.
Idea
So what I believe would work is if my recordingsDB
would replicate all of the recordings to a masterRecordingsDB
where the clientUser
is no longer a member. After each document from the recordingsDB
is copied to the masterRecordingsDB
it would be deleted.
Setup
I have configured a replicator which copies the content from recordingsDB to masterRecordingsDB.
{
"_id": "recordings_to_master_recordings",
"_rev": "3-a6fa84dd89c2ec037353c17f65b4c765",
"continuous": true,
"source": {
"url": "http://localhost:5984/recordings",
"headers": {
"Authorization": "Basic YWRtaW46cGFzc3dvcmQ="
}
},
"target": {
"url": "http://localhost:5984/master_recordings",
"headers": {
"Authorization": "Basic YWRtaW46cGFzc3dvcmQ="
}
},
"user_ctx": {
"name": "admin",
"roles": [
"_admin"
]
},
"owner": "admin"
}
How can I delete the items in recordingsDB after each successful replication?
There are two methods I can think of to approach your problem.
Normally, in CouchDB, document deletion is a bit of a misnomer. When a document is deleted, due to the append-only nature of CouchDB, a new revision of the given document is created, consisting solely of its ID, revision, and the field
_deleted
which is true. This 'tombstone' as it's called allows for the deletion to be replicated across all databases. However, the tombstone remains in the database and takes up (albeit very small) space. You could implement this yourself, and just create a revision of the document that looks something like{"_id": "0", "_rev": "1-62657917", "_deleted": true}
. It is important to note that documents with the deleted flag will not be returned in requests anymore.The second option for deletion is
purge
. This will permanently remove all references to a given document from a database. This is not the standard form for deletion in couchDB, information on how to format a purge request is linked below.More information:
CouchDB Docs on Deletion
CouchDB Docs on Purge
And finally, a helpful article about deletion in CouchDB