@opensaas/stack-core 0.44.0-alpha.5 → 0.44.0-alpha.7

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 (402) hide show
  1. package/CHANGELOG.md +1051 -0
  2. package/dist/.build-hash +1 -1
  3. package/dist/access/actor.d.ts +56 -0
  4. package/dist/access/actor.d.ts.map +1 -0
  5. package/dist/access/actor.js +66 -0
  6. package/dist/access/actor.js.map +1 -0
  7. package/dist/access/decide-advertisement.d.ts +55 -0
  8. package/dist/access/decide-advertisement.d.ts.map +1 -0
  9. package/dist/access/decide-advertisement.js +125 -0
  10. package/dist/access/decide-advertisement.js.map +1 -0
  11. package/dist/access/declared-dependencies.d.ts +4 -89
  12. package/dist/access/declared-dependencies.d.ts.map +1 -1
  13. package/dist/access/declared-dependencies.js +14 -84
  14. package/dist/access/declared-dependencies.js.map +1 -1
  15. package/dist/access/depth-limits.d.ts +2 -0
  16. package/dist/access/depth-limits.d.ts.map +1 -1
  17. package/dist/access/depth-limits.js +2 -0
  18. package/dist/access/depth-limits.js.map +1 -1
  19. package/dist/access/engine.d.ts +1 -24
  20. package/dist/access/engine.d.ts.map +1 -1
  21. package/dist/access/engine.js +0 -35
  22. package/dist/access/engine.js.map +1 -1
  23. package/dist/access/field-access.d.ts +43 -19
  24. package/dist/access/field-access.d.ts.map +1 -1
  25. package/dist/access/field-access.js +206 -185
  26. package/dist/access/field-access.js.map +1 -1
  27. package/dist/access/index.d.ts +5 -6
  28. package/dist/access/index.d.ts.map +1 -1
  29. package/dist/access/index.js +6 -7
  30. package/dist/access/index.js.map +1 -1
  31. package/dist/access/rule-item.d.ts +42 -0
  32. package/dist/access/rule-item.d.ts.map +1 -0
  33. package/dist/access/rule-item.js +215 -0
  34. package/dist/access/rule-item.js.map +1 -0
  35. package/dist/access/session-vocabulary.d.ts +124 -0
  36. package/dist/access/session-vocabulary.d.ts.map +1 -0
  37. package/dist/access/session-vocabulary.js +366 -0
  38. package/dist/access/session-vocabulary.js.map +1 -0
  39. package/dist/access/transaction-registry.d.ts +6 -0
  40. package/dist/access/transaction-registry.d.ts.map +1 -1
  41. package/dist/access/transaction-registry.js +19 -2
  42. package/dist/access/transaction-registry.js.map +1 -1
  43. package/dist/access/types.d.ts +66 -0
  44. package/dist/access/types.d.ts.map +1 -1
  45. package/dist/access/vocabulary-filter-specs.d.ts +17 -0
  46. package/dist/access/vocabulary-filter-specs.d.ts.map +1 -0
  47. package/dist/access/vocabulary-filter-specs.js +27 -0
  48. package/dist/access/vocabulary-filter-specs.js.map +1 -0
  49. package/dist/client-safe.d.ts +8 -0
  50. package/dist/client-safe.d.ts.map +1 -0
  51. package/dist/client-safe.js +8 -0
  52. package/dist/client-safe.js.map +1 -0
  53. package/dist/config/field-kind.d.ts +7 -21
  54. package/dist/config/field-kind.d.ts.map +1 -1
  55. package/dist/config/field-kind.js +8 -5
  56. package/dist/config/field-kind.js.map +1 -1
  57. package/dist/config/has-access-gate.d.ts +20 -0
  58. package/dist/config/has-access-gate.d.ts.map +1 -0
  59. package/dist/config/has-access-gate.js +26 -0
  60. package/dist/config/has-access-gate.js.map +1 -0
  61. package/dist/config/index.d.ts +1 -1
  62. package/dist/config/index.d.ts.map +1 -1
  63. package/dist/config/index.js +8 -2
  64. package/dist/config/index.js.map +1 -1
  65. package/dist/config/list-view-keys.d.ts +5 -0
  66. package/dist/config/list-view-keys.d.ts.map +1 -0
  67. package/dist/config/list-view-keys.js +18 -0
  68. package/dist/config/list-view-keys.js.map +1 -0
  69. package/dist/config/mcp-default-tools.d.ts +3 -0
  70. package/dist/config/mcp-default-tools.d.ts.map +1 -0
  71. package/dist/config/mcp-default-tools.js +14 -0
  72. package/dist/config/mcp-default-tools.js.map +1 -0
  73. package/dist/config/mcp-tool-names.d.ts +2 -0
  74. package/dist/config/mcp-tool-names.d.ts.map +1 -1
  75. package/dist/config/mcp-tool-names.js +17 -5
  76. package/dist/config/mcp-tool-names.js.map +1 -1
  77. package/dist/config/nav-count.d.ts +8 -16
  78. package/dist/config/nav-count.d.ts.map +1 -1
  79. package/dist/config/nav-count.js +20 -32
  80. package/dist/config/nav-count.js.map +1 -1
  81. package/dist/config/plugin-engine.d.ts.map +1 -1
  82. package/dist/config/plugin-engine.js +6 -0
  83. package/dist/config/plugin-engine.js.map +1 -1
  84. package/dist/config/preset-views.d.ts +22 -0
  85. package/dist/config/preset-views.d.ts.map +1 -0
  86. package/dist/config/preset-views.js +46 -0
  87. package/dist/config/preset-views.js.map +1 -0
  88. package/dist/config/row-action-availability.d.ts +4 -0
  89. package/dist/config/row-action-availability.d.ts.map +1 -0
  90. package/dist/config/row-action-availability.js +20 -0
  91. package/dist/config/row-action-availability.js.map +1 -0
  92. package/dist/config/types.d.ts +409 -34
  93. package/dist/config/types.d.ts.map +1 -1
  94. package/dist/context/engine-context.d.ts +2 -0
  95. package/dist/context/engine-context.d.ts.map +1 -1
  96. package/dist/context/engine-context.js +8 -0
  97. package/dist/context/engine-context.js.map +1 -1
  98. package/dist/context/hook-pipeline.d.ts +6 -1
  99. package/dist/context/hook-pipeline.d.ts.map +1 -1
  100. package/dist/context/hook-pipeline.js +6 -4
  101. package/dist/context/hook-pipeline.js.map +1 -1
  102. package/dist/context/index.d.ts +10 -1
  103. package/dist/context/index.d.ts.map +1 -1
  104. package/dist/context/index.js +217 -94
  105. package/dist/context/index.js.map +1 -1
  106. package/dist/context/junction.d.ts +28 -21
  107. package/dist/context/junction.d.ts.map +1 -1
  108. package/dist/context/junction.js +74 -69
  109. package/dist/context/junction.js.map +1 -1
  110. package/dist/context/plugin-field-write.d.ts.map +1 -1
  111. package/dist/context/plugin-field-write.js +19 -25
  112. package/dist/context/plugin-field-write.js.map +1 -1
  113. package/dist/context/process-lifetime.d.ts +8 -0
  114. package/dist/context/process-lifetime.d.ts.map +1 -0
  115. package/dist/context/process-lifetime.js +14 -0
  116. package/dist/context/process-lifetime.js.map +1 -0
  117. package/dist/context/relationship-input.d.ts +2 -15
  118. package/dist/context/relationship-input.d.ts.map +1 -1
  119. package/dist/context/relationship-input.js +44 -85
  120. package/dist/context/relationship-input.js.map +1 -1
  121. package/dist/context/transaction-boundary.d.ts +3 -2
  122. package/dist/context/transaction-boundary.d.ts.map +1 -1
  123. package/dist/context/transaction-boundary.js +17 -2
  124. package/dist/context/transaction-boundary.js.map +1 -1
  125. package/dist/context/write-pipeline.d.ts +5 -19
  126. package/dist/context/write-pipeline.d.ts.map +1 -1
  127. package/dist/context/write-pipeline.js +54 -92
  128. package/dist/context/write-pipeline.js.map +1 -1
  129. package/dist/context/write-target.d.ts +52 -0
  130. package/dist/context/write-target.d.ts.map +1 -0
  131. package/dist/context/write-target.js +118 -0
  132. package/dist/context/write-target.js.map +1 -0
  133. package/dist/contract/dependencies.d.ts +24 -10
  134. package/dist/contract/dependencies.d.ts.map +1 -1
  135. package/dist/contract/dependencies.js +120 -80
  136. package/dist/contract/dependencies.js.map +1 -1
  137. package/dist/contract/derive.d.ts +6 -11
  138. package/dist/contract/derive.d.ts.map +1 -1
  139. package/dist/contract/derive.js +256 -211
  140. package/dist/contract/derive.js.map +1 -1
  141. package/dist/contract/fixtures/contract-configs.d.ts +5 -0
  142. package/dist/contract/fixtures/contract-configs.d.ts.map +1 -1
  143. package/dist/contract/fixtures/contract-configs.js +21 -0
  144. package/dist/contract/fixtures/contract-configs.js.map +1 -1
  145. package/dist/contract/id-boundary.js +1 -1
  146. package/dist/contract/id-boundary.js.map +1 -1
  147. package/dist/contract/prisma.js +2 -2
  148. package/dist/contract/prisma.js.map +1 -1
  149. package/dist/contract/strategy.d.ts +13 -0
  150. package/dist/contract/strategy.d.ts.map +1 -0
  151. package/dist/contract/strategy.js +38 -0
  152. package/dist/contract/strategy.js.map +1 -0
  153. package/dist/db/client-alias.d.ts +12 -0
  154. package/dist/db/client-alias.d.ts.map +1 -0
  155. package/dist/db/client-alias.js +8 -0
  156. package/dist/db/client-alias.js.map +1 -0
  157. package/dist/extend.d.ts +2 -2
  158. package/dist/extend.d.ts.map +1 -1
  159. package/dist/extend.js.map +1 -1
  160. package/dist/fields/index.d.ts +12 -14
  161. package/dist/fields/index.d.ts.map +1 -1
  162. package/dist/fields/index.js +217 -67
  163. package/dist/fields/index.js.map +1 -1
  164. package/dist/filter/collect.d.ts +43 -25
  165. package/dist/filter/collect.d.ts.map +1 -1
  166. package/dist/filter/collect.js +107 -38
  167. package/dist/filter/collect.js.map +1 -1
  168. package/dist/filter/index.d.ts +7 -5
  169. package/dist/filter/index.d.ts.map +1 -1
  170. package/dist/filter/index.js +6 -14
  171. package/dist/filter/index.js.map +1 -1
  172. package/dist/filter/map.d.ts +112 -20
  173. package/dist/filter/map.d.ts.map +1 -1
  174. package/dist/filter/map.js +271 -52
  175. package/dist/filter/map.js.map +1 -1
  176. package/dist/filter/parse.d.ts +70 -26
  177. package/dist/filter/parse.d.ts.map +1 -1
  178. package/dist/filter/parse.js +212 -79
  179. package/dist/filter/parse.js.map +1 -1
  180. package/dist/filter/record-id.d.ts +19 -0
  181. package/dist/filter/record-id.d.ts.map +1 -0
  182. package/dist/filter/record-id.js +37 -0
  183. package/dist/filter/record-id.js.map +1 -0
  184. package/dist/filter/serialize.d.ts +45 -19
  185. package/dist/filter/serialize.d.ts.map +1 -1
  186. package/dist/filter/serialize.js +146 -50
  187. package/dist/filter/serialize.js.map +1 -1
  188. package/dist/filter/types.d.ts +132 -21
  189. package/dist/filter/types.d.ts.map +1 -1
  190. package/dist/filter/types.js +2 -11
  191. package/dist/filter/types.js.map +1 -1
  192. package/dist/hooks/index.d.ts +6 -3
  193. package/dist/hooks/index.d.ts.map +1 -1
  194. package/dist/hooks/index.js +38 -8
  195. package/dist/hooks/index.js.map +1 -1
  196. package/dist/index.d.ts +10 -9
  197. package/dist/index.d.ts.map +1 -1
  198. package/dist/index.js +5 -5
  199. package/dist/index.js.map +1 -1
  200. package/dist/internal.d.ts +11 -2
  201. package/dist/internal.d.ts.map +1 -1
  202. package/dist/internal.js +8 -8
  203. package/dist/internal.js.map +1 -1
  204. package/dist/lib/database-errors.d.ts +2 -1
  205. package/dist/lib/database-errors.d.ts.map +1 -1
  206. package/dist/lib/database-errors.js +5 -4
  207. package/dist/lib/database-errors.js.map +1 -1
  208. package/dist/mcp/advertise.d.ts +5 -13
  209. package/dist/mcp/advertise.d.ts.map +1 -1
  210. package/dist/mcp/advertise.js +7 -22
  211. package/dist/mcp/advertise.js.map +1 -1
  212. package/dist/mcp/arguments.d.ts.map +1 -1
  213. package/dist/mcp/arguments.js +8 -11
  214. package/dist/mcp/arguments.js.map +1 -1
  215. package/dist/mcp/field-schema.d.ts +4 -15
  216. package/dist/mcp/field-schema.d.ts.map +1 -1
  217. package/dist/mcp/field-schema.js +50 -69
  218. package/dist/mcp/field-schema.js.map +1 -1
  219. package/dist/mcp/handler.d.ts +111 -18
  220. package/dist/mcp/handler.d.ts.map +1 -1
  221. package/dist/mcp/handler.js +231 -87
  222. package/dist/mcp/handler.js.map +1 -1
  223. package/dist/mcp/index.d.ts +2 -1
  224. package/dist/mcp/index.d.ts.map +1 -1
  225. package/dist/mcp/index.js +1 -1
  226. package/dist/mcp/index.js.map +1 -1
  227. package/dist/mcp/procedure-tools.d.ts +63 -0
  228. package/dist/mcp/procedure-tools.d.ts.map +1 -0
  229. package/dist/mcp/procedure-tools.js +139 -0
  230. package/dist/mcp/procedure-tools.js.map +1 -0
  231. package/dist/mcp/projection.d.ts +6 -5
  232. package/dist/mcp/projection.d.ts.map +1 -1
  233. package/dist/mcp/projection.js +33 -38
  234. package/dist/mcp/projection.js.map +1 -1
  235. package/dist/mcp/reachability.d.ts +13 -0
  236. package/dist/mcp/reachability.d.ts.map +1 -0
  237. package/dist/mcp/reachability.js +51 -0
  238. package/dist/mcp/reachability.js.map +1 -0
  239. package/dist/mcp/relation-member.d.ts +15 -0
  240. package/dist/mcp/relation-member.d.ts.map +1 -0
  241. package/dist/mcp/relation-member.js +50 -0
  242. package/dist/mcp/relation-member.js.map +1 -0
  243. package/dist/mcp/types.d.ts +12 -0
  244. package/dist/mcp/types.d.ts.map +1 -1
  245. package/dist/mcp/where-id-boundary.d.ts.map +1 -1
  246. package/dist/mcp/where-id-boundary.js +12 -24
  247. package/dist/mcp/where-id-boundary.js.map +1 -1
  248. package/dist/mcp/where-reachability.d.ts +10 -0
  249. package/dist/mcp/where-reachability.d.ts.map +1 -0
  250. package/dist/mcp/where-reachability.js +88 -0
  251. package/dist/mcp/where-reachability.js.map +1 -0
  252. package/dist/members/claims.d.ts +71 -0
  253. package/dist/members/claims.d.ts.map +1 -0
  254. package/dist/members/claims.js +199 -0
  255. package/dist/members/claims.js.map +1 -0
  256. package/dist/members/list-members.d.ts +87 -0
  257. package/dist/members/list-members.d.ts.map +1 -0
  258. package/dist/members/list-members.js +250 -0
  259. package/dist/members/list-members.js.map +1 -0
  260. package/dist/procedures/client.d.ts +163 -0
  261. package/dist/procedures/client.d.ts.map +1 -0
  262. package/dist/procedures/client.js +152 -0
  263. package/dist/procedures/client.js.map +1 -0
  264. package/dist/procedures/codes.d.ts +23 -0
  265. package/dist/procedures/codes.d.ts.map +1 -0
  266. package/dist/procedures/codes.js +46 -0
  267. package/dist/procedures/codes.js.map +1 -0
  268. package/dist/procedures/context-face.d.ts +4 -0
  269. package/dist/procedures/context-face.d.ts.map +1 -0
  270. package/dist/procedures/context-face.js +31 -0
  271. package/dist/procedures/context-face.js.map +1 -0
  272. package/dist/procedures/failures.d.ts +31 -0
  273. package/dist/procedures/failures.d.ts.map +1 -0
  274. package/dist/procedures/failures.js +51 -0
  275. package/dist/procedures/failures.js.map +1 -0
  276. package/dist/procedures/http.d.ts +114 -0
  277. package/dist/procedures/http.d.ts.map +1 -0
  278. package/dist/procedures/http.js +224 -0
  279. package/dist/procedures/http.js.map +1 -0
  280. package/dist/procedures/index.d.ts +7 -0
  281. package/dist/procedures/index.d.ts.map +1 -0
  282. package/dist/procedures/index.js +11 -0
  283. package/dist/procedures/index.js.map +1 -0
  284. package/dist/procedures/procedure.d.ts +204 -0
  285. package/dist/procedures/procedure.d.ts.map +1 -0
  286. package/dist/procedures/procedure.js +123 -0
  287. package/dist/procedures/procedure.js.map +1 -0
  288. package/dist/procedures/result.d.ts +42 -0
  289. package/dist/procedures/result.d.ts.map +1 -0
  290. package/dist/procedures/result.js +51 -0
  291. package/dist/procedures/result.js.map +1 -0
  292. package/dist/procedures/run.d.ts +36 -0
  293. package/dist/procedures/run.d.ts.map +1 -0
  294. package/dist/procedures/run.js +76 -0
  295. package/dist/procedures/run.js.map +1 -0
  296. package/dist/procedures/server.d.ts +131 -0
  297. package/dist/procedures/server.d.ts.map +1 -0
  298. package/dist/procedures/server.js +115 -0
  299. package/dist/procedures/server.js.map +1 -0
  300. package/dist/procedures/validate.d.ts +60 -0
  301. package/dist/procedures/validate.d.ts.map +1 -0
  302. package/dist/procedures/validate.js +254 -0
  303. package/dist/procedures/validate.js.map +1 -0
  304. package/dist/procedures/web-session.d.ts +12 -0
  305. package/dist/procedures/web-session.d.ts.map +1 -0
  306. package/dist/procedures/web-session.js +35 -0
  307. package/dist/procedures/web-session.js.map +1 -0
  308. package/dist/query/relationship-options.d.ts +8 -0
  309. package/dist/query/relationship-options.d.ts.map +1 -1
  310. package/dist/query/relationship-options.js +18 -4
  311. package/dist/query/relationship-options.js.map +1 -1
  312. package/dist/secured/binding.d.ts +84 -0
  313. package/dist/secured/binding.d.ts.map +1 -0
  314. package/dist/secured/binding.js +52 -0
  315. package/dist/secured/binding.js.map +1 -0
  316. package/dist/secured/declared-branch.d.ts +11 -0
  317. package/dist/secured/declared-branch.d.ts.map +1 -0
  318. package/dist/secured/declared-branch.js +17 -0
  319. package/dist/secured/declared-branch.js.map +1 -0
  320. package/dist/secured/field-visibility.d.ts +56 -0
  321. package/dist/secured/field-visibility.d.ts.map +1 -0
  322. package/dist/secured/field-visibility.js +624 -0
  323. package/dist/secured/field-visibility.js.map +1 -0
  324. package/dist/secured/include.d.ts +77 -37
  325. package/dist/secured/include.d.ts.map +1 -1
  326. package/dist/secured/include.js +228 -122
  327. package/dist/secured/include.js.map +1 -1
  328. package/dist/secured/lock.d.ts +8 -2
  329. package/dist/secured/lock.d.ts.map +1 -1
  330. package/dist/secured/lock.js +44 -15
  331. package/dist/secured/lock.js.map +1 -1
  332. package/dist/secured/lower.d.ts.map +1 -1
  333. package/dist/secured/lower.js +15 -4
  334. package/dist/secured/lower.js.map +1 -1
  335. package/dist/secured/read.d.ts +13 -52
  336. package/dist/secured/read.d.ts.map +1 -1
  337. package/dist/secured/read.js +88 -614
  338. package/dist/secured/read.js.map +1 -1
  339. package/dist/secured/select.d.ts +0 -24
  340. package/dist/secured/select.d.ts.map +1 -1
  341. package/dist/secured/select.js +60 -153
  342. package/dist/secured/select.js.map +1 -1
  343. package/dist/secured/vocabulary.d.ts +13 -1
  344. package/dist/secured/vocabulary.d.ts.map +1 -1
  345. package/dist/secured/vocabulary.js +173 -83
  346. package/dist/secured/vocabulary.js.map +1 -1
  347. package/dist/testing/untyped-rule.d.ts +8 -0
  348. package/dist/testing/untyped-rule.d.ts.map +1 -0
  349. package/dist/testing/untyped-rule.js +4 -0
  350. package/dist/testing/untyped-rule.js.map +1 -0
  351. package/dist/types/index.d.ts +1 -1
  352. package/dist/types/index.d.ts.map +1 -1
  353. package/dist/types/rows.d.ts +18 -1
  354. package/dist/types/rows.d.ts.map +1 -1
  355. package/dist/validation/config-refusal.d.ts +3 -1
  356. package/dist/validation/config-refusal.d.ts.map +1 -1
  357. package/dist/validation/database-config.d.ts +1 -15
  358. package/dist/validation/database-config.d.ts.map +1 -1
  359. package/dist/validation/database-config.js +32 -1
  360. package/dist/validation/database-config.js.map +1 -1
  361. package/dist/validation/default-value.d.ts.map +1 -1
  362. package/dist/validation/default-value.js +3 -8
  363. package/dist/validation/default-value.js.map +1 -1
  364. package/dist/validation/extension-packs.d.ts.map +1 -1
  365. package/dist/validation/extension-packs.js +1 -0
  366. package/dist/validation/extension-packs.js.map +1 -1
  367. package/dist/validation/field-config.d.ts +3 -2
  368. package/dist/validation/field-config.d.ts.map +1 -1
  369. package/dist/validation/field-config.js +3 -1
  370. package/dist/validation/field-config.js.map +1 -1
  371. package/dist/validation/field-names.d.ts +2 -1
  372. package/dist/validation/field-names.d.ts.map +1 -1
  373. package/dist/validation/field-names.js +76 -63
  374. package/dist/validation/field-names.js.map +1 -1
  375. package/dist/validation/identifier-names.d.ts +1 -0
  376. package/dist/validation/identifier-names.d.ts.map +1 -1
  377. package/dist/validation/identifier-names.js +17 -9
  378. package/dist/validation/identifier-names.js.map +1 -1
  379. package/dist/validation/needs-closure.d.ts +30 -14
  380. package/dist/validation/needs-closure.d.ts.map +1 -1
  381. package/dist/validation/needs-closure.js +184 -31
  382. package/dist/validation/needs-closure.js.map +1 -1
  383. package/dist/validation/relations.d.ts.map +1 -1
  384. package/dist/validation/relations.js +11 -0
  385. package/dist/validation/relations.js.map +1 -1
  386. package/dist/validation/schema.d.ts +4 -3
  387. package/dist/validation/schema.d.ts.map +1 -1
  388. package/dist/validation/schema.js +8 -6
  389. package/dist/validation/schema.js.map +1 -1
  390. package/package.json +26 -10
  391. package/dist/access/field-visibility.d.ts +0 -58
  392. package/dist/access/field-visibility.d.ts.map +0 -1
  393. package/dist/access/field-visibility.js +0 -383
  394. package/dist/access/field-visibility.js.map +0 -1
  395. package/dist/access/query-validation.d.ts +0 -30
  396. package/dist/access/query-validation.d.ts.map +0 -1
  397. package/dist/access/query-validation.js +0 -49
  398. package/dist/access/query-validation.js.map +0 -1
  399. package/dist/access/system-fields.d.ts +0 -2
  400. package/dist/access/system-fields.d.ts.map +0 -1
  401. package/dist/access/system-fields.js +0 -7
  402. package/dist/access/system-fields.js.map +0 -1
