I am currently involved in the development of hybrid mobile applications using PouchDB (client-side) and CouchDB (server-side) to store data. Everything worked pretty smoothly until I decided to implement the security of the data stored in PouchDB. To do this, I use transform-pouch , with which I encrypt the data in the inbound method and decrypt the data in the outbound method.
Below is a snippet of code.
var transformFunctions = {
incoming: function(doc) {
Object.keys(doc).forEach(function(field) {
if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
if (service.isJsonObject(doc[field])) {
doc[field] = CipherService.encrypt(JSON.stringify(doc[field]), "password");
} else {
doc[field] = CipherService.encrypt(doc[field], "password");
}
}
});
return doc;
},
outgoing: function(doc) {
Object.keys(doc).forEach(function(field) {
if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
var decData = CipherService.decrypt(doc[field], "password");
if (service.isJsonString(decData)) {
doc[field] = JSON.parse(decData);
} else {
doc[field] = decData;
}
}
});
return doc;
}
};
service.commonDB.transform(transformFunctions);
Now the problem that I encountered especially arises in live replication, in the outgoing function it receives already decrypted data. I already defined the modified PouchDB event.
localChanges = service.localDB.changes({
since: 'now',
live: 'true',
include_docs: true
}).on('change', function(change) {
var docData = change.doc;
if (!change.deleted) {
if (docData.endUser) {
$rootScope.$broadcast("endUser", docData);
} else {
$rootScope.$broadcast(docData._id, docData);
}
} else {
$rootScope.$apply(function() {
$rootScope.$broadcast('delete', docData._id);
});
}
}).on('paused', function(info) {
console.log(JSON.stringify(info));
}).on('error', function(err) {
console.log(err);
});
. , .
, transform-pouch ? - ?
(Single-shot) , . .
.

, .
Live PouchDB.
- ?