api-foundry 0.8.1__tar.gz → 0.8.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: api-foundry
3
- Version: 0.8.1
3
+ Version: 0.8.3
4
4
  Summary: The `api_foundry` project is a powerful tool designed to automate the deployment of REST APIs on AWS using Lambda services to access and interact with relational databases (RDBMS). This project leverages the OpenAPI specification to define and manage the APIs
5
5
  Project-URL: Documentation, https://github.com/DanRepik/api-foundry
6
6
  Project-URL: Source, https://github.com/DanRepik/api-foundry
@@ -124,6 +124,11 @@ class SchemaObjectProperty(OpenAPIElement):
124
124
  self.key_type = None
125
125
  self.sequence_name = None
126
126
  self.concurrency_control = self._concurrency_control(schema_name, prop)
127
+ # Value injection attributes
128
+ self.inject_value = prop.get("x-af-inject-value", None)
129
+ self.inject_on = self._parse_inject_on(name, prop) or None
130
+ # Soft delete configuration
131
+ self.soft_delete = self._parse_soft_delete(schema_name, name, prop)
127
132
  # For embedded objects/arrays
128
133
  self.sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
129
134
  self.items_sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
@@ -143,6 +148,121 @@ class SchemaObjectProperty(OpenAPIElement):
143
148
  )
144
149
  return concurrency_control
145
150
 
151
+ def _parse_inject_on(self, property_name: str, prop_dict: dict) -> list[str]:
152
+ """
153
+ Parse x-af-inject-on attribute or infer default behavior based on
154
+ property name and x-af-inject-value presence.
155
+ """
156
+ inject_value = prop_dict.get("x-af-inject-value")
157
+ if not inject_value:
158
+ return []
159
+
160
+ # Explicit configuration takes precedence
161
+ if "x-af-inject-on" in prop_dict:
162
+ inject_on = prop_dict["x-af-inject-on"]
163
+ if isinstance(inject_on, list):
164
+ return inject_on
165
+ return [inject_on]
166
+
167
+ # Infer from property name patterns
168
+ # Properties starting with "created" default to create-only
169
+ if property_name.startswith("created_"):
170
+ return ["create"]
171
+
172
+ # Properties starting with "updated" default to update-only
173
+ if property_name.startswith("updated_"):
174
+ return ["update"]
175
+
176
+ # Properties ending with "_by" or "_at" default to create-only
177
+ if property_name.endswith(("_by", "_at")):
178
+ return ["create"]
179
+
180
+ # Tenant/owner fields default to create-only (immutable)
181
+ if property_name in [
182
+ "tenant_id",
183
+ "owner_id",
184
+ "organization_id",
185
+ "workspace_id",
186
+ ]:
187
+ return ["create"]
188
+
189
+ # Default: inject only on create for safety
190
+ return ["create"]
191
+
192
+ def _parse_soft_delete(
193
+ self, schema_name: str, property_name: str, prop_dict: dict
194
+ ) -> Optional[Dict[str, Any]]:
195
+ """
196
+ Parse x-af-soft-delete attribute with validation.
197
+ """
198
+ soft_delete_config = prop_dict.get("x-af-soft-delete")
199
+ if not soft_delete_config:
200
+ return None
201
+
202
+ if not isinstance(soft_delete_config, dict):
203
+ raise ApplicationException(
204
+ 500,
205
+ f"Invalid x-af-soft-delete configuration for property "
206
+ f"'{property_name}' in schema '{schema_name}'. "
207
+ f"Must be an object with strategy and configuration.",
208
+ )
209
+
210
+ strategy = soft_delete_config.get("strategy")
211
+ if not strategy:
212
+ raise ApplicationException(
213
+ 500,
214
+ f"Missing 'strategy' in x-af-soft-delete configuration "
215
+ f"for property '{property_name}' in schema '{schema_name}'.",
216
+ )
217
+
218
+ valid_strategies = [
219
+ "null_check",
220
+ "boolean_flag",
221
+ "exclude_values",
222
+ "audit_field",
223
+ ]
224
+ if strategy not in valid_strategies:
225
+ raise ApplicationException(
226
+ 500,
227
+ f"Invalid soft delete strategy '{strategy}' for property "
228
+ f"'{property_name}' in schema '{schema_name}'. "
229
+ f"Valid strategies are: {', '.join(valid_strategies)}",
230
+ )
231
+
232
+ # Validate strategy-specific configuration
233
+ if strategy == "boolean_flag":
234
+ active_value = soft_delete_config.get("active_value", True)
235
+ if not isinstance(active_value, bool):
236
+ raise ApplicationException(
237
+ 500,
238
+ f"Property '{property_name}' in schema '{schema_name}' "
239
+ f"uses boolean_flag strategy but active_value is not "
240
+ f"boolean.",
241
+ )
242
+
243
+ elif strategy == "exclude_values":
244
+ values = soft_delete_config.get("values", [])
245
+ if not isinstance(values, list) or not values:
246
+ raise ApplicationException(
247
+ 500,
248
+ f"Property '{property_name}' in schema '{schema_name}' "
249
+ f"uses exclude_values strategy but 'values' is not a "
250
+ f"non-empty list.",
251
+ )
252
+
253
+ elif strategy == "audit_field":
254
+ action = soft_delete_config.get("action")
255
+ valid_actions = ["delete", "restore", "restore_timestamp"]
256
+ if action not in valid_actions:
257
+ raise ApplicationException(
258
+ 500,
259
+ f"Property '{property_name}' in schema '{schema_name}' "
260
+ f"uses audit_field strategy but action '{action}' is not "
261
+ f"valid. Valid actions are: {', '.join(valid_actions)}",
262
+ )
263
+
264
+ return soft_delete_config
265
+
146
266
 
147
267
  class SchemaObjectKey(SchemaObjectProperty):
148
268
  """Represents a primary key in a schema object."""
@@ -206,6 +326,7 @@ class SchemaObject(OpenAPIElement):
206
326
  self.relations = self._resolve_relations(schema_object)
207
327
  self.concurrency_property = self._get_concurrency_property(schema_object)
208
328
  self.permissions = self._get_permissions(schema_object)
329
+ self.inject_properties = self._get_inject_properties()
209
330
 
210
331
  def _get_table_name(self, schema_object: dict) -> str:
211
332
  schema = schema_object.get("x-af-schema")
@@ -380,6 +501,21 @@ class SchemaObject(OpenAPIElement):
380
501
  validate_permissions(normalized)
381
502
  return normalized or {}
382
503
 
504
+ def _get_inject_properties(self) -> dict:
505
+ """
506
+ Collect all properties that have injection attributes.
507
+
508
+ Returns a dictionary mapping property names to their injection metadata.
509
+ """
510
+ inject_props = {}
511
+ for prop_name, prop in self.properties.items():
512
+ if hasattr(prop, "inject_value") and prop.inject_value:
513
+ inject_props[prop_name] = {
514
+ "inject_value": prop.inject_value,
515
+ "inject_on": getattr(prop, "inject_on", ["create"]),
516
+ }
517
+ return inject_props
518
+
383
519
  def to_dict(self) -> Dict[str, Any]:
384
520
  """Recursively convert this schema object and its properties."""
385
521
  data = super().to_dict()
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "api-foundry"
7
- version = "0.8.1"
7
+ version = "0.8.3"
8
8
  authors = [
9
9
  { name="Daniel Repik", email="danrepik@icloud.com" },
10
10
  ]
File without changes
File without changes