activemodel 0.5.0__py3-none-any.whl → 0.7.0__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.
- activemodel/__init__.py +2 -0
- activemodel/base_model.py +105 -29
- activemodel/celery.py +28 -0
- activemodel/errors.py +6 -0
- activemodel/get_column_from_field_patch.py +137 -0
- activemodel/mixins/__init__.py +2 -0
- activemodel/mixins/pydantic_json.py +69 -0
- activemodel/mixins/soft_delete.py +17 -0
- activemodel/mixins/typeid.py +27 -17
- activemodel/pytest/truncate.py +1 -1
- activemodel/query_wrapper.py +24 -10
- activemodel/session_manager.py +75 -5
- activemodel/types/__init__.py +1 -0
- activemodel/types/typeid.py +140 -5
- activemodel/utils.py +51 -1
- activemodel-0.7.0.dist-info/METADATA +235 -0
- activemodel-0.7.0.dist-info/RECORD +24 -0
- {activemodel-0.5.0.dist-info → activemodel-0.7.0.dist-info}/WHEEL +1 -2
- activemodel/_session_manager.py +0 -153
- activemodel-0.5.0.dist-info/METADATA +0 -66
- activemodel-0.5.0.dist-info/RECORD +0 -20
- activemodel-0.5.0.dist-info/top_level.txt +0 -1
- {activemodel-0.5.0.dist-info → activemodel-0.7.0.dist-info}/entry_points.txt +0 -0
- {activemodel-0.5.0.dist-info → activemodel-0.7.0.dist-info/licenses}/LICENSE +0 -0
activemodel/_session_manager.py
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Adapted from: https://github.com/fastapiutils/fastapi-utils/blob/master/fastapi_utils/session.py
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from contextlib import contextmanager
|
|
6
|
-
|
|
7
|
-
import sqlalchemy as sa
|
|
8
|
-
from sqlalchemy.orm import Session
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class FastSessionMaker:
|
|
12
|
-
"""
|
|
13
|
-
A convenience class for managing a (cached) sqlalchemy ORM engine and sessionmaker.
|
|
14
|
-
|
|
15
|
-
Intended for use creating ORM sessions injected into endpoint functions by FastAPI.
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
def __init__(self, database_uri: str):
|
|
19
|
-
"""
|
|
20
|
-
`database_uri` should be any sqlalchemy-compatible database URI.
|
|
21
|
-
|
|
22
|
-
In particular, `sqlalchemy.create_engine(database_uri)` should work to create an engine.
|
|
23
|
-
|
|
24
|
-
Typically, this would look like:
|
|
25
|
-
|
|
26
|
-
"<scheme>://<user>:<password>@<host>:<port>/<database>"
|
|
27
|
-
|
|
28
|
-
A concrete example looks like "postgresql://db_user:password@db:5432/app"
|
|
29
|
-
"""
|
|
30
|
-
self.database_uri = database_uri
|
|
31
|
-
|
|
32
|
-
self._cached_engine: sa.engine.Engine | None = None
|
|
33
|
-
self._cached_sessionmaker: sa.orm.sessionmaker | None = None
|
|
34
|
-
|
|
35
|
-
@property
|
|
36
|
-
def cached_engine(self) -> sa.engine.Engine:
|
|
37
|
-
"""
|
|
38
|
-
Returns a lazily-cached sqlalchemy engine for the instance's database_uri.
|
|
39
|
-
"""
|
|
40
|
-
engine = self._cached_engine
|
|
41
|
-
if engine is None:
|
|
42
|
-
engine = self.get_new_engine()
|
|
43
|
-
self._cached_engine = engine
|
|
44
|
-
return engine
|
|
45
|
-
|
|
46
|
-
@property
|
|
47
|
-
def cached_sessionmaker(self) -> sa.orm.sessionmaker:
|
|
48
|
-
"""
|
|
49
|
-
Returns a lazily-cached sqlalchemy sessionmaker using the instance's (lazily-cached) engine.
|
|
50
|
-
"""
|
|
51
|
-
sessionmaker = self._cached_sessionmaker
|
|
52
|
-
if sessionmaker is None:
|
|
53
|
-
sessionmaker = self.get_new_sessionmaker(self.cached_engine)
|
|
54
|
-
self._cached_sessionmaker = sessionmaker
|
|
55
|
-
return sessionmaker
|
|
56
|
-
|
|
57
|
-
def get_new_engine(self) -> sa.engine.Engine:
|
|
58
|
-
"""
|
|
59
|
-
Returns a new sqlalchemy engine using the instance's database_uri.
|
|
60
|
-
"""
|
|
61
|
-
return get_engine(self.database_uri)
|
|
62
|
-
|
|
63
|
-
def get_new_sessionmaker(
|
|
64
|
-
self, engine: sa.engine.Engine | None
|
|
65
|
-
) -> sa.orm.sessionmaker:
|
|
66
|
-
"""
|
|
67
|
-
Returns a new sessionmaker for the provided sqlalchemy engine. If no engine is provided, the
|
|
68
|
-
instance's (lazily-cached) engine is used.
|
|
69
|
-
"""
|
|
70
|
-
engine = engine or self.cached_engine
|
|
71
|
-
return get_sessionmaker_for_engine(engine)
|
|
72
|
-
|
|
73
|
-
def get_db(self) -> Iterator[Session]:
|
|
74
|
-
"""
|
|
75
|
-
A generator function that yields a sqlalchemy orm session and cleans up the session once resumed after yielding.
|
|
76
|
-
|
|
77
|
-
Can be used directly as a context-manager FastAPI dependency, or yielded from inside a separate dependency.
|
|
78
|
-
"""
|
|
79
|
-
yield from _get_db(self.cached_sessionmaker)
|
|
80
|
-
|
|
81
|
-
@contextmanager
|
|
82
|
-
def context_session(self) -> Iterator[Session]:
|
|
83
|
-
"""
|
|
84
|
-
A context-manager wrapped version of the `get_db` method.
|
|
85
|
-
|
|
86
|
-
This makes it possible to get a context-managed orm session for the relevant database_uri without
|
|
87
|
-
needing to rely on FastAPI's dependency injection.
|
|
88
|
-
|
|
89
|
-
Usage looks like:
|
|
90
|
-
|
|
91
|
-
session_maker = FastAPISessionMaker(database_uri)
|
|
92
|
-
with session_maker.context_session() as session:
|
|
93
|
-
session.query(...)
|
|
94
|
-
...
|
|
95
|
-
"""
|
|
96
|
-
yield from self.get_db()
|
|
97
|
-
|
|
98
|
-
def reset_cache(self) -> None:
|
|
99
|
-
"""
|
|
100
|
-
Resets the engine and sessionmaker caches.
|
|
101
|
-
|
|
102
|
-
After calling this method, the next time you try to use the cached engine or sessionmaker,
|
|
103
|
-
new ones will be created.
|
|
104
|
-
"""
|
|
105
|
-
self._cached_engine = None
|
|
106
|
-
self._cached_sessionmaker = None
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
def get_engine(uri: str) -> sa.engine.Engine:
|
|
110
|
-
"""
|
|
111
|
-
Returns a sqlalchemy engine with pool_pre_ping enabled.
|
|
112
|
-
|
|
113
|
-
This function may be updated over time to reflect recommended engine configuration for use with FastAPI.
|
|
114
|
-
"""
|
|
115
|
-
return sa.create_engine(uri, pool_pre_ping=True)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
def get_sessionmaker_for_engine(engine: sa.engine.Engine) -> sa.orm.sessionmaker:
|
|
119
|
-
"""
|
|
120
|
-
Returns a sqlalchemy sessionmaker for the provided engine with recommended configuration settings.
|
|
121
|
-
|
|
122
|
-
This function may be updated over time to reflect recommended sessionmaker configuration for use with FastAPI.
|
|
123
|
-
"""
|
|
124
|
-
return sa.orm.sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
@contextmanager
|
|
128
|
-
def context_session(engine: sa.engine.Engine) -> Iterator[Session]:
|
|
129
|
-
"""
|
|
130
|
-
This contextmanager yields a managed session for the provided engine.
|
|
131
|
-
|
|
132
|
-
Usage is similar to `FastAPISessionMaker.context_session`, except that you have to provide the engine to use.
|
|
133
|
-
|
|
134
|
-
A new sessionmaker is created for each call, so the FastAPISessionMaker.context_session
|
|
135
|
-
method may be preferable in performance-sensitive contexts.
|
|
136
|
-
"""
|
|
137
|
-
sessionmaker = get_sessionmaker_for_engine(engine)
|
|
138
|
-
yield from _get_db(sessionmaker)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
def _get_db(sessionmaker: sa.orm.sessionmaker) -> Iterator[Session]:
|
|
142
|
-
"""
|
|
143
|
-
A generator function that yields an ORM session using the provided sessionmaker, and cleans it up when resumed.
|
|
144
|
-
"""
|
|
145
|
-
session = sessionmaker()
|
|
146
|
-
try:
|
|
147
|
-
yield session
|
|
148
|
-
session.commit()
|
|
149
|
-
except Exception as exc:
|
|
150
|
-
session.rollback()
|
|
151
|
-
raise exc
|
|
152
|
-
finally:
|
|
153
|
-
session.close()
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: activemodel
|
|
3
|
-
Version: 0.5.0
|
|
4
|
-
Summary: Make SQLModel more like an a real ORM
|
|
5
|
-
Author-email: Michael Bianco <iloveitaly@gmail.com>
|
|
6
|
-
Project-URL: Repository, https://github.com/iloveitaly/activemodel
|
|
7
|
-
Keywords: sqlmodel,orm,activerecord,activemodel,sqlalchemy
|
|
8
|
-
Requires-Python: >=3.10
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
License-File: LICENSE
|
|
11
|
-
Requires-Dist: pydash>=8.0.4
|
|
12
|
-
Requires-Dist: python-decouple-typed>=3.11.0
|
|
13
|
-
Requires-Dist: sqlmodel>=0.0.22
|
|
14
|
-
Requires-Dist: typeid-python>=0.3.1
|
|
15
|
-
|
|
16
|
-
# ActiveModel: ORM Wrapper for SQLModel
|
|
17
|
-
|
|
18
|
-
No, this isn't *really* [ActiveModel](https://guides.rubyonrails.org/active_model_basics.html). It's just a wrapper around SQLModel that provides a more ActiveRecord-like interface.
|
|
19
|
-
|
|
20
|
-
SQLModel is *not* an ORM. It's a SQL query builder and a schema definition tool.
|
|
21
|
-
|
|
22
|
-
This package provides a thin wrapper around SQLModel that provides a more ActiveRecord-like interface with things like:
|
|
23
|
-
|
|
24
|
-
* Timestamp column mixins
|
|
25
|
-
* Lifecycle hooks
|
|
26
|
-
|
|
27
|
-
## Limitations
|
|
28
|
-
|
|
29
|
-
### Validation
|
|
30
|
-
|
|
31
|
-
SQLModel does not currently support pydantic validations (when `table=True`). This is very surprising, but is actually the intended functionality:
|
|
32
|
-
|
|
33
|
-
* https://github.com/fastapi/sqlmodel/discussions/897
|
|
34
|
-
* https://github.com/fastapi/sqlmodel/pull/1041
|
|
35
|
-
* https://github.com/fastapi/sqlmodel/issues/453
|
|
36
|
-
* https://github.com/fastapi/sqlmodel/issues/52#issuecomment-1311987732
|
|
37
|
-
|
|
38
|
-
For validation:
|
|
39
|
-
|
|
40
|
-
* When consuming API data, use a separate shadow model to validate the data with `table=False` and then inherit from that model in a model with `table=True`.
|
|
41
|
-
* When validating ORM data, use SQL Alchemy hooks.
|
|
42
|
-
|
|
43
|
-
<!--
|
|
44
|
-
|
|
45
|
-
This looks neat
|
|
46
|
-
https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d2a4/src/my_model/model.py#L155
|
|
47
|
-
schema_extra={
|
|
48
|
-
'pattern': r'^[a-z0-9_\-\.]+\@[a-z0-9_\-\.]+\.[a-z\.]+$'
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
extra constraints
|
|
52
|
-
|
|
53
|
-
https://github.com/DarylStark/my_data/blob/a17b8b3a8463b9953821b89fee895e272f94d2a4/src/my_model/model.py#L424C1-L426C6
|
|
54
|
-
-->
|
|
55
|
-
## Related Projects
|
|
56
|
-
|
|
57
|
-
* https://github.com/woofz/sqlmodel-basecrud
|
|
58
|
-
|
|
59
|
-
## Inspiration
|
|
60
|
-
|
|
61
|
-
* https://github.com/peterdresslar/fastapi-sqlmodel-alembic-pg
|
|
62
|
-
* [Albemic instructions](https://github.com/fastapi/sqlmodel/pull/899/files)
|
|
63
|
-
* https://github.com/fastapiutils/fastapi-utils/
|
|
64
|
-
* https://github.com/fastapi/full-stack-fastapi-template
|
|
65
|
-
* https://github.com/DarylStark/my_data/
|
|
66
|
-
* https://github.com/petrgazarov/FastAPI-app/tree/main/fastapi_app
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
activemodel/__init__.py,sha256=lO75TeJeSm7spmP4P1_Z5oIXBHlL7kKjr1LS2Z4dNUA,109
|
|
2
|
-
activemodel/_session_manager.py,sha256=ojFIpK_fl1-JZsj98zn-zp4RIK1_eyDl-Ga4F1XXbVw,5249
|
|
3
|
-
activemodel/base_model.py,sha256=y8gLmowqk950AELE67u69YJAT9pJSmm67apjOd1g2yo,8081
|
|
4
|
-
activemodel/logger.py,sha256=vU7QiGSy_AJuJFmClUocqIJ-Ltku_8C24ZU8L6fLJR0,53
|
|
5
|
-
activemodel/query_wrapper.py,sha256=zqaA3yAHPTfGLrkzZ6ge6byedV497C7YG1QJe_4ah7Q,2100
|
|
6
|
-
activemodel/session_manager.py,sha256=zU4Eu3YeKSlZrYEBkN70XAHJOHka40Rl4qMBZvEgNwA,1822
|
|
7
|
-
activemodel/utils.py,sha256=dY6wvAS2RCqMVMQfnpXGJxgWRBet0jV5ZXxGIRX1iRw,476
|
|
8
|
-
activemodel/mixins/__init__.py,sha256=VAVLc96oSSultP0BNAlaE3ZBNGlcuVD0JjS1FMCno7k,72
|
|
9
|
-
activemodel/mixins/timestamps.py,sha256=Q-IFljeVVJQqw3XHdOi7dkqzefiVg1zhJvq_bldpmjg,992
|
|
10
|
-
activemodel/mixins/typeid.py,sha256=0TOJ74As9JL4WVvlRm0yuZB49xHAcX0n1fzwKpgdpys,894
|
|
11
|
-
activemodel/pytest/__init__.py,sha256=W9KKQHbPkyq0jrMXaiL8hG2Nsbjy_LN9HhvgGm8W_7g,98
|
|
12
|
-
activemodel/pytest/transaction.py,sha256=rrsoHnbu79kNdnI5fZeOZr5hzrLB-cQH10MueQp5jV4,1670
|
|
13
|
-
activemodel/pytest/truncate.py,sha256=BdltCtLQNPDgRSxpBnGYGSjB_7DAceV5kHdQ_vLrw74,1583
|
|
14
|
-
activemodel/types/typeid.py,sha256=rcr9tSiu5rowD_WOcF4zzBpEUy2izmYEPteDQgCIbhs,1799
|
|
15
|
-
activemodel-0.5.0.dist-info/LICENSE,sha256=L8mmpX47rB-xtJ_HsK0zpfO6viEjxbLYGn70BMp8os4,1071
|
|
16
|
-
activemodel-0.5.0.dist-info/METADATA,sha256=4nieriFioXBxTPOIuxul3RXzNMwKPiDgSYX4KMkwSbQ,2429
|
|
17
|
-
activemodel-0.5.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
18
|
-
activemodel-0.5.0.dist-info/entry_points.txt,sha256=YLX62TP_hR-n3HMBkdBex4W7XRiyOtIPkwy22puIjjQ,61
|
|
19
|
-
activemodel-0.5.0.dist-info/top_level.txt,sha256=JCMUN_seFIi6GXtnTQRWfxXDx6Oj1uok8qapQWbWKDM,12
|
|
20
|
-
activemodel-0.5.0.dist-info/RECORD,,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
activemodel
|
|
File without changes
|
|
File without changes
|