Note that there are some explanatory texts on larger screens.

plurals
  1. POSequelize.js afterUpdate hook pass changed values
    primarykey
    data
    text
    <p>I'm building a node.js app and I'm evaluating Sequelize.js for persistent objects. One thing I need to do is publish new values when objects are modified. The most sensible place to do this would seem to be using the afterUpdate hook.</p> <p>It almost works perfectly, but when I save an object the hook is passed ALL the values of the saved object. Normally this is desirable, but to keep the publish/subscribe chatter down, I would rather not republish fields that weren't saved.</p> <p>So for instance, running the following</p> <pre><code>tasks[0].updateAttributes({assignee: 10}, ['assignee']); </code></pre> <p>Would automagically publish the new value for the assignee for that task on the appropriate channel, but not republish any of the other fields, which didn't change.</p> <p>The closest I've come is with an afterUpdate hook:</p> <pre><code>Task.hook('afterUpdate', function(task, fn) { Object.keys(task).forEach(function publishValue(key) { pubSub.publish('Task:'+task.id+'#'+key, task[key]); }); return fn(); }); </code></pre> <p>which is pretty straightforward, but since the 'task' object has all the fields, I'm being unnecessarily noisy. (The pubSub system is ignorant of previous values and I'd like to keep it that way.)</p> <p>I could override the setters in the task object (and all my other objects), but I would prefer not to publish until the object is saved. The object to be saved doesn't seem to have the old values (that I can find), so I can't base my publish on that.</p> <p>So far the best answer I've come up with from a design standpoint is to tweak one line of dao.js to add the saved values to the returned object, and use that in the hook:</p> <pre><code>self.__factory.runHooks('after' + hook, _.extend({}, result.values, {savedVals: args[2]} ), function(err, newValues) { Task.hook('afterUpdate', function(task, fn) { Object.keys(task.savedVals).forEach(function publishValue(key) { pubSub.publish('Task:'+task.id+'#'+key, task[key]); }); return fn(); }); </code></pre> <p>Obviously changing the Sequelize library is not ideal from a maintenance standpoint.</p> <p>So my question is twofold: is there a better way to get the needed information to my hook without modifying dao.js, or is there a better way to attack my fundamental requirement?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload