database-wrapper 0.1.32__py3-none-any.whl → 0.1.37__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.
@@ -9,7 +9,7 @@ import logging
9
9
  from . import utils
10
10
  from .db_backend import DatabaseBackend
11
11
  from .db_data_model import DBDataModel, DBDefaultsDataModel
12
- from .db_wrapper_interface import DBWrapperInterface, OrderByItem, NoParam, T
12
+ from .common import OrderByItem, DataModelType, NoParam
13
13
  from .db_wrapper import DBWrapper
14
14
  from .db_wrapper_async import DBWrapperAsync
15
15
 
@@ -27,12 +27,11 @@ __all__ = [
27
27
  "DBDataModel",
28
28
  "DBDefaultsDataModel",
29
29
  # Wrappers
30
- "DBWrapperInterface",
31
30
  "DBWrapper",
32
31
  "DBWrapperAsync",
33
32
  # Helpers
33
+ "DataModelType",
34
34
  "OrderByItem",
35
35
  "NoParam",
36
- "T",
37
36
  "utils",
38
37
  ]
@@ -0,0 +1,14 @@
1
+ from typing import TypeVar
2
+
3
+ from .db_data_model import DBDataModel
4
+
5
+ # Bound T to DBDataModel
6
+ DataModelType = TypeVar("DataModelType", bound=DBDataModel)
7
+
8
+ # OrderByItem type
9
+ OrderByItem = list[tuple[str, str | None]]
10
+
11
+
12
+ # NoParam class
13
+ class NoParam:
14
+ pass
@@ -3,7 +3,7 @@ from typing import Any
3
3
  CONFIG: dict[str, Any] = {
4
4
  # These are supposed to be set automatically by a git pre-compile script
5
5
  # They are one git commit hash behind, if used automatically
6
- "git_commit_hash": "62e32a790347feeaf12bdebdf1ac89888a8799c3",
7
- "git_commit_date": "08.11.2024 16:23",
8
- "app_version": "0.1.32",
6
+ "git_commit_hash": "6e44a2436719d47a75251397bc59a7abdc77bc86",
7
+ "git_commit_date": "11.11.2024 22:12",
8
+ "app_version": "0.1.37",
9
9
  }
@@ -13,7 +13,7 @@ from psycopg import sql
13
13
 
14
14
 
15
15
  @dataclass
16
- class DBDataModel(object):
16
+ class DBDataModel:
17
17
  """
18
18
  Base class for all database models.
19
19
 
@@ -38,6 +38,14 @@ class DBDataModel(object):
38
38
  - strToBool(value: Any): Converts a string to a boolean value.
39
39
  - strToInt(value: Any): Converts a string to an integer value.
40
40
  - validate(): Validates the instance.
41
+
42
+ To enable storing and updating fields that by default are not stored or updated, use the following methods:
43
+ - setStore(field_name: str, enable: bool = True): Enable/Disable storing a field.
44
+ - setUpdate(field_name: str, enable: bool = True): Enable/Disable updating a field.
45
+
46
+ To exclude a field from the dictionary representation of the instance, set metadata key "exclude" to True.
47
+ To change exclude status of a field, use the following method:
48
+ - setExclude(field_name: str, enable: bool = True): Exclude a field from dict representation.
41
49
  """
42
50
 
43
51
  ######################
@@ -62,7 +70,7 @@ class DBDataModel(object):
62
70
 
63
71
  @property
64
72
  def idValue(self) -> Any:
65
- return getattr(self, self.idKey, None)
73
+ return getattr(self, self.idKey)
66
74
 
67
75
  # Id should be readonly by default and should be always present if record exists
68
76
  id: int = field(
@@ -80,6 +88,7 @@ class DBDataModel(object):
80
88
  default_factory=dict,
81
89
  metadata={
82
90
  "db_field": ("raw_data", "jsonb"),
91
+ "exclude": True,
83
92
  "store": False,
84
93
  "update": False,
85
94
  },
@@ -114,8 +123,19 @@ class DBDataModel(object):
114
123
  return self.toJsonString()
115
124
 
116
125
  # Dict
126
+ def dictFilter(self, pairs: list[tuple[str, Any]]) -> dict[str, Any]:
127
+ newDict: dict[str, Any] = {}
128
+ for field in pairs:
129
+ classField = self.__dataclass_fields__.get(field[0], None)
130
+ if classField is not None:
131
+ metadata = classField.metadata
132
+ if not "exclude" in metadata or not metadata["exclude"]:
133
+ newDict[field[0]] = field[1]
134
+
135
+ return newDict
136
+
117
137
  def toDict(self) -> dict[str, Any]:
118
- return asdict(self)
138
+ return asdict(self, dict_factory=self.dictFilter)
119
139
 
120
140
  def toFormattedDict(self) -> dict[str, Any]:
121
141
  return self.toDict()
@@ -213,6 +233,33 @@ class DBDataModel(object):
213
233
  def validate(self) -> bool:
214
234
  raise NotImplementedError("`validate` is not implemented")
215
235
 
236
+ def setStore(self, field_name: str, enable: bool = True) -> None:
237
+ """
238
+ Enable/Disable storing a field (insert into database)
239
+ """
240
+ if field_name in self.__dataclass_fields__:
241
+ currentMetadata = self.__dataclass_fields__[field_name].metadata
242
+ currentMetadata["store"] = enable
243
+ self.__dataclass_fields__[field_name].metadata = currentMetadata
244
+
245
+ def setUpdate(self, field_name: str, enable: bool = True) -> None:
246
+ """
247
+ Enable/Disable updating a field (update in database)
248
+ """
249
+ if field_name in self.__dataclass_fields__:
250
+ currentMetadata = self.__dataclass_fields__[field_name].metadata
251
+ currentMetadata["update"] = enable
252
+ self.__dataclass_fields__[field_name].metadata = currentMetadata
253
+
254
+ def setExclude(self, field_name: str, enable: bool = True) -> None:
255
+ """
256
+ Exclude a field from dict representation
257
+ """
258
+ if field_name in self.__dataclass_fields__:
259
+ currentMetadata = dict(self.__dataclass_fields__[field_name].metadata)
260
+ currentMetadata["exclude"] = enable
261
+ self.__dataclass_fields__[field_name].metadata = currentMetadata
262
+
216
263
  ########################
217
264
  ### Database methods ###
218
265
  ########################