renus 3.2.0__tar.gz → 3.2.2__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.
Files changed (187) hide show
  1. {renus-3.2.0/renus.egg-info → renus-3.2.2}/PKG-INFO +1 -1
  2. {renus-3.2.0 → renus-3.2.2}/renus/__init__.py +1 -1
  3. {renus-3.2.0 → renus-3.2.2}/renus/commands/app/temp.py +2 -5
  4. {renus-3.2.0 → renus-3.2.2}/renus/datastructures/id.py +8 -4
  5. {renus-3.2.0 → renus-3.2.2}/renus/db/models/base.py +57 -39
  6. {renus-3.2.0 → renus-3.2.2}/renus/db/mongodb/compiler.py +46 -6
  7. {renus-3.2.0 → renus-3.2.2}/renus/db/mongodb/dialect.py +1 -1
  8. {renus-3.2.0 → renus-3.2.2}/renus/db/mysql/adapter.py +2 -1
  9. {renus-3.2.0 → renus-3.2.2}/renus/db/mysql/dialect.py +1 -1
  10. {renus-3.2.0 → renus-3.2.2}/renus/db/sql/__init__.py +2 -2
  11. {renus-3.2.0 → renus-3.2.2}/renus/db/sql/adapter.py +24 -3
  12. {renus-3.2.0/renus/db/sql/compiler → renus-3.2.2/renus/db/sql}/compiler.py +82 -12
  13. renus-3.2.0/renus/db/sql/dialect/base.py → renus-3.2.2/renus/db/sql/dialect.py +8 -3
  14. {renus-3.2.0 → renus-3.2.2}/renus/db/sql/mutation.py +2 -2
  15. {renus-3.2.0 → renus-3.2.2}/renus/db/sqlite/adapter.py +1 -1
  16. {renus-3.2.0 → renus-3.2.2}/renus/db/sqlite/dialect.py +1 -1
  17. {renus-3.2.0 → renus-3.2.2}/renus/schedule/schedule.py +14 -18
  18. {renus-3.2.0 → renus-3.2.2}/renus/schedule/state.py +1 -4
  19. {renus-3.2.0 → renus-3.2.2}/renus/util/serialization.py +6 -5
  20. {renus-3.2.0 → renus-3.2.2/renus.egg-info}/PKG-INFO +1 -1
  21. {renus-3.2.0 → renus-3.2.2}/renus.egg-info/SOURCES.txt +2 -4
  22. {renus-3.2.0 → renus-3.2.2}/tests/test_schedule.py +29 -4
  23. renus-3.2.0/renus/db/sql/compiler/__init__.py +0 -5
  24. renus-3.2.0/renus/db/sql/dialect/__init__.py +0 -1
  25. {renus-3.2.0 → renus-3.2.2}/LICENSE +0 -0
  26. {renus-3.2.0 → renus-3.2.2}/MANIFEST.in +0 -0
  27. {renus-3.2.0 → renus-3.2.2}/README.md +0 -0
  28. {renus-3.2.0 → renus-3.2.2}/pyproject.toml +0 -0
  29. {renus-3.2.0 → renus-3.2.2}/pytest.ini +0 -0
  30. {renus-3.2.0 → renus-3.2.2}/renus/__main__.py +0 -0
  31. {renus-3.2.0 → renus-3.2.2}/renus/_speedup.c +0 -0
  32. {renus-3.2.0 → renus-3.2.2}/renus/_speedup.pyx +0 -0
  33. {renus-3.2.0 → renus-3.2.2}/renus/app/__init__.py +0 -0
  34. {renus-3.2.0 → renus-3.2.2}/renus/app/app.py +0 -0
  35. {renus-3.2.0 → renus-3.2.2}/renus/app/dispatch.py +0 -0
  36. {renus-3.2.0 → renus-3.2.2}/renus/app/handlers.py +0 -0
  37. {renus-3.2.0 → renus-3.2.2}/renus/app/lifespan.py +0 -0
  38. {renus-3.2.0 → renus-3.2.2}/renus/cache/__init__.py +0 -0
  39. {renus-3.2.0 → renus-3.2.2}/renus/cache/base.py +0 -0
  40. {renus-3.2.0 → renus-3.2.2}/renus/cache/cache.py +0 -0
  41. {renus-3.2.0 → renus-3.2.2}/renus/cache/file.py +0 -0
  42. {renus-3.2.0 → renus-3.2.2}/renus/cache/redis.py +0 -0
  43. {renus-3.2.0 → renus-3.2.2}/renus/cache/signing.py +0 -0
  44. {renus-3.2.0 → renus-3.2.2}/renus/cache/utils.py +0 -0
  45. {renus-3.2.0 → renus-3.2.2}/renus/commands/__init__.py +0 -0
  46. {renus-3.2.0 → renus-3.2.2}/renus/commands/app/__init__.py +0 -0
  47. {renus-3.2.0 → renus-3.2.2}/renus/commands/app/run.py +0 -0
  48. {renus-3.2.0 → renus-3.2.2}/renus/commands/base.py +0 -0
  49. {renus-3.2.0 → renus-3.2.2}/renus/commands/cli.py +0 -0
  50. {renus-3.2.0 → renus-3.2.2}/renus/commands/create/__init__.py +0 -0
  51. {renus-3.2.0 → renus-3.2.2}/renus/commands/create/local_run_template.py +0 -0
  52. {renus-3.2.0 → renus-3.2.2}/renus/commands/create/run.py +0 -0
  53. {renus-3.2.0 → renus-3.2.2}/renus/commands/db/__init__.py +0 -0
  54. {renus-3.2.0 → renus-3.2.2}/renus/commands/db/run.py +0 -0
  55. {renus-3.2.0 → renus-3.2.2}/renus/commands/extension/__init__.py +0 -0
  56. {renus-3.2.0 → renus-3.2.2}/renus/commands/extension/build.py +0 -0
  57. {renus-3.2.0 → renus-3.2.2}/renus/commands/extension/remove.py +0 -0
  58. {renus-3.2.0 → renus-3.2.2}/renus/commands/extension/run.py +0 -0
  59. {renus-3.2.0 → renus-3.2.2}/renus/commands/help.py +0 -0
  60. {renus-3.2.0 → renus-3.2.2}/renus/commands/routing.py +0 -0
  61. {renus-3.2.0 → renus-3.2.2}/renus/commands/run/__init__.py +0 -0
  62. {renus-3.2.0 → renus-3.2.2}/renus/commands/run/run.py +0 -0
  63. {renus-3.2.0 → renus-3.2.2}/renus/config/__init__.py +0 -0
  64. {renus-3.2.0 → renus-3.2.2}/renus/config/config.py +0 -0
  65. {renus-3.2.0 → renus-3.2.2}/renus/datastructures/__init__.py +0 -0
  66. {renus-3.2.0 → renus-3.2.2}/renus/datastructures/background.py +0 -0
  67. {renus-3.2.0 → renus-3.2.2}/renus/datastructures/multidict.py +0 -0
  68. {renus-3.2.0 → renus-3.2.2}/renus/datastructures/uploads.py +0 -0
  69. {renus-3.2.0 → renus-3.2.2}/renus/db/__init__.py +0 -0
  70. {renus-3.2.0 → renus-3.2.2}/renus/db/core/__init__.py +0 -0
  71. {renus-3.2.0 → renus-3.2.2}/renus/db/core/adapter.py +0 -0
  72. {renus-3.2.0 → renus-3.2.2}/renus/db/core/backup.py +0 -0
  73. {renus-3.2.0 → renus-3.2.2}/renus/db/core/convert.py +0 -0
  74. {renus-3.2.0 → renus-3.2.2}/renus/db/core/events.py +0 -0
  75. {renus-3.2.0 → renus-3.2.2}/renus/db/core/exceptions.py +0 -0
  76. {renus-3.2.0 → renus-3.2.2}/renus/db/core/state.py +0 -0
  77. {renus-3.2.0 → renus-3.2.2}/renus/db/database.py +0 -0
  78. {renus-3.2.0 → renus-3.2.2}/renus/db/errors.py +0 -0
  79. {renus-3.2.0 → renus-3.2.2}/renus/db/metadata/__init__.py +0 -0
  80. {renus-3.2.0 → renus-3.2.2}/renus/db/metadata/core.py +0 -0
  81. {renus-3.2.0 → renus-3.2.2}/renus/db/metadata/inference.py +0 -0
  82. {renus-3.2.0 → renus-3.2.2}/renus/db/migrations/__init__.py +0 -0
  83. {renus-3.2.0 → renus-3.2.2}/renus/db/migrations/exceptions.py +0 -0
  84. {renus-3.2.0 → renus-3.2.2}/renus/db/migrations/manager.py +0 -0
  85. {renus-3.2.0 → renus-3.2.2}/renus/db/migrations/operations.py +0 -0
  86. {renus-3.2.0 → renus-3.2.2}/renus/db/migrations/tracker.py +0 -0
  87. {renus-3.2.0 → renus-3.2.2}/renus/db/models/__init__.py +0 -0
  88. {renus-3.2.0 → renus-3.2.2}/renus/db/models/db_field.py +0 -0
  89. {renus-3.2.0 → renus-3.2.2}/renus/db/models/field.py +0 -0
  90. {renus-3.2.0 → renus-3.2.2}/renus/db/mongodb/__init__.py +0 -0
  91. {renus-3.2.0 → renus-3.2.2}/renus/db/mongodb/adapter.py +0 -0
  92. {renus-3.2.0 → renus-3.2.2}/renus/db/mutation/__init__.py +0 -0
  93. {renus-3.2.0 → renus-3.2.2}/renus/db/mutation/ast.py +0 -0
  94. {renus-3.2.0 → renus-3.2.2}/renus/db/mysql/__init__.py +0 -0
  95. {renus-3.2.0 → renus-3.2.2}/renus/db/plugins/__init__.py +0 -0
  96. {renus-3.2.0 → renus-3.2.2}/renus/db/plugins/core.py +0 -0
  97. {renus-3.2.0 → renus-3.2.2}/renus/db/plugins/registry.py +0 -0
  98. {renus-3.2.0 → renus-3.2.2}/renus/db/query/__init__.py +0 -0
  99. {renus-3.2.0 → renus-3.2.2}/renus/db/query/ast.py +0 -0
  100. {renus-3.2.0 → renus-3.2.2}/renus/db/query/builder.py +0 -0
  101. {renus-3.2.0 → renus-3.2.2}/renus/db/relationships.py +0 -0
  102. {renus-3.2.0 → renus-3.2.2}/renus/db/repository/__init__.py +0 -0
  103. {renus-3.2.0 → renus-3.2.2}/renus/db/repository/core.py +0 -0
  104. {renus-3.2.0 → renus-3.2.2}/renus/db/repository/normalize.py +0 -0
  105. {renus-3.2.0 → renus-3.2.2}/renus/db/repository/relationships.py +0 -0
  106. {renus-3.2.0 → renus-3.2.2}/renus/db/serialization/__init__.py +0 -0
  107. {renus-3.2.0 → renus-3.2.2}/renus/db/serialization/core.py +0 -0
  108. {renus-3.2.0 → renus-3.2.2}/renus/db/sqlite/__init__.py +0 -0
  109. {renus-3.2.0 → renus-3.2.2}/renus/db/transaction/__init__.py +0 -0
  110. {renus-3.2.0 → renus-3.2.2}/renus/db/transaction/core.py +0 -0
  111. {renus-3.2.0 → renus-3.2.2}/renus/db/validation/__init__.py +0 -0
  112. {renus-3.2.0 → renus-3.2.2}/renus/db/validation/base.py +0 -0
  113. {renus-3.2.0 → renus-3.2.2}/renus/db/validation/engine.py +0 -0
  114. {renus-3.2.0 → renus-3.2.2}/renus/db/validation/rules.py +0 -0
  115. {renus-3.2.0 → renus-3.2.2}/renus/log/__init__.py +0 -0
  116. {renus-3.2.0 → renus-3.2.2}/renus/log/formatters.py +0 -0
  117. {renus-3.2.0 → renus-3.2.2}/renus/log/handlers.py +0 -0
  118. {renus-3.2.0 → renus-3.2.2}/renus/log/logger.py +0 -0
  119. {renus-3.2.0 → renus-3.2.2}/renus/request/__init__.py +0 -0
  120. {renus-3.2.0 → renus-3.2.2}/renus/request/exceptions.py +0 -0
  121. {renus-3.2.0 → renus-3.2.2}/renus/request/formparsers.py +0 -0
  122. {renus-3.2.0 → renus-3.2.2}/renus/request/injection.py +0 -0
  123. {renus-3.2.0 → renus-3.2.2}/renus/request/limits.py +0 -0
  124. {renus-3.2.0 → renus-3.2.2}/renus/request/options.py +0 -0
  125. {renus-3.2.0 → renus-3.2.2}/renus/request/parsers.py +0 -0
  126. {renus-3.2.0 → renus-3.2.2}/renus/request/request.py +0 -0
  127. {renus-3.2.0 → renus-3.2.2}/renus/request/xss.py +0 -0
  128. {renus-3.2.0 → renus-3.2.2}/renus/response/__init__.py +0 -0
  129. {renus-3.2.0 → renus-3.2.2}/renus/response/base.py +0 -0
  130. {renus-3.2.0 → renus-3.2.2}/renus/response/exceptions.py +0 -0
  131. {renus-3.2.0 → renus-3.2.2}/renus/response/file.py +0 -0
  132. {renus-3.2.0 → renus-3.2.2}/renus/response/gzip.py +0 -0
  133. {renus-3.2.0 → renus-3.2.2}/renus/response/json.py +0 -0
  134. {renus-3.2.0 → renus-3.2.2}/renus/response/private.py +0 -0
  135. {renus-3.2.0 → renus-3.2.2}/renus/response/redirects.py +0 -0
  136. {renus-3.2.0 → renus-3.2.2}/renus/response/status.py +0 -0
  137. {renus-3.2.0 → renus-3.2.2}/renus/response/streaming.py +0 -0
  138. {renus-3.2.0 → renus-3.2.2}/renus/response/text.py +0 -0
  139. {renus-3.2.0 → renus-3.2.2}/renus/rich/__init__.py +0 -0
  140. {renus-3.2.0 → renus-3.2.2}/renus/rich/_style.py +0 -0
  141. {renus-3.2.0 → renus-3.2.2}/renus/rich/header.py +0 -0
  142. {renus-3.2.0 → renus-3.2.2}/renus/rich/markdown.py +0 -0
  143. {renus-3.2.0 → renus-3.2.2}/renus/rich/progress.py +0 -0
  144. {renus-3.2.0 → renus-3.2.2}/renus/rich/style_context.py +0 -0
  145. {renus-3.2.0 → renus-3.2.2}/renus/rich/table.py +0 -0
  146. {renus-3.2.0 → renus-3.2.2}/renus/rich/terminal.py +0 -0
  147. {renus-3.2.0 → renus-3.2.2}/renus/router/__init__.py +0 -0
  148. {renus-3.2.0 → renus-3.2.2}/renus/router/chain.py +0 -0
  149. {renus-3.2.0 → renus-3.2.2}/renus/router/convertors.py +0 -0
  150. {renus-3.2.0 → renus-3.2.2}/renus/router/middleware.py +0 -0
  151. {renus-3.2.0 → renus-3.2.2}/renus/router/path.py +0 -0
  152. {renus-3.2.0 → renus-3.2.2}/renus/router/registry.py +0 -0
  153. {renus-3.2.0 → renus-3.2.2}/renus/router/router.py +0 -0
  154. {renus-3.2.0 → renus-3.2.2}/renus/schedule/__init__.py +0 -0
  155. {renus-3.2.0 → renus-3.2.2}/renus/schedule/matcher.py +0 -0
  156. {renus-3.2.0 → renus-3.2.2}/renus/schedule/runner.py +0 -0
  157. {renus-3.2.0 → renus-3.2.2}/renus/util/__init__.py +0 -0
  158. {renus-3.2.0 → renus-3.2.2}/renus/util/codec.py +0 -0
  159. {renus-3.2.0 → renus-3.2.2}/renus/util/concurrency.py +0 -0
  160. {renus-3.2.0 → renus-3.2.2}/renus/util/naming.py +0 -0
  161. {renus-3.2.0 → renus-3.2.2}/renus/util/random.py +0 -0
  162. {renus-3.2.0 → renus-3.2.2}/renus/util/security.py +0 -0
  163. {renus-3.2.0 → renus-3.2.2}/renus/util/version.py +0 -0
  164. {renus-3.2.0 → renus-3.2.2}/renus/websockets/__init__.py +0 -0
  165. {renus-3.2.0 → renus-3.2.2}/renus/websockets/messages.py +0 -0
  166. {renus-3.2.0 → renus-3.2.2}/renus/websockets/protocol.py +0 -0
  167. {renus-3.2.0 → renus-3.2.2}/renus/websockets/session.py +0 -0
  168. {renus-3.2.0 → renus-3.2.2}/renus/websockets/state.py +0 -0
  169. {renus-3.2.0 → renus-3.2.2}/renus.egg-info/dependency_links.txt +0 -0
  170. {renus-3.2.0 → renus-3.2.2}/renus.egg-info/entry_points.txt +0 -0
  171. {renus-3.2.0 → renus-3.2.2}/renus.egg-info/requires.txt +0 -0
  172. {renus-3.2.0 → renus-3.2.2}/renus.egg-info/top_level.txt +0 -0
  173. {renus-3.2.0 → renus-3.2.2}/setup.cfg +0 -0
  174. {renus-3.2.0 → renus-3.2.2}/setup.py +0 -0
  175. {renus-3.2.0 → renus-3.2.2}/tests/test_app.py +0 -0
  176. {renus-3.2.0 → renus-3.2.2}/tests/test_cache.py +0 -0
  177. {renus-3.2.0 → renus-3.2.2}/tests/test_commands.py +0 -0
  178. {renus-3.2.0 → renus-3.2.2}/tests/test_concurrency.py +0 -0
  179. {renus-3.2.0 → renus-3.2.2}/tests/test_config.py +0 -0
  180. {renus-3.2.0 → renus-3.2.2}/tests/test_datastructures.py +0 -0
  181. {renus-3.2.0 → renus-3.2.2}/tests/test_log.py +0 -0
  182. {renus-3.2.0 → renus-3.2.2}/tests/test_request.py +0 -0
  183. {renus-3.2.0 → renus-3.2.2}/tests/test_response.py +0 -0
  184. {renus-3.2.0 → renus-3.2.2}/tests/test_rich.py +0 -0
  185. {renus-3.2.0 → renus-3.2.2}/tests/test_routing.py +0 -0
  186. {renus-3.2.0 → renus-3.2.2}/tests/test_util.py +0 -0
  187. {renus-3.2.0 → renus-3.2.2}/tests/test_websockets.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: renus
