I am using couchdb and nodejs for server side. I have a document with all transactions history array saved on couchdb. The array can have multiple transactions with same trnxId with different status. I need to filter the history based on some parameters and to fetch latest records but not deleted ones.
Doc in couchDB
{
"_id": "hist",
"_rev": "3-62da7472a24652fd5cf42e01bcef294f",
"type": "history",
"status": "ACTIVE",
"accounts": {
"payment": [
{
"trnxId": "5784365893938",
"trnxDate": "2017-11-07T06:12:15.000Z",
"trnxMode": "cash",
"amount": 50,
"trnxStatus": "new",
"accName": "Cash",
"category": "others",
"comments": "no comments"
}
],
"expense": [],
"transfer": []
}
}
For filtering, i'm using below code in node js:
var __ = require('lodash');
function fetchAllHist(docObj, trnxType) {
var trnxArray = docObj.accounts[trnxType];
var histArray = [];
for (var i = trnxArray.length - 1; i >= 0; i--) { //inverse loop as trnxArray is having data in ascending order over creation date
var trnxId = trnxArray[i].trnxId;
if (__.findIndex(histArray, { 'trnxId': trnxId }) == -1) {
histArray.push(trnxArray[i]);
}
}
histArray = __.reject(histArray, { 'trnxStatus': "deleted" });
return histArray;
};
Can i do the same processing in couchDB itself using Show function? I this case, i won't be able to use lodash functions. Please guide me which way is better, either node js or Show function of couchDB?