/red-bird

Repository Patterns for Python

Primary LanguagePythonMIT LicenseMIT

Red Bird

Repository Patterns for Python

Generic database implemetation for SQL, MongoDB and in-memory lists


Pypi version build codecov Documentation Status PyPI pyversions

Repository pattern is a technique to abstract the data access from the domain/business logic. In other words, it decouples the database access from the application code. The aim is that the code runs the same regardless if the data is stored to an SQL database, NoSQL database, file or even as an in-memory list.

Read more about the repository patterns in the official documentation.

Why?

Repository pattern has several benefits over embedding the database access to the application:

  • Faster prototyping and development
  • Easier to migrate the database
  • More readable code, Pythonic
  • Unit testing and testing in general is safer and easier

Features

Main features:

  • Support for Pydantic models for data validation
  • Identical way to create, read, update and delete (CRUD)
  • Pythonic and simple syntax
  • Support for more complex queries (greater than, not equal, less than etc.)

Supported repositories:

  • SQL
  • MongoDB
  • In-memory (Python list)
  • JSON files
  • CSV file

Examples

First, we create a simple repo:

from redbird.repos import MemoryRepo
repo = MemoryRepo()

Note: the following examples work on any repository, not just in-memory repository.

Adding/creating items:

repo.add({"name": "Anna", "nationality": "British"})

Reading items:

repo.filter_by(name="Anna").all()

Updating items:

repo.filter_by(name="Anna").update(nationality="Finnish")

Deleting items:

repo.filter_by(name="Anna").delete()

See more from the official documentation.

Author