rb-commons 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: rb-commons
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Orm commons for sqlalchemy based projects
|
5
|
+
Home-page: https://github.com/Abdulvoris101/rb-commons
|
6
|
+
Author: Abdulvoris
|
7
|
+
Author-email: erkinovabdulvoris101@gmail.com
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
9
|
+
Classifier: Operating System :: OS Independent
|
10
|
+
Requires-Python: >=3.11
|
11
|
+
Description-Content-Type: text/markdown
|
12
|
+
Requires-Dist: annotated-types~=0.7.0
|
13
|
+
Requires-Dist: anyio~=4.7.0
|
14
|
+
Requires-Dist: click~=8.1.8
|
15
|
+
Requires-Dist: colorama~=0.4.6
|
16
|
+
Requires-Dist: fastapi~=0.115.6
|
17
|
+
Requires-Dist: greenlet~=3.1.1
|
18
|
+
Requires-Dist: h11~=0.14.0
|
19
|
+
Requires-Dist: httptools~=0.6.4
|
20
|
+
Requires-Dist: idna~=3.10
|
21
|
+
Requires-Dist: pydantic~=2.10.4
|
22
|
+
Requires-Dist: pydantic-settings~=2.7.1
|
23
|
+
Requires-Dist: pydantic_core~=2.27.2
|
24
|
+
Requires-Dist: python-dotenv~=1.0.1
|
25
|
+
Requires-Dist: PyYAML~=6.0.2
|
26
|
+
Requires-Dist: sniffio~=1.3.1
|
27
|
+
Requires-Dist: SQLAlchemy~=2.0.36
|
28
|
+
Requires-Dist: starlette~=0.41.3
|
29
|
+
Requires-Dist: typing_extensions~=4.12.2
|
30
|
+
Requires-Dist: uvicorn~=0.34.0
|
31
|
+
Requires-Dist: watchfiles~=1.0.3
|
32
|
+
Requires-Dist: websockets~=14.1
|
33
|
+
|
34
|
+
# RB-Commons
|
35
|
+
|
36
|
+
RB-Commons is a lightweight async Python library that simplifies database operations through a clean manager interface built on top of SQLAlchemy's async capabilities. It provides a robust foundation for handling common database operations while maintaining full type safety through Python's typing system.
|
37
|
+
|
38
|
+
## Features
|
39
|
+
|
40
|
+
### Async-First Design
|
41
|
+
- Built on top of SQLAlchemy's async functionality
|
42
|
+
- Efficient handling of async database operations
|
43
|
+
- Proper transaction and session management
|
44
|
+
- Type-safe operations with Generic types
|
45
|
+
|
46
|
+
### Core Functionality
|
47
|
+
- **CRUD Operations**: Complete set of Create, Read, Update, and Delete operations
|
48
|
+
- **Flexible Filtering**: Support for dynamic query filtering
|
49
|
+
- **Instance Management**: Both instance-level and query-level updates
|
50
|
+
- **Error Handling**: Comprehensive error handling with custom exceptions
|
51
|
+
- **Transaction Safety**: Automatic rollbacks on failures
|
52
|
+
|
53
|
+
## Installation
|
54
|
+
|
55
|
+
```bash
|
56
|
+
pip install rb-commons
|
57
|
+
```
|
58
|
+
|
59
|
+
## Quick Start
|
60
|
+
|
61
|
+
```python
|
62
|
+
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
63
|
+
from sqlalchemy.ext.declarative import declarative_base
|
64
|
+
from sqlalchemy import Column, Integer, String
|
65
|
+
from rb_commons.orm import BaseManager
|
66
|
+
|
67
|
+
# Define your model
|
68
|
+
Base = declarative_base()
|
69
|
+
|
70
|
+
class User(Base):
|
71
|
+
__tablename__ = 'users'
|
72
|
+
|
73
|
+
id = Column(Integer, primary_key=True)
|
74
|
+
name = Column(String)
|
75
|
+
email = Column(String)
|
76
|
+
|
77
|
+
# Create your manager
|
78
|
+
class UserManager(BaseManager[User]):
|
79
|
+
model = User
|
80
|
+
|
81
|
+
# Usage in async context
|
82
|
+
async def main():
|
83
|
+
# Setup database connection
|
84
|
+
engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
|
85
|
+
async with AsyncSession(engine) as session:
|
86
|
+
# Initialize manager
|
87
|
+
user_manager = UserManager(session)
|
88
|
+
|
89
|
+
# Create user
|
90
|
+
user = await user_manager.create(
|
91
|
+
name="John Doe",
|
92
|
+
email="john@example.com"
|
93
|
+
)
|
94
|
+
|
95
|
+
# Get user
|
96
|
+
user = await user_manager.get(id=1)
|
97
|
+
|
98
|
+
# Filter users
|
99
|
+
users = await user_manager.filter(name="John Doe")
|
100
|
+
|
101
|
+
# Update user by filters
|
102
|
+
updated_user = await user_manager.update_by_filters(
|
103
|
+
filters={"id": 1},
|
104
|
+
name="Jane Doe"
|
105
|
+
)
|
106
|
+
|
107
|
+
# Delete user
|
108
|
+
success = await user_manager.delete(id=1)
|
109
|
+
```
|
110
|
+
|
111
|
+
## Core Operations
|
112
|
+
|
113
|
+
### Get and Filter
|
114
|
+
|
115
|
+
```python
|
116
|
+
# Get single instance (returns Optional[ModelType])
|
117
|
+
user = await user_manager.get(id=1)
|
118
|
+
|
119
|
+
# Filter multiple instances (returns List[ModelType])
|
120
|
+
active_users = await user_manager.filter(is_active=True)
|
121
|
+
|
122
|
+
# Check existence
|
123
|
+
exists = await user_manager.is_exists(email="john@example.com")
|
124
|
+
```
|
125
|
+
|
126
|
+
### Create
|
127
|
+
|
128
|
+
```python
|
129
|
+
try:
|
130
|
+
user = await user_manager.create(
|
131
|
+
name="John Doe",
|
132
|
+
email="john@example.com"
|
133
|
+
)
|
134
|
+
except DatabaseException as e:
|
135
|
+
print(f"Database error: {e}")
|
136
|
+
except InternalException as e:
|
137
|
+
print(f"Internal error: {e}")
|
138
|
+
```
|
139
|
+
|
140
|
+
### Update
|
141
|
+
|
142
|
+
RB-Commons provides three different ways to update records:
|
143
|
+
|
144
|
+
```python
|
145
|
+
# 1. Update by filters - updates records matching filters and returns updated instance
|
146
|
+
updated_user = await user_manager.update_by_filters(
|
147
|
+
filters={"id": 1},
|
148
|
+
name="New Name"
|
149
|
+
)
|
150
|
+
|
151
|
+
# 2. Update instance with specific fields
|
152
|
+
user = await user_manager.get(id=1)
|
153
|
+
if user:
|
154
|
+
updated_user = await user_manager.update(
|
155
|
+
instance=user,
|
156
|
+
name="New Name"
|
157
|
+
)
|
158
|
+
|
159
|
+
# 3. Save modified instance
|
160
|
+
user = await user_manager.get(id=1)
|
161
|
+
if user:
|
162
|
+
user.name = "New Name"
|
163
|
+
saved_user = await user_manager.save(user)
|
164
|
+
```
|
165
|
+
|
166
|
+
### Delete
|
167
|
+
|
168
|
+
```python
|
169
|
+
# Delete by ID
|
170
|
+
success = await user_manager.delete(id=1)
|
171
|
+
|
172
|
+
# Delete by filters
|
173
|
+
success = await user_manager.delete(email="old@example.com")
|
174
|
+
|
175
|
+
# Bulk delete
|
176
|
+
deleted_count = await user_manager.bulk_delete(is_active=False)
|
177
|
+
```
|
178
|
+
|
179
|
+
## Error Handling
|
180
|
+
|
181
|
+
RB-Commons provides custom exceptions for better error handling:
|
182
|
+
|
183
|
+
- `DatabaseException`: For SQLAlchemy and database-related errors
|
184
|
+
- `InternalException`: For internal operation errors
|
185
|
+
|
186
|
+
```python
|
187
|
+
try:
|
188
|
+
user = await user_manager.create(name="John")
|
189
|
+
except DatabaseException as e:
|
190
|
+
# Handle database errors (e.g., constraint violations)
|
191
|
+
print(f"Database error: {e}")
|
192
|
+
except InternalException as e:
|
193
|
+
# Handle internal errors
|
194
|
+
print(f"Internal error: {e}")
|
195
|
+
```
|
196
|
+
|
197
|
+
## Method Return Types
|
198
|
+
|
199
|
+
- `get()`: `Optional[ModelType]`
|
200
|
+
- `filter()`: `List[ModelType]`
|
201
|
+
- `create()`: `ModelType`
|
202
|
+
- `delete()`: `bool | None`
|
203
|
+
- `bulk_delete()`: `int`
|
204
|
+
- `update()`: `Optional[ModelType]`
|
205
|
+
- `update_by_filters()`: `Optional[ModelType]`
|
206
|
+
- `save()`: `Optional[ModelType]`
|
207
|
+
- `is_exists()`: `bool`
|
208
|
+
|
209
|
+
## Best Practices
|
210
|
+
|
211
|
+
1. **Choose the Right Update Method**:
|
212
|
+
- Use `update_by_filters()` for query-based updates
|
213
|
+
- Use `update()` for updating specific fields of an instance
|
214
|
+
- Use `save()` for saving modified instances
|
215
|
+
2. **Always Use Async Context**: The library is designed for async operations, ensure you're running within an async context
|
216
|
+
3. **Session Management**: Properly manage your database sessions using async context managers
|
217
|
+
4. **Error Handling**: Implement proper error handling using the provided exception classes
|
218
|
+
5. **Type Safety**: Utilize the generic typing system for better IDE support and type safety
|
219
|
+
|
220
|
+
## Contributing
|
221
|
+
|
222
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
223
|
+
|
224
|
+
## License
|
225
|
+
|
226
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
@@ -0,0 +1,4 @@
|
|
1
|
+
rb_commons-0.1.0.dist-info/METADATA,sha256=Sk7kcuRLGSWvp0FHU6UkpKYi_2At2g8Tgd-MxGqJ-hI,6703
|
2
|
+
rb_commons-0.1.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
3
|
+
rb_commons-0.1.0.dist-info/top_level.txt,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
4
|
+
rb_commons-0.1.0.dist-info/RECORD,,
|
@@ -0,0 +1 @@
|
|
1
|
+
|