rb-commons 0.1.11__tar.gz → 0.1.13__tar.gz

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.
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: rb-commons
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: Commons of project and simplified orm based on sqlalchemy.
5
- Home-page: https://github.com/Abdulvoris101/rb-commons
5
+ Home-page: https://github.com/RoboSell-organization/rb-commons
6
6
  Author: Abdulvoris
7
7
  Author-email: erkinovabdulvoris101@gmail.com
8
8
  Classifier: Programming Language :: Python :: 3
@@ -17,6 +17,15 @@ Requires-Dist: PyJWT==2.10.1
17
17
  Requires-Dist: python-dotenv==1.0.1
18
18
  Requires-Dist: SQLAlchemy==2.0.36
19
19
  Requires-Dist: fastapi<0.120.0,>=0.115.6
20
+ Dynamic: author
21
+ Dynamic: author-email
22
+ Dynamic: classifier
23
+ Dynamic: description
24
+ Dynamic: description-content-type
25
+ Dynamic: home-page
26
+ Dynamic: requires-dist
27
+ Dynamic: requires-python
28
+ Dynamic: summary
20
29
 
21
30
  # RB-Commons
22
31
 
@@ -0,0 +1,193 @@
1
+ # RB-Commons
2
+
3
+ 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.
4
+
5
+ ## Features
6
+
7
+ ### Async-First Design
8
+ - Built on top of SQLAlchemy's async functionality
9
+ - Efficient handling of async database operations
10
+ - Proper transaction and session management
11
+ - Type-safe operations with Generic types
12
+
13
+ ### Core Functionality
14
+ - **CRUD Operations**: Complete set of Create, Read, Update, and Delete operations
15
+ - **Flexible Filtering**: Support for dynamic query filtering
16
+ - **Instance Management**: Both instance-level and query-level updates
17
+ - **Error Handling**: Comprehensive error handling with custom exceptions
18
+ - **Transaction Safety**: Automatic rollbacks on failures
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install rb-commons
24
+ ```
25
+
26
+ ## Quick Start
27
+
28
+ ```python
29
+ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
30
+ from sqlalchemy.ext.declarative import declarative_base
31
+ from sqlalchemy import Column, Integer, String
32
+ from rb_commons.orm import BaseManager
33
+
34
+ # Define your model
35
+ Base = declarative_base()
36
+
37
+ class User(Base):
38
+ __tablename__ = 'users'
39
+
40
+ id = Column(Integer, primary_key=True)
41
+ name = Column(String)
42
+ email = Column(String)
43
+
44
+ # Create your manager
45
+ class UserManager(BaseManager[User]):
46
+ model = User
47
+
48
+ # Usage in async context
49
+ async def main():
50
+ # Setup database connection
51
+ engine = create_async_engine("postgresql+asyncpg://user:pass@localhost/db")
52
+ async with AsyncSession(engine) as session:
53
+ # Initialize manager
54
+ user_manager = UserManager(session)
55
+
56
+ # Create user
57
+ user = await user_manager.create(
58
+ name="John Doe",
59
+ email="john@example.com"
60
+ )
61
+
62
+ # Get user
63
+ user = await user_manager.get(id=1)
64
+
65
+ # Filter users
66
+ users = await user_manager.filter(name="John Doe")
67
+
68
+ # Update user by filters
69
+ updated_user = await user_manager.update_by_filters(
70
+ filters={"id": 1},
71
+ name="Jane Doe"
72
+ )
73
+
74
+ # Delete user
75
+ success = await user_manager.delete(id=1)
76
+ ```
77
+
78
+ ## Core Operations
79
+
80
+ ### Get and Filter
81
+
82
+ ```python
83
+ # Get single instance (returns Optional[ModelType])
84
+ user = await user_manager.get(id=1)
85
+
86
+ # Filter multiple instances (returns List[ModelType])
87
+ active_users = await user_manager.filter(is_active=True)
88
+
89
+ # Check existence
90
+ exists = await user_manager.is_exists(email="john@example.com")
91
+ ```
92
+
93
+ ### Create
94
+
95
+ ```python
96
+ try:
97
+ user = await user_manager.create(
98
+ name="John Doe",
99
+ email="john@example.com"
100
+ )
101
+ except DatabaseException as e:
102
+ print(f"Database error: {e}")
103
+ except InternalException as e:
104
+ print(f"Internal error: {e}")
105
+ ```
106
+
107
+ ### Update
108
+
109
+ RB-Commons provides three different ways to update records:
110
+
111
+ ```python
112
+ # 1. Update by filters - updates records matching filters and returns updated instance
113
+ updated_user = await user_manager.update_by_filters(
114
+ filters={"id": 1},
115
+ name="New Name"
116
+ )
117
+
118
+ # 2. Update instance with specific fields
119
+ user = await user_manager.get(id=1)
120
+ if user:
121
+ updated_user = await user_manager.update(
122
+ instance=user,
123
+ name="New Name"
124
+ )
125
+
126
+ # 3. Save modified instance
127
+ user = await user_manager.get(id=1)
128
+ if user:
129
+ user.name = "New Name"
130
+ saved_user = await user_manager.save(user)
131
+ ```
132
+
133
+ ### Delete
134
+
135
+ ```python
136
+ # Delete by ID
137
+ success = await user_manager.delete(id=1)
138
+
139
+ # Delete by filters
140
+ success = await user_manager.delete(email="old@example.com")
141
+
142
+ # Bulk delete
143
+ deleted_count = await user_manager.bulk_delete(is_active=False)
144
+ ```
145
+
146
+ ## Error Handling
147
+
148
+ RB-Commons provides custom exceptions for better error handling:
149
+
150
+ - `DatabaseException`: For SQLAlchemy and database-related errors
151
+ - `InternalException`: For internal operation errors
152
+
153
+ ```python
154
+ try:
155
+ user = await user_manager.create(name="John")
156
+ except DatabaseException as e:
157
+ # Handle database errors (e.g., constraint violations)
158
+ print(f"Database error: {e}")
159
+ except InternalException as e:
160
+ # Handle internal errors
161
+ print(f"Internal error: {e}")
162
+ ```
163
+
164
+ ## Method Return Types
165
+
166
+ - `get()`: `Optional[ModelType]`
167
+ - `filter()`: `List[ModelType]`
168
+ - `create()`: `ModelType`
169
+ - `delete()`: `bool | None`
170
+ - `bulk_delete()`: `int`
171
+ - `update()`: `Optional[ModelType]`
172
+ - `update_by_filters()`: `Optional[ModelType]`
173
+ - `save()`: `Optional[ModelType]`
174
+ - `is_exists()`: `bool`
175
+
176
+ ## Best Practices
177
+
178
+ 1. **Choose the Right Update Method**:
179
+ - Use `update_by_filters()` for query-based updates
180
+ - Use `update()` for updating specific fields of an instance
181
+ - Use `save()` for saving modified instances
182
+ 2. **Always Use Async Context**: The library is designed for async operations, ensure you're running within an async context
183
+ 3. **Session Management**: Properly manage your database sessions using async context managers
184
+ 4. **Error Handling**: Implement proper error handling using the provided exception classes
185
+ 5. **Type Safety**: Utilize the generic typing system for better IDE support and type safety
186
+
187
+ ## Contributing
188
+
189
+ Contributions are welcome! Please feel free to submit a Pull Request.
190
+
191
+ ## License
192
+
193
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -21,7 +21,7 @@ class Claims(BaseModel):
21
21
  def from_headers(cls, headers: dict) -> 'Claims':
22
22
  raw_claims = {
23
23
  "x-user-id": headers.get("x-user-id"),
24
- "x-user-role": headers.get("x-user-role"),
24
+ "x-user-role": headers.get("x-user-role", "admin"),
25
25
  "x-shop-id": headers.get("x-shop-id")
26
26
  }
27
27
 
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: rb-commons
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: Commons of project and simplified orm based on sqlalchemy.
5
- Home-page: https://github.com/Abdulvoris101/rb-commons
5
+ Home-page: https://github.com/RoboSell-organization/rb-commons
6
6
  Author: Abdulvoris
7
7
  Author-email: erkinovabdulvoris101@gmail.com
8
8
  Classifier: Programming Language :: Python :: 3
@@ -17,6 +17,15 @@ Requires-Dist: PyJWT==2.10.1
17
17
  Requires-Dist: python-dotenv==1.0.1
18
18
  Requires-Dist: SQLAlchemy==2.0.36
19
19
  Requires-Dist: fastapi<0.120.0,>=0.115.6
20
+ Dynamic: author
21
+ Dynamic: author-email
22
+ Dynamic: classifier
23
+ Dynamic: description
24
+ Dynamic: description-content-type
25
+ Dynamic: home-page
26
+ Dynamic: requires-dist
27
+ Dynamic: requires-python
28
+ Dynamic: summary
20
29
 
21
30
  # RB-Commons
22
31
 
@@ -1,3 +1,4 @@
1
+ README.md
1
2
  setup.py
2
3
  rb_commons/__init__.py
3
4
  rb_commons.egg-info/PKG-INFO
@@ -5,13 +5,13 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="rb-commons",
8
- version="0.1.11",
8
+ version="0.1.13",
9
9
  author="Abdulvoris",
10
10
  author_email="erkinovabdulvoris101@gmail.com",
11
11
  description="Commons of project and simplified orm based on sqlalchemy.",
12
12
  long_description=long_description,
13
13
  long_description_content_type="text/markdown",
14
- url="https://github.com/Abdulvoris101/rb-commons", # Replace with your repo URL
14
+ url="https://github.com/RoboSell-organization/rb-commons", # Replace with your repo URL
15
15
  packages=find_packages(),
16
16
  classifiers=[
17
17
  "Programming Language :: Python :: 3",
File without changes