@@ -2,54 +2,28 @@
2
2
  // Prisma 8 collection, its `where`/`orderBy` composition, and the
3
3
  // `all()`/`first()`/`nearest()`/`aggregate()` terminals the engine owns. See ADR-0041,
4
4
  // ADR-0044, ADR-0045, ADR-0046, ADR-0055 and ADR-0058.
5
- import { assertAccessFilterConstrains, checkAccess, checkFieldAccess, filterReadableFields, getRelatedListConfig, } from '../access/index.js';
5
+ import { assertAccessFilterConstrains, checkAccess } from '../access/index.js';
6
6
  import { withOrigin } from '../origin.js';
7
7
  import { lowerOrder, lowerWhere, vectorLowering, whereCombinators, } from './lower.js';
8
- import { resolveColumns, resolveNearest, resolveOrderBy, resolveRelatedAccessPlan, assertWhereWithinLimits, resolveWhere, unqueryableKey, } from './vocabulary.js';
9
- import { buildIncludeRequest, DECLARED_COUNT_BRANCH_KEY, DECLARED_ROWS_BRANCH_KEY, foreignKeyOwningRelations, orderList, resolveIncludes, } from './include.js';
10
- import { dependencyAdditions, hasRowsBranch, reducedDeclaredKeys, resolveProjection, selectionScope, } from './select.js';
11
- import { getListDependencies, } from '../access/declared-dependencies.js';
8
+ import { resolveColumns, resolveNearest, resolveOrderBy, assertWhereWithinLimits, resolveWhere, unqueryableKey, } from './vocabulary.js';
9
+ import { buildIncludeRequest, DECLARED_COUNT_BRANCH_KEY, DECLARED_ROWS_BRANCH_KEY, declaredRelations, orderList, planLevel, resolveIncludes, } from './include.js';
10
+ import { resolveProjection } from './select.js';
11
+ import { collectionFor, engineRead, identityIn, } from './binding.js';
12
+ import { declaredBranch } from './declared-branch.js';
13
+ import { visibleRows } from './field-visibility.js';
12
14
  import { aggregations, checkSpec, specKeys, zeroed } from './aggregate.js';
