Skip to main content

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)

Metadata

Release files for crud-repository 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crud-repository 0.2.3
File Size Uploaded
crud_repository-0.2.3.tar.gz 15.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crud-repository 0.2.3
File Interpreter ABI Platform
crud_repository-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 32.3 kB

Release files / crud_repository-0.2.3.tar.gz

Download URL crud_repository-0.2.3.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7a1db00d79a3ac85467de9b330def7868aad3002a4d270532ff4b781f8759f9f
BLAKE2b-256 checksum
How to use checksums
27a55af768b2f8970f69f640bce22fb9feba1992b87d00fcf94cc9e2b4945384
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Release files / crud_repository-0.2.3-py3-none-any.whl

Download URL crud_repository-0.2.3-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d4e44402c4ccc6c758287a5fde80ba6927fa108585d0d36801112507eabcdfe
BLAKE2b-256 checksum
How to use checksums
c126088f9e7480fef29ae0758482fddb5110ea0923293f053191a553f4aa0343
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page