Name | sqlmodel-plus JSON |
Version |
0.2.2
JSON |
| download |
home_page | |
Summary | Extend SQLModel functionalities. |
upload_time | 2023-12-06 14:43:29 |
maintainer | |
docs_url | None |
author | Soami Charan |
requires_python | >=3.8,<4.0 |
license | |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# SQLModel Plus
[![GitHub license badge](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/badge-license.svg)](http://www.apache.org/licenses/LICENSE-2.0)
[![Coverage](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/coverage.svg)]()
[![pypi](https://img.shields.io/pypi/v/sqlmodel-plus.svg)](https://pypi.python.org/pypi/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.
```python
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
```python
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.
```python
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero").save()
hero.name = "hero_2"
hero.save()
```
#### create
Create new record in database.
```python
Hero(id=1, name="hero_1", secret_name="Secret_hero").create()
```
#### update
Update record in database.
```python
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero")
hero.name = "hero_2"
hero.update()
```
#### delete
Delete record from database
```python
hero = Hero(id=1, name="hero_1", secret_name="Secret_hero").save()
hero.delete()
```
#### select
```python
statement = Hero.select # This is equivalant to select(Hero)
statement.where(Hero.id == 1)
```
#### query
```python
statement = Hero.select.where(Hero.id == 1)
Hero.query(statement).all
Hero.query("SELECT id FROM hero").first
```
#### Session
```python
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.
```python
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.
```python
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)
```
Raw data
{
"_id": null,
"home_page": "",
"name": "sqlmodel-plus",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Soami Charan",
"author_email": "charansoami@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9e/d2/1b6a2a73bb1c1979d8fb246df34f4938917769c3e0c8c2709a3f6fbe0056/sqlmodel_plus-0.2.2.tar.gz",
"platform": null,
"description": "# SQLModel Plus\n\n[![GitHub license badge](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/badge-license.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n[![Coverage](https://raw.githubusercontent.com/soamicharan/sqlmodel-plus/main/badges/coverage.svg)]()\n[![pypi](https://img.shields.io/pypi/v/sqlmodel-plus.svg)](https://pypi.python.org/pypi/sqlmodel-plus)\n\n## Installation\n\nInstall package using pip -> `pip install sqlmodel-plus`\n\n## Usage\n\nUse `SQLModelPlus` class to define data models and tables as it inherits `SQLModel`. \\\nTo use in-built functions, an engine needs to set using `set_engine` method.\n\n```python\nfrom sqlmodel_plus import SQLModelPlus, EngineException\nfrom typing import Optional\nfrom sqlmodel import Field, SQLModel, create_engine\n\nclass Hero(SQLModelPlus, table=True):\n __tablename__ = \"hero\"\n id: Optional[int] = Field(default=None, primary_key=True)\n name: str\n secret_name: str\n age: Optional[int] = None\n\nSQLModelPlus.set_engine(create_engine(f\"sqlite:///database.db\"))\n```\n\nIf engine is not set it will raise `EngineException`\n\nBy using `SQLModelPlus` class, it provide following classmethods available to all inherited classes -\n\n#### find_by_id\n\n```python\nHero.find_by_id(1)\nHero.find_by_id(\"uuid\")\nHero.find_by_id((1, 2,))\nHero.find_by_id({\"id\": 1, \"version\": 3})\n```\n\n#### save\n\nCreate or Update Record in database.\n```python\nhero = Hero(id=1, name=\"hero_1\", secret_name=\"Secret_hero\").save()\nhero.name = \"hero_2\"\nhero.save()\n\n```\n\n#### create\n\nCreate new record in database.\n```python\nHero(id=1, name=\"hero_1\", secret_name=\"Secret_hero\").create()\n```\n\n#### update\n\nUpdate record in database.\n```python\nhero = Hero(id=1, name=\"hero_1\", secret_name=\"Secret_hero\")\nhero.name = \"hero_2\"\nhero.update()\n```\n\n#### delete\n\nDelete record from database\n```python\nhero = Hero(id=1, name=\"hero_1\", secret_name=\"Secret_hero\").save()\nhero.delete()\n```\n\n#### select\n\n```python\nstatement = Hero.select # This is equivalant to select(Hero)\nstatement.where(Hero.id == 1)\n```\n\n#### query\n\n```python\nstatement = Hero.select.where(Hero.id == 1)\nHero.query(statement).all\nHero.query(\"SELECT id FROM hero\").first\n```\n\n#### Session\n\n```python\nsession = Hero.Session # Return Session object\nsession.exec(Hero.select).all()\n\nwith Hero.Session as session:\n session.exec(Hero.select.where(Hero.id == 1)).first()\n```\n\n#### create_tables\n\nCreate tables in database. \\\nIt is equivalant of `SQLModel.metadata.create_all` method where you don't need to provide bind parameter.\n```python\nSQLModelPlus.create_tables()\n```\n\n## Handle multiple database\n\nIf you have multiple databases ORM models, then you use `__scope__` class attribute to define database scopes. \\\nSet `__scope__` variable to any unique identifier string to identify databases.\n\n```python\nfrom sqlalchemy.orm import registry\nfrom sqlmodel_plus import SQLModelPlus\nfrom typing import Optional\nfrom sqlmodel import Field\nclass DatabaseA(SQLModelPlus, registry=registry()):\n __scope__ = \"db1\"\n\nclass DatabaseB(SQLModelPlus, registry=registry()):\n __scope__ = \"db2\"\n\nDatabaseA.set_engine(create_engine(f\"sqlite:///database_A.db\"))\n\nDatabaseB.set_engine(create_engine(f\"sqlite:///database_B.db\"))\n\n# This uses DatabaseA as base ORM model and interacts with database_A\nclass TableA(DatabaseA, table=True):\n id: Optional[int] = Field(default=None, primary_key=True)\n\n# This uses DatabaseB as base ORM model and interacts with database_B\nclass TableB(DatabaseB, table=True):\n id: Optional[int] = Field(default=None, primary_key=True)\n```\n",
"bugtrack_url": null,
"license": "",
"summary": "Extend SQLModel functionalities.",
"version": "0.2.2",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ff8d0eee4faf3a04ddd0bb473edd607443a94d4a856cb1bb6a8b54fcc1ac3598",
"md5": "812af02e3dd67f46e3eb270bb189660a",
"sha256": "add650be6e7770e7c71589aa8db2b72e32e9ad3d2bd499995d886de299a822d1"
},
"downloads": -1,
"filename": "sqlmodel_plus-0.2.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "812af02e3dd67f46e3eb270bb189660a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 7559,
"upload_time": "2023-12-06T14:43:28",
"upload_time_iso_8601": "2023-12-06T14:43:28.425490Z",
"url": "https://files.pythonhosted.org/packages/ff/8d/0eee4faf3a04ddd0bb473edd607443a94d4a856cb1bb6a8b54fcc1ac3598/sqlmodel_plus-0.2.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9ed21b6a2a73bb1c1979d8fb246df34f4938917769c3e0c8c2709a3f6fbe0056",
"md5": "e6fa92e5309c3e86a44a576d0232d3a1",
"sha256": "0aec6bd9d474c236a77743b20a78e0cebf7c626ebdd42ce5c0c0cabc935703ab"
},
"downloads": -1,
"filename": "sqlmodel_plus-0.2.2.tar.gz",
"has_sig": false,
"md5_digest": "e6fa92e5309c3e86a44a576d0232d3a1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 6827,
"upload_time": "2023-12-06T14:43:29",
"upload_time_iso_8601": "2023-12-06T14:43:29.665385Z",
"url": "https://files.pythonhosted.org/packages/9e/d2/1b6a2a73bb1c1979d8fb246df34f4938917769c3e0c8c2709a3f6fbe0056/sqlmodel_plus-0.2.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-06 14:43:29",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "sqlmodel-plus"
}