Package: pydbrepo
PyDBRepoIs a simple implementation of the Repository pattern to access data in python, providing extensibility flexibility
and builtin tools to manage databases with this pattern.Supported databasesSQLiteMySQLPostgreSQLMongoDBAmazon QLDBRequirementsPython >= 3.7Postgrespsychopg2-binaryMysqlmysql-connector-pythonMongoDBpymongodnspythonAmazon QLDBpyqldbExamplesEntity usageEntity modelThis class brings the build it in methods:to_dict: Will take all properties of the created class and will convert it into a dict instance.from_dict: This will take a dict instance and will set the values of every key into a model property with
the same name.from_record: It takes an ordered Iterable object with the name of the fields that will be loaded into the model,
and a tuple with the corresponding valuesEntity models will be used with simple class properties or can be used with theFielddescriptor of the packageExample with simple propertiesfrompydbrepoimportEntityclassModel(Entity):id=Nonename=Nonemodel=Model.from_dict({"id":1,"name":"some"})# Model({"id": 1, "name": "some"})print(model.id)# => 1print(model.name)# => someExample with property decoratorsfrompydbrepoimportEntityclassModel(Entity):def__init__(self):super().__init__()self.id=Noneself.name=None@propertydefid(self):returnself._id@id.setterdefid(self,value):self._id=value@propertydefname(self):returnself._name@name.setterdefname(self,value):self._name=valuemodel=Model.from_dict({"id":1,"name":"some"})# Model({"id": 1, "name": "some"})print(model.id)# => 1print(model.name)# => someExample with Field descriptorfrompydbrepoimportEntity,Field,named_fields@named_fieldsclassModel(Entity):id=Field(type_=int)name=Field(type_=str)model=Model.from_dict({"id":1,"name":"some"})# Model({"id": 1, "name": "some"})print(model.id)# => 1print(model.name)# => someExample of casting values with Field descriptorfromuuidimportUUIDfrompydbrepoimportEntity,Field,named_fields@named_fieldsclassModel(Entity):id=Field(type_=(UUID,str),cast_to=UUID,cast_if=str)name=Field(type_=str)model=Model.from_dict({"id":'10620c02-d80e-4950-b0a2-34a5f2d34ae5',"name":"some"})# Model({"id": UUID('10620c02-d80e-4950-b0a2-34a5f2d34ae5'), "name": "some"})print(model.id)# => 10620c02-d80e-4950-b0a2-34a5f2d34ae5print(model.name)# => someExample of casting from a callback functionfromdatetimeimportdate,datetimefrompydbrepoimportEntity,Field,named_fieldsdefcast_epoch(value):ifisinstance(value,date):returnint(value.strftime("%s"))ifisinstance(value,datetime):returnint(value.timestamp())@named_fieldsclassModel(Entity):name=Field(type_=str)epoch=Field(type_=(int,date,datetime),cast_to=cast_epoch,cast_if=(date,datetime))model=Model.from_dict({"name":"some","epoch":datetime.now()})# Model({"name": "some", "epoch": 1231231231})print(model.name)# => someprint(model.epoch)# => 1231231231Example of iterable fields and casting with Field descriptorfrompydbrepoimportEntity,Field,named_fields@named_fieldsclassItem(Entity):name=Field(type_=str)price=Field(type_=float)@named_fieldsclassModel(Entity):id=Field(type_=int)name=Field(type_=str)items=Field(type_=list,cast_items_to=Item)model=Model.from_dict({"id":1,"name":"some","items":[{"name":"some","price":5.99},{"name":"nothing","price":6.99},]})# Model({"id": 1, "name": "some", "items": [Item({"name": "some", "price": 5.99}), Item({"name": "nothing", "price": 6.99})]})print(model.id)# => 1print(model.name)# => someprint(model.items)# => [Item({"name": "some", "price": 5.99}), Item({"name": "nothing", "price": 6.99})]print(model.items[0].price)# => 5.99