To ease load on my MongoDB server, I want to cache some objects from Waterline in Redis. To achieve this, I have to serialize the object to JSON.
My question is, how can I construct my JSON back to an instance of the Waterline model, with datatype handling, member functions etc, making this transparent to the consuming code?
I have also wanted this whenever I run native MongoDB queries, giving me objects with native ObjectIDs, mismatching date types etc.
User.findOne(id, function (err, user) {
// to string and back again, could be stored in cache in the meantime
var object = JSON.parse(JSON.stringify(user));
var user = new User(object); //doesn't work
var user = User.toObject(object); // doesn't work
}