0

I'm using LokiJs as an in-memory cache for an application that stores data in CouchDB and allows retrieval through a NodeJS server. Per the documentation on constructing queries, the results of my query are assigned to a result set object:

let result = combinedCache.find({'left.errorCode': errorCode});

This combinedCache collection is the result of an .eqJoin of two other collections:

    var cache = new loki('loki.db');

    errorCodeCache = cache.addCollection('errorCodes');
    messageCache = cache.addCollection('messages');
    errorCodeCache.insert(docs.errorCodes);
    messageCache.insert(docs.messages);
    combinedCache = errorCodeCache.eqJoin(
        messageCache,
        'messageId',
        '_id'
    );

This creates two collections, an errorCodeCache collection and a messageCache collection. The combinedCache is the result of the join on the messageId of the errorCode docs and the _id of the message docs. The size of the resulting combinedCache is well over 1000 when I check using combinedCache.data().length.

However, after I perform a query against the cache, the size of the original collection is changed to the size of the result; in other words, if my query returns 1 CouchDB document, combinedCache's size is now 1. I couldn't find any reference in the documentation that stated that .find queries on a collection mutate the results of the collection itself. If this is the case, how can the size of the collection be preserved? Thanks!

1 Answers1

0

For anyone else who sees this, I managed to figure it out using this article.

// branch just long enough to determine count of customers under 30 without filter affecting irishCustomers results
var lt30count = irishCustomers.copy().find({ ‘age’: { ‘$lt’ : 30 }).data().length;

So the solution is to add .copy() to create a temporary copy of the data if you do not want the filter to modify the collection.