Package: clean-architecture-mongodb-adapter
Clean Architecture MongoDB AdapterConcrete implementation of adapter to MongoDB, using clean architecture adapter layer.IntroductionIf you don’t know the clean architecture pattern, you can learn about on thisbook.Clean architecture pattern, in resume, can develop complex systems with a hight flexible architecture that can maintain easily.
This model provides a set of layers can be isolates all logic levels. Simply, it has these layers:Domain-entity layer: all entity classes that provide system objects or “actors”. In a common “entity layer”, we can instantialize business elements as objects.Data-access layer (adapters): all data access classes.Interactors: where exists all business logic. Here, we implement business use-cases as a flow peocess.API and other edge interfaces: all edge interfaces, as APIs, events (to a desktop app), and others.How to UseIn an example, I have a User model with some common user fields (username, name, password). This model needs to import marshmallow tom implement a model check.
First, I present the basic entity structure:frommarshmallowimportSchema,fieldsfromuuidimportuuid4classBasicEntity(BasicValue):def__init__(self,_id=None):self._id=_idorstr(uuid4())self.adapter=Nonedefset_adapter(self,adapter):self.adapter=adapterdefsave(self):my_id=self.adapter.save(self.to_json())returnmy_iddefupdate(self):my_id=self.adapter.save(self.to_json())returnmy_iddefdelete(self):self.adapter.delete(self._id)@classmethoddeffrom_json(cls,dict_data):returncls.Schema().load(dict_data)defto_json(self):returnself.Schema().dump(self)def__eq__(self,other):returnall([getattr(self,attr)==getattr(other,attr)forattrinself.Schema().fields.keys()])classSchema(Schema):_id=fields.String(required=True,allow_none=True)Second, you can see the User model:from.basic_entityimportBasicEntityclassUser(BasicEntity):def__init__(self,username:str,password:str=None,name:str=None,_id:str=None):super(User,self).__init__(_id=_id)self.name=nameself.username=usernameself.password=passwordclassSchema(BasicEntity.Schema):name=fields.Str(required=False,allow_none=True)username=fields.String(required=True,allow_none=False)password=fields.String(required=True,allow_none=False)@post_loaddefpost_load(self,data,many,partial):returnUser(**data)On a example, if you want to create a new user (on a interactor, for example), first you need to define a User adapter.
Because all persist logic exists on basic adapter, you only need to inherit from basic adapter.
For example:from.basic_mongodb_adapterimportBasicMongodbAdapterfromapp_example.app_domainimportUserclassUserAdapter(BasicMongodbAdapter):def__init__(self,table_name:str,db_name:str,db_url:str,db_username:str,db_password:str):super(UserAdapter,self).__init__(table_name=table_name,db_name=db_name,db_url=db_url,db_username=db_username,db_password=db_password,adapted_class=User)With it, you can create your interactor or other code layer to create users.
In this case, I assume that you use a config.py file with this format:fromdecoupleimportconfigclassConfig:MONGODB_URL=config('MONGODB_URL','mongo_url')MONGODB_DATABASE=config('MONGODB_DATABASE','database_name')MONGODB_USERNAME=config('MONGODB_USERNAME','username')MONGODB_PASSWORD=config('MONGODB_PASSWORD','password')USER_TABLE_NAME=config('USER_TABLE_NAME','user')With it, I instantiate an user adapter and can create easily the user:fromapp_example.app_adaptersimportUserAdapterfromapp_examploe.app_domainimportUserdefget_user_adapter(self):config_obj=Config()adapter=UserAdapter(table_name=config_obj.USER_TABLE_NAME,db_name=config_obj.MONGODB_DATABASE,db_url=config_obj.MONGODB_URL,db_username=config_obj.MONGODB_USERNAME,db_password=config_obj.MONGODB_PASSWORD)returnadapterdefcreate_user(username:str,password:str,name:str):user_adapter=get_user_adapter()user_obj=User(username=username,password=password,name=name)user.set_adapter(user_adapter)user.save()InstallationInstall viapip:$ pip install clean-architecture-mongodb-adapterInstall from source:$ git clone https://github.com/aberriel/clean_architecture_mongodb_adapter.git
$ cd boto
$ python setup.py installHistory0.1.0 (2021-01-25)First release on PyPI.Implementation of basic CRUD methods (GET, UPDATE, DELETE, LIST), with json node normalization100% code coverage in tests (TDD)Initial README and HISTORY0.1.1 (2021-01-31)How to use basic adapter on in clean architecture projects.0.1.3 (2021-11-10)Updating line length to flake8 testsUpdating clean_architecture_basic_classes version, from 0.1.1 to 0.1.3Adjusting all code by according new line size