3
- Version: 3.2.0
3
+ Version: 3.2.2
4
4
  Summary: Renus Core Framework - a lightweight ASGI web framework and toolkit.
5
5
  Author-email: Smko Bayazidi <ba.smko@gmail.com>
6
6
  License-Expression: BSD-3-Clause
@@ -1,3 +1,3 @@
1
1
  """Renus v3 package."""
2
2
 
3
- __version__ = "3.2.0"
3
+ __version__ = "3.2.2"
@@ -43,9 +43,6 @@ from renus.db.validation import MaxLen
43
43
  from renus.datastructures import ID
44
44
 
45
45
  class {name_camel}(Model):
46
- """Domain model for {name}.
47
- """
48
-
49
46
  id: ID = Field(default_factory=ID,database=DatabaseField(primary_key=True,generated=True))
50
47
 
51
48
  # --- Example fields; customize for your domain. ---
@@ -92,8 +89,8 @@ admin_router.put("/{names}/{id:id}", {name_camel}Controller, "update", middlewar
92
89
  admin_router.delete("/{names}/{id:id}", {name_camel}Controller, "delete", middlewares=[PermissionMiddleware("{names}.delete")])
93
90
  {soft_delete_kit_routes}
94
91
 
95
- user_router = Router(prefix="/user", middlewares=[AuthMiddleware])
96
- home_router = Router(prefix="/home")
92
+ user_router = Router(prefix="/api/user", middlewares=[AuthMiddleware])
93
+ home_router = Router(prefix="/api/home")
97
94
  router = Router()
98
95
  router.include(admin_router)
99
96
  router.include(user_router)
@@ -7,11 +7,13 @@
7
7
 
8
8
  The concrete identity object is read from ``Config("database")`` under ``ID``
9
9
  and may be ``int``, ``bson.ObjectId``, ``uuid.UUID``, ``str``, or any callable
10
- that mints a unique value. An ``ID`` instance wraps the resolved value, so the
11
- model stores a small value object whose string form is the identity itself.
10
+ that mints a unique value. An ``ID`` instance wraps the resolved value, while
11
+ its tagged string form distinguishes serialized IDs from ordinary text.
12
12
  Encoding and decoding are performed by the adapters: a document store keeps the
13
13
  identity in its native form (a MongoDB ``ObjectId`` stays an ``ObjectId``),
14
- while a relational store keeps it as text.
14
+ and a relational store keeps the configured value in its bindable form. Its
15
+ string representation is tagged to distinguish serialized IDs from ordinary
16
+ text values.
15
17
 
16
18
  An integer identity is never minted here. It is allocated by the backend, either
17
19
  through native auto increment or, for document stores without one, through a
@@ -80,12 +82,14 @@ class ID:
80
82
  def _coerce(self, value: Any) -> Any:
81
83
  """Coerce ``value`` into the configured identity type."""
82
84
  obj = self.obj
85
+ if isinstance(value, str) and value.startswith("id:"):
86
+ value = value[3:]
83
87
  if isinstance(value, obj):
84
88
  return value
85
89
  return obj(value)
86
90
 
87
91
  def __str__(self) -> str:
88
- return str(self.value)
92
+ return f"id:{self.value}"
89
93
 
90
94
  def __repr__(self) -> str:
91
95
  return f"ID({self.value!r})"
@@ -4,6 +4,7 @@ import copy
4
4
  from datetime import date, datetime, timezone
5
5
  from typing import Any, ClassVar, dataclass_transform, get_origin
6
6
 
7
+ from renus.datastructures.id import ID
7
8
  from renus.db.core.exceptions import ValidationError
8
9
  from renus.db.core.state import IdentityKind, Lifecycle
9
10
  from renus.db.metadata.core import (
@@ -15,15 +16,14 @@ from renus.db.metadata.core import (
15
16
  )
16
17
  from renus.db.metadata.inference import analyze_field_type, infer_identity_kind
17
18
  from renus.db.models.db_field import UNSET, DatabaseField
18
- from renus.db.models.field import Field, FieldInfo, _UNSET
19
- from renus.db.validation.base import Rule
20
- from renus.datastructures.id import ID
19
+ from renus.db.models.field import _UNSET, Field, FieldInfo
21
20
  from renus.db.query.ast import Path
22
21
  from renus.db.relationships import Relationship
23
-
22
+ from renus.db.validation.base import Rule
24
23
 
25
24
  _MODEL_REGISTRY: dict[str, type] = {}
26
25
 
26
+
27
27
  class ModelMeta(type):
28
28
  """Metaclass that builds model metadata and field definitions."""
29
29
 
@@ -54,12 +54,14 @@ class ModelMeta(type):
54
54
  ns[key] = Field(default=default_val, database=val)
55
55
  elif isinstance(val, Field):
56
56
  extra = getattr(val, "extra", {}) or {}
57
- db = getattr(val, "database", None) or extra.get("database") or extra.get("db")
57
+ db = (
58
+ getattr(val, "database", None)
59
+ or extra.get("database")
60
+ or extra.get("db")
61
+ )
58
62
  if isinstance(db, DatabaseField):
59
63
  declared_db_fields[key] = db
60
64
 
61
-
62
-
63
65
  meta_cls = ns.get("Meta")
64
66
  timestamps = bool(getattr(meta_cls, "timestamps", False))
65
67
  soft_delete = bool(getattr(meta_cls, "soft_delete", False))
@@ -78,7 +80,9 @@ class ModelMeta(type):
78
80
  if timestamps:
79
81
  if "created_at" not in annotations:
80
82
  annotations["created_at"] = datetime | None
81
- ns["created_at"] = Field(default=None)
83
+ ns["created_at"] = Field(
84
+ default=None, database=DatabaseField(indexed=True)
85
+ )
82
86
  if "updated_at" not in annotations:
83
87
  annotations["updated_at"] = datetime | None
84
88
  ns["updated_at"] = Field(default=None)
@@ -89,7 +93,9 @@ class ModelMeta(type):
89
93
  ns["deleted_at"] = Field(default=None)
90
94
  if "is_deleted" not in annotations:
91
95
  annotations["is_deleted"] = bool
92
- ns["is_deleted"] = Field(default=False)
96
+ ns["is_deleted"] = Field(
97
+ default=False, database=DatabaseField(indexed=True)
98
+ )
93
99
 
94
100
  ns["__annotations__"] = annotations
95
101
 
@@ -100,7 +106,6 @@ class ModelMeta(type):
100
106
 
101
107
  table = getattr(meta_cls, "table", None)
102
108
  if not table:
103
-
104
109
  table = f"{name.lower()}s"
105
110
 
106
111
  all_relationships: dict[str, Relationship] = {}
@@ -110,7 +115,6 @@ class ModelMeta(type):
110
115
  all_relationships[k] = v
111
116
  all_relationships.update(declared_relationships)
112
117
 
113
-
114
118
  validation_fields: dict[str, FieldInfo] = getattr(cls, "_model_fields", {})
115
119
  for rname, rel in all_relationships.items():
116
120
  rel.__set_name__(cls, rname)
@@ -176,9 +180,10 @@ class ModelMeta(type):
176
180
  )
177
181
  col_name = db.column_name or fname
178
182
 
179
- pk = db.primary_key or (fname in ("id", "_id") and not any(
180
- d.primary_key for d in declared_db_fields.values()
181
- ))
183
+ pk = db.primary_key or (
184
+ fname in ("id", "_id")
185
+ and not any(d.primary_key for d in declared_db_fields.values())
186
+ )
182
187
 
183
188
  if db.auto_inc and not pk:
184
189
  raise ValueError(
@@ -278,7 +283,6 @@ class ModelMeta(type):
278
283
  generated=pk_meta.generated,
279
284
  )
280
285
  else:
281
-
282
286
  if "_id" in fields_dict:
283
287
  pk_meta = fields_dict["_id"]
284
288
  object.__setattr__(pk_meta, "primary_key", True)
@@ -299,7 +303,6 @@ class ModelMeta(type):
299
303
  generated=pk_meta.generated,
300
304
  )