13
15
  import { distanceToScore, requireVector, vectorDistance } from './vector.js';
14
16
  import { ROW_LOCK_MAX_KEYS, RowLockIdentityError, RowLockKeyLimitExceededError, RowLockUnavailableError, } from './lock.js';
15
17
  import { ValidationError } from '../hooks/index.js';
18
+ export { SecuredCollectionMissingError } from './binding.js';
16
19
  export { AccessFilterRecursionError, ACCESS_FILTER_MAX_DEPTH } from './vocabulary.js';
17
20
  export { NEAREST_DEFAULT_LIMIT } from './vocabulary.js';
18
21
  export { DuplicateIncludeError, InvalidCombineBranchError, InvalidRefinementError, MultipleCombineRowBranchesError, ReducedToOneIncludeError, ReservedCombineKeyError, UnreducibleRefinementError, } from './include.js';
19
22
  export { RelationSelectError } from './select.js';
20
23
  export { ROW_LOCK_MAX_KEYS, RowLockIdentityError, RowLockKeyLimitExceededError, RowLockLaneUnavailableError, RowLockUnavailableError, } from './lock.js';
21
24
  export { VectorDecodeError } from './vector.js';
22
- /**
23
- * Thrown when the ORM client carries no collection for a list the config
24
- * declares — a generation or wiring fault rather than an access denial, so it
25
- * is reported rather than silently read as an empty result.
26
- */
27
- export class SecuredCollectionMissingError extends Error {
28
- listName;
29
- constructor(listName) {
30
- super(`The ORM client has no collection for list "${listName}". Re-run \`opensaas generate\` so ` +
31
- `the emitted contract matches the config.`);
32
- this.listName = listName;
33
- this.name = 'SecuredCollectionMissingError';
34
- }
35
- }
36
- /**
37
- * Presence, not completeness — the same rule `isDelegate` states in
38
- * `access/orm-client.ts`, and for the same reason. A test double implements
39
- * only the operations its test reaches, so requiring the full set would refuse
40
- * a client the engine can drive, and would refuse it with advice ("re-run
41
- * `opensaas generate`") that does not describe what is actually wrong. A
42
- * member that is genuinely absent surfaces at its own call site.
43
- */
44
- function isReadableCollection(value) {
45
- return typeof value === 'object' && value !== null;
46
- }
47
- function collectionFor(ormHandle, listName) {
48
- const collection = ormHandle[listName];
49
- if (!isReadableCollection(collection))
50
- throw new SecuredCollectionMissingError(listName);
51
- return collection;
52
- }
25
+ /** Whether the ORM can select these columns by name: it keys them in a plain object. */
26
+ const isProjectable = (columns) => !columns.some((column) => column in Object.prototype);
53
27
  /** Whether the plan actually carries this member — an empty one refuses nothing. */
