thenativeweb / cqrs-sample

CQRS, EventSourcing, (DDDD) Sample in node.js
148 stars 50 forks source link

[Question] how to do async within defineCommand #21

Closed Jan-Jan closed 7 years ago

Jan-Jan commented 7 years ago

I want to do an async action in the callback of require('cqrs-domain').defineCommand(options, callback). (I want to use bcrypt should by async.) How would one go about it?

Eg,

module.exports = require('cqrs-domain').defineCommand({
  name: 'verbNoun'
}, (data, aggregate) => {
  doSomethingAsync(data.value, result => {
    aggregate.apply('nounVerb', result)
  })
})
adrai commented 7 years ago

Not at all. Why not sending all information you need in the cmd (message). So make the async stuff before sending de command to the domain.

Jan-Jan commented 7 years ago

Based on @adrai's answer in https://github.com/adrai/node-cqrs-domain/issues/67 I might be able to use the precondition to do something async and add that tot the aggregate. Will report back if I am successful.

Jan-Jan commented 7 years ago

Cannot set a value on the aggregate in the precondition step :-( So back to square one...

adrai commented 7 years ago

Manipulating the aggragate is only possible in the defineEvent handler