Skip to main content

The CRUDRepository is a Python project designed to provide a generic implementation of Create, Read, Update, and Delete (CRUD) operations for various databases.

Project description

Build test and deploy to package repository


CRUD Repository


Description

The CRUDRepository is a Python project designed to provide a generic implementation of Create, Read, Update, and Delete (CRUD) operations for various databases. It uses the Repository design pattern to abstract the data access layer, allowing for easy switching between different databases using the Factory pattern in which each database object implements a Singleton object.

The project includes classes for handling different types of databases such as PostgreSQL, MySQL, and MariaDB. Each of these classes implements a common Interface, ensuring a consistent method of interaction regardless of the underlying database.

The CRUDRepository also includes a Repository class that provides generic CRUD operations. This class can be used as a base for creating more specific repositories, like the test Repository UserRepository included in the project, which is designed to manage User instances.

The project uses SQLAlchemy for ORM, providing a high-level, Pythonic interface for database operations. It also includes a DatabaseFactory for creating instances of the appropriate database class based on provided configuration.

In summary, CRUDRepository is a flexible and extensible foundation for Python applications that require database interactions, abstracting the complexities of direct database access and providing a clear and simple interface for performing CRUD operations.


Installation

pip install crud-repository

Code Example Usage

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from crud_repository.db.factory import DatabaseFactory
from typing import Optional
from sqlalchemy import Column, Sequence, Integer, String
from sqlalchemy.orm import Mapped
from crud_repository.model.base import Base
from db.idatabase import IDatabase
from crud_repository.repo.repository import Repository


# ---------------------------------------------------------
# Create a User model
# ---------------------------------------------------------
class User(Base):
    __tablename__ = "user"

    id: Mapped[int] = Column(
        Integer,
        Sequence("user_id_seq"),
        primary_key=True,
        autoincrement=True,
        nullable=False,
        unique=True,
        index=True,
    )
    username: Mapped[str] = Column(String(128), nullable=False)
    password: Mapped[Optional[str]] = Column(String(128), nullable=True)

    def to_dict(self) -> dict:
        return {"id": self.id, "username": self.username, "password": self.password}

    def as_dict(self) -> dict:  # renamed from __dict__ to as_dict
        return self.to_dict()

    def __repr__(self) -> str:
        return (
            f"User(id={self.id!r}, name={self.username!r}, fullname={self.password!r})"
        )


# ---------------------------------------------------------
# Create a UserRepository instance with the database instance
# ---------------------------------------------------------
class UserRepository(Repository[User]):
    def __init__(self, database: IDatabase):
        super().__init__(database, User)


# ---------------------------------------------------------
# Create a new user
# ---------------------------------------------------------
if __name__ == '__main__':
    # Create a new database instance
    db_config = {
        'type': 'postgresql',
        'db_name': 'volunteer',
        'user': "postgres",
        'password': "adminpassword",
        'host': "127.0.0.1",
        'port': "5432"
    }
    # Create a new database instance
    db = DatabaseFactory.create(db_config)

    # Create a User Repository instance with the 
    # database instance and the User model
    user_repo = Repository(db, User)

    # Create a new user
    user = User(username='Candy', password='password')

    # Add the user to the database
    user_repo.create(user)

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

crud_repository-0.2.3.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crud_repository-0.2.3-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file crud_repository-0.2.3.tar.gz.

File metadata

  • Download URL: crud_repository-0.2.3.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for crud_repository-0.2.3.tar.gz
Algorithm Hash digest
SHA256 7a1db00d79a3ac85467de9b330def7868aad3002a4d270532ff4b781f8759f9f
MD5 ac1995dc8ba08d7f662357431c228a9c
BLAKE2b-256 27a55af768b2f8970f69f640bce22fb9feba1992b87d00fcf94cc9e2b4945384

See more details on using hashes here.

File details

Details for the file crud_repository-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for crud_repository-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3d4e44402c4ccc6c758287a5fde80ba6927fa108585d0d36801112507eabcdfe
MD5 29c78176497cda57ff0efdcea43d8235
BLAKE2b-256 c126088f9e7480fef29ae0758482fddb5110ea0923293f053191a553f4aa0343

See more details on using hashes here.

Supported by

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