rfink / sequelize-redis-cache

Small fluent interface for caching sequelize database query results in redis more easily
MIT License
174 stars 47 forks source link

Multiple find with promise #29

Open krisnamargono opened 7 years ago

krisnamargono commented 7 years ago

Hello, is it possible to query twice and then save it into redis? I kinda confused on how to explain it. So here is my example code.

var cacheObj = models.cacher(models.sequelize, models.rc)
                .model('model1')
                .ttl(60);

            var allModel1Data = [];

            cacheObj.findAll({}).then(function(model1) {
                var allModel1Data = JSON.parse(JSON.stringify(model1));

                return models.Model2.findAll({})
            }).then(function(model2) {
                for(var i = 0; i < allModel1Data.length; i++) {
                    allModel1Data[i].Model2Array = [];

                    for(var j = 0; j < model2.length; j++) {
                        if(model2[j].Model1Id == allModel1Data[i].Id) {
                            allModel1Data[i].Model2Array.push(model2[j]);
                        }
                    }
                }

                return res.json({
                    success: true,
                    model1: allModel1Data
                })
            })

In the 'allModel1Data', it should be containing all the data combined from Model1 and Model2. But instead of that, it only returns []. Am I wrong? Thanks for your help!