301
305
  else:
302
-
303
306
  default_db = DatabaseField(primary_key=True, generated=True)
304
307
  id_meta = FieldMetadata(
305
308
  name="id",
@@ -345,9 +348,13 @@ class ModelMeta(type):
345
348
 
346
349
  for fname, fmeta in fields_dict.items():
347
350
  if fmeta.unique:
348
- indexes.append(Index(fmeta.column_name, unique=True, name=fmeta.index_name))
351
+ indexes.append(
352
+ Index(fmeta.column_name, unique=True, name=fmeta.index_name)
353
+ )
349
354
  elif fmeta.indexed:
350
- indexes.append(Index(fmeta.column_name, unique=False, name=fmeta.index_name))
355
+ indexes.append(
356
+ Index(fmeta.column_name, unique=False, name=fmeta.index_name)
357
+ )
351
358
 
352
359
  meta_indexes = getattr(meta_cls, "indexes", ())
353
360
  if isinstance(meta_indexes, (list, tuple)):
@@ -355,7 +362,6 @@ class ModelMeta(type):
355
362
  if isinstance(idx, Index):
356
363
  indexes.append(idx)
357
364
 
358
-
359
365
  model_meta = ModelMetadata(
360
366
  model=cls,
361
367
  table=table,
@@ -376,6 +382,7 @@ class ModelMeta(type):
376
382
 
377
383
  return cls
378
384
 
385
+
379
386
  @dataclass_transform(
380
387
  field_specifiers=(Field, DatabaseField, Relationship),
381
388
  kw_only_default=True,
@@ -406,8 +413,7 @@ class Model(metaclass=ModelMeta):
406
413
  if name.startswith("_"):
407
414
  continue
408
415
  if get_origin(field_type) is ClassVar or (
409
- isinstance(field_type, str)
410
- and field_type.startswith("ClassVar[")
416
+ isinstance(field_type, str) and field_type.startswith("ClassVar[")
411
417
  ):
412
418
  continue
413
419
 
@@ -432,7 +438,6 @@ class Model(metaclass=ModelMeta):
432
438
 
433
439
  cls._model_fields = fields
434
440
 
435
-
436
441
  @classmethod
437
442
  def schema(cls) -> dict[str, Any]:
438
443
  """Return a JSON Schema for the model.
@@ -533,7 +538,6 @@ class Model(metaclass=ModelMeta):
533
538
  elif field_type in (Any, object):
534
539
  continue
535
540
  else:
536
-
537
541
  # Custom runtime types need an explicit schema rule or a
538
542
  # custom Model schema implementation.
539
543
  continue
@@ -563,7 +567,9 @@ class Model(metaclass=ModelMeta):
563
567
  result = add_nullable(result)
564
568
  return result
565
569
 
566
- def relationship_schema(rel: RelationshipMetadata, stack: set[type]) -> dict[str, Any]:
570
+ def relationship_schema(
571
+ rel: RelationshipMetadata, stack: set[type]
572
+ ) -> dict[str, Any]:
567
573
  target = rel.target
568
574
  if target is dict:
569
575
  result: dict[str, Any] = {"type": "object"}
@@ -603,7 +609,6 @@ class Model(metaclass=ModelMeta):
603
609
  ):
604
610
  required.append(name)
605
611
 
606
-
607
612
  result: dict[str, Any] = {
608
613
  "title": model.__name__,
609
614
  "type": "object",
@@ -632,7 +637,11 @@ class Model(metaclass=ModelMeta):
632
637
 
633
638
  @staticmethod
634
639
  def _is_model_type(field_type: Any) -> bool:
635
- return bool(field_type) and isinstance(field_type, type) and issubclass(field_type, Model)
640
+ return (
641
+ bool(field_type)
642
+ and isinstance(field_type, type)
643
+ and issubclass(field_type, Model)
644
+ )
636
645
 
637
646
  @classmethod
638
647
  def _dump_value(cls, val: Any) -> Any:
@@ -646,7 +655,6 @@ class Model(metaclass=ModelMeta):
646
655
  return {k: cls._dump_value(v) for k, v in val.items()}
647
656
  return val
648
657
 
649
-
650
658
  def get_sensitive(self, name: str) -> Any:
651
659
  """Return a field's raw value, bypassing sensitive masking."""
652
660
  if name == "is_deleted":
@@ -671,7 +679,9 @@ class Model(metaclass=ModelMeta):
671
679
  def __iter__(self):
672
680
  keys = list(self._model_fields.keys())
673
681
  for k in self.__dict__:
674
- if (not k.startswith("_") and k not in keys) or (k == "_id" and k not in keys):
682
+ if (not k.startswith("_") and k not in keys) or (
683
+ k == "_id" and k not in keys
684
+ ):
675
685
  keys.append(k)
676
686
 
677
687
  meta = self.__db_metadata__
@@ -734,10 +744,11 @@ class Model(metaclass=ModelMeta):
734
744
  meta = getattr(self.__class__, "__db_metadata__", None)
735
745
 
736
746
  try:
737
-
738
747
  object.__setattr__(self, "_initialized", False)
739
748
 
740
- if not self._model_fields and getattr(self.__class__, "__annotations__", None):
749
+ if not self._model_fields and getattr(
750
+ self.__class__, "__annotations__", None
751
+ ):
741
752
  self.__class__._setup_model_fields()
742
753
 
743
754
  data: dict[str, Any] = {}
@@ -751,7 +762,6 @@ class Model(metaclass=ModelMeta):
751
762
  if kwargs:
752
763
  data.update(kwargs)
753
764
 
754
-
755
765
  errors: dict[str, list[Any]] = {}
756
766
  values: dict[str, Any] = {}
757
767
 
@@ -771,7 +781,9 @@ class Model(metaclass=ModelMeta):
771
781
  else:
772
782
  val = raw_val
773
783
 
774
- cleaned_val, field_errors = field_info.clean_and_validate(val, self.__class__)
784
+ cleaned_val, field_errors = field_info.clean_and_validate(
785
+ val, self.__class__
786
+ )
775
787
  if field_errors:
776
788
  errors[name] = field_errors
777
789
  else:
@@ -806,7 +818,6 @@ class Model(metaclass=ModelMeta):
806
818
  raise
807
819
  raise ValidationError(e.args or str(e)) from e
808
820
 
809
-
810
821
  if meta and meta.timestamps:
811
822
  now = datetime.now(timezone.utc)
812
823
  if getattr(self, "created_at", None) is None:
@@ -864,7 +875,9 @@ class Model(metaclass=ModelMeta):
864
875
  try:
865
876
  if name in self._model_fields:
866
877
  field_info = self._model_fields[name]
867
- cleaned_val, field_errors = field_info.clean_and_validate(value, self.__class__)
878
+ cleaned_val, field_errors = field_info.clean_and_validate(
879
+ value, self.__class__
880
+ )
868
881
  if field_errors:
869
882
  raise ValidationError({name: field_errors})
870
883
  if name == "is_deleted":
@@ -887,14 +900,17 @@ class Model(metaclass=ModelMeta):
887
900
  if is_relationship or self._is_field_dirty(name):
888
901
  object.__setattr__(self, "_db_state", Lifecycle.DIRTY)
889
902
  else:
890
-
891
903
  if not self._any_field_dirty():
892
904
  object.__setattr__(self, "_db_state", Lifecycle.PERSISTED)
893
905
  else:
894
906
  object.__setattr__(self, name, value)
895
907
 
896
908
  def _is_field_dirty(self, name: str) -> bool:
897
- current = self.get_sensitive(name) if name == "is_deleted" else self.__dict__.get(name)
909
+ current = (
910
+ self.get_sensitive(name)
911
+ if name == "is_deleted"
912
+ else self.__dict__.get(name)
913
+ )
898
914
  original = self._db_original_snapshot.get(name)
899
915
  return current != original
900
916
 
@@ -949,7 +965,11 @@ class Model(metaclass=ModelMeta):
949
965
  changes: dict[str, tuple[Any, Any]] = {}
950
966
  for fname in self.get_dirty_fields():
951
967
  original = self._db_original_snapshot.get(fname)
952
- current = self.get_sensitive(fname) if fname == "is_deleted" else self.__dict__.get(fname)
968
+ current = (
969
+ self.get_sensitive(fname)
970
+ if fname == "is_deleted"
971
+ else self.__dict__.get(fname)
972
+ )
953
973
  changes[fname] = (original, current)
954
974
  return changes
955
975
 
@@ -1002,9 +1022,7 @@ class Model(metaclass=ModelMeta):
1002
1022
  meta = self.__db_metadata__
1003
1023
  return {f: getattr(self, f, None) for f in meta.identity.fields}
1004
1024
 
1005
-
1006
1025
  @classmethod
1007
1026
  def metadata(cls) -> ModelMetadata:
1008
1027
  """Return the model's metadata object."""
1009
1028
  return cls.__db_metadata__
1010
-
@@ -17,6 +17,7 @@ from renus.db.query.ast import (
17
17
  LIKE_ESCAPE,
18
18
  Condition,
19
19
  HavingCondition,
20
+ Include,
20
21
  Logical,
21
22
  Operator,
22
23
  Path,
@@ -314,10 +315,30 @@ class MongoCompiler:
314
315
  pipeline.append({"$limit": ast.limit})
315
316
 
316
317
  if ast.projection:
317
- pipeline.append({"$project": {
318
- "_id": 0,
319
- **{self._path_str(path): 1 for path in ast.projection.paths},
320
- }})
318
+ project = {"_id": 0}
319
+ for path in ast.projection.paths:
320
+ if not path.parts:
321
+ continue
322
+ name = path.parts[-1]
323
+ if (
324
+ path.model is source
325
+ and len(path.parts) == 1
326
+ and meta is not None
327
+ and name in meta.relationships
328
+ ):
329
+ project[name] = 1
330
+ elif (
331
+ path.model is source
332
+ and len(path.parts) == 1
333
+ and meta is not None
334
+ and name == meta.primary_key_field == "id"
335
+ ):
336
+ project[name] = f"${self._path_str(path)}"
337
+ elif len(path.parts) > 1:
338
+ project[name] = f"${self._path_str(path)}"
339
+ else:
340
+ project[self._path_str(path)] = 1
341
+ pipeline.append({"$project": project})
321
342
  return MongoCompiledQuery(
322
343
  collection=collection_name,
323
344
  filter_doc={},
@@ -548,12 +569,22 @@ class MongoCompiler:
548
569
  ast: QueryAST,
549
570
  ) -> tuple[list[dict[str, Any]], list[str]]:
550
571
  """Build recursive relationship lookups in one aggregation pipeline."""
551
- if ast.projection or ast.aggregates:
572
+ if ast.aggregates:
552
573
  return [], []
553
574
  root_meta = getattr(ast.source, "__db_metadata__", None)
554
575
  if root_meta is None:
555
576
  return [], []
556
577
 
578
+ projected_relationships = tuple(
579
+ dict.fromkeys(
580
+ path.parts[0]
581
+ for path in (ast.projection.paths if ast.projection else ())
582
+ if path.model is ast.source
583
+ and len(path.parts) == 1
584
+ and path.parts[0] in root_meta.relationships
585
+ )
586
+ )
587
+ projected_relationship_names = set(projected_relationships)
557
588
  requested: dict[tuple[str, ...], Any] = {}
558
589
 
559
590
  def collect(item: Any, prefix: tuple[str, ...] = ()) -> None:
@@ -563,7 +594,14 @@ class MongoCompiler:
563
594
  collect(nested, path)
564
595
 
565
596
  for item in ast.includes:
597
+ if ast.projection and (
598
+ not item.path.parts
599
+ or item.path.parts[0] not in projected_relationship_names
600
+ ):
601
+ continue
566
602
  collect(item)
603
+ for name in projected_relationships:
604
+ requested.setdefault((name,), Include(Path(ast.source, (name,))))
567
605
 
568
606
  def mongo_field(model_meta: Any, field_name: str) -> str:
569
607
  return "_id" if field_name == model_meta.primary_key_field else field_name
@@ -578,7 +616,9 @@ class MongoCompiler:
578
616
  direct.setdefault(remainder[0], include)
579
617
  for name, rel in owner_meta.relationships.items():
580
618
  loading = getattr(rel.loading, "value", rel.loading)
581
- if loading == "joined":
619
+ if loading == "joined" and (
620
+ not ast.projection or prefix or name in projected_relationship_names
621
+ ):
582
622
  direct.setdefault(name, None)
583
623
 
584
624
  stages: list[dict[str, Any]] = []
@@ -126,7 +126,7 @@ class MongoDialect:
126
126
  return None
127
127
  if isinstance(raw, _ID_NATIVE_TYPES):
128
128
  return raw
129
- return str(value)
129
+ return str(raw)
130
130
  if value is not None and not isinstance(value, _ID_NATIVE_TYPES):
131
131
  return str(value)
132
132
  return value
@@ -20,7 +20,7 @@ from renus.db import errors
20
20
  from renus.db.mutation.ast import InsertMany, MutationAST
21
21
  from renus.db.query.ast import QueryAST
22
22
  from renus.db.sql.adapter import SQLAdapter
23
- from renus.db.sql.compiler.compiler import SQLCompiled, SQLCompiler
23
+ from renus.db.sql.compiler import SQLCompiled, SQLCompiler
24
24
  from renus.db.mysql.dialect import MySQLDialect
25
25
  from renus.db.sql.mutation import SQLMutationCompiler
26
26
 
@@ -369,6 +369,7 @@ class MySQLAdapter(SQLAdapter):
369
369
  joined=sql_compiled.joined,
370
370
  root_limit=sql_compiled.root_limit,
371
371
  root_offset=sql_compiled.root_offset,
372
+ projection_paths=sql_compiled.projection_paths,
372
373
  )
373
374
 
374
375
  cursor_cls = aiomysql.DictCursor if aiomysql is not None else None
@@ -5,7 +5,7 @@ from typing import Any
5
5
 
6
6
  from renus.datastructures.id import ID
7
7
  from renus.db.metadata.core import FieldMetadata
8
- from renus.db.sql.dialect.base import SQLDialect
8
+ from renus.db.sql.dialect import SQLDialect
9
9
 
10
10
 
11
11
  class MySQLDialect(SQLDialect):
@@ -1,8 +1,8 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from renus.db.sql.adapter import SQLAdapter
4
- from renus.db.sql.compiler.compiler import SQLCompiled, SQLCompiler
5
- from renus.db.sql.dialect.base import SQLDialect
4
+ from renus.db.sql.compiler import SQLCompiled, SQLCompiler
5
+ from renus.db.sql.dialect import SQLDialect
6
6
  from renus.db.sql.mutation import SQLMutationCompiler
7
7
 
8
8
  __all__ = [
@@ -9,8 +9,8 @@ from renus.db.core.backup import DEFAULT_PAGE_SIZE, BackupFileWriter, load_rows_
9
9
  from renus.db.mutation.ast import InsertMany, MutationAST
10
10
  from renus.db.query.ast import QueryAST
11
11
 
12
- from renus.db.sql.compiler.compiler import SQLCompiled, SQLCompiler
13
- from renus.db.sql.dialect.base import SQLDialect
12
+ from renus.db.sql.compiler import SQLCompiled, SQLCompiler
13
+ from renus.db.sql.dialect import SQLDialect
14
14
  from renus.db.sql.mutation import SQLMutationCompiler
15
15
  from renus.db.transaction.core import Transaction
16
16
  from renus.db.core.exceptions import IntegrityError
@@ -118,6 +118,17 @@ class SQLAdapter(AsyncDatabaseAdapter):
118
118
  root_meta = compiled.meta
119
119
  root_pk = root_meta.get_field(root_meta.primary_key_field)
120
120
  root_column = root_pk.column_name if root_pk else root_meta.primary_key_field
121
+ projection_paths = getattr(compiled, "projection_paths", ())
122
+ projected_relationships = tuple(
123
+ dict.fromkeys(
124
+ path.parts[0]
125
+ for path in projection_paths
126
+ if path.model is root_meta.model
127
+ and path.parts
128
+ and path.parts[0] in root_meta.relationships
129
+ )
130
+ )
131
+ projected_relationship_names = set(projected_relationships)
121
132
  grouped: dict[Any, dict[str, Any]] = {}
122
133
  child_keys: dict[tuple[Any, tuple[str, ...]], set[Any]] = {}
123
134
 
@@ -127,9 +138,12 @@ class SQLAdapter(AsyncDatabaseAdapter):
127
138
  key: value
128
139
  for key, value in row.items()
129
140
  if not key.startswith("__renus_joined__")
141
+ and key != "__renus_root_pk"
130
142
  }
131
143
  root = self.decode_row(root_data, root_meta)
132
- root_id = root.get(root_meta.primary_key_field, root.get(root_column))
144
+ root_id = row.get("__renus_root_pk")
145
+ if root_id is None:
146
+ root_id = root.get(root_meta.primary_key_field, root.get(root_column))
133
147
  if root_id not in grouped:
134
148
  grouped[root_id] = root
135
149
  root = grouped[root_id]
@@ -174,6 +188,13 @@ class SQLAdapter(AsyncDatabaseAdapter):
174
188
  )
175
189
 
176
190
  result = list(grouped.values())
191
+ if projection_paths:
192
+ for root in result:
193
+ for name in projected_relationship_names:
194
+ root.setdefault(
195
+ name,
196
+ [] if root_meta.relationships[name].is_collection else None,
197
+ )
177
198
  start = compiled.root_offset or 0
178
199
  end = start + compiled.root_limit if compiled.root_limit is not None else None
179
200
  return result[start:end]