Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+225 -316
View File
@@ -1,365 +1,274 @@
---
description: Comprehensive guide for Peewee ORM best practices, covering code organization, performance, security, testing, and common pitfalls. Provides actionable guidance for developers to write maintainable and efficient database-driven applications using Peewee.
globs: *.py
description: Definitive guidelines for writing robust, maintainable, and performant Peewee ORM code in Python, covering data modeling, querying, and application structure.
globs: **/*.py
---
# Peewee ORM Best Practices and Coding Standards
# peewee Best Practices
This document outlines best practices and coding standards for using Peewee ORM in Python development. Following these guidelines will lead to more maintainable, efficient, and secure database-driven applications.
Peewee is a lightweight, expressive ORM for Python. These guidelines ensure your Peewee code is clean, efficient, and aligns with modern best practices as of 2025.
## Library Information:
- Name: peewee
- Tags: database, orm, python, sql
---
## 1. Code Organization and Structure
### 1.1. Directory Structure
Always establish a clear, DRY (Don't Repeat Yourself) structure for your Peewee models and database connection.
### 1.1 Define a `BaseModel` for Database Connection
project_root/
├── models/
│ ├── __init__.py
│ ├── user.py
│ ├── product.py
│ └── ...
├── migrations/
│ ├── 0001_initial.py
│ ├── 0002_add_index.py
│ └── ...
├── utils/
│ ├── db.py # Database connection and initialization
│ └── ...
├── tests/
│ ├── __init__.py
│ ├── test_user.py
│ ├── test_product.py
│ └── ...
├── main.py # Application entry point
└── requirements.txt
Centralize your database instance within a `BaseModel` to avoid repetitive declarations and ensure all models use the same connection.
- **models/:** Contains model definitions, each in a separate file.
- **migrations/:** Stores database migration scripts.
- **utils/:** Utility functions, including database connection management.
- **tests/:** Unit and integration tests.
- **main.py:** The main application file.
### 1.2. File Naming Conventions
- Model files: Use lowercase, singular nouns (e.g., `user.py`, `product.py`).
- Migration files: Use a sequential numbering scheme (e.g., `0001_initial.py`, `0002_add_index.py`).
- Test files: Prefix with `test_` (e.g., `test_user.py`).
- Utility files: Use descriptive names (e.g., `db.py`).
### 1.3. Module Organization
- Group related models into the same module. For example, models related to user management would reside in `models/user.py`.
- Use `__init__.py` files to make directories importable as packages and to provide a convenient way to import all models from the `models` directory. Example:
python
# models/__init__.py
from .user import User
from .product import Product
### 1.4. Component Architecture
- Separate database interactions from business logic. Create service layers or repositories that handle Peewee queries and data manipulation, keeping models thin and focused on schema definition.
- Favor composition over inheritance where appropriate, using mixins for reusable model functionality.
### 1.5. Code Splitting
- Use separate files for different model definitions to improve readability and maintainability.
- Decompose complex queries into smaller, reusable functions.
- Consider using submodules within `models/` for logical groupings of models when projects grow large.
## 2. Common Patterns and Anti-patterns
### 2.1. Design Patterns
- **Repository Pattern:** Create a layer between the application and the database to abstract data access logic. This makes it easier to switch databases or change ORM implementations later.
- **Unit of Work:** Manage database transactions within a single unit of work, ensuring consistency and atomicity. Peewee's `atomic()` context manager facilitates this pattern.
- **Data Mapper:** Maps data between domain objects and the database. Peewee models inherently implement this pattern.
### 2.2. Recommended Approaches
- **Explicit Database Connection Management:** Always open and close database connections explicitly, especially in web applications or concurrent environments. Use `with db:` or `@db.connection_context()` to ensure connections are properly handled. Disable `autoconnect` when initializing the database:
python
db = SqliteDatabase('my_app.db', autoconnect=False)
- **Base Model Class:** Define a base model class that all other models inherit from. This centralizes database configuration and ensures consistency:
python
from peewee import *
db = SqliteDatabase('my_app.db')
class BaseModel(Model):
class Meta:
database = db
- **Relationships and Joins:** Utilize Peewee's built-in support for relationships and joins to simplify complex queries:
python
class User(BaseModel):
username = CharField()
class Tweet(BaseModel):
user = ForeignKeyField(User, backref='tweets')
content = TextField()
query = Tweet.select().join(User).where(User.username == 'john_doe')
- **Migrations:** Use a migration library (like `peewee-migrate`) to manage schema changes effectively. This ensures that database schemas can evolve smoothly over time.
### 2.3. Anti-patterns and Code Smells
- **Implicit Database Connections:** Relying on Peewee's `autoconnect` feature can lead to unpredictable behavior and connection leaks. Manage connections explicitly.
- **Global Database Instance:** Avoid creating a single global database instance without proper connection management, especially in multi-threaded applications. Use connection pools or context managers to manage connections per request or thread.
- **String Interpolation:** Never use string interpolation to construct SQL queries, as this can lead to SQL injection vulnerabilities. Always use parameterized queries.
python
# Anti-pattern (SQL injection vulnerability!)
username = input("Enter username:")
query = User.select().where(SQL("username = '%s'" % username))
# Correct approach (parameterized query)
query = User.select().where(User.username == username)
- **Over-fetching Data:** Avoid selecting all columns when only a subset is needed. Specify the required fields in the `select()` method.
### 2.4. State Management
- In web applications, manage database connections on a per-request basis. Open a connection at the beginning of the request and close it at the end. Framework integration examples are provided in the base documentation.
- In asynchronous applications, utilize connection pools and context managers to manage connections concurrently.
### 2.5. Error Handling
- Use try-except blocks to handle database errors, such as `IntegrityError` (for unique constraint violations) and `DoesNotExist` (when a record is not found).
- Implement logging to track database errors and diagnose issues.
- Provide informative error messages to the user, without exposing sensitive database details.
## 3. Performance Considerations
### 3.1. Optimization Techniques
- **Indexing:** Add indexes to frequently queried columns to improve query performance. Use Peewee's indexing features when defining your models or use migrations to create them:
python
class User(BaseModel):
username = CharField(index=True) # Simple index
class Meta:
indexes = (
(('email', 'is_active'), True), # Composite index (unique=True)
)
- **Caching:** Implement caching for frequently accessed data to reduce database load. Consider using a caching library like Redis or Memcached.
- **Connection Pooling:** Use connection pooling to reduce the overhead of establishing new database connections for each request. Peewee's `playhouse.pool` module provides pooled database classes:
python
from playhouse.pool import PooledSqliteDatabase
db = PooledSqliteDatabase('my_app.db', max_connections=16)
- **Batch Inserts/Updates:** Use `insert_many()` and `update()` methods for bulk operations instead of iterating and executing individual queries.
python
data = [
{'username': 'user1', 'email': 'user1@example.com'},
{'username': 'user2', 'email': 'user2@example.com'},
]
with db.atomic(): # Transaction for atomicity
User.insert_many(data).execute()
- **Query Optimization:** Use `.prefetch()` to reduce N+1 query problems. Use `.defer()` to avoid fetching unnecessary columns. Profile your queries and analyze execution plans to identify bottlenecks.
### 3.2. Memory Management
- Limit the number of records returned by queries to avoid loading large amounts of data into memory. Use pagination or filtering to retrieve data in smaller chunks.
- Close database connections promptly after use to release resources.
- Be mindful of large data types (e.g., BLOBs) and handle them efficiently to avoid memory exhaustion.
### 3.3. Bundle Size Optimization
- Peewee is a lightweight library, so its direct impact on bundle size is minimal. However, be mindful of dependencies introduced by database drivers or extensions.
- Use a minifier to reduce the size of your Python code before deployment.
### 3.4. Lazy Loading
- Use `ForeignKeyField` with caution, understanding that accessing the related object will trigger a new database query if the related object is not prefetched. Utilize `.prefetch()` for efficient loading of related data.
## 4. Security Best Practices
### 4.1. Common Vulnerabilities
- **SQL Injection:** Prevent SQL injection by using parameterized queries and avoiding string concatenation when constructing SQL statements.
- **Cross-Site Scripting (XSS):** If displaying data retrieved from the database in a web application, sanitize the data to prevent XSS attacks. (This is primarily a front-end concern, but relevant when handling user-generated content stored in the database).
- **Sensitive Data Exposure:** Avoid storing sensitive data (e.g., passwords, API keys) in plain text. Use proper encryption or hashing techniques. Peewee itself does not provide encryption; use appropriate Python libraries for this (like `bcrypt` for passwords).
### 4.2. Input Validation
- Validate all user inputs before using them in database queries or model fields. This prevents malicious data from being stored in the database or used to exploit vulnerabilities.
- Use Peewee's field validation capabilities to enforce data types, lengths, and other constraints at the model level:
python
class User(BaseModel):
username = CharField(max_length=50)
email = CharField(unique=True)
age = IntegerField(null=True, constraints=[Check('age > 0')]) #CHECK constraint
### 4.3. Authentication and Authorization
- Implement robust authentication and authorization mechanisms to protect sensitive data and restrict access to authorized users only. Peewee can integrate with authentication frameworks like Flask-Login or Django's authentication system.
- Do not rely solely on client-side validation for security. Always perform server-side validation.
### 4.4. Data Protection
- Implement data encryption at rest and in transit to protect sensitive data from unauthorized access. Consider using database-level encryption or encrypting sensitive fields at the application level.
- Use HTTPS to secure communication between the client and the server.
- Comply with relevant data privacy regulations (e.g., GDPR, CCPA).
### 4.5. Secure API Communication
- Use API keys or tokens to authenticate API requests.
- Implement rate limiting to prevent abuse and denial-of-service attacks.
- Validate API request data and sanitize API responses to prevent vulnerabilities.
## 5. Testing Approaches
### 5.1. Unit Testing
- Unit test model methods, query logic, and other database-related functionality in isolation. Use mocking and stubbing to isolate units of code from external dependencies (e.g., the database itself).
- Utilize an in-memory SQLite database for unit tests to avoid modifying the production database. Bind the models to the in-memory database during testing.
python
import unittest
❌ BAD: Direct database assignment in every model
```python
from peewee import *
db = SqliteDatabase(':memory:')
db = SqliteDatabase('app.db')
class User(Model):
username = CharField()
class Meta:
database = db # Repeated for every model
```
✅ GOOD: Inherit from a `BaseModel`
```python
from peewee import *
db = SqliteDatabase('app.db')
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField()
username = CharField(unique=True)
```
class TestUser(unittest.TestCase):
def setUp(self):
db.bind([User])
db.create_tables([User])
### 1.2 Model Naming Convention
def tearDown(self):
db.drop_tables([User])
db.close()
Name your model classes in the singular form. Peewee automatically pluralizes table names (e.g., `User` -> `user`).
def test_user_creation(self):
user = User.create(username='test_user')
self.assertEqual(user.username, 'test_user')
❌ BAD: Plural model name
```python
class Users(BaseModel): # Table might be 'userss' or 'users' depending on backend
name = CharField()
```
✅ GOOD: Singular model name
```python
class User(BaseModel): # Table will be 'user'
name = CharField()
```
### 1.3 Explicit Imports for Model Definitions
For model definition files, use `from peewee import *` for convenience. This is a common and accepted pattern within the Peewee ecosystem. For other modules or specific utility functions, prefer explicit imports.
❌ BAD: Overly verbose imports for model definitions
```python
from peewee import Model, CharField, IntegerField, SqliteDatabase, DateTimeField # ... and so on
```
✅ GOOD: Concise model definition imports
```python
from peewee import * # For model definition files (e.g., models.py)
import datetime
# In other files (e.g., main.py, services.py), for specific functions/classes:
from peewee import SqliteDatabase, DoesNotExist
```
## 2. Data Modeling
Define robust models with clear field types, constraints, and relationships.
### 2.1 Explicit Field Constraints and Defaults
Always define fields with appropriate Python types, `unique=True` where necessary, and sensible `default` values. Peewee uses `id` as an auto-incrementing primary key by default if none is specified.
```python
import datetime
from peewee import *
db = SqliteDatabase('app.db')
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField(unique=True, index=True) # Ensure uniqueness and fast lookups
email = CharField(unique=True, null=False)
created_at = DateTimeField(default=datetime.datetime.now) # Use datetime.datetime.now, not datetime.now()
is_active = BooleanField(default=True)
class Post(BaseModel):
user = ForeignKeyField(User, backref='posts', on_delete='CASCADE') # Define relationship and cascade delete
title = CharField(max_length=255, null=False)
content = TextField(null=False)
published_at = DateTimeField(null=True) # Can be null until published
views = IntegerField(default=0)
```
### 2.2 Foreign Key Relationships with `backref` and `on_delete`
Always define `ForeignKeyField` with a `backref` to easily access related objects from the parent model. Specify `on_delete` for database-level referential integrity.
❌ BAD: Missing `backref` and `on_delete`
```python
class Comment(BaseModel):
post = ForeignKeyField(Post) # Cannot easily get comments from a Post instance
text = TextField()
```
✅ GOOD: With `backref` and `on_delete`
```python
class Comment(BaseModel):
post = ForeignKeyField(Post, backref='comments', on_delete='CASCADE') # `post.comments` will work
author = ForeignKeyField(User, backref='comments', on_delete='SET NULL', null=True) # Example with SET NULL
text = TextField()
```
## 3. Database Connection and Transactions
Manage your database connections and ensure data integrity with transactions.
### 3.1 Connect and Create Tables Once at Application Startup
Establish the database connection and create tables only once when your application initializes. Close the connection if not using persistent connections or a framework that manages it.
❌ BAD: Connecting/creating tables in every script or function
```python
# In script A
db.connect()
db.create_tables([User, Post])
# In script B
db.connect() # Redundant, potential for errors
```
✅ GOOD: Centralized application startup
```python
# app_init.py (or main entry point)
from peewee import *
import datetime
db = SqliteDatabase('app.db')
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField(unique=True)
class Post(BaseModel):
user = ForeignKeyField(User, backref='posts')
title = CharField()
def initialize_db():
db.connect()
db.create_tables([User, Post]) # Pass all models here
db.close() # Close immediately if not using persistent connections
if __name__ == '__main__':
unittest.main()
initialize_db()
print("Database initialized.")
# Now, in your application logic, you'd open/close connections per request/operation
# or use a connection pool.
```
### 3.2 Use Transactions for Atomic Operations
### 5.2. Integration Testing
Wrap any multi-step database modifications in a transaction (`with db.atomic():`) to guarantee atomicity and improve performance.
- Integration test the interaction between different components of the application, including database interactions.
- Use a separate test database (e.g., a dedicated SQLite file or a testing instance of your production database) for integration tests.
- Ensure that the test database is properly initialized and cleaned up before and after each test.
❌ BAD: Multiple, non-atomic operations
```python
user = User.create(username='alice')
Post.create(user=user, title='First post') # If this fails, user is still created, leading to inconsistency
```
### 5.3. End-to-End Testing
✅ GOOD: Atomic transaction
```python
try:
with db.atomic():
user = User.create(username='bob')
Post.create(user=user, title='Second post') # Both succeed or both fail
except IntegrityError:
print("Failed to create user and post due to integrity error.")
```
- End-to-end tests verify the entire application workflow, including database interactions, from the user's perspective.
- Use tools like Selenium or Cypress to automate end-to-end tests.
- Ensure that the end-to-end tests cover critical business scenarios and data flows.
## 4. Query Optimization
### 5.4. Test Organization
Write efficient queries to minimize database load and improve response times.
- Organize tests into separate modules or directories based on the component or functionality being tested.
- Use descriptive test names to clearly indicate the purpose of each test.
- Follow the Arrange-Act-Assert pattern to structure tests.
### 4.1 Avoid N+1 Query Problem
### 5.5. Mocking and Stubbing
Use `join()` or `prefetch()` to load related objects in a single query, preventing the N+1 problem.
- Use mocking and stubbing to isolate units of code during unit testing and to simulate database behavior.
- Use libraries like `unittest.mock` or `pytest-mock` for mocking and stubbing.
- Mock database connections, queries, and model methods as needed.
❌ BAD: N+1 queries
```python
# Fetching posts, then iterating to get each user's username
for post in Post.select():
print(f"{post.title} by {post.user.username}") # Each post.user.username triggers a new query
```
## 6. Common Pitfalls and Gotchas
✅ GOOD: Single query with `join()`
```python
for post in Post.select().join(User):
print(f"{post.title} by {post.user.username}") # User data loaded with post
```
### 6.1. Frequent Mistakes
### 4.2 Use Bulk Operations for Inserts
- **Forgetting to Handle Exceptions:** Neglecting to handle database exceptions can lead to application crashes or data corruption.
- **Incorrect Data Types:** Using incorrect data types for model fields can result in data truncation or validation errors.
- **Not Closing Connections:** Failing to close database connections can lead to connection leaks and performance issues.
- **N+1 Query Problem:** Inefficiently fetching related data can result in the N+1 query problem, where a query is executed for each related record. Use `prefetch()` to mitigate this.
For inserting many records, use `bulk_create()` for significant performance gains by reducing the number of database round trips.
### 6.2. Edge Cases
❌ BAD: Individual `create()` calls in a loop
```python
users_data = [{'username': f'user_{i}'} for i in range(100)]
for data in users_data:
User.create(**data) # 100 separate INSERT statements
```
- **Concurrency Issues:** Be aware of concurrency issues when multiple threads or processes access the database simultaneously. Use transactions and locking mechanisms to ensure data consistency.
- **Large Datasets:** Handle large datasets efficiently by using pagination, streaming, or batch processing techniques.
- **Database-Specific Features:** Be aware of database-specific features and syntax differences when writing queries. Use Peewee's database-specific extensions when necessary.
✅ GOOD: Single `bulk_create()` call
```python
users_data = [{'username': f'user_{i}'} for i in range(100)]
User.bulk_create([User(**data) for data in users_data]) # Single INSERT statement (or few, depending on batch size)
```
### 6.3. Version-Specific Issues
### 4.3 Atomic Updates
- Consult the Peewee documentation and release notes for version-specific issues and compatibility concerns.
- Test your application thoroughly after upgrading Peewee or your database driver.
Perform updates atomically using `Model.update().where().execute()` to prevent race conditions and improve efficiency.
### 6.4. Compatibility Concerns
❌ BAD: Fetch-modify-save (prone to race conditions)
```python
post = Post.get(Post.id == 1)
post.views += 1
post.save() # Another process could have incremented views between get and save
```
- Ensure that the Peewee version is compatible with the Python version and the database driver being used.
- Be aware of potential compatibility issues when integrating Peewee with other libraries or frameworks.
✅ GOOD: Atomic update
```python
Post.update(views=Post.views + 1).where(Post.id == 1).execute() # Single, atomic operation
```
### 6.5. Debugging Strategies
### 4.4 Pagination
- Enable logging to track database queries and errors.
- Use a database profiler to analyze query performance.
- Use a debugger to step through code and inspect variables.
- Simplify complex queries to isolate the source of the problem.
Use the built-in `paginate()` method for efficient result set slicing, especially for large datasets.
## 7. Tooling and Environment
```python
# Get page 2, with 20 items per page, ordered by username
users_page_2 = User.select().order_by(User.username).paginate(2, 20)
```
### 7.1. Recommended Development Tools
## 5. Security Best Practices
- **Integrated Development Environment (IDE):** VS Code, PyCharm, or other IDEs with Python support.
- **Database Client:** DB Browser for SQLite, pgAdmin, MySQL Workbench, or other database clients for interacting with the database.
- **Migration Tool:** Alembic or `peewee-migrate` for managing database schema changes.
Peewee handles SQL injection prevention by default through parameterized queries. Focus on proper input validation in your application logic.
### 7.2. Build Configuration
### 5.1 Trust Peewee's Query Builder
- Use `requirements.txt` or `pyproject.toml` to manage project dependencies.
- Specify the Peewee version and database driver versions in the dependencies file.
- Use a virtual environment to isolate project dependencies from the system environment.
Avoid constructing SQL strings manually. Always use Peewee's ORM methods and expressions, which automatically parameterize inputs.
### 7.3. Linting and Formatting
- Use a linter like Flake8 or Pylint to enforce code style and identify potential errors.
- Use a code formatter like Black to automatically format your code according to a consistent style.
- Configure the linter and formatter to adhere to the Peewee coding standards.
### 7.4. Deployment
- Use a deployment tool like Docker or Kubernetes to containerize and deploy your application.
- Configure the application to connect to the production database using environment variables or configuration files.
- Ensure that the database schema is up-to-date before deploying the application. Run database migrations as part of the deployment process.
- Monitor the application and database for performance and security issues.
### 7.5. CI/CD Integration
- Integrate Peewee tests into your CI/CD pipeline to automatically run tests whenever code is committed or deployed.
- Use a CI/CD tool like Jenkins, GitLab CI, or GitHub Actions to automate the build, test, and deployment process.
- Configure the CI/CD pipeline to run database migrations and seed the database with test data.
This guide provides a comprehensive overview of Peewee ORM best practices. By following these guidelines, you can create robust, efficient, and maintainable database-driven applications.
❌ BAD: Manual string formatting (vulnerable to injection)
```python
# DO NOT DO THIS. This is a severe security vulnerability.
user_input = "'; DROP TABLE users; --"
query = f"SELECT * FROM user WHERE username = '{user_input