Skip to main content

Repository Patterns for Python

Project description

Red Base: Repository Patterns for Python

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

NOTE: Experimential.

Examples

class Person(BaseModel):
    id: str
    name: str
    age: int

Creating

>>> repo = Repository()

# Add some items
>>> repo.add(Person(id="11-11-11", name="Jack", age=30, language="English"))
>>> repo.add(Person(id="22-22-22", name="John", age=33, language="English"))
>>> repo.add(Person(id="33-33-33", name="James", age=36, language="English"))
>>> repo.add(Person(id="44-44-44", name="Jaakko", age=40, language="Finnish"))

Reading

# Get an item
>>> repo["11-11-11"]
Person(id="11-11-11", name="Jack", age=30, language="English")

>>> # Filter items
>>> repo.filter_by(language="English").all()
[Person(id="11-11-11", name="Jack", age=30, language="English"),
 Person(id="22-22-22", name="John", age=33, language="English"),
 Person(id="33-33-33", name="James", age=36, language="English")]

>>> # Get first
>>> repo.filter_by(language="English").first()
Person(id="11-11-11", name="Jack", age=30, language="English")

>>> # Get last
>>> repo.filter_by(language="English").last()
Person(id="33-33-33", name="John", age=33, language="English")

>>> # Get first 2
>>> repo.filter_by(language="English").limit(2)
[Person(id="11-11-11", name="Jack", age=30, language="English"),
 Person(id="22-22-22", name="John", age=33, language="English")]

>>> # Use operations
>>> from redbase.operations import greater_than
>>> repo.filter_by(age=greater_than(35)).all()
[Person(id="33-33-33", name="James", age=36, language="English"),
 Person(id="44-44-44", name="Jaakko", age=40, language="Finnish")]

Updating

>>> # Update single item
>>> person = repo["44-44-44"]
>>> person.age = 50
>>> repo.update(person)

>>> # Update multiple items
>>> repo.filter_by(language="English").update(age=35)

Deleting

>>> # Delete single item
>>> del repo["44-44-44"]

>>> # Delete multiple items
>>> repo.filter_by(language="English").delete()

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

redbase-0.0.0.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

redbase-0.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file redbase-0.0.0.tar.gz.

File metadata

  • Download URL: redbase-0.0.0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for redbase-0.0.0.tar.gz
Algorithm Hash digest
SHA256 4c24ae040e63f00924c2cd45c6f044da0484aeb7845878a598f1c68ff4f6ef6f
MD5 aaacbf30c8cc45ae98b9a6e71e69b26f
BLAKE2b-256 cf9c0534e988f4fc335b58b7921f815ae983d1b23fe60db82479713ffd906645

See more details on using hashes here.

File details

Details for the file redbase-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: redbase-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

File hashes

Hashes for redbase-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99d2e7a458aca31e8a4b89b8513449decc44b64db217f4814a380dbd8452deb6
MD5 c0eb8a7e878f96dc2e0e95a54bc3f989
BLAKE2b-256 59b716bfe2c518c71ee3913cb911932132dc81d7612de4a177bb5e76840dcf70

See more details on using hashes here.

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