Introduction
EllarSQL Module adds support for SQLAlchemy and Alembic package to your Ellar application
Installation
$(venv) pip install ellar-sql
This library was inspired by Flask-SQLAlchemy
Features
- Migration
- Single/Multiple Database
- Pagination
- Compatible with SQLAlchemy tools
Usage
In your ellar application, create a module called db or any name of your choice,
ellar create-module db
Then, in models/base.py define your model base as shown below:
# db/models/base.py
from datetime import datetime
from sqlalchemy import DateTime, func
from sqlalchemy.orm import Mapped, mapped_column
from ellar_sql.model import Model
class Base(Model):
__base_config__ = {'as_base': True}
__database__ = 'default'
created_date: Mapped[datetime] = mapped_column(
"created_date", DateTime, default=datetime.utcnow, nullable=False
)
time_updated: Mapped[datetime] = mapped_column(
"time_updated", DateTime, nullable=False, default=datetime.utcnow, onupdate=func.now()
)
Use Base to create other models, like users in User in
# db/models/users.py
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
class User(Base):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
username: Mapped[str] = mapped_column(String, unique=True, nullable=False)
email: Mapped[str] = mapped_column(String)
Configure Module
# db/module.py
from ellar.app import App
from ellar.common import Module, IApplicationStartup
from ellar.core import ModuleBase
from ellar.di import Container
from ellar_sql import EllarSQLAlchemyModule, EllarSQLService
from .controllers import DbController
@Module(
controllers=[DbController],
providers=[],
routers=[],
modules=[
EllarSQLAlchemyModule.setup(
databases={
'default': 'sqlite:///project.db',
},
echo=True,
migration_options={
'directory': 'my_migrations_folder'
},
models=['db.models.users']
)
]
)
class DbModule(ModuleBase, IApplicationStartup):
"""
Db Module
"""
async def on_startup(self, app: App) -> None:
db_service = app.injector.get(EllarSQLService)
db_service.create_all()
def register_providers(self, container: Container) -> None:
"""for more complicated provider registrations, use container.register_instance(...) """
Model Usage
Database session exist at model level and can be accessed through model.get_db_session() eg, User.get_db_session()
# db/models/controllers.py
from ellar.common import Controller, ControllerBase, get, post, Body
from pydantic import EmailStr
from sqlalchemy import select
from .models.users import User
@Controller
class DbController(ControllerBase):
@post("/users")
async def create_user(self, username: Body[str], email: Body[EmailStr]):
session = User.get_db_session()
user = User(username=username, email=email)
session.add(user)
session.commit()
return user.dict()
@get("/users/{user_id:int}")
def get_user_by_id(self, user_id: int):
session = User.get_db_session()
stmt = select(User).filter(User.id==user_id)
user = session.execute(stmt).scalar()
return user.dict()
@get("/users")
async def get_all_users(self):
session = User.get_db_session()
stmt = select(User)
rows = session.execute(stmt.offset(0).limit(100)).scalars()
return [row.dict() for row in rows]
License
Ellar is MIT licensed.
Metadata
Release files for ellar-sql 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ellar_sql-0.1.8.tar.gz | 34.3 kB | Without provenance | Details for ellar_sql-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ellar_sql-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for ellar_sql-0.1.8-py3-none-any.whl |
Total release size: 83.8 kB
Release files / ellar_sql-0.1.8.tar.gz
| Download URL | ellar_sql-0.1.8.tar.gz |
|---|---|
| Size | 34.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3f22116f07ca7795f7ef191ae4796688d43bc4ad4c3ab00dbe5a1ec0916153ee
|
|
BLAKE2b-256 checksum How to use checksums |
b76c6f3eb48bc93820caadfddb6db96bd69b707dbcdfb58049fa0412cb26df64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / ellar_sql-0.1.8-py3-none-any.whl
| Download URL | ellar_sql-0.1.8-py3-none-any.whl |
|---|---|
| Size | 49.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88144abadf18a13a248a9a1df518e2bf97dc11201462c910d70e384dcde0cbca
|
|
BLAKE2b-256 checksum How to use checksums |
408e46861e7d4b510f8f67c27617b5a62217d398e276db4d235f9e06fde8c789
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for ellar-sql 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.