pjdev-sqlmodel 0.0.1a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ __version__ = "0.0.1a0"
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
@@ -0,0 +1,55 @@
1
+ from contextlib import contextmanager
2
+ from typing import (
3
+ Optional,
4
+ Type,
5
+ List,
6
+ )
7
+
8
+ from sqlalchemy import Engine, NullPool
9
+ from sqlmodel import SQLModel, create_engine, Session as SQLModelSession
10
+
11
+ from pjdev_sqlmodel.pjdev_db_settings import SqlModelSettings
12
+
13
+
14
+ class DBContext:
15
+ initialized: bool = False
16
+ engine: Optional[Engine] = None
17
+
18
+
19
+ __ctx = DBContext()
20
+
21
+
22
+ def init(settings: SqlModelSettings) -> None:
23
+ database_url = f"sqlite:///{settings.data_path}"
24
+ __ctx.engine = create_engine(database_url, echo=False, poolclass=NullPool)
25
+
26
+
27
+ def initialize_engine(
28
+ settings: SqlModelSettings,
29
+ tables: List[Type[SQLModel]],
30
+ echo: bool = False,
31
+ ) -> Engine:
32
+ if len(tables) == 0:
33
+ raise ValueError("Must specify at least one table")
34
+
35
+ database_url = f"sqlite:///{settings.data_path}"
36
+
37
+ engine = create_engine(database_url, echo=echo, poolclass=NullPool)
38
+
39
+ for t in tables:
40
+ t.__table__.create(bind=engine, checkfirst=True)
41
+
42
+ return engine
43
+
44
+
45
+ def configure_single_context(settings: SqlModelSettings, tables: List[Type[SQLModel]]):
46
+ __ctx.engine = initialize_engine(settings, tables)
47
+
48
+
49
+ @contextmanager
50
+ def session_context() -> SQLModelSession:
51
+ with SQLModelSession(__ctx.engine) as session:
52
+ try:
53
+ yield session
54
+ finally:
55
+ session.close()
@@ -0,0 +1,34 @@
1
+ from pathlib import Path
2
+ from typing import (
3
+ Optional,
4
+ )
5
+
6
+ from pydantic_settings import BaseSettings, SettingsConfigDict
7
+
8
+
9
+ class SqlModelSettings(BaseSettings):
10
+ data_path: Path
11
+
12
+ model_config = SettingsConfigDict(
13
+ case_sensitive=False, extra="ignore", env_prefix="PJDEV_"
14
+ )
15
+
16
+
17
+ class Context:
18
+ settings: Optional[SqlModelSettings] = None
19
+
20
+
21
+ __ctx = Context()
22
+
23
+
24
+ def init_settings(root: Path, data_directory_name: str = ".data") -> None:
25
+ __ctx.settings = SqlModelSettings(
26
+ _env_file=root / ".env", data_path=root / data_directory_name
27
+ )
28
+
29
+
30
+ def get_settings() -> SqlModelSettings:
31
+ if __ctx.settings is None:
32
+ msg = "Settings are not initialized -- call init_settings()"
33
+ raise Exception(msg)
34
+ return __ctx.settings
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.3
2
+ Name: pjdev-sqlmodel
3
+ Version: 0.0.1a0
4
+ Project-URL: Documentation, https://gitlab.purplejay.net/keystone/python/-/tree/main/pjdev-sqlmodel#readme
5
+ Project-URL: Issues, https://gitlab.purplejay.net/keystone/python/issues
6
+ Project-URL: Source, https://gitlab.purplejay.net/keystone/python
7
+ Author-email: Purple Jay LLC <app-support@purplejay.io>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: Implementation :: CPython
14
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
15
+ Requires-Python: >=3.12
16
+ Requires-Dist: pydantic-settings>=2.3.4
17
+ Requires-Dist: sqlmodel>=0.0.19
18
+ Description-Content-Type: text/markdown
19
+
20
+ # pjdev-sqlmodel
21
+
22
+ [![PyPI - Version](https://img.shields.io/pypi/v/pjdev-sqlmodel.svg)](https://pypi.org/project/pjdev-sqlmodel)
23
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pjdev-sqlmodel.svg)](https://pypi.org/project/pjdev-sqlmodel)
24
+
25
+ -----
26
+
27
+ ## Table of Contents
28
+
29
+ - [Installation](#installation)
30
+ - [License](#license)
31
+
32
+ ## Installation
33
+
34
+ ```console
35
+ pip install pjdev-sqlmodel
36
+ ```
37
+
38
+ ## License
39
+
40
+ `pjdev-sqlmodel` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,8 @@
1
+ pjdev_sqlmodel/__about__.py,sha256=bzIP6vtYs0LcBFI4x0VhzEGCFSAcNUl1ZE4Jue6xNBQ,131
2
+ pjdev_sqlmodel/__init__.py,sha256=p8HS8DuMfFNAzuLS_PWRIyHOQdNes0L5TqNE9M-L7nc,107
3
+ pjdev_sqlmodel/pjdev_db_service.py,sha256=4-7AYSLAUUKcd3PvePrQWg5DEZ-Mfj9pAX59tkjSXxA,1337
4
+ pjdev_sqlmodel/pjdev_db_settings.py,sha256=mJXC9-cIpUoPxdK5J19d8bbFR4N8_BydwtposUbV3bc,772
5
+ pjdev_sqlmodel-0.0.1a0.dist-info/METADATA,sha256=tjwvEqGFafcRF69w4lKWaOahIAP_jzM3DpXxkWbIVlM,1346
6
+ pjdev_sqlmodel-0.0.1a0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
7
+ pjdev_sqlmodel-0.0.1a0.dist-info/licenses/LICENSE.txt,sha256=0wYf-NtzsFHBi7LG7ANo5X1v8sZfdv-7ZoslV6Vy3nA,1099
8
+ pjdev_sqlmodel-0.0.1a0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.25.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Chris O'Neill <chris@purplejay.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.