Extend SQLModel functionalities.
Project description
SQLModel Plus
Installation
Install package using pip -> pip install sqlmodel-plus
Usage
Use SQLModelPlus
class to define data models and tables as it inherits SQLModel
.
To use in-built functions, an engine needs to set using set_engine
method.
from sqlmodel_plus import SQLModelPlus, EngineException
from typing import Optional
from sqlmodel import Field, SQLModel, create_engine
class Hero(SQLModelPlus, table=True):
__tablename__ = "hero"
id: Optional[int] = Field(default=None, primary_key=True)
name: str
secret_name: str
age: Optional[int] = None
SQLModelPlus.set_engine(create_engine(f"sqlite:///database.db"))
If engine is not set it will raise EngineException
By using SQLModelPlus
class, it provide following classmethods available to all inherited classes -
find_by_id
Hero.find_by_id(1)
Hero.find_by_id("uuid")
Hero.find_by_id((1, 2,))
Hero.find_by_id({"id": 1, "version": 3})
save
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero").save()
hero.name = "hero_2"
hero.save()
create
Hero(id=1, name="hero_1", secret_name="Secret_hero").create()
update
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero")
hero.name = "hero_2"
hero.update()
select
statement = Hero.select # This is equivalant to select(Hero)
statement.where(Hero.id == 1)
query
statement = Hero.select.where(Hero.id == 1)
Hero.query(statement).all()
Hero.query("SELECT id FROM hero").first()
Hero.query(statement).count()
Session
session = Hero.Session # Return Session object
session.exec(Hero.select).all()
with Hero.Session as session:
session.exec(Hero.select.where(Hero.id == 1)).first()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sqlmodel_plus-0.1.1.tar.gz
(2.3 kB
view hashes)
Built Distribution
Close
Hashes for sqlmodel_plus-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5446ae9ff86d4372f84bf1faaa365ccd13d7943422138f6b3211096fb3cdf8d |
|
MD5 | 3eb35fe9f86bb6453d73440ccac26e6b |
|
BLAKE2b-256 | 127e371cea548539699109360d85f12444b7412d3b1800fea4fa9b00f66a8e25 |