Concrete implementation of adapter to MongoDB
Project description
Clean Architecture MongoDB Adapter
Concrete implementation of adapter to MongoDB, using clean architecture adapter layer.
Introduction
If you don’t know the clean architecture pattern, you can learn about on this book.
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 Use
In 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:
from marshmallow import Schema, fields
from uuid import uuid4
class BasicEntity(BasicValue):
def __init__(self, _id=None):
self._id = _id or str(uuid4())
self.adapter = None
def set_adapter(self, adapter):
self.adapter = adapter
def save(self):
my_id = self.adapter.save(self.to_json())
return my_id
def update(self):
my_id = self.adapter.save(self.to_json())
return my_id
def delete(self):
self.adapter.delete(self._id)
@classmethod
def from_json(cls, dict_data):
return cls.Schema().load(dict_data)
def to_json(self):
return self.Schema().dump(self)
def __eq__(self, other):
return all([getattr(self, attr) == getattr(other, attr)
for attr in self.Schema().fields.keys()])
class Schema(Schema):
_id = fields.String(required=True, allow_none=True)
Second, you can see the User model:
from .basic_entity import BasicEntity
class User(BasicEntity):
def __init__(self,
username: str,
password: str = None,
name: str = None,
_id: str = None):
super(User, self).__init__(_id=_id)
self.name = name
self.username = username
self.password = password
class Schema(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_load
def post_load(self, data, many, partial):
return User(**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_adapter import BasicMongodbAdapter
from app_example.app_domain import User
class UserAdapter(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:
from decouple import config
class Config:
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:
from app_example.app_adapters import UserAdapter
from app_examploe.app_domain import User
def get_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)
return adapter
def create_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()
Installation
Install via pip:
$ pip install clean-architecture-mongodb-adapter
Install from source:
$ git clone https://github.com/aberriel/clean_architecture_mongodb_adapter.git $ cd boto $ python setup.py install
History
0.1.0 (2021-01-25)
First release on PyPI.
Implementation of basic CRUD methods (GET, UPDATE, DELETE, LIST), with json node normalization
100% code coverage in tests (TDD)
Initial README and HISTORY
0.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 tests
Updating clean_architecture_basic_classes version, from 0.1.1 to 0.1.3
Adjusting all code by according new line size
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file clean_architecture_mongodb_adapter-0.1.3.tar.gz
.
File metadata
- Download URL: clean_architecture_mongodb_adapter-0.1.3.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29273f03ab809310a40db72a665781972d37f0c1fb4d5e653b404d9e762d5a30 |
|
MD5 | 416067643f5e604f00347f56febbcaf1 |
|
BLAKE2b-256 | 9345166fb781e657ad687af4fd64c0ec0d4c6a2584e8dfd67e6ba83d01928537 |
File details
Details for the file clean_architecture_mongodb_adapter-0.1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: clean_architecture_mongodb_adapter-0.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69b11e259cc26853986563fa7c66275d2f2629495d951d9ec2dc6a68c7a34452 |
|
MD5 | 21fa5ec0e5225b8fcaf3bfd6c6317f59 |
|
BLAKE2b-256 | 51e76f1ff3ebbe67cb49b5743938cfe8a910eb6e6c51802f4e13ce22ee6e60a2 |