Skip to content

Commit

Permalink
Merge pull request #20 from hexfrost/feat/update-6
Browse files Browse the repository at this point in the history
Fix
  • Loading branch information
kaziamov authored Mar 2, 2024
2 parents e3078d5 + 9ebfa7d commit d3c9a84
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 85 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hexfrost-simplecrud"
version = "0.2.0"
version = "0.3.0"
description = "Library with operation like (get, create, update, delete) for SQLAlchemy ORM"
authors = ["Ilia Kaziamov <kaziamov@outlook.com>"]
packages = [
Expand All @@ -9,14 +9,14 @@ packages = [
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.8"
python = ">=3.8.1,<4.0"
sqlalchemy = "^2.0.0"
pytest-cov = "^4.1.0"

[tool.poetry.group.dev.dependencies]
flake8 = "^7.0.0"
pytest = "^7.4.3"
aiosqlite = "^0.19.0"
pytest-cov = "^4.1.0"

[build-system]
requires = ["poetry-core"]
Expand Down
1 change: 0 additions & 1 deletion simplecrud/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import crud
from .settings import CRUDConfig
55 changes: 22 additions & 33 deletions simplecrud/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from typing import Dict, List

from sqlalchemy import select, delete
from .utils import inject_connection
from sqlalchemy.ext.asyncio import AsyncSession


# READ / GET
@inject_connection
async def get_object(model, filters, conn=None) -> object:

async def get_object(model, filters: dict, conn: AsyncSession = None) -> object:
"""Get object from db"""
query = select(model).filter_by(**filters)
async with conn:
Expand All @@ -16,8 +16,7 @@ async def get_object(model, filters, conn=None) -> object:
return obj


@inject_connection
async def get_all(model, conn=None) -> List[object]:
async def get_all(model, conn: AsyncSession = None) -> List[object]:
"""Get objects from db"""
query = select(model)
async with conn:
Expand All @@ -26,8 +25,7 @@ async def get_all(model, conn=None) -> List[object]:
return objects


@inject_connection
async def get_all_with_filter(model, filters: dict, conn=None) -> List[object]:
async def get_all_with_filter(model, filters: dict, conn: AsyncSession = None) -> List[object]:
"""Get objects from db"""
query = select(model).filter_by(**filters)
async with conn:
Expand All @@ -36,8 +34,7 @@ async def get_all_with_filter(model, filters: dict, conn=None) -> List[object]:
return objects


@inject_connection
async def get_objects(model, filters: Dict, limit=10, offset=10, conn=None) -> List[object]:
async def get_objects(model, filters: Dict, limit: int = 10, offset: int = 10, conn: AsyncSession = None) -> List[object]:
"""Get objects from db"""
query = select(model).filter_by(**filters).limit(limit).offset(offset)
async with conn:
Expand All @@ -46,8 +43,7 @@ async def get_objects(model, filters: Dict, limit=10, offset=10, conn=None) -> L
return objects


@inject_connection
async def get_or_create_object(model, params, conn=None) -> object:
async def get_or_create_object(model, params: dict, conn: AsyncSession = None) -> object:
"""Get object from db or create new one"""
obj = await get_object(model, params, conn=conn)
if not obj:
Expand All @@ -56,8 +52,8 @@ async def get_or_create_object(model, params, conn=None) -> object:


# CREATE
@inject_connection
async def create_object(model, params, conn=None) -> object:

async def create_object(model, params, conn: AsyncSession = None) -> object:
"""Create object in db"""
new_obj = model(**params)
async with conn:
Expand All @@ -66,15 +62,14 @@ async def create_object(model, params, conn=None) -> object:
return new_obj


@inject_connection
async def bulk_create(model, data: List[Dict], conn=None) -> List[object]:
async def bulk_create(model, data: List[Dict], conn: AsyncSession = None) -> List[object]:
"""Bulk create objects in db"""
return [await create_object(model, params, conn=conn) for params in data]


# UPDATE
@inject_connection
async def update_object(obj, params, conn=None) -> object:

async def update_object(obj, params, conn: AsyncSession = None) -> object:
"""
Soft Update object in db.
If attribute not exists in model`s fields, then skip field without error
Expand All @@ -89,8 +84,7 @@ async def update_object(obj, params, conn=None) -> object:
return obj


@inject_connection
async def update_or_error(obj, params, conn=None) -> object:
async def update_or_error(obj, params, conn: AsyncSession = None) -> object:
"""
Soft Update object in db.
If attribute not exists in model`s fields, then skip field without error
Expand All @@ -103,16 +97,15 @@ async def update_or_error(obj, params, conn=None) -> object:
return obj


@inject_connection
async def update_object_by_id(model, id: int, params, conn=None) -> object:
async def update_object_by_id(model, id: int, params, conn: AsyncSession = None) -> object:
"""Update object in db by id"""
obj = await get_object(model, dict(id=id))
updated_obj = await update_object(obj, params, conn=conn)
return updated_obj


# @inject_connection
# async def bulk_update(objects, params, conn=None) -> List[object]:
#
# async def bulk_update(objects, params, conn: AsyncSession = None) -> List[object]:
# """Bulk update objects in db"""
# updated_objects = []
# for obj in objects:
Expand All @@ -121,38 +114,34 @@ async def update_object_by_id(model, id: int, params, conn=None) -> object:
# return updated_objects


@inject_connection
async def update_or_create_object(model, filters, params, conn=None) -> object:
async def update_or_create_object(model, filters, params, conn: AsyncSession = None) -> object:
obj = await get_or_create_object(model, filters, conn=conn)
return await update_object(obj, params, conn=conn)


# DELETE
@inject_connection
async def delete_object(obj, conn=None) -> bool:

async def delete_object(obj, conn: AsyncSession = None) -> bool:
model = obj.__class__
id_ = obj.id
return await delete_object_by_id(model, id_, conn=conn)


@inject_connection
async def delete_object_by_id(model, id_: int, conn=None) -> bool:
async def delete_object_by_id(model, id_: int, conn: AsyncSession = None) -> bool:
query = delete(model).where(model.id == id_)
async with conn:
await conn.execute(query)
await conn.commit()
return True


@inject_connection
async def bulk_delete(objects, conn=None) -> bool:
async def bulk_delete(objects, conn: AsyncSession = None) -> bool:
for obj in objects:
await delete_object(obj, conn=conn)
return True


@inject_connection
async def bulk_delete_by_id(model, ids, conn=None) -> bool:
async def bulk_delete_by_id(model, ids, conn: AsyncSession = None) -> bool:
for id_ in ids:
await delete_object_by_id(model, id_, conn=conn)
return True
31 changes: 0 additions & 31 deletions simplecrud/settings.py

This file was deleted.

14 changes: 0 additions & 14 deletions simplecrud/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
from functools import wraps

from simplecrud.settings import session


def async_to_sync(func):
Expand All @@ -17,19 +16,6 @@ def wrapper(*args, **kwargs):
return wrapper


def inject_connection(func):
"""Decorator to inject database connection to function"""

@wraps(func)
def inner(*args, **kwargs):
if kwargs.get('conn') is None:
kwargs['conn'] = session()
result = func(*args, **kwargs)
return result

return inner


def add_log(func):
"""Decorator to add log"""

Expand Down

0 comments on commit d3c9a84

Please sign in to comment.