maleo-database 0.0.21__py3-none-any.whl → 0.0.24__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.
@@ -1,8 +1,5 @@
1
1
  from pydantic import BaseModel, ConfigDict, Field
2
- from sqlalchemy import MetaData
3
2
  from typing import Generic, Optional, TypeVar
4
- from maleo.dtos.contexts.service import ServiceContext
5
- from maleo.logging.logger import Database
6
3
  from .config import (
7
4
  ElasticsearchConfig,
8
5
  MongoConfig,
@@ -23,6 +20,7 @@ from .managers import (
23
20
  SQLServerManager,
24
21
  ManagerT,
25
22
  )
23
+ from .types import DeclarativeBaseT
26
24
 
27
25
 
28
26
  class Handler(
@@ -42,17 +40,7 @@ HandlerT = TypeVar("HandlerT", bound=Handler)
42
40
 
43
41
 
44
42
  class ElasticsearchHandler(Handler[ElasticsearchConfig, ElasticsearchManager]):
45
- @classmethod
46
- def new(
47
- cls,
48
- config: ElasticsearchConfig,
49
- logger: Database,
50
- service_context: Optional[ServiceContext] = None,
51
- ) -> "ElasticsearchHandler":
52
- manager = ElasticsearchManager(
53
- config=config, logger=logger, service_context=service_context
54
- )
55
- return cls(config=config, manager=manager)
43
+ pass
56
44
 
57
45
 
58
46
  class MongoHandler(Handler[MongoConfig, MongoManager]):
@@ -63,33 +51,29 @@ class RedisHandler(Handler[RedisConfig, RedisManager]):
63
51
  pass
64
52
 
65
53
 
66
- class MySQLHandler(Handler[MySQLConfig, MySQLManager]):
54
+ class MySQLHandler(
55
+ Handler[MySQLConfig, MySQLManager[DeclarativeBaseT]], Generic[DeclarativeBaseT]
56
+ ):
57
+ pass
58
+
59
+
60
+ class PostgreSQLHandler(
61
+ Handler[PostgreSQLConfig, PostgreSQLManager[DeclarativeBaseT]],
62
+ Generic[DeclarativeBaseT],
63
+ ):
67
64
  pass
68
65
 
69
66
 
70
- class PostgreSQLHandler(Handler[PostgreSQLConfig, PostgreSQLManager]):
71
- @classmethod
72
- def new(
73
- cls,
74
- config: PostgreSQLConfig,
75
- logger: Database,
76
- metadata: MetaData,
77
- service_context: Optional[ServiceContext] = None,
78
- ) -> "PostgreSQLHandler":
79
- manager = PostgreSQLManager(
80
- config=config,
81
- logger=logger,
82
- metadata=metadata,
83
- service_context=service_context,
84
- )
85
- return cls(config=config, manager=manager)
86
-
87
-
88
- class SQLiteHandler(Handler[SQLiteConfig, SQLiteManager]):
67
+ class SQLiteHandler(
68
+ Handler[SQLiteConfig, SQLiteManager[DeclarativeBaseT]], Generic[DeclarativeBaseT]
69
+ ):
89
70
  pass
90
71
 
91
72
 
92
- class SQLServerHandler(Handler[SQLServerConfig, SQLServerManager]):
73
+ class SQLServerHandler(
74
+ Handler[SQLServerConfig, SQLServerManager[DeclarativeBaseT]],
75
+ Generic[DeclarativeBaseT],
76
+ ):
93
77
  pass
94
78
 
95
79
 
@@ -190,7 +174,7 @@ class Handlers(
190
174
  ],
191
175
  ):
192
176
  nosql: NoSQLHandlersT = Field(..., description="NoSQL handlers")
193
- sql: NoSQLHandlersT = Field(..., description="SQL handlers")
177
+ sql: SQLHandlersT = Field(..., description="SQL handlers")
194
178
 
195
179
 
196
180
  HandlersT = TypeVar("HandlersT", bound=Optional[Handlers])
@@ -6,8 +6,8 @@ from motor.motor_asyncio import AsyncIOMotorClient
6
6
  from pymongo import MongoClient
7
7
  from redis.asyncio import Redis as AsyncRedis
8
8
  from redis import Redis as SyncRedis
9
- from sqlalchemy import MetaData, text
10
- from typing import Generic, Optional, TypeVar
9
+ from sqlalchemy import text
10
+ from typing import Generic, Optional, Type, TypeVar
11
11
  from uuid import uuid4
12
12
  from maleo.dtos.authentication import GenericAuthentication
13
13
  from maleo.dtos.contexts.operation import generate_operation_context
@@ -43,6 +43,7 @@ from ..config import (
43
43
  ConfigT,
44
44
  )
45
45
  from ..enums import Connection
46
+ from ..types import DeclarativeBaseT
46
47
  from .client import (
47
48
  AsyncClientT,
48
49
  SyncClientT,
@@ -96,16 +97,15 @@ class Manager(ABC, Generic[ConfigT]):
96
97
  pass
97
98
 
98
99
 
99
- class SQLManager(Manager[SQLConfigT], Generic[SQLConfigT]):
100
+ class SQLManager(Manager[SQLConfigT], Generic[SQLConfigT, DeclarativeBaseT]):
100
101
  def __init__(
101
102
  self,
103
+ Base: Type[DeclarativeBaseT],
102
104
  config: SQLConfigT,
103
105
  logger: Database,
104
- metadata: MetaData,
105
106
  service_context: Optional[ServiceContext] = None,
106
107
  ) -> None:
107
108
  super().__init__(config, logger, service_context)
108
- self._metadata = metadata
109
109
  self._operation_context.target.details = self._config.model_dump()
110
110
  self._engine_manager = EngineManager[SQLConfigT](config)
111
111
  self._session_manager = SessionManager(
@@ -114,7 +114,8 @@ class SQLManager(Manager[SQLConfigT], Generic[SQLConfigT]):
114
114
  logger=self._logger,
115
115
  service_context=self._service_context,
116
116
  )
117
- self._metadata.create_all(bind=self._engine_manager.get(Connection.SYNC))
117
+ self.Base = Base
118
+ self.Base.metadata.create_all(bind=self._engine_manager.get(Connection.SYNC))
118
119
 
119
120
  @property
120
121
  def engine(self) -> EngineManager[SQLConfigT]:
@@ -251,19 +252,27 @@ class SQLManager(Manager[SQLConfigT], Generic[SQLConfigT]):
251
252
  await self._engine_manager.dispose()
252
253
 
253
254
 
254
- class MySQLManager(SQLManager[MySQLConfig]):
255
+ class MySQLManager(
256
+ SQLManager[MySQLConfig, DeclarativeBaseT], Generic[DeclarativeBaseT]
257
+ ):
255
258
  pass
256
259
 
257
260
 
258
- class PostgreSQLManager(SQLManager[PostgreSQLConfig]):
261
+ class PostgreSQLManager(
262
+ SQLManager[PostgreSQLConfig, DeclarativeBaseT], Generic[DeclarativeBaseT]
263
+ ):
259
264
  pass
260
265
 
261
266
 
262
- class SQLiteManager(SQLManager[SQLiteConfig]):
267
+ class SQLiteManager(
268
+ SQLManager[SQLiteConfig, DeclarativeBaseT], Generic[DeclarativeBaseT]
269
+ ):
263
270
  pass
264
271
 
265
272
 
266
- class SQLServerManager(SQLManager[SQLServerConfig]):
273
+ class SQLServerManager(
274
+ SQLManager[SQLServerConfig, DeclarativeBaseT], Generic[DeclarativeBaseT]
275
+ ):
267
276
  pass
268
277
 
269
278
 
@@ -1,9 +1,9 @@
1
1
  from sqlalchemy import asc, cast, desc, or_, select
2
- from sqlalchemy.orm import DeclarativeBase, Session, aliased
2
+ from sqlalchemy.orm import Session, aliased
3
3
  from sqlalchemy.orm.attributes import InstrumentedAttribute
4
4
  from sqlalchemy.sql import Select
5
5
  from sqlalchemy.types import DATE, String, TEXT, TIMESTAMP
6
- from typing import Sequence, Type, TypeVar
6
+ from typing import Any, Sequence, Tuple, Type, TypeVar
7
7
  from maleo.enums.sort import Order
8
8
  from maleo.mixins.general import DateFilter, SortColumn
9
9
  from maleo.types.base.any import OptionalAny
@@ -11,18 +11,21 @@ from maleo.types.base.boolean import OptionalBoolean
11
11
  from maleo.types.base.integer import OptionalListOfIntegers
12
12
  from maleo.types.base.string import OptionalListOfStrings, OptionalString
13
13
  from maleo.types.enums.status import OptionalListOfDataStatuses
14
+ from ..types import DeclarativeBaseT
14
15
 
15
16
 
16
- T = TypeVar("T", bound=DeclarativeBase)
17
+ R = TypeVar(
18
+ "R", bound=Tuple[Any, ...]
19
+ ) # result shape inferred from Select (tuple[...])
17
20
 
18
21
 
19
22
  def filter_column(
20
- stmt: Select,
21
- table: Type[T],
23
+ stmt: Select[R],
24
+ table: Type[DeclarativeBaseT],
22
25
  column: str,
23
26
  value: OptionalAny = None,
24
27
  include_null: bool = False,
25
- ) -> Select:
28
+ ) -> Select[R]:
26
29
  column_attr = getattr(table, column, None)
27
30
  if column_attr is None or not isinstance(column_attr, InstrumentedAttribute):
28
31
  return stmt
@@ -40,12 +43,12 @@ def filter_column(
40
43
 
41
44
 
42
45
  def filter_ids(
43
- stmt: Select,
44
- table: Type[T],
46
+ stmt: Select[R],
47
+ table: Type[DeclarativeBaseT],
45
48
  column: str,
46
49
  ids: OptionalListOfIntegers = None,
47
50
  include_null: bool = False,
48
- ) -> Select:
51
+ ) -> Select[R]:
49
52
  column_attr = getattr(table, column, None)
50
53
  if column_attr is None or not isinstance(column_attr, InstrumentedAttribute):
51
54
  return stmt
@@ -63,10 +66,10 @@ def filter_ids(
63
66
 
64
67
 
65
68
  def filter_timestamps(
66
- stmt: Select,
67
- table: Type[T],
69
+ stmt: Select[R],
70
+ table: Type[DeclarativeBaseT],
68
71
  date_filters: Sequence[DateFilter],
69
- ) -> Select:
72
+ ) -> Select[R]:
70
73
  if date_filters:
71
74
  for date_filter in date_filters:
72
75
  try:
@@ -90,10 +93,10 @@ def filter_timestamps(
90
93
 
91
94
 
92
95
  def filter_statuses(
93
- stmt: Select,
94
- table: Type[T],
96
+ stmt: Select[R],
97
+ table: Type[DeclarativeBaseT],
95
98
  statuses: OptionalListOfDataStatuses,
96
- ) -> Select:
99
+ ) -> Select[R]:
97
100
  if statuses is not None:
98
101
  status_filters = [table.status == status for status in statuses] # type: ignore
99
102
  stmt = stmt.filter(or_(*status_filters))
@@ -101,11 +104,11 @@ def filter_statuses(
101
104
 
102
105
 
103
106
  def filter_is_root(
104
- stmt: Select,
105
- table: Type[T],
107
+ stmt: Select[R],
108
+ table: Type[DeclarativeBaseT],
106
109
  parent_column: str = "parent_id",
107
110
  is_root: OptionalBoolean = None,
108
- ) -> Select:
111
+ ) -> Select[R]:
109
112
  parent_attr = getattr(table, parent_column, None)
110
113
  if parent_attr is None or not isinstance(parent_attr, InstrumentedAttribute):
111
114
  return stmt
@@ -118,12 +121,12 @@ def filter_is_root(
118
121
 
119
122
  def filter_is_parent(
120
123
  session: Session,
121
- stmt: Select,
122
- table: Type[T],
124
+ stmt: Select[R],
125
+ table: Type[DeclarativeBaseT],
123
126
  id_column: str = "id",
124
127
  parent_column: str = "parent_id",
125
128
  is_parent: OptionalBoolean = None,
126
- ) -> Select:
129
+ ) -> Select[R]:
127
130
  id_attr = getattr(table, id_column, None)
128
131
  if id_attr is None or not isinstance(id_attr, InstrumentedAttribute):
129
132
  return stmt
@@ -139,11 +142,11 @@ def filter_is_parent(
139
142
 
140
143
 
141
144
  def filter_is_child(
142
- stmt: Select,
143
- table: Type[T],
145
+ stmt: Select[R],
146
+ table: Type[DeclarativeBaseT],
144
147
  parent_column: str = "parent_id",
145
148
  is_child: OptionalBoolean = None,
146
- ) -> Select:
149
+ ) -> Select[R]:
147
150
  parent_attr = getattr(table, parent_column, None)
148
151
  if parent_attr is None or not isinstance(parent_attr, InstrumentedAttribute):
149
152
  return stmt
@@ -156,12 +159,12 @@ def filter_is_child(
156
159
 
157
160
  def filter_is_leaf(
158
161
  session: Session,
159
- stmt: Select,
160
- table: Type[T],
162
+ stmt: Select[R],
163
+ table: Type[DeclarativeBaseT],
161
164
  id_column: str = "id",
162
165
  parent_column: str = "parent_id",
163
166
  is_leaf: OptionalBoolean = None,
164
- ) -> Select:
167
+ ) -> Select[R]:
165
168
  id_attr = getattr(table, id_column, None)
166
169
  if id_attr is None or not isinstance(id_attr, InstrumentedAttribute):
167
170
  return stmt
@@ -177,11 +180,11 @@ def filter_is_leaf(
177
180
 
178
181
 
179
182
  def search(
180
- stmt: Select,
181
- table: Type[T],
183
+ stmt: Select[R],
184
+ table: Type[DeclarativeBaseT],
182
185
  search: OptionalString = None,
183
186
  columns: OptionalListOfStrings = None,
184
- ) -> Select:
187
+ ) -> Select[R]:
185
188
  if search is None:
186
189
  return stmt
187
190
 
@@ -211,10 +214,10 @@ def search(
211
214
 
212
215
 
213
216
  def sort(
214
- stmt: Select,
215
- table: Type[T],
217
+ stmt: Select[R],
218
+ table: Type[DeclarativeBaseT],
216
219
  sort_columns: Sequence[SortColumn],
217
- ) -> Select:
220
+ ) -> Select[R]:
218
221
  for sort_column in sort_columns:
219
222
  try:
220
223
  sort_col = getattr(table, sort_column.name)
@@ -227,7 +230,7 @@ def sort(
227
230
  return stmt
228
231
 
229
232
 
230
- def paginate(stmt: Select, page: int, limit: int) -> Select:
233
+ def paginate(stmt: Select[R], page: int, limit: int) -> Select[R]:
231
234
  offset: int = int((page - 1) * limit)
232
235
  stmt = stmt.limit(limit).offset(offset)
233
236
  return stmt
@@ -0,0 +1,5 @@
1
+ from sqlalchemy.orm import DeclarativeBase
2
+ from typing import TypeVar
3
+
4
+
5
+ DeclarativeBaseT = TypeVar("DeclarativeBaseT", bound=DeclarativeBase)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: maleo-database
3
- Version: 0.0.21
3
+ Version: 0.0.24
4
4
  Summary: Database package for MaleoSuite
5
5
  Author-email: Agra Bima Yuda <agra@nexmedis.com>
6
6
  License: Proprietary
@@ -1,21 +1,22 @@
1
1
  maleo/database/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  maleo/database/dtos.py,sha256=DArPtSXzPJd896i-t4OmoqBP1mW-it8NVxvfWq7wq8Y,220
3
3
  maleo/database/enums.py,sha256=tHJugOgGWEarwyHbUXmqK-AFXO-D-pmhIdXHkE81ik8,1691
4
- maleo/database/handlers.py,sha256=JsFlxvt9idY3cMXhXu_eBtcKGm9l4nyd8Gzw1WOR9vc,4722
4
+ maleo/database/handlers.py,sha256=A8cY9yyqNhFO0L6kC1P3XdWO4xC9JXZQhK1JQs6AU2Q,4040
5
+ maleo/database/types.py,sha256=FFosx0E46pq7423Fc85P1uA8CRPaJsY0m88KG86oLjY,142
5
6
  maleo/database/config/__init__.py,sha256=ygBWG1iq9tPCKo_mtcWgd5jcJ6gKhHmxXuZOmLuL2Hc,11102
6
7
  maleo/database/config/additional.py,sha256=8uTEQDoKjAlXif-1njKuicIT07H_slTWr9pE0OENAU0,1098
7
8
  maleo/database/config/connection.py,sha256=0Cg0XUxf20Q_RVqFmzOtBUXl9m7I-x9BEyEhZVoWEh4,22869
8
9
  maleo/database/config/identifier.py,sha256=b1MjhoKl3h7xJe1eVIj1wjvYH9BrjHzKnjzdjaEaTeQ,626
9
10
  maleo/database/config/pooling.py,sha256=4Q0rkM4FAt6xAbBG1PItHmMSzS7aCZNBgFPeaEjKwTo,11552
10
- maleo/database/managers/__init__.py,sha256=45un_rhnAeSFQLxmEsEXNsYLfg6kpWWljsXpqDFWZQk,13073
11
+ maleo/database/managers/__init__.py,sha256=9fITbrn9qjmzWp2oe5KmYVa2KZZxf-7INbiXd9ctxic,13333
11
12
  maleo/database/managers/client.py,sha256=hdfPn8Ae8G78yZYDxI1FylcM-rPMIL8Ty4XmsASDrt0,3065
12
13
  maleo/database/managers/engine.py,sha256=aF7JCjSYujodbn2QQrT_OeLX-WjUdIv5cBgWtvLgHI0,1725
13
14
  maleo/database/managers/session.py,sha256=aKyuiHiA171Vptr92AA5U-mRwzwbKXb9RAZ9Wq5Wo4s,19557
14
15
  maleo/database/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
16
  maleo/database/orm/mixins.py,sha256=rW-FVOrRm_R8yJyrcwGoVIPPV1oAOhSVFmANHR7bowU,2372
16
- maleo/database/orm/queries.py,sha256=o_6KHMfQeSp_3sJWusXll-NQBxPm8XUJepnWBahoyv4,7152
17
- maleo_database-0.0.21.dist-info/licenses/LICENSE,sha256=aftGsecnk7TWVX-7KW94FqK4Syy6YSZ8PZEF7EcIp3M,2621
18
- maleo_database-0.0.21.dist-info/METADATA,sha256=YPQk2kQ8dTfnfnnwwvgm7M23GLDgWEnJDpENItdGTt0,3837
19
- maleo_database-0.0.21.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
- maleo_database-0.0.21.dist-info/top_level.txt,sha256=3Tpd1siVsfYoeI9FEOJNYnffx_shzZ3wsPpTvz5bljc,6
21
- maleo_database-0.0.21.dist-info/RECORD,,
17
+ maleo/database/orm/queries.py,sha256=GGt9Ca92IQwQ6YTjmx03xULQ7UPKkFckbHwZCzjdZDY,7456
18
+ maleo_database-0.0.24.dist-info/licenses/LICENSE,sha256=aftGsecnk7TWVX-7KW94FqK4Syy6YSZ8PZEF7EcIp3M,2621
19
+ maleo_database-0.0.24.dist-info/METADATA,sha256=tqqD2BBgJgJSWMuQhREnMtIa4kSyxBiDyDaGwwKiBJQ,3837
20
+ maleo_database-0.0.24.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ maleo_database-0.0.24.dist-info/top_level.txt,sha256=3Tpd1siVsfYoeI9FEOJNYnffx_shzZ3wsPpTvz5bljc,6
22
+ maleo_database-0.0.24.dist-info/RECORD,,