Writing the models

To implement CQRS, we have two representations of entities for the database—a representation serving the CommandStack and another representation to service the QueryStack. We'll create the models.py file.

As usual in Python files, we start with importing the dependencies. First, we should import the native library dependencies and MongoEngine, such as types of fields and the connect function that is responsible for connecting the model with an instance of MongoDB:

import os 
from datetime import datetime 
from mongoengine import ( 
    connect, 
    Document, 
    DateTimeField, 
    ListField, 
    IntField, 
    StringField, 
) 

Then, we add the imports to SQLAlchemy. These imports are for field definition and to indicate what type of SQLAlchemy database ...

Get Microservice Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.