54
28
  function carried(value) {
55
29
  if (Array.isArray(value))
@@ -141,11 +115,8 @@ async function resolvePlan(binding, state) {
141
115
  return {
142
116
  predicates,
143
117
  orders,
144
- includes,
145
118
  projection,
146
- selection: selectionScope(projection, includes),
147
- additions: dependencyAdditions(includes),
148
- reducedDeclared: reducedDeclaredKeys(includes),
119
+ root: planLevel(ctx, projection.caller, includes),
149
120
  limit: state.limit,
150
121
  offset: state.offset,
151
122
  ...(distinct ? { distinct } : {}),
@@ -258,11 +229,10 @@ function refine(collection, plan, ops) {
258
229
  return refined.combine(combineSpec(refined, plan, plan.reduce.branches, ops));
259
230
  }
260
231
  // Both the caller's own reduce AND the declared dependency's rows ride in
261
- // this one query, under keys the caller cannot reach (#1357) —
262
- // `maskReductions`/`restoreReductions` are where the two get split apart
263
- // again on the way out.
232
+ // this one query, under keys the caller cannot reach (#1357) — Field
233
+ // Visibility's `Reduction` splits the two apart again on the way out.
264
234
  const spec = {
265
- [DECLARED_ROWS_BRANCH_KEY]: declaredRowsBranch(collection, plan, ops),
235
+ [DECLARED_ROWS_BRANCH_KEY]: declaredBranch(collection, plan.declaredRows, plan.declaredIncludes, ops),
266
236
  ...(plan.reduce.kind === 'count'
267
237
  ? { [DECLARED_COUNT_BRANCH_KEY]: refined.count() }
268
238
  : combineSpec(refined, plan, plan.reduce.branches, ops)),
@@ -283,21 +253,6 @@ function combineSpec(refined, plan, branches, ops) {
283
253
  }
284
254
  return spec;
285
255
  }
286
- /**
287
- * The internal, caller-invisible rows branch a reduced-and-declared relation
288
- * rides alongside its own reduction (`IncludePlan.declaredRows`). Built off
289
- * the UNFILTERED `collection` rather than `refined` — it is scoped by the
290
- * Access Filter alone, never by the caller's own `where()` on the reduce,
291
- * because it stands in for an ordinary declared branch (full stored width,
292
- * one hop), not for the caller's own view of the relation.
293
- */
294
- function declaredRowsBranch(collection, plan, ops) {
295
- let branched = collection;
296
- for (const predicate of plan.declaredRows ?? []) {
297
- branched = branched.where((model) => lowerWhere(predicate, model, ops));
298
- }
299
- return branched;
300
- }
301
256
  /**
302
257
  * Apply the projection, the sort, the page and the next level to a relation's
303
258
  * rows. The projection and the nested includes come from the plan, which for a
@@ -307,7 +262,7 @@ function declaredRowsBranch(collection, plan, ops) {
307
262
  */
308
263
  function shape(collection, plan, rows, ops) {
309
264
  let shaped = collection;
310
- if (plan.projection.columns !== undefined) {
265
+ if (plan.projection.columns !== undefined && isProjectable(plan.projection.columns)) {
311
266
  shaped = shaped.select(...plan.projection.columns);
312
267
  }
313
268
  if (rows.orders.length > 0) {
@@ -339,7 +294,9 @@ function scope(binding, plan, ops, dispositions, refuse) {
339
294
  collection = collection.where((model) => lowerWhere(predicate, model, ops));
340
295
  }
341
296
  }
342
- if (applies('projection') && plan.projection.columns !== undefined) {
297
+ if (applies('projection') &&
298
+ plan.projection.columns !== undefined &&
299
+ isProjectable(plan.projection.columns)) {
343
300
  collection = collection.select(...plan.projection.columns);
344
301
  }
345
302
  if (applies('orders') && plan.orders.length > 0) {
@@ -354,8 +311,8 @@ function scope(binding, plan, ops, dispositions, refuse) {
354
311
  }
355
312
  if (applies('cursor') && plan.cursor !== undefined)
356
313
  collection = collection.cursor(plan.cursor);
357
- if (applies('includes')) {
358
- for (const include of plan.includes) {
314
+ if (applies('root')) {
315
+ for (const include of plan.root.includes) {
359
316
  collection = collection.include(include.relation, (child) => refine(child, include, ops));
360
317
  }
361
318
  }
@@ -365,519 +322,12 @@ function scope(binding, plan, ops, dispositions, refuse) {
365
322
  collection = collection.limit(plan.limit);
366
323
  return collection;
367
324
  }
368
- function isRow(value) {
369
- return typeof value === 'object' && value !== null && !Array.isArray(value);
370
- }
371
- /**
372
- * The rows one include plan contributed to a row, whichever shape the key
373
- * holds them in: a to-many's array, a to-one's single row, and a reduction's
374
- * envelope before {@link maskReductions} substitutes its rows back out. Both
375
- * foreign-key passes walk a row through here, so neither can be reading a
376
- * shape the other is not.
377
- */
378
- function relatedRowsOf(row, plan) {
379
- const value = row[plan.relation];
380
- if (Array.isArray(value))
381
- return value.filter(isRow);
382
- if (plan.reduce !== undefined)
383
- return combinedRows(value, rowsKeyOf(plan));
384
- return isRow(value) ? [value] : [];
385
- }
386
- /**
387
- * Narrow the foreign-key column of every included to-one to what the caller
388
- * may see.
389
- *
390
- * The foreign key is a second name for the related row's identity, so a
391
- * relation the Access Filter scoped away or Field Visibility stripped would
392
- * otherwise survive under it as the invisible row's id.
393
- *
394
- * The pass is driven by the contract's own foreign-key member
395
- * (`IncludePlan.foreignKey`) rather than by the shape of whatever is stored,
396
- * which makes it independent of `db: { foreignKey: { map } }`; and it runs
397
- * AFTER Field Visibility, which makes visibility authoritative over the
398
- * column. The value written is the one the caller may see: the related row's
399
- * id when the relation is visible, `null` when it is not — denied, scoped away
400
- * and stripped alike, which is what a to-one the caller may not see means
401
- * everywhere else (ADR-0058).
402
- *
403
- * A relation the caller's own `where()` refined is the exception: a `null`
404
- * there may be the caller's filter rather than access, so the column is left
405
- * for {@link narrowUnincludedForeignKeys}, which decides on access alone.
406
- */
407
- function applyForeignKeys(row, plans) {
408
- for (const plan of plans) {
409
- if (!plan.declared &&
410
- plan.arity === 'one' &&
411
- plan.foreignKey !== undefined &&
412
- plan.foreignKey in row) {
413
- const value = row[plan.relation];
414
- if (isRow(value))
415
- row[plan.foreignKey] = value.id;
416
- else if (!plan.callerRefined)
417
- row[plan.foreignKey] = null;
418
- }
419
- if (plan.includes.length === 0)
420
- continue;
421
- for (const related of relatedRowsOf(row, plan))
422
- applyForeignKeys(related, plan.includes);
423
- }
424
- }
425
- /** The `id IN (...)` predicate a batched existence check narrows its scan to. */
426
- function identityIn(model, listName, keys, column = 'id') {
427
- const expression = model[column]?.in?.(keys);
428
- if (expression === undefined)
429
- throw unqueryableKey(listName, column);
430
- return expression;
431
- }
432
- /**
433
- * Resolve the read-time context `foreignKeyOwningRelations` and
434
- * `resolveRelatedAccessPlan` need for one list, mirroring what `resolveInclude`
435
- * builds for a relation it actually resolves.
436
- */
437
- function relatedResolveContext(binding, listName, listConfig) {
438
- return {
439
- listName,
440
- listConfig,
441
- config: binding.config,
442
- session: binding.context.session,
443
- context: binding.context,
444
- checkFieldRead: false,
445
- applyRelationAccess: true,
446
- accessFilterPath: [],
447
- };
448
- }
449
- function assembleColumnFields(row, fieldConfigs) {
450
- const assembled = { ...row };
451
- for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
452
- if (!fieldConfig.assembleColumns || !fieldConfig.getColumnNames)
453
- continue;
454
- const columnNames = fieldConfig.getColumnNames(fieldName);
455
- if (!columnNames.some((name) => name in assembled))
456
- continue;
457
- const value = fieldConfig.assembleColumns(fieldName, assembled);
458
- for (const name of columnNames)
459
- delete assembled[name];
460
- assembled[fieldName] = value;
461
- }
462
- return assembled;
463
- }
464
- function callerIndependent(row, plans) {
465
- const refined = plans.filter((plan) => plan.callerRefined);
466
- if (refined.length === 0)
467
- return row;
468
- const copy = { ...row };
469
- for (const plan of refined)
470
- delete copy[plan.relation];
471
- return copy;
472
- }
473
- /**
474
- * Narrow the foreign-key column of every to-one relationship this read did
475
- * NOT include or declare — the gap {@link applyForeignKeys} cannot close,
476
- * because a relation nobody named has no {@link IncludePlan} entry for it to
477
- * walk (issue #1243).
478
- *
479
- * The column is on the row regardless of `include` (ADR-0043), so it needs
480
- * its own visibility decision: the owning relationship field's own `read`
481
- * rule, evaluated per row against the RAW row (before Field Visibility
482
- * strips anything else), and the related list's own `query` access — `true`
483
- * leaves the column as the plain pass-through value it already is, `false`
484
- * nulls it outright, and a filter is resolved with one batched existence
485
- * check per relation across the whole page, keyed off the column's own value
486
- * rather than a fetched related row (there is no fetched related row here to
487
- * key off).
488
- *
489
- * That existence check cannot tell a row the filter denies from a row deleted
490
- * between the two statements — both come back absent from it. This function
491
- * does not try to: absence nulls the column either way, so a vanished row
492
- * fails exactly as closed as a denied one rather than surviving under the id
493
- * this statement read before it vanished (issue #1386).
494
- *
495
- * Runs AFTER {@link applyForeignKeys}, on its output — mutated in place —
496
- * and recurses into every relation THAT pass over its results the same way
497
- * {@link applyForeignKeys} does, so a related list reached only through an
498
- * include gets its own un-included to-ones narrowed too.
499
- */
500
- /**
501
- * Pairs each shown related row with its raw twin. Field Visibility may drop
502
- * rows from a to-many (a synthetic back-relation's source `read` rule), so
503
- * once the lengths differ the pairing is by `id`, never by position.
504
- */
505
- function pairRelatedRows(shown, raw) {
506
- const pairs = [];
507
- if (shown.length === raw.length) {
508
- shown.forEach((related, index) => {
509
- const original = raw[index];
510
- if (isRow(related) && isRow(original))
511
- pairs.push([related, original]);
512
- });
513
- return pairs;
514
- }
515
- const rawById = new Map();
516
- for (const original of raw)
517
- if (isRow(original))
518
- rawById.set(String(original.id), original);
519
- for (const related of shown) {
520
- if (!isRow(related))
521
- continue;
522
- const original = rawById.get(String(related.id));
523
- if (original !== undefined)
524
- pairs.push([related, original]);
525
- }
526
- return pairs;
527
- }
528
- async function narrowUnincludedForeignKeys(binding, filteredRows, rawRows, listName, listConfig, resolvedIncludes) {
529
- if (binding.context._isSudo === true)
530
- return;
531
- const ctx = relatedResolveContext(binding, listName, listConfig);
532
- const alreadyIncluded = new Set(resolvedIncludes
533
- .filter((plan) => !plan.declared && !plan.callerRefined)
534
- .map((plan) => plan.relation));
535
- for (const owner of foreignKeyOwningRelations(ctx)) {
536
- if (alreadyIncluded.has(owner.relation))
537
- continue;
538
- if (!rawRows.some((row) => owner.foreignKey in row))
539
- continue;
540
- const related = getRelatedListConfig(owner.ref, binding.config);
541
- if (!related)
542
- continue;
543
- const access = await resolveRelatedAccessPlan({ listName: related.listName, listConfig: related.listConfig }, ctx);
544
- for (let i = 0; i < filteredRows.length; i++) {
545
- const raw = rawRows[i];
546
- if (!(owner.foreignKey in raw) || !(owner.foreignKey in filteredRows[i]))
547
- continue;
548
- const canReadField = await checkFieldAccess(owner.fieldConfig.access, 'read', {
549
- session: binding.context.session,
550
- context: binding.context,
551
- item: assembleColumnFields(callerIndependent(raw, resolvedIncludes), listConfig.fields),
552
- });
553
- if (!canReadField || access.kind === 'false')
554
- filteredRows[i][owner.foreignKey] = null;
555
- }
556
- if (access.kind === 'true' || access.kind === 'false')
557
- continue;
558
- const idsByRow = rawRows.map((row) => row[owner.foreignKey]);
559
- const idMap = new Map();
560
- for (let i = 0; i < filteredRows.length; i++) {
561
- if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
562
- continue;
563
- const value = idsByRow[i];
564
- if (value !== null && value !== undefined)
565
- idMap.set(String(value), value);
566
- }
567
- if (idMap.size === 0)
568
- continue;
569
- const ops = await whereCombinators();
570
- const visible = await engineRead(binding, () => collectionFor(binding.ormHandle, related.listName)
571
- .where((model) => lowerWhere(access, model, ops))
572
- .where((model) => identityIn(model, related.listName, [...idMap.values()]))
573
- .select('id')
574
- .all());
575
- const visibleIds = new Set(visible.map((row) => String(row.id)));
576
- for (let i = 0; i < filteredRows.length; i++) {
577
- if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
578
- continue;
579
- const value = idsByRow[i];
580
- if (value === null || value === undefined)
581
- continue;
582
- if (!visibleIds.has(String(value)))
583
- filteredRows[i][owner.foreignKey] = null;
584
- }
585
- }
586
- for (const plan of resolvedIncludes) {
587
- const relatedListConfig = binding.config.lists[plan.relatedListName];
588
- if (relatedListConfig === undefined)
589
- continue;
590
- const nestedFiltered = [];
591
- const nestedRaw = [];
592
- for (let i = 0; i < filteredRows.length; i++) {
593
- const filteredValue = filteredRows[i][plan.relation];
594
- const rawValue = rawRows[i][plan.relation];
595
- if (Array.isArray(filteredValue) && Array.isArray(rawValue)) {
596
- for (const [related, original] of pairRelatedRows(filteredValue, rawValue)) {
597
- nestedFiltered.push(related);
598
- nestedRaw.push(original);
599
- }
600
- }
601
- else if (plan.reduce !== undefined) {
602
- const key = rowsKeyOf(plan);
603
- for (const [related, original] of pairRelatedRows(combinedRows(filteredValue, key), combinedRows(rawValue, key))) {
604
- nestedFiltered.push(related);
605
- nestedRaw.push(original);
606
- }
607
- }
608
- else if (isRow(filteredValue) && isRow(rawValue)) {
609
- nestedFiltered.push(filteredValue);
610
- nestedRaw.push(rawValue);
611
- }
612
- }
613
- if (nestedFiltered.length === 0)
614
- continue;
615
- await narrowUnincludedForeignKeys(binding, nestedFiltered, nestedRaw, plan.relatedListName, relatedListConfig, plan.includes);
616
- }
617
- }
618
- function reduces(plans) {
619
- return plans.some((plan) => plan.reduce !== undefined || reduces(plan.includes));
620
- }
621
- /**
622
- * Stand an empty to-many in for every relation the read reduced.
623
- *
624
- * Field Visibility decides whether a relation key survives by evaluating the
625
- * relationship field's own `read` rule, and it reaches that decision on the
626
- * path it takes for a relation's ROWS. A count is not rows, so handing it the
627
- * reduced value would take it down neither path and the key would fall out of
628
- * the result whether or not the session may read it. Substituting `[]` puts
629
- * the decision back on the one path that makes it — Field Visibility runs the
630
- * same rule over the same parent row — and {@link restoreReductions} then
631
- * writes the count back under exactly the keys that survived. A reduction is
632
- * to-many only ({@link ReducedToOneIncludeError}), so `[]` is the shape the
633
- * relation would otherwise have had.
634
- *
635
- * A `combine` that named the relation's rows under one key stands in that
636
- * array instead of `[]`, so those rows reach Field Visibility exactly as an
637
- * unreduced relation's do and {@link restoreReductions} writes them back
638
- * beside the counts.
639
- *
640
- * A relation that is both reduced and a live declared dependency of a
641
- * computed field returned alongside it (#1357) is the third case: its
642
- * `IncludePlan.declaredRows` fetched real rows purely for that declaration,
643
- * under {@link DECLARED_ROWS_BRANCH_KEY}, and those are what stand in here —
644
- * never `[]` — so the declaring hook's `needs` sees them via its own `item`,
645
- * and a relationship `read` rule that inspects `item.<reducedRelation>`
646
- * decides against real rows rather than an empty array. Field Visibility
647
- * still must not compute over them as though they were an ordinary relation
648
- * (ADR-0051): `ReducedDeclaredKeys` (`select.ts`) is what tells it to treat
649
- * this key as a raw pass-through, and {@link restoreReductions} overwrites it
650
- * with the reduction's own value regardless of what Field Visibility computed
651
- * for it.
652
- *
653
- * Known limits: a relation reduced with NEITHER a rows branch of its own NOR
654
- * a live declared dependency still stands in `[]`, so a relationship `read`
655
- * rule inspecting `item.<reducedRelation>` without declaring it via `needs`
656
- * decides on an empty array rather than on what the count counted — the
657
- * common `item.posts.length > 0` shape fails closed there, and an inverted
658
- * `item.posts.length === 0` fails open. A rule that reaches into a relation
659
- * must declare it, the same requirement `needs` already states for a computed
660
- * field's `resolveOutput`.
661
- */
662
- function maskReductions(row, plans) {
663
- if (!reduces(plans))
664
- return row;
665
- const masked = { ...row };
666
- for (const plan of plans) {
667
- if (plan.reduce !== undefined) {
668
- const rows = plan.declaredRows !== undefined && !hasRowsBranch(plan)
669
- ? declaredRowsOf(row[plan.relation])
670
- : combinedRows(row[plan.relation], rowsKeyOf(plan));
671
- masked[plan.relation] = reduces(plan.includes)
672
- ? rows.map((related) => maskReductions(related, plan.includes))
673
- : rows;
674
- continue;
675
- }
676
- if (!reduces(plan.includes))
677
- continue;
678
- const value = masked[plan.relation];
679
- if (Array.isArray(value)) {
680
- masked[plan.relation] = value.map((related) => isRow(related) ? maskReductions(related, plan.includes) : related);
681
- }
682
- else if (isRow(value)) {
683
- masked[plan.relation] = maskReductions(value, plan.includes);
684
- }
685
- }
686
- return masked;
687
- }
688
- /** The key a `combine` returned the relation's own rows under, if it named one. */
689
- function rowsKeyOf(plan) {
690
- if (plan.reduce?.kind !== 'combine')
691
- return undefined;
692
- for (const branch of plan.reduce.branches) {
693
- if (branch.kind === 'rows')
694
- return branch.key;
695
- }
696
- return undefined;
697
- }
698
- /** The rows an internal declared-dependency branch fetched (`IncludePlan.declaredRows`). */
699
- function declaredRowsOf(value) {
700
- return combinedRows(value, DECLARED_ROWS_BRANCH_KEY);
701
- }
702
- /** The rows a `combine` returned under {@link rowsKeyOf}, or `[]` for a bare count. */
703
- function combinedRows(value, key) {
704
- if (key === undefined || !isRow(value))
705
- return [];
706
- const rows = value[key];
707
- return Array.isArray(rows) ? rows.filter(isRow) : [];
708
- }
709
- /**
710
- * The reduction's own value, once {@link declaredRowsBranch}'s internal
711
- * branch is spliced back out: a bare count unwrapped from
712
- * {@link DECLARED_COUNT_BRANCH_KEY}, or a caller's own combine object with
713
- * {@link DECLARED_ROWS_BRANCH_KEY} removed. The caller never sees either
714
- * reserved key.
715
- */
716
- function publicReductionValue(raw, reduce) {
717
- if (!isRow(raw))
718
- return raw;
719
- if (reduce.kind === 'count')
720
- return raw[DECLARED_COUNT_BRANCH_KEY] ?? 0;
721
- const publicValue = { ...raw };
722
- delete publicValue[DECLARED_ROWS_BRANCH_KEY];
723
- return publicValue;
724
- }
725
- /** Write each reduced relation back, under the keys Field Visibility kept. */
726
- function restoreReductions(shown, source, plans) {
727
- for (const plan of plans) {
728
- if (plan.reduce !== undefined) {
729
- if (!(plan.relation in shown))
730
- continue;
731
- const raw = source[plan.relation];
732
- if (plan.declaredRows !== undefined && !hasRowsBranch(plan)) {
733
- shown[plan.relation] = publicReductionValue(raw, plan.reduce);
734
- continue;
735
- }
736
- const key = rowsKeyOf(plan);
737
- if (key === undefined || !isRow(raw)) {
738
- shown[plan.relation] = raw;
739
- continue;
740
- }
741
- const kept = shown[plan.relation];
742
- if (reduces(plan.includes) && Array.isArray(kept)) {
743
- const rows = combinedRows(raw, key);
744
- for (const [related, original] of pairRelatedRows(kept, rows)) {
745
- restoreReductions(related, original, plan.includes);
746
- }
747
- }
748
- const base = publicReductionValue(raw, plan.reduce);
749
- shown[plan.relation] = { ...(isRow(base) ? base : raw), [key]: kept };
750
- continue;
751
- }
752
- if (!reduces(plan.includes))
753
- continue;
754
- const kept = shown[plan.relation];
755
- const raw = source[plan.relation];
756
- if (Array.isArray(kept) && Array.isArray(raw)) {
757
- for (const [related, original] of pairRelatedRows(kept, raw)) {
758
- restoreReductions(related, original, plan.includes);
759
- }
760
- }
761
- else if (isRow(kept) && isRow(raw)) {
762
- restoreReductions(kept, raw, plan.includes);
763
- }
764
- }
765
- }
766
- async function unshapedDeclaredRows(binding, listName, rows, plans) {
767
- const byRelation = new Map();
768
- for (const plan of plans) {
769
- if (plan.reduce === undefined || plan.declaredRows === undefined || !hasRowsBranch(plan)) {
770
- continue;
771
- }
772
- const values = new Map();
773
- for (const row of rows)
774
- values.set(String(row['id']), declaredRowsOf(row[plan.relation]));
775
- byRelation.set(plan.relation, values);
776
- }
777
- const fetched = plans.filter((plan) => plan.declaredFetch !== undefined);
778
- const ids = rows
779
- .map((row) => row['id'])
780
- .filter((id) => typeof id === 'string' || typeof id === 'number');
781
- if (fetched.length === 0 || ids.length === 0)
782
- return byRelation;
783
- const ops = await whereCombinators();
784
- for (const plan of fetched) {
785
- const owners = await withOrigin('engine', () => collectionFor(binding.ormHandle, listName)
786
- .where((model) => identityIn(model, listName, ids))
787
- .select('id')
788
- .include(plan.relation, (child) => {
789
- let scoped = child;
790
- for (const predicate of plan.declaredFetch ?? []) {
791
- scoped = scoped.where((model) => lowerWhere(predicate, model, ops));
792
- }
793
- return scoped;
794
- })
795
- .all());
796
- const values = new Map();
797
- for (const owner of owners)
798
- values.set(String(owner['id']), owner[plan.relation]);
799
- byRelation.set(plan.relation, values);
800
- }
801
- return byRelation;
802
- }
803
- async function ruleViewTreeFor(binding, listName, rows, plans) {
804
- const unshaped = await unshapedDeclaredRows(binding, listName, rows, plans);
805
- const nested = {};
806
- for (const plan of plans) {
807
- if (plan.declared || plan.includes.length === 0)
808
- continue;
809
- const rowsKey = rowsKeyOf(plan);
810
- if (plan.reduce !== undefined && rowsKey === undefined)
811
- continue;
812
- const children = [];
813
- for (const row of rows) {
814
- const value = row[plan.relation];
815
- if (plan.reduce !== undefined)
816
- children.push(...combinedRows(value, rowsKey));
817
- else if (Array.isArray(value))
818
- children.push(...value.filter(isRow));
819
- else if (isRow(value))
820
- children.push(value);
821
- }
822
- if (children.length === 0)
823
- continue;
824
- nested[plan.relation] = await ruleViewTreeFor(binding, plan.relatedListName, children, plan.includes);
825
- }
826
- return { viewFor: (row) => ruleViewFor(row, plans, unshaped), nested };
827
- }
828
- function ruleViewFor(row, plans, unshaped) {
829
- const removed = new Set();
830
- const declared = {};
831
- for (const plan of plans) {
832
- if (plan.declared)
833
- continue;
834
- if (plan.reduce !== undefined) {
835
- const stored = unshaped.get(plan.relation);
836
- if (plan.declaredRows !== undefined && hasRowsBranch(plan) && stored !== undefined) {
837
- declared[plan.relation] = stored.get(String(row['id']));
838
- }
839
- continue;
840
- }
841
- if (!plan.callerShaped)
842
- continue;
843
- const values = unshaped.get(plan.relation);
844
- if (plan.declaredFetch === undefined || values === undefined) {
845
- removed.add(plan.relation);
846
- continue;
847
- }
848
- declared[plan.relation] = values.get(String(row['id']));
849
- }
850
- return { removed, declared };
851
- }
852
- /**
853
- * What every terminal returns rows through — the one place a row this engine
854
- * read becomes a row a caller may see, and the only place any foreign-key
855
- * pass runs. `all()`, `first()`, the `forUpdate()` lane and `nearest()` all
856
- * come through here, so none of them can drift apart on what a column reads
857
- * as, and neither can a hook or a `read` rule.
858
- *
859
- * The batch is what lets {@link narrowUnincludedForeignKeys} resolve a
860
- * to-one this read never named at all for a whole page in one query per
861
- * relation rather than one per row.
862
- */
863
- async function visibleRows(binding, rows, plan) {
864
- const { listConfig, context, config, listName } = binding;
865
- const ruleViews = await ruleViewTreeFor(binding, listName, rows, plan.includes);
866
- const results = await Promise.all(rows.map(async (row) => {
867
- const filtered = await filterReadableFields(maskReductions(row, plan.includes), listConfig.fields, { session: context.session, context }, config, 0, listName, plan.additions, plan.selection, plan.reducedDeclared, ruleViews);
868
- applyForeignKeys(filtered, plan.includes);
869
- restoreReductions(filtered, row, plan.includes);
870
- return filtered;
871
- }));
872
- await narrowUnincludedForeignKeys(binding, results, rows, listName, listConfig, plan.includes);
873
- return results;
874
- }
875
325
  /**
876
326
  * Whether the session's operation-level `query` access reaches the row with
877
327
  * this id, resolved as a read would: a filter rule is run as a scoped read of
878
328
  * the row's identity on the binding's own handle.
879
329
  */
880
- export async function writtenRowQueryable(binding, id) {
330
+ async function writtenRowQueryable(binding, id) {
881
331
  if (binding.context._isSudo === true)
882
332
  return true;
883
333
  if (typeof id !== 'string' && typeof id !== 'number')
@@ -898,35 +348,36 @@ export async function writtenRowQueryable(binding, id) {
898
348
  const collection = scope(binding, plan, await whereCombinators(), FIRST_DISPOSITIONS, unreachableRefusal);
899
349
  return (await withOrigin('engine', () => collection.first())) !== null;
900
350
  }
901
- /**
902
- * What a write hands back: the row Field Visibility leaves, then the
903
- * foreign-key pass a read of the same row would give it, so `create()` and
904
- * `update()` never return an id `first()` hides. A row the session cannot
905
- * `query` comes back as the list's system fields alone.
906
- */
907
- export async function visibleWrittenRow(binding, row, queryable) {
908
- const { listConfig, context, config, listName } = binding;
909
- if (!queryable) {
910
- const kept = {};
911
- for (const key of getListDependencies(config, listName).systemFields) {
912
- if (key in row)
913
- kept[key] = row[key];
914
- }
915
- return kept;
351
+ /** Whether a computed field on this list declares a relation a write's return must fetch. */
352
+ export function writeReturnReadsRelations(binding) {
353
+ return declaredRelations(binding).size > 0;
354
+ }
355
+ /** Runs on the binding's own handle: the write's transaction, never a second connection. */
356
+ export async function readWrittenRow(binding, id) {
357
+ if ((typeof id !== 'string' && typeof id !== 'number') || !writeReturnReadsRelations(binding)) {
358
+ return (await writtenRowQueryable(binding, id))
359
+ ? { kind: 'written', root: planLevel(binding, undefined, []) }
360
+ : { kind: 'hidden' };
916
361
  }
917
- const filtered = await filterReadableFields(row, listConfig.fields, { session: context.session, context: { ...context, _isSudo: context._isSudo } }, config, 0, listName);
918
- await narrowUnincludedForeignKeys(binding, [filtered], [row], listName, listConfig, []);
919
- return filtered;
362
+ const plan = await resolvePlan(binding, {
363
+ predicates: [{ id: { equals: id } }],
364
+ orders: [],
365
+ includes: [],
366
+ distincts: [],
367
+ lock: false,
368
+ });
369
+ if (plan === null)
370
+ return { kind: 'hidden' };
371
+ const collection = scope(binding, plan, await whereCombinators(), FIRST_DISPOSITIONS, unreachableRefusal);
372
+ const row = await engineRead(binding, () => collection.first());
373
+ return row === null ? { kind: 'hidden' } : { kind: 'read', row, root: plan.root };
920
374
  }
921
375
  /** `all()` is the terminal every plan member was designed for. */
922
376
  const ALL_DISPOSITIONS = {
923
377
  predicates: 'applied',
924
378
  orders: 'applied',
925
- includes: 'applied',
379
+ root: 'applied',
926
380
  projection: 'applied',
927
- selection: 'applied',
928
- additions: 'applied',
929
- reducedDeclared: 'applied',
930
381
  limit: 'applied',
931
382
  offset: 'applied',
932
383
  distinct: 'applied',
@@ -992,21 +443,12 @@ async function locked(binding, taken, plan, rows) {
992
443
  }
993
444
  if (keys.length === 0)
994
445
  return [];
995
- const held = await engineRead(binding, () => taken.lane.lock(listName, taken.identity, keys));
446
+ const held = await engineRead(binding, () => taken.lane.lock(listName, taken.identity, keys, 'forUpdate'));
996
447
  if (held.length === 0)
997
448
  return [];
998
449
  const refreshed = scope(binding, { ...plan, limit: undefined, offset: undefined }, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal).where((model) => identityIn(model, listName, held, column));
999
450
  return await engineRead(binding, () => refreshed.all());
1000
451
  }
1001
- async function engineRead(binding, run) {
1002
- try {
1003
- return await withOrigin('engine', run);
1004
- }
1005
- catch (error) {
1006
- binding.context._transactionOwner?.poison(error);
1007
- throw error;
1008
- }
1009
- }
1010
452
  async function runAll(binding, state) {
1011
453
  const plan = await resolvePlan(binding, state);
1012
454
  if (plan === null)
@@ -1015,7 +457,7 @@ async function runAll(binding, state) {
1015
457
  const collection = scope(binding, plan, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal);
1016
458
  const read = await engineRead(binding, () => collection.all());
1017
459
  const rows = taken === undefined ? read : await locked(binding, taken, plan, read);
1018
- return await visibleRows(binding, rows, plan);
460
+ return await visibleRows(binding, rows, plan.root);
1019
461
  }
1020
462
  async function runFirst(binding, state) {
1021
463
  const plan = await resolvePlan(binding, state);
@@ -1029,7 +471,7 @@ async function runFirst(binding, state) {
1029
471
  const rows = taken === undefined ? [read] : await locked(binding, taken, plan, [read]);
1030
472
  if (rows.length === 0)
1031
473
  return null;
1032
- return (await visibleRows(binding, rows, plan))[0];
474
+ return (await visibleRows(binding, rows, plan.root))[0];
1033
475
  }
1034
476
  function countOf(result, listName, key) {
1035
477
  const value = result[key];
@@ -1056,11 +498,8 @@ function countOf(result, listName, key) {
1056
498
  const AGGREGATE_DISPOSITIONS = {
1057
499
  predicates: 'applied',
1058
500
  orders: 'inapplicable',
1059
- includes: 'inapplicable',
501
+ root: 'inapplicable',
1060
502
  projection: 'inapplicable',
1061
- selection: 'inapplicable',
1062
- additions: 'inapplicable',
1063
- reducedDeclared: 'inapplicable',
1064
503
  limit: 'refused',
1065
504
  offset: 'refused',
1066
505
  distinct: 'refused',
@@ -1123,11 +562,8 @@ function present(near) {
1123
562
  const NEAREST_DISPOSITIONS = {
1124
563
  predicates: 'applied',
1125
564
  orders: 'inapplicable',
1126
- includes: 'applied',
565
+ root: 'applied',
1127
566
  projection: 'applied',
1128
- selection: 'applied',
1129
- additions: 'applied',
1130
- reducedDeclared: 'applied',
1131
567
  limit: 'inapplicable',
1132
568
  offset: 'refused',
1133
569
  distinct: 'refused',
@@ -1191,7 +627,7 @@ async function runNearest(binding, state, field, vector, options) {
1191
627
  }
1192
628
  const rows = await engineRead(binding, () => collection.limit(near.limit).all());
1193
629
  const scores = rows.map((row) => score(near, row));
1194
- const items = await visibleRows(binding, rows, plan);
630
+ const items = await visibleRows(binding, rows, plan.root);
1195
631
  return items.map((item, index) => ({ item, score: scores[index] }));
1196
632
  }
1197
633
  function score(near, row) {
@@ -1227,6 +663,44 @@ function query(binding, state) {
1227
663
  aggregate: (build) => runAggregate(binding, state, build),
1228
664
  };
1229
665
  }
666
+ async function canOrderBy(binding, ctx, key) {
667
+ try {
668
+ const orders = await resolveOrderBy([{ [key]: 'asc' }], ctx);
669
+ collectionFor(binding.ormHandle, binding.listName).orderBy(orders.map((order) => (model) => lowerOrder(order, model)));
670
+ return true;
671
+ }
672
+ catch {
673
+ return false;
674
+ }
675
+ }
676
+ /**
677
+ * Which of `keys` name a column of `listKey` a read can order by at all, asked
678
+ * of the resolution and lowering `orderBy` uses with no access rule consulted.
679
+ * Whether this session may order by them is the caller's question.
680
+ */
681
+ export async function columnOrderedKeys(context, config, listKey, keys) {
682
+ const binding = readBindingFor(context, config, listKey);
683
+ if (binding === undefined)
684
+ return new Set();
685
+ return await orderableAmong(binding, false, keys);
686
+ }
687
+ function readBindingFor(context, config, listKey) {
688
+ if (!Object.hasOwn(config.lists, listKey))
689
+ return undefined;
690
+ return {
691
+ listName: listKey,
692
+ listConfig: config.lists[listKey],
693
+ ormHandle: context.ormHandle,
694
+ context,
695
+ config,
696
+ };
697
+ }
698
+ async function orderableAmong(binding, secured, keys) {
699
+ const ctx = resolveContext(binding, secured);
700
+ const unique = [...new Set(keys)];
701
+ const answers = await Promise.all(unique.map((key) => canOrderBy(binding, ctx, key)));
702
+ return new Set(unique.filter((_key, index) => answers[index]));
703
+ }
1230
704
  /**
1231
705
  * The read members of one list's secured surface, bound to `ormHandle` and
1232
706
  * `context`. The collection is held by the closure and never handed out: no