mphasize/sails-generate-ember-blueprints

Using an unserialized object as a model in actionUtil/emberizeJSON

camsjams opened this issue · 0 comments

Hi,

Thanks for the wonderful blueprint!

I'm manually calling the blueprint in one of my controllers for a special case and it's working great.

var actionUtil = require('sails-generate-ember-blueprints/templates/basic/api/blueprints/_util/actionUtil'),
    etc...

User.find(filter)
    .then(function (users) {
        res.ok(actionUtil.emberizeJSON(User, users, req.options.associations, false));
    });

Now I'm trying to add a cache layer to that same Sails controller but was having some issues when it comes to deserializing the stored data.

Perhaps I'm using the library in a way it wasn't intended but basically I'm using this process:

// Get my data from Sails model
User.find(filter)
    .then(function (users) {
        // Manually emberize the JSON data and return response
        res.ok(actionUtil.emberizeJSON(User, users, req.options.associations, false));
        // send the user result down the promise chain as a string
        return JSON.stringify(users);
    })
    .then(function (serializedUsers) {
        // Save results in my cache layer (in this case it's redis via redis-q)
        return client.hsetQ(CONST_REDIS_KEY_CONTENT, 'users', serializedUsers)
            .then(function () {
                return client.expireQ(CONST_REDIS_KEY_CONTENT, sails.config.cache.redis.featured.ttl);
            });
    })

Then on the next request, I check for the cache

client.hgetQ(CONST_REDIS_KEY_CONTENT, 'users')
    .then(function (data) {
        if (data && data.length) {
            data = JSON.parse(data);
            if (data.length && data[0] && data[0].content) {
                // load this stored data and again emberize the data for the response
                res.ok(actionUtil.emberizeJSON(Bump, data, req.options.associations, false));
                return true;
            }
        }
        return false;
    })
    .done(function (hasCache) {
        // original code here to get content based on having cache
    });

The problem comes in the actionUtil function where it tries to prepare the record

This portion of code in sails-generate-ember-blueprints/templates/basic/api/blueprints/_util/actionUtil ends up invoking an undefined function on record, as this time around, the records are just simple objects.

var prepareOneRecord = function ( record ) {
    // get rid of the record's prototype ( otherwise the .toJSON called in res.send would re-insert embedded records)
    record = create( {}, record.toJSON() );

I've added this change to solve this case

var prepareOneRecord = function ( record ) {
    // get rid of the record's prototype ( otherwise the .toJSON called in res.send would re-insert embedded records)
    record = create( {}, record.toJSON ? record.toJSON() : record );

But I wanted to ask:
Is this the proper method for serialized data usage?
If so, would you like a PR with the changes?

Somewhat related to this item:
#41