Skip to main content

Repository Patterns for Python

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redbird-0.6.1.tar.gz (139.2 kB view hashes)

Uploaded Source

Built Distribution

redbird-0.6.1-py3-none-any.whl (41.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page