sqltypes 0.1.0__tar.gz

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,33 @@
1
+ Metadata-Version: 2.1
2
+ Name: sqltypes
3
+ Version: 0.1.0
4
+ Summary: Custom types for SQLModel/SQLalchemy
5
+ Author-email: Marcel Claramunt <marcel@moveread.com>
6
+ Project-URL: repo, https://github.com/marciclabas/python-storage.git
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: pydantic
10
+ Requires-Dist: sqlalchemy
11
+ Requires-Dist: lazy-loader
12
+
13
+ # Sql Types
14
+
15
+ > Custom types for SQLModel/SQLalchemy
16
+
17
+ ## Usage
18
+
19
+ ```python
20
+ from typing import Sequence
21
+ from pydantic import BaseModel
22
+ from sqlmodel import Field, SQLModel
23
+ from sqltypes import PydanticModel, SpaceDelimitedList
24
+
25
+ class User(BaseModel):
26
+ name: str
27
+ age: int
28
+
29
+ class MyDBItem(SQLModel, table=True):
30
+ id: int | None = Field(default=None, primary_key=True)
31
+ tags: Sequence[str] = Field(sa_type=SpaceDelimitedList)
32
+ user: User = Field(sa_type=PydanticModel(User))
33
+ ```
@@ -0,0 +1,21 @@
1
+ # Sql Types
2
+
3
+ > Custom types for SQLModel/SQLalchemy
4
+
5
+ ## Usage
6
+
7
+ ```python
8
+ from typing import Sequence
9
+ from pydantic import BaseModel
10
+ from sqlmodel import Field, SQLModel
11
+ from sqltypes import PydanticModel, SpaceDelimitedList
12
+
13
+ class User(BaseModel):
14
+ name: str
15
+ age: int
16
+
17
+ class MyDBItem(SQLModel, table=True):
18
+ id: int | None = Field(default=None, primary_key=True)
19
+ tags: Sequence[str] = Field(sa_type=SpaceDelimitedList)
20
+ user: User = Field(sa_type=PydanticModel(User))
21
+ ```
@@ -0,0 +1,25 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "sqltypes"
7
+ version = "0.1.0"
8
+ authors = [
9
+ {name="Marcel Claramunt", email="marcel@moveread.com"}
10
+ ]
11
+ description = "Custom types for SQLModel/SQLalchemy"
12
+ dependencies = [
13
+ "pydantic", "sqlalchemy", "lazy-loader"
14
+ ]
15
+ requires-python = ">=3.10"
16
+ readme = {file="README.md", content-type="text/markdown"}
17
+
18
+ [project.urls]
19
+ repo = "https://github.com/marciclabas/python-storage.git"
20
+
21
+ # [project.optional-dependencies]
22
+ # test = [
23
+ # "pytest < 5.0.0",
24
+ # "pytest-cov[all]"
25
+ # ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,6 @@
1
+ """
2
+ ### Sql Types
3
+ > Custom types for SQLModel, SQLalchemy
4
+ """
5
+ import lazy_loader as lazy
6
+ __getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)
@@ -0,0 +1,9 @@
1
+ from .meta import CustomTypeMeta
2
+ from .string import CustomString, SpaceDelimitedList, CustomStringMeta
3
+ from .pydantic import PydanticModel
4
+
5
+ __all__ = [
6
+ 'CustomTypeMeta',
7
+ 'CustomStringMeta', 'CustomString', 'SpaceDelimitedList',
8
+ 'PydanticModel',
9
+ ]
@@ -0,0 +1,26 @@
1
+ from typing import TypeVar, Callable
2
+ from pydantic import BaseModel
3
+ from sqlalchemy.types import TypeDecorator, TypeEngine
4
+
5
+ T = TypeVar('T')
6
+ D = TypeVar('D')
7
+ E = TypeVar('E', bound=TypeEngine)
8
+ M = TypeVar('M', bound=BaseModel)
9
+
10
+ class CustomTypeMeta(type):
11
+ def __new__(cls, name: str, bases: tuple, dct: dict, Impl: type[E], dump: Callable[[T], D], parse: Callable[[D], T]):
12
+
13
+ class CustomType(TypeDecorator):
14
+ impl = Impl
15
+ cache_ok = True
16
+
17
+ def process_bind_param(self, value: T | None, dialect) -> D | None:
18
+ if value is not None:
19
+ return dump(value)
20
+
21
+ def process_result_value(self, value: D | None, dialect) -> T | None:
22
+ if value is not None:
23
+ return parse(value)
24
+
25
+ # Return the new class type
26
+ return type(name, (CustomType, *bases), dct)
@@ -0,0 +1,10 @@
1
+ from typing import TypeVar, Generic
2
+ from sqlalchemy.types import TypeDecorator, JSON
3
+ from pydantic import BaseModel
4
+ from .meta import CustomTypeMeta
5
+
6
+ M = TypeVar('M', bound=BaseModel)
7
+
8
+ class PydanticModel(type):
9
+ def __new__(cls, Model: type[M], name: str | None = None) -> type[TypeDecorator[M]]:
10
+ return CustomTypeMeta(f'DB{name or Model.__name__}', (), {}, JSON, dump=lambda M: M.model_dump(exclude_none=True), parse=Model.model_validate)
@@ -0,0 +1,15 @@
1
+ from typing import TypeVar, Callable
2
+ from sqlalchemy.types import TypeDecorator, String
3
+ from .meta import CustomTypeMeta
4
+
5
+ T = TypeVar('T')
6
+
7
+ class CustomStringMeta(type):
8
+ def __new__(cls, name: str, bases, dct, dump: Callable[[T], str], parse: Callable[[str], T]) -> type[TypeDecorator[T]]:
9
+ return CustomTypeMeta(name, bases, dct, String, dump=dump, parse=parse)
10
+
11
+ def CustomString(name: str, dump: Callable[[T], str], parse: Callable[[str], T]) -> type[TypeDecorator[T]]:
12
+ return CustomTypeMeta(name, (), {}, String, dump=dump, parse=parse)
13
+
14
+ class SpaceDelimitedList(metaclass=CustomStringMeta, dump=' '.join, parse=str.split):
15
+ ...
@@ -0,0 +1,33 @@
1
+ Metadata-Version: 2.1
2
+ Name: sqltypes
3
+ Version: 0.1.0
4
+ Summary: Custom types for SQLModel/SQLalchemy
5
+ Author-email: Marcel Claramunt <marcel@moveread.com>
6
+ Project-URL: repo, https://github.com/marciclabas/python-storage.git
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: pydantic
10
+ Requires-Dist: sqlalchemy
11
+ Requires-Dist: lazy-loader
12
+
13
+ # Sql Types
14
+
15
+ > Custom types for SQLModel/SQLalchemy
16
+
17
+ ## Usage
18
+
19
+ ```python
20
+ from typing import Sequence
21
+ from pydantic import BaseModel
22
+ from sqlmodel import Field, SQLModel
23
+ from sqltypes import PydanticModel, SpaceDelimitedList
24
+
25
+ class User(BaseModel):
26
+ name: str
27
+ age: int
28
+
29
+ class MyDBItem(SQLModel, table=True):
30
+ id: int | None = Field(default=None, primary_key=True)
31
+ tags: Sequence[str] = Field(sa_type=SpaceDelimitedList)
32
+ user: User = Field(sa_type=PydanticModel(User))
33
+ ```
@@ -0,0 +1,12 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/sqltypes/__init__.py
4
+ src/sqltypes/__init__.pyi
5
+ src/sqltypes/meta.py
6
+ src/sqltypes/pydantic.py
7
+ src/sqltypes/string.py
8
+ src/sqltypes.egg-info/PKG-INFO
9
+ src/sqltypes.egg-info/SOURCES.txt
10
+ src/sqltypes.egg-info/dependency_links.txt
11
+ src/sqltypes.egg-info/requires.txt
12
+ src/sqltypes.egg-info/top_level.txt
@@ -0,0 +1,3 @@
1
+ pydantic
2
+ sqlalchemy
3
+ lazy-loader
@@ -0,0 +1 @@
1
+ sqltypes