2
votes

I'm learning CQRS pattern as we going to use it on our new project. And I have few questions so far:

Example task: I'll have cron command to fetch information from different providers (different API's) and responsibility of this cron command is:

  1. fetch data from all provided;
  2. make additional API call's to get images and videos;
  3. process those videos and images (store to aws s3) and to uploads table in DB;
  4. fetch existing data from DB;
  5. transform new API data to system entities, update existing entities and delete nonexistent;
  6. persist DB. ;

CQRS related questions:

  1. Can I have few CQRS commands and queries inside of one system request? In the example above I need to get existing data from DB (query), persist data (command) and so on.
  2. what about the logic of fetching data from API's can I consider it as a CQRS query as its process of getting data or CQRS query it's the only process of getting data from internal storage, not from external API?
  3. What about the process of storing videos to s3 and storing information to uploads table, can I consider the process of storing assets to S3 as a CQRS command and this command will return data I need to store later to uploads? I do not want to store it immediately as upload entity is a part of aggregate to store main info where main info entity is the main aggregate entity. I know command should return nothing or entity ID but here it will return all data about stores assets

If all the questions from above are true, so I can make:

  1. query to fetch API data
  2. query to get existing data
  3. command to process images/videos
  4. command to insert/update/delete data

Don't judge me very strict, I'm in process of learning concepts of DDD and related patterns. And I just ask the questions what is not clear for me. Thank you very much

1

1 Answers

3
votes

Can I have few CQRS commands and queries inside of one system request? In the example above I need to get existing data from DB (query), persist data (command) and so on.

No, you cannot. Each request is either one command or one query.

what about the logic of fetching data from API's can I consider it as a CQRS query as its process of getting data or CQRS query it's the only process of getting data from internal storage, not from external API?

Commands and Queries refer to the local database. Fetching data from external services through remote API is an integration with another BC (see DDD context mapping patterns).

What about the process of storing videos to s3 and storing information to uploads table, can I consider the process of storing assets to S3 as a CQRS command and this command will return data I need to store later to uploads?

Storing videos to s3 is not a command, is an integration with an external service. You will have to integrate (again context mapping pattern).

I do not want to store it immediately as upload entity is a part of aggregate to store main info where main info entity is the main aggregate entity.

I dont know your domain model, but if uploads is a child entity in an aggregate, then storing things in your uploads table isnt a command neither. A command refers to the aggregate. Storing info in uploads table would be a part of the command.

AS A CONCLUSION:

A command or a query is a transactional operation at the application layer boundary (application service). They deal with data from your DB. Each command/query is a transaction.