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
Create or Update Record in database.
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero").save()
hero.name = "hero_2"
hero.save()
create
Create new record in database.
Hero(id=1, name="hero_1", secret_name="Secret_hero").create()
update
Update record in database.
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero")
hero.name = "hero_2"
hero.update()
delete
Delete record from database
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero").save()
hero.delete()
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
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()
create_tables
Create tables in database.
It is equivalant of SQLModel.metadata.create_all method where you don't need to provide bind parameter.
SQLModelPlus.create_tables()
Handle multiple database
If you have multiple databases ORM models, then you use __scope__ class attribute to define database scopes.
Set __scope__ variable to any unique identifier string to identify databases.
from sqlalchemy.orm import registry
from sqlmodel_plus import SQLModelPlus
from typing import Optional
from sqlmodel import Field
class DatabaseA(SQLModelPlus, registry=registry()):
__scope__ = "db1"
class DatabaseB(SQLModelPlus, registry=registry()):
__scope__ = "db2"
DatabaseA.set_engine(create_engine(f"sqlite:///database_A.db"))
DatabaseB.set_engine(create_engine(f"sqlite:///database_B.db"))
# This uses DatabaseA as base ORM model and interacts with database_A
class TableA(DatabaseA, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
# This uses DatabaseB as base ORM model and interacts with database_B
class TableB(DatabaseB, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
Metadata
Release files for sqlmodel-plus 0.2.2
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 |
|---|---|---|---|---|
| sqlmodel_plus-0.2.2.tar.gz | 6.8 kB | Without provenance | Details for sqlmodel_plus-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlmodel_plus-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for sqlmodel_plus-0.2.2-py3-none-any.whl |
Total release size: 14.4 kB
Release files / sqlmodel_plus-0.2.2.tar.gz
| Download URL | sqlmodel_plus-0.2.2.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0aec6bd9d474c236a77743b20a78e0cebf7c626ebdd42ce5c0c0cabc935703ab
|
|
BLAKE2b-256 checksum How to use checksums |
9ed21b6a2a73bb1c1979d8fb246df34f4938917769c3e0c8c2709a3f6fbe0056
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Darwin/23.1.0
|
Release files / sqlmodel_plus-0.2.2-py3-none-any.whl
| Download URL | sqlmodel_plus-0.2.2-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
add650be6e7770e7c71589aa8db2b72e32e9ad3d2bd499995d886de299a822d1
|
|
BLAKE2b-256 checksum How to use checksums |
ff8d0eee4faf3a04ddd0bb473edd607443a94d4a856cb1bb6a8b54fcc1ac3598
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.6 Darwin/23.1.0
|
Security for sqlmodel-plus 0.2.2
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.