crud-repository


Namecrud-repository JSON
Version 0.2.3 PyPI version JSON
download
home_pagehttps://github.com/dellius-alexander/CRUDRepository.git
SummaryThe CRUDRepository is a Python project designed to provide a generic implementation of Create, Read, Update, and Delete (CRUD) operations for various databases.
upload_time2024-05-11 15:49:17
maintainerNone
docs_urlNone
authorDellius Alexander
requires_pythonNone
licenseMIT License Copyright (c) 2024 Dellius Alexander Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords crud-repository database model abstraction sqlalchemy
VCS
bugtrack_url
requirements sqlalchemy psycopg2-binary python-dotenv colorlog cryptography pymysql SQLAlchemy-Utils
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build test and deploy to package repository](https://github.com/dellius-alexander/CRUDRepository/actions/workflows/deploy.yml/badge.svg)](https://github.com/dellius-alexander/CRUDRepository/actions/workflows/deploy.yml)

---

# 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
    
```bash
pip install crud-repository
```

## Code Example Usage

```python
#!/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)
```
---


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dellius-alexander/CRUDRepository.git",
    "name": "crud-repository",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": "Dellius Alexander <dalexander@hyfisolutions.com>",
    "keywords": "crud-repository, database, model, abstraction, sqlalchemy",
    "author": "Dellius Alexander",
    "author_email": "Dellius Alexander <dalexander@hyfisolutions.com>, info@hyfisolutions.com",
    "download_url": "https://files.pythonhosted.org/packages/27/a5/5af768b2f8970f69f640bce22fb9feba1992b87d00fcf94cc9e2b4945384/crud_repository-0.2.3.tar.gz",
    "platform": null,
    "description": "[![Build test and deploy to package repository](https://github.com/dellius-alexander/CRUDRepository/actions/workflows/deploy.yml/badge.svg)](https://github.com/dellius-alexander/CRUDRepository/actions/workflows/deploy.yml)\n\n---\n\n# CRUD Repository\n\n---\n## Description\n\nThe CRUDRepository is a Python project designed to provide a \ngeneric implementation of Create, Read, Update, and Delete (CRUD) \noperations for various databases. It uses the Repository design pattern \nto abstract the data access layer, allowing for easy switching between \ndifferent databases using the Factory pattern in which each database \nobject implements a Singleton object.  \n\nThe project includes classes for handling different types of databases \nsuch as PostgreSQL, MySQL, and MariaDB. Each of these classes implements \na common Interface, ensuring a consistent method of interaction \nregardless of the underlying database.  \n\nThe CRUDRepository also includes a Repository class that provides generic \nCRUD operations. This class can be used as a base for creating more specific \nrepositories, like the test Repository UserRepository included in the project, which is \ndesigned to manage User instances.  \n\nThe project uses SQLAlchemy for ORM, providing a high-level, Pythonic \ninterface for database operations. It also includes a DatabaseFactory for \ncreating instances of the appropriate database class based on provided \nconfiguration.  \n\nIn summary, CRUDRepository is a flexible and extensible \nfoundation for Python applications that require database interactions, \nabstracting the complexities of direct database access and providing a \nclear and simple interface for performing CRUD operations.\n\n---\n\n## Installation\n    \n```bash\npip install crud-repository\n```\n\n## Code Example Usage\n\n```python\n#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\nfrom crud_repository.db.factory import DatabaseFactory\nfrom typing import Optional\nfrom sqlalchemy import Column, Sequence, Integer, String\nfrom sqlalchemy.orm import Mapped\nfrom crud_repository.model.base import Base\nfrom db.idatabase import IDatabase\nfrom crud_repository.repo.repository import Repository\n\n\n# ---------------------------------------------------------\n# Create a User model\n# ---------------------------------------------------------\nclass User(Base):\n    __tablename__ = \"user\"\n\n    id: Mapped[int] = Column(\n        Integer,\n        Sequence(\"user_id_seq\"),\n        primary_key=True,\n        autoincrement=True,\n        nullable=False,\n        unique=True,\n        index=True,\n    )\n    username: Mapped[str] = Column(String(128), nullable=False)\n    password: Mapped[Optional[str]] = Column(String(128), nullable=True)\n\n    def to_dict(self) -> dict:\n        return {\"id\": self.id, \"username\": self.username, \"password\": self.password}\n\n    def as_dict(self) -> dict:  # renamed from __dict__ to as_dict\n        return self.to_dict()\n\n    def __repr__(self) -> str:\n        return (\n            f\"User(id={self.id!r}, name={self.username!r}, fullname={self.password!r})\"\n        )\n\n\n# ---------------------------------------------------------\n# Create a UserRepository instance with the database instance\n# ---------------------------------------------------------\nclass UserRepository(Repository[User]):\n    def __init__(self, database: IDatabase):\n        super().__init__(database, User)\n\n\n# ---------------------------------------------------------\n# Create a new user\n# ---------------------------------------------------------\nif __name__ == '__main__':\n    # Create a new database instance\n    db_config = {\n        'type': 'postgresql',\n        'db_name': 'volunteer',\n        'user': \"postgres\",\n        'password': \"adminpassword\",\n        'host': \"127.0.0.1\",\n        'port': \"5432\"\n    }\n    # Create a new database instance\n    db = DatabaseFactory.create(db_config)\n\n    # Create a User Repository instance with the \n    # database instance and the User model\n    user_repo = Repository(db, User)\n\n    # Create a new user\n    user = User(username='Candy', password='password')\n\n    # Add the user to the database\n    user_repo.create(user)\n```\n---\n\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Dellius Alexander  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "The CRUDRepository is a Python project designed to provide a generic implementation of Create, Read, Update, and Delete (CRUD) operations for various databases.",
    "version": "0.2.3",
    "project_urls": {
        "Homepage": "https://github.com/dellius-alexander/CRUDRepository.git"
    },
    "split_keywords": [
        "crud-repository",
        " database",
        " model",
        " abstraction",
        " sqlalchemy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c126088f9e7480fef29ae0758482fddb5110ea0923293f053191a553f4aa0343",
                "md5": "29c78176497cda57ff0efdcea43d8235",
                "sha256": "3d4e44402c4ccc6c758287a5fde80ba6927fa108585d0d36801112507eabcdfe"
            },
            "downloads": -1,
            "filename": "crud_repository-0.2.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "29c78176497cda57ff0efdcea43d8235",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 16416,
            "upload_time": "2024-05-11T15:49:15",
            "upload_time_iso_8601": "2024-05-11T15:49:15.849150Z",
            "url": "https://files.pythonhosted.org/packages/c1/26/088f9e7480fef29ae0758482fddb5110ea0923293f053191a553f4aa0343/crud_repository-0.2.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "27a55af768b2f8970f69f640bce22fb9feba1992b87d00fcf94cc9e2b4945384",
                "md5": "ac1995dc8ba08d7f662357431c228a9c",
                "sha256": "7a1db00d79a3ac85467de9b330def7868aad3002a4d270532ff4b781f8759f9f"
            },
            "downloads": -1,
            "filename": "crud_repository-0.2.3.tar.gz",
            "has_sig": false,
            "md5_digest": "ac1995dc8ba08d7f662357431c228a9c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15846,
            "upload_time": "2024-05-11T15:49:17",
            "upload_time_iso_8601": "2024-05-11T15:49:17.909144Z",
            "url": "https://files.pythonhosted.org/packages/27/a5/5af768b2f8970f69f640bce22fb9feba1992b87d00fcf94cc9e2b4945384/crud_repository-0.2.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-11 15:49:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dellius-alexander",
    "github_project": "CRUDRepository",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "sqlalchemy",
            "specs": [
                [
                    "==",
                    "2.0.29"
                ]
            ]
        },
        {
            "name": "psycopg2-binary",
            "specs": [
                [
                    "==",
                    "2.9.9"
                ]
            ]
        },
        {
            "name": "python-dotenv",
            "specs": [
                [
                    "==",
                    "1.0.1"
                ]
            ]
        },
        {
            "name": "colorlog",
            "specs": [
                [
                    "==",
                    "6.4.1"
                ]
            ]
        },
        {
            "name": "cryptography",
            "specs": [
                [
                    "==",
                    "42.0.5"
                ]
            ]
        },
        {
            "name": "pymysql",
            "specs": [
                [
                    "==",
                    "1.1.0"
                ]
            ]
        },
        {
            "name": "SQLAlchemy-Utils",
            "specs": [
                [
                    "==",
                    "0.41.2"
                ]
            ]
        }
    ],
    "lcname": "crud-repository"
}
        
Elapsed time: 0.24332s