0
votes

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?

1
Have you try Mango or Map/Reduce? - Alexis Côté
No. Because Mango and Map/reduce would be helpful if there are more than 1 documents to process and to fetch limited no. of data/fields. Here the situation is quite different. I want to process an array of json objects residing in a single document and the expected result should be a json array. That won't be possible using mango query or map/reduce. Please correct me if i'm wrong. - Vandana Jain
Can you do this with Show function? Probably. What's the best option? I think you should just process it in your application layer. Using show/list functions are not recommended from what I have read - Alexis Côté
Thanks Alexis! Can you please share the link for what you have mentioned ("Using show/list functions are not recommended from what I have read") - Vandana Jain
Look at this Joan Touzet's presentation - Alexis Côté

1 Answers

0
votes

To use lodash you may put lodash code as a string into, say, .lodash branch of your design document. Then you can use lodash inside your show function putting require("lodash") into show fn code.

Using show functions, however, implies some limitations:

  • you only have 64Mb of RAM for any query server instance, so your processed doc can not be very long;

  • query server uses old SpiderMonkey engine, which means you have only ES5 onboard, so no arrow functions and other ES6 niceties;

  • built-in query server code (like shows/lists) tend to be painful for debugging.

It might be reasonable in some cases, like if you plan to distribute your code using sync mechanics, or do not want to decouple some of your biz logic bits from map/reduce code.

However, in most cases if you have application layer, better use application layer.