ODBMS - Object Document/Relational Mapping System
A flexible and modern Python ORM supporting multiple databases (MongoDB, PostgreSQL, MySQL) with both synchronous and asynchronous operations.
Features
- Support for multiple databases:
- SQLite (using sqlite3)
- MongoDB (using Motor)
- PostgreSQL (using aiopg)
- MySQL (using aiomysql)
- Both synchronous and asynchronous operations
- Connection pooling for better performance
- Type-safe field definitions
- Pydantic integration for validation
- Automatic table/collection creation
- Relationship handling
- Computed fields
- Flexible query interface
Installation
pip install -r requirements.txt
Quick Start
from odbms import Model, StringField, IntegerField, EmailField
from odbms.dbms import DBMS
DBMS.initialize(
dbms='postgresql',
host='localhost',
port=5432,
database='mydb',
username='user',
password='pass'
)
class User(Model):
name: str = StringField()
email: str = EmailField()
age: int = IntegerField(min_value=0)
user = User(name='John Doe', email='john@example.com', age=30)
await user.save_async()
users = await User.find_async({'age': {'$gte': 25}})
Field Types
StringField
: For text dataIntegerField
: For integer valuesFloatField
: For floating-point numbersBooleanField
: For true/false valuesDateTimeField
: For timestampsEmailField
: For email addresses with validationIDField
: For primary keys/IDsComputedField
: For dynamically computed valuesListField
: For arrays/listsDictField
: For nested documents/objects
Database Operations
Synchronous Operations
user = User(name='John', email='john@example.com')
user.save()
user = User.find_one({'email': 'john@example.com'})
users = User.find({'age': {'$gte': 25}})
all_users = User.all()
User.update({'age': {'$lt': 18}}, {'is_minor': True})
User.remove({'status': 'inactive'})
total_age = User.sum('age', {'country': 'US'})
Asynchronous Operations
user = User(name='Jane', email='jane@example.com')
await user.save_async()
user = await User.find_one_async({'email': 'jane@example.com'})
users = await User.find_async({'age': {'$gte': 25}})
all_users = await User.all_async()
await User.update_async({'age': {'$lt': 18}}, {'is_minor': True})
await User.remove_async({'status': 'inactive'})
total_age = await User.sum_async('age', {'country': 'US'})
Relationships
class Post(Model):
title: str = StringField()
content: str = StringField()
author_id: str = IDField()
class User(Model):
name: str = StringField()
posts: List[Post] = ListField(model=Post)
user = User(name='John')
await user.save_async()
post = Post(title='Hello', content='World', author_id=user.id)
await post.save_async()
user_posts = await user.posts
Testing
Run the test suite:
pytest tests/
The test suite includes comprehensive tests for:
- All database operations (CRUD)
- Both sync and async operations
- Field validations
- Relationships
- Computed fields
- Aggregations
Requirements
- Python 3.7+
- pydantic >= 2.0.0
- motor >= 3.3.0 (for MongoDB)
- aiopg >= 1.4.0 (for PostgreSQL)
- aiomysql >= 0.2.0 (for MySQL)
- inflect >= 5.0.0
- python-dotenv >= 0.19.0
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
MIT License