thenativeweb / node-cqrs-eventdenormalizer

Node-cqrs-eventdenormalizer is a node.js module that implements the cqrs pattern. It can be very useful as eventdenormalizer component if you work with (d)ddd, cqrs, domain, host, etc.
http://cqrs.js.org/pages/eventdenormalizer.html
MIT License
38 stars 27 forks source link

Update multiple viewmodels question #83

Closed ssipos90 closed 5 years ago

ssipos90 commented 5 years ago

Hi,

I am implementing a tree structure based on nested set and when adding a node in the tree, some other nodes need updates. What I have in mind is handling the domain event in 2 view builders, one adding the node and one updating the other nodes but the problem is I can't specify in the query attribute of the viewBuilder the ID of the tree for example.

module.exports = require('cqrs-eventdenormalizer') .defineViewBuilder(
    {
      name: 'nodeAdded',
      aggregate: 'node',
      context: 'nodes',
      version: 0,
      query: {
          // here
      },
      autoCreate: false,
      priority: 1
    },
    function(data, vm, cb) {
        // ...
    });

On my mind only using a function for the query attribute would do the trick.

...
query: function (ev /*, cb */) {
    return {
        treeId: ev.aggregate.id // in my case the domain aggregate is the tree
    }
},
...
ssipos90 commented 5 years ago

Also, I have a question, how would you optimize to run some update query on many models? Reading from the docs "A lot of viewmodels can slow down the denormalization process!", how would you tackle if you have to update multiple models?

adrai commented 5 years ago

There is a useAsQuery function, see readme

ssipos90 commented 5 years ago

Haven't read all the comments in the readme (talk about RTFM...).

Thanks!