@opensaas/stack-core 0.44.0-alpha.6 → 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 (352) hide show
  1. package/CHANGELOG.md +655 -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 +21 -19
  24. package/dist/access/field-access.d.ts.map +1 -1
  25. package/dist/access/field-access.js +100 -122
  26. package/dist/access/field-access.js.map +1 -1
  27. package/dist/access/index.d.ts +3 -6
  28. package/dist/access/index.d.ts.map +1 -1
  29. package/dist/access/index.js +3 -6
  30. package/dist/access/index.js.map +1 -1
  31. package/dist/access/rule-item.d.ts +7 -7
  32. package/dist/access/rule-item.d.ts.map +1 -1
  33. package/dist/access/rule-item.js +38 -38
  34. package/dist/access/rule-item.js.map +1 -1
  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 +55 -6
  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/index.d.ts +1 -1
  58. package/dist/config/index.d.ts.map +1 -1
  59. package/dist/config/index.js +3 -2
  60. package/dist/config/index.js.map +1 -1
  61. package/dist/config/mcp-default-tools.d.ts +3 -0
  62. package/dist/config/mcp-default-tools.d.ts.map +1 -0
  63. package/dist/config/mcp-default-tools.js +14 -0
  64. package/dist/config/mcp-default-tools.js.map +1 -0
  65. package/dist/config/mcp-tool-names.d.ts +2 -0
  66. package/dist/config/mcp-tool-names.d.ts.map +1 -1
  67. package/dist/config/mcp-tool-names.js +17 -5
  68. package/dist/config/mcp-tool-names.js.map +1 -1
  69. package/dist/config/nav-count.d.ts +8 -16
  70. package/dist/config/nav-count.d.ts.map +1 -1
  71. package/dist/config/nav-count.js +20 -32
  72. package/dist/config/nav-count.js.map +1 -1
  73. package/dist/config/plugin-engine.d.ts.map +1 -1
  74. package/dist/config/plugin-engine.js +6 -0
  75. package/dist/config/plugin-engine.js.map +1 -1
  76. package/dist/config/types.d.ts +141 -28
  77. package/dist/config/types.d.ts.map +1 -1
  78. package/dist/context/engine-context.d.ts +2 -0
  79. package/dist/context/engine-context.d.ts.map +1 -1
  80. package/dist/context/engine-context.js +8 -0
  81. package/dist/context/engine-context.js.map +1 -1
  82. package/dist/context/hook-pipeline.d.ts +2 -1
  83. package/dist/context/hook-pipeline.d.ts.map +1 -1
  84. package/dist/context/hook-pipeline.js +4 -2
  85. package/dist/context/hook-pipeline.js.map +1 -1
  86. package/dist/context/index.d.ts +5 -1
  87. package/dist/context/index.d.ts.map +1 -1
  88. package/dist/context/index.js +156 -84
  89. package/dist/context/index.js.map +1 -1
  90. package/dist/context/junction.d.ts +28 -21
  91. package/dist/context/junction.d.ts.map +1 -1
  92. package/dist/context/junction.js +74 -69
  93. package/dist/context/junction.js.map +1 -1
  94. package/dist/context/plugin-field-write.d.ts.map +1 -1
  95. package/dist/context/plugin-field-write.js +19 -25
  96. package/dist/context/plugin-field-write.js.map +1 -1
  97. package/dist/context/process-lifetime.d.ts +8 -0
  98. package/dist/context/process-lifetime.d.ts.map +1 -0
  99. package/dist/context/process-lifetime.js +14 -0
  100. package/dist/context/process-lifetime.js.map +1 -0
  101. package/dist/context/relationship-input.d.ts +2 -15
  102. package/dist/context/relationship-input.d.ts.map +1 -1
  103. package/dist/context/relationship-input.js +44 -85
  104. package/dist/context/relationship-input.js.map +1 -1
  105. package/dist/context/transaction-boundary.d.ts +3 -2
  106. package/dist/context/transaction-boundary.d.ts.map +1 -1
  107. package/dist/context/transaction-boundary.js +17 -2
  108. package/dist/context/transaction-boundary.js.map +1 -1
  109. package/dist/context/write-pipeline.d.ts +5 -19
  110. package/dist/context/write-pipeline.d.ts.map +1 -1
  111. package/dist/context/write-pipeline.js +40 -98
  112. package/dist/context/write-pipeline.js.map +1 -1
  113. package/dist/context/write-target.d.ts +52 -0
  114. package/dist/context/write-target.d.ts.map +1 -0
  115. package/dist/context/write-target.js +118 -0
  116. package/dist/context/write-target.js.map +1 -0
  117. package/dist/contract/dependencies.d.ts +24 -10
  118. package/dist/contract/dependencies.d.ts.map +1 -1
  119. package/dist/contract/dependencies.js +120 -80
  120. package/dist/contract/dependencies.js.map +1 -1
  121. package/dist/contract/derive.d.ts +6 -11
  122. package/dist/contract/derive.d.ts.map +1 -1
  123. package/dist/contract/derive.js +256 -211
  124. package/dist/contract/derive.js.map +1 -1
  125. package/dist/contract/id-boundary.js +1 -1
  126. package/dist/contract/id-boundary.js.map +1 -1
  127. package/dist/contract/strategy.d.ts +13 -0
  128. package/dist/contract/strategy.d.ts.map +1 -0
  129. package/dist/contract/strategy.js +38 -0
  130. package/dist/contract/strategy.js.map +1 -0
  131. package/dist/db/client-alias.d.ts +12 -0
  132. package/dist/db/client-alias.d.ts.map +1 -0
  133. package/dist/db/client-alias.js +8 -0
  134. package/dist/db/client-alias.js.map +1 -0
  135. package/dist/extend.d.ts +1 -1
  136. package/dist/extend.d.ts.map +1 -1
  137. package/dist/extend.js.map +1 -1
  138. package/dist/fields/index.d.ts +0 -3
  139. package/dist/fields/index.d.ts.map +1 -1
  140. package/dist/fields/index.js +25 -10
  141. package/dist/fields/index.js.map +1 -1
  142. package/dist/filter/collect.d.ts +28 -39
  143. package/dist/filter/collect.d.ts.map +1 -1
  144. package/dist/filter/collect.js +46 -68
  145. package/dist/filter/collect.js.map +1 -1
  146. package/dist/filter/index.d.ts +1 -1
  147. package/dist/filter/index.d.ts.map +1 -1
  148. package/dist/filter/index.js +1 -1
  149. package/dist/filter/index.js.map +1 -1
  150. package/dist/filter/map.d.ts +4 -0
  151. package/dist/filter/map.d.ts.map +1 -1
  152. package/dist/filter/map.js +36 -3
  153. package/dist/filter/map.js.map +1 -1
  154. package/dist/hooks/index.d.ts +4 -1
  155. package/dist/hooks/index.d.ts.map +1 -1
  156. package/dist/hooks/index.js +34 -5
  157. package/dist/hooks/index.js.map +1 -1
  158. package/dist/index.d.ts +8 -7
  159. package/dist/index.d.ts.map +1 -1
  160. package/dist/index.js +5 -5
  161. package/dist/index.js.map +1 -1
  162. package/dist/internal.d.ts +5 -2
  163. package/dist/internal.d.ts.map +1 -1
  164. package/dist/internal.js +4 -5
  165. package/dist/internal.js.map +1 -1
  166. package/dist/mcp/advertise.d.ts +5 -13
  167. package/dist/mcp/advertise.d.ts.map +1 -1
  168. package/dist/mcp/advertise.js +7 -22
  169. package/dist/mcp/advertise.js.map +1 -1
  170. package/dist/mcp/arguments.d.ts.map +1 -1
  171. package/dist/mcp/arguments.js +8 -11
  172. package/dist/mcp/arguments.js.map +1 -1
  173. package/dist/mcp/field-schema.d.ts +4 -15
  174. package/dist/mcp/field-schema.d.ts.map +1 -1
  175. package/dist/mcp/field-schema.js +41 -71
  176. package/dist/mcp/field-schema.js.map +1 -1
  177. package/dist/mcp/handler.d.ts +111 -18
  178. package/dist/mcp/handler.d.ts.map +1 -1
  179. package/dist/mcp/handler.js +168 -73
  180. package/dist/mcp/handler.js.map +1 -1
  181. package/dist/mcp/index.d.ts +2 -1
  182. package/dist/mcp/index.d.ts.map +1 -1
  183. package/dist/mcp/index.js +1 -1
  184. package/dist/mcp/index.js.map +1 -1
  185. package/dist/mcp/procedure-tools.d.ts +63 -0
  186. package/dist/mcp/procedure-tools.d.ts.map +1 -0
  187. package/dist/mcp/procedure-tools.js +139 -0
  188. package/dist/mcp/procedure-tools.js.map +1 -0
  189. package/dist/mcp/projection.d.ts.map +1 -1
  190. package/dist/mcp/projection.js +28 -36
  191. package/dist/mcp/projection.js.map +1 -1
  192. package/dist/mcp/reachability.d.ts +5 -1
  193. package/dist/mcp/reachability.d.ts.map +1 -1
  194. package/dist/mcp/reachability.js +24 -5
  195. package/dist/mcp/reachability.js.map +1 -1
  196. package/dist/mcp/relation-member.d.ts +15 -0
  197. package/dist/mcp/relation-member.d.ts.map +1 -0
  198. package/dist/mcp/relation-member.js +50 -0
  199. package/dist/mcp/relation-member.js.map +1 -0
  200. package/dist/mcp/types.d.ts +12 -0
  201. package/dist/mcp/types.d.ts.map +1 -1
  202. package/dist/mcp/where-id-boundary.d.ts.map +1 -1
  203. package/dist/mcp/where-id-boundary.js +12 -24
  204. package/dist/mcp/where-id-boundary.js.map +1 -1
  205. package/dist/mcp/where-reachability.d.ts +2 -0
  206. package/dist/mcp/where-reachability.d.ts.map +1 -1
  207. package/dist/mcp/where-reachability.js +39 -23
  208. package/dist/mcp/where-reachability.js.map +1 -1
  209. package/dist/members/claims.d.ts +71 -0
  210. package/dist/members/claims.d.ts.map +1 -0
  211. package/dist/members/claims.js +199 -0
  212. package/dist/members/claims.js.map +1 -0
  213. package/dist/members/list-members.d.ts +87 -0
  214. package/dist/members/list-members.d.ts.map +1 -0
  215. package/dist/members/list-members.js +250 -0
  216. package/dist/members/list-members.js.map +1 -0
  217. package/dist/procedures/client.d.ts +163 -0
  218. package/dist/procedures/client.d.ts.map +1 -0
  219. package/dist/procedures/client.js +152 -0
  220. package/dist/procedures/client.js.map +1 -0
  221. package/dist/procedures/codes.d.ts +23 -0
  222. package/dist/procedures/codes.d.ts.map +1 -0
  223. package/dist/procedures/codes.js +46 -0
  224. package/dist/procedures/codes.js.map +1 -0
  225. package/dist/procedures/context-face.d.ts +4 -0
  226. package/dist/procedures/context-face.d.ts.map +1 -0
  227. package/dist/procedures/context-face.js +31 -0
  228. package/dist/procedures/context-face.js.map +1 -0
  229. package/dist/procedures/failures.d.ts +31 -0
  230. package/dist/procedures/failures.d.ts.map +1 -0
  231. package/dist/procedures/failures.js +51 -0
  232. package/dist/procedures/failures.js.map +1 -0
  233. package/dist/procedures/http.d.ts +114 -0
  234. package/dist/procedures/http.d.ts.map +1 -0
  235. package/dist/procedures/http.js +224 -0
  236. package/dist/procedures/http.js.map +1 -0
  237. package/dist/procedures/index.d.ts +7 -0
  238. package/dist/procedures/index.d.ts.map +1 -0
  239. package/dist/procedures/index.js +11 -0
  240. package/dist/procedures/index.js.map +1 -0
  241. package/dist/procedures/procedure.d.ts +204 -0
  242. package/dist/procedures/procedure.d.ts.map +1 -0
  243. package/dist/procedures/procedure.js +123 -0
  244. package/dist/procedures/procedure.js.map +1 -0
  245. package/dist/procedures/result.d.ts +42 -0
  246. package/dist/procedures/result.d.ts.map +1 -0
  247. package/dist/procedures/result.js +51 -0
  248. package/dist/procedures/result.js.map +1 -0
  249. package/dist/procedures/run.d.ts +36 -0
  250. package/dist/procedures/run.d.ts.map +1 -0
  251. package/dist/procedures/run.js +76 -0
  252. package/dist/procedures/run.js.map +1 -0
  253. package/dist/procedures/server.d.ts +131 -0
  254. package/dist/procedures/server.d.ts.map +1 -0
  255. package/dist/procedures/server.js +115 -0
  256. package/dist/procedures/server.js.map +1 -0
  257. package/dist/procedures/validate.d.ts +60 -0
  258. package/dist/procedures/validate.d.ts.map +1 -0
  259. package/dist/procedures/validate.js +254 -0
  260. package/dist/procedures/validate.js.map +1 -0
  261. package/dist/procedures/web-session.d.ts +12 -0
  262. package/dist/procedures/web-session.d.ts.map +1 -0
  263. package/dist/procedures/web-session.js +35 -0
  264. package/dist/procedures/web-session.js.map +1 -0
  265. package/dist/query/relationship-options.d.ts +2 -0
  266. package/dist/query/relationship-options.d.ts.map +1 -1
  267. package/dist/query/relationship-options.js +3 -2
  268. package/dist/query/relationship-options.js.map +1 -1
  269. package/dist/secured/binding.d.ts +84 -0
  270. package/dist/secured/binding.d.ts.map +1 -0
  271. package/dist/secured/binding.js +52 -0
  272. package/dist/secured/binding.js.map +1 -0
  273. package/dist/secured/declared-branch.d.ts +11 -0
  274. package/dist/secured/declared-branch.d.ts.map +1 -0
  275. package/dist/secured/declared-branch.js +17 -0
  276. package/dist/secured/declared-branch.js.map +1 -0
  277. package/dist/secured/field-visibility.d.ts +56 -0
  278. package/dist/secured/field-visibility.d.ts.map +1 -0
  279. package/dist/secured/field-visibility.js +624 -0
  280. package/dist/secured/field-visibility.js.map +1 -0
  281. package/dist/secured/include.d.ts +68 -41
  282. package/dist/secured/include.d.ts.map +1 -1
  283. package/dist/secured/include.js +217 -115
  284. package/dist/secured/include.js.map +1 -1
  285. package/dist/secured/lower.d.ts.map +1 -1
  286. package/dist/secured/lower.js +1 -3
  287. package/dist/secured/lower.js.map +1 -1
  288. package/dist/secured/read.d.ts +11 -108
  289. package/dist/secured/read.d.ts.map +1 -1
  290. package/dist/secured/read.js +46 -633
  291. package/dist/secured/read.js.map +1 -1
  292. package/dist/secured/select.d.ts +0 -24
  293. package/dist/secured/select.d.ts.map +1 -1
  294. package/dist/secured/select.js +60 -153
  295. package/dist/secured/select.js.map +1 -1
  296. package/dist/secured/vocabulary.d.ts +13 -1
  297. package/dist/secured/vocabulary.d.ts.map +1 -1
  298. package/dist/secured/vocabulary.js +173 -83
  299. package/dist/secured/vocabulary.js.map +1 -1
  300. package/dist/testing/untyped-rule.d.ts +8 -0
  301. package/dist/testing/untyped-rule.d.ts.map +1 -0
  302. package/dist/testing/untyped-rule.js +4 -0
  303. package/dist/testing/untyped-rule.js.map +1 -0
  304. package/dist/types/index.d.ts +1 -1
  305. package/dist/types/index.d.ts.map +1 -1
  306. package/dist/types/rows.d.ts +18 -1
  307. package/dist/types/rows.d.ts.map +1 -1
  308. package/dist/validation/config-refusal.d.ts +3 -1
  309. package/dist/validation/config-refusal.d.ts.map +1 -1
  310. package/dist/validation/database-config.d.ts +1 -0
  311. package/dist/validation/database-config.d.ts.map +1 -1
  312. package/dist/validation/database-config.js +1 -1
  313. package/dist/validation/database-config.js.map +1 -1
  314. package/dist/validation/default-value.d.ts.map +1 -1
  315. package/dist/validation/default-value.js +3 -8
  316. package/dist/validation/default-value.js.map +1 -1
  317. package/dist/validation/extension-packs.d.ts.map +1 -1
  318. package/dist/validation/extension-packs.js +1 -0
  319. package/dist/validation/extension-packs.js.map +1 -1
  320. package/dist/validation/field-config.d.ts +3 -2
  321. package/dist/validation/field-config.d.ts.map +1 -1
  322. package/dist/validation/field-config.js +3 -1
  323. package/dist/validation/field-config.js.map +1 -1
  324. package/dist/validation/field-names.d.ts +2 -1
  325. package/dist/validation/field-names.d.ts.map +1 -1
  326. package/dist/validation/field-names.js +76 -63
  327. package/dist/validation/field-names.js.map +1 -1
  328. package/dist/validation/identifier-names.d.ts +1 -0
  329. package/dist/validation/identifier-names.d.ts.map +1 -1
  330. package/dist/validation/identifier-names.js +17 -9
  331. package/dist/validation/identifier-names.js.map +1 -1
  332. package/dist/validation/needs-closure.d.ts +30 -14
  333. package/dist/validation/needs-closure.d.ts.map +1 -1
  334. package/dist/validation/needs-closure.js +184 -31
  335. package/dist/validation/needs-closure.js.map +1 -1
  336. package/dist/validation/schema.d.ts +1 -0
  337. package/dist/validation/schema.d.ts.map +1 -1
  338. package/dist/validation/schema.js +4 -2
  339. package/dist/validation/schema.js.map +1 -1
  340. package/package.json +20 -4
  341. package/dist/access/field-visibility.d.ts +0 -11
  342. package/dist/access/field-visibility.d.ts.map +0 -1
  343. package/dist/access/field-visibility.js +0 -366
  344. package/dist/access/field-visibility.js.map +0 -1
  345. package/dist/access/query-validation.d.ts +0 -30
  346. package/dist/access/query-validation.d.ts.map +0 -1
  347. package/dist/access/query-validation.js +0 -49
  348. package/dist/access/query-validation.js.map +0 -1
  349. package/dist/access/system-fields.d.ts +0 -2
  350. package/dist/access/system-fields.d.ts.map +0 -1
  351. package/dist/access/system-fields.js +0 -7
  352. 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, checkRowRead, filterReadableFields, getRelatedListConfig, NO_RULE_VIEWS, ruleRowOf, } 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, declaredRelations, 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,502 +322,6 @@ 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
- /**
465
- * Narrow the foreign-key column of every to-one relationship this read did
466
- * NOT include or declare — the gap {@link applyForeignKeys} cannot close,
467
- * because a relation nobody named has no {@link IncludePlan} entry for it to
468
- * walk (issue #1243).
469
- *
470
- * The column is on the row regardless of `include` (ADR-0043), so it needs
471
- * its own visibility decision: the owning relationship field's own `read`
472
- * rule, evaluated per row against the row Field Visibility's rules see, and
473
- * the related list's own `query` access — `true`
474
- * leaves the column as the plain pass-through value it already is, `false`
475
- * nulls it outright, and a filter is resolved with one batched existence
476
- * check per relation across the whole page, keyed off the column's own value
477
- * rather than a fetched related row (there is no fetched related row here to
478
- * key off).
479
- *
480
- * That existence check cannot tell a row the filter denies from a row deleted
481
- * between the two statements — both come back absent from it. This function
482
- * does not try to: absence nulls the column either way, so a vanished row
483
- * fails exactly as closed as a denied one rather than surviving under the id
484
- * this statement read before it vanished (issue #1386).
485
- *
486
- * Runs AFTER {@link applyForeignKeys}, on its output — mutated in place —
487
- * and recurses into every relation THAT pass over its results the same way
488
- * {@link applyForeignKeys} does, so a related list reached only through an
489
- * include gets its own un-included to-ones narrowed too.
490
- */
491
- /**
492
- * Pairs each shown related row with its raw twin. Field Visibility may drop
493
- * rows from a to-many (a synthetic back-relation's source `read` rule), so
494
- * once the lengths differ the pairing is by `id`, never by position.
495
- */
496
- function pairRelatedRows(shown, raw) {
497
- const pairs = [];
498
- if (shown.length === raw.length) {
499
- shown.forEach((related, index) => {
500
- const original = raw[index];
501
- if (isRow(related) && isRow(original))
502
- pairs.push([related, original]);
503
- });
504
- return pairs;
505
- }
506
- const rawById = new Map();
507
- for (const original of raw)
508
- if (isRow(original))
509
- rawById.set(String(original.id), original);
510
- for (const related of shown) {
511
- if (!isRow(related))
512
- continue;
513
- const original = rawById.get(String(related.id));
514
- if (original !== undefined)
515
- pairs.push([related, original]);
516
- }
517
- return pairs;
518
- }
519
- async function narrowUnincludedForeignKeys(binding, filteredRows, rawRows, listName, listConfig, resolvedIncludes, ruleViews) {
520
- if (binding.context._isSudo === true)
521
- return;
522
- const ctx = relatedResolveContext(binding, listName, listConfig);
523
- const alreadyIncluded = new Set(resolvedIncludes
524
- .filter((plan) => !plan.declared && !plan.callerRefined)
525
- .map((plan) => plan.relation));
526
- const owners = foreignKeyOwningRelations(ctx);
527
- const ruleRows = owners.length === 0
528
- ? []
529
- : rawRows.map((row) => ruleRowOf(assembleColumnFields(maskReductions(row, resolvedIncludes), listConfig.fields), ruleViews, listConfig.fields, binding.config, listName));
530
- for (const owner of owners) {
531
- if (alreadyIncluded.has(owner.relation))
532
- continue;
533
- if (!rawRows.some((row) => owner.foreignKey in row))
534
- continue;
535
- const related = getRelatedListConfig(owner.ref, binding.config);
536
- if (!related)
537
- continue;
538
- const access = await resolveRelatedAccessPlan({ listName: related.listName, listConfig: related.listConfig }, ctx);
539
- for (let i = 0; i < filteredRows.length; i++) {
540
- const raw = rawRows[i];
541
- if (!(owner.foreignKey in raw) || !(owner.foreignKey in filteredRows[i]))
542
- continue;
543
- const canReadField = await checkRowRead(owner.fieldConfig.access, { session: binding.context.session, context: binding.context }, ruleRows[i]);
544
- if (!canReadField || access.kind === 'false')
545
- filteredRows[i][owner.foreignKey] = null;
546
- }
547
- if (access.kind === 'true' || access.kind === 'false')
548
- continue;
549
- const idsByRow = rawRows.map((row) => row[owner.foreignKey]);
550
- const idMap = new Map();
551
- for (let i = 0; i < filteredRows.length; i++) {
552
- if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
553
- continue;
554
- const value = idsByRow[i];
555
- if (value !== null && value !== undefined)
556
- idMap.set(String(value), value);
557
- }
558
- if (idMap.size === 0)
559
- continue;
560
- const ops = await whereCombinators();
561
- const visible = await engineRead(binding, () => collectionFor(binding.ormHandle, related.listName)
562
- .where((model) => lowerWhere(access, model, ops))
563
- .where((model) => identityIn(model, related.listName, [...idMap.values()]))
564
- .select('id')
565
- .all());
566
- const visibleIds = new Set(visible.map((row) => String(row.id)));
567
- for (let i = 0; i < filteredRows.length; i++) {
568
- if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
569
- continue;
570
- const value = idsByRow[i];
571
- if (value === null || value === undefined)
572
- continue;
573
- if (!visibleIds.has(String(value)))
574
- filteredRows[i][owner.foreignKey] = null;
575
- }
576
- }
577
- for (const plan of resolvedIncludes) {
578
- const relatedListConfig = binding.config.lists[plan.relatedListName];
579
- if (relatedListConfig === undefined)
580
- continue;
581
- const nestedFiltered = [];
582
- const nestedRaw = [];
583
- for (let i = 0; i < filteredRows.length; i++) {
584
- const filteredValue = filteredRows[i][plan.relation];
585
- const rawValue = rawRows[i][plan.relation];
586
- if (Array.isArray(filteredValue) && Array.isArray(rawValue)) {
587
- for (const [related, original] of pairRelatedRows(filteredValue, rawValue)) {
588
- nestedFiltered.push(related);
589
- nestedRaw.push(original);
590
- }
591
- }
592
- else if (plan.reduce !== undefined) {
593
- const key = rowsKeyOf(plan);
594
- for (const [related, original] of pairRelatedRows(combinedRows(filteredValue, key), combinedRows(rawValue, key))) {
595
- nestedFiltered.push(related);
596
- nestedRaw.push(original);
597
- }
598
- }
599
- else if (isRow(filteredValue) && isRow(rawValue)) {
600
- nestedFiltered.push(filteredValue);
601
- nestedRaw.push(rawValue);
602
- }
603
- }
604
- if (nestedFiltered.length === 0)
605
- continue;
606
- await narrowUnincludedForeignKeys(binding, nestedFiltered, nestedRaw, plan.relatedListName, relatedListConfig, plan.includes, ruleViews.nested[plan.relation] ?? NO_RULE_VIEWS);
607
- }
608
- }
609
- function reduces(plans) {
610
- return plans.some((plan) => plan.reduce !== undefined || reduces(plan.includes));
611
- }
612
- /**
613
- * Stand an empty to-many in for every relation the read reduced.
614
- *
615
- * Field Visibility decides whether a relation key survives by evaluating the
616
- * relationship field's own `read` rule, and it reaches that decision on the
617
- * path it takes for a relation's ROWS. A count is not rows, so handing it the
618
- * reduced value would take it down neither path and the key would fall out of
619
- * the result whether or not the session may read it. Substituting `[]` puts
620
- * the decision back on the one path that makes it — Field Visibility runs the
621
- * same rule over the same parent row — and {@link restoreReductions} then
622
- * writes the count back under exactly the keys that survived. A reduction is
623
- * to-many only ({@link ReducedToOneIncludeError}), so `[]` is the shape the
624
- * relation would otherwise have had.
625
- *
626
- * A `combine` that named the relation's rows under one key stands in that
627
- * array instead of `[]`, and {@link restoreReductions} writes it back beside
628
- * the counts. What a `read` rule or a hook sees of it is {@link ruleViewFor}'s,
629
- * and it takes a `[]` standing in for rows nobody fetched back out (ADR-0082).
630
- *
631
- * A relation that is both reduced and a live declared dependency of a
632
- * computed field returned alongside it (#1357) is the third case: its
633
- * `IncludePlan.declaredRows` fetched real rows purely for that declaration,
634
- * under {@link DECLARED_ROWS_BRANCH_KEY}, and those are what stand in here —
635
- * never `[]` — so the declaring hook's `needs` sees them via its own `item`,
636
- * and a relationship `read` rule that inspects `item.<reducedRelation>`
637
- * decides against real rows rather than an empty array. Field Visibility
638
- * still must not compute over them as though they were an ordinary relation
639
- * (ADR-0051): `ReducedDeclaredKeys` (`select.ts`) is what tells it to treat
640
- * this key as a raw pass-through, and {@link restoreReductions} overwrites it
641
- * with the reduction's own value regardless of what Field Visibility computed
642
- * for it.
643
- */
644
- function maskReductions(row, plans) {
645
- if (!reduces(plans))
646
- return row;
647
- const masked = { ...row };
648
- for (const plan of plans) {
649
- if (plan.reduce !== undefined) {
650
- const rows = plan.declaredRows !== undefined && !hasRowsBranch(plan)
651
- ? declaredRowsOf(row[plan.relation])
652
- : combinedRows(row[plan.relation], rowsKeyOf(plan));
653
- masked[plan.relation] = reduces(plan.includes)
654
- ? rows.map((related) => maskReductions(related, plan.includes))
655
- : rows;
656
- continue;
657
- }
658
- if (!reduces(plan.includes))
659
- continue;
660
- const value = masked[plan.relation];
661
- if (Array.isArray(value)) {
662
- masked[plan.relation] = value.map((related) => isRow(related) ? maskReductions(related, plan.includes) : related);
663
- }
664
- else if (isRow(value)) {
665
- masked[plan.relation] = maskReductions(value, plan.includes);
666
- }
667
- }
668
- return masked;
669
- }
670
- /** The key a `combine` returned the relation's own rows under, if it named one. */
671
- function rowsKeyOf(plan) {
672
- if (plan.reduce?.kind !== 'combine')
673
- return undefined;
674
- for (const branch of plan.reduce.branches) {
675
- if (branch.kind === 'rows')
676
- return branch.key;
677
- }
678
- return undefined;
679
- }
680
- /** The rows an internal declared-dependency branch fetched (`IncludePlan.declaredRows`). */
681
- function declaredRowsOf(value) {
682
- return combinedRows(value, DECLARED_ROWS_BRANCH_KEY);
683
- }
684
- /** The rows a `combine` returned under {@link rowsKeyOf}, or `[]` for a bare count. */
685
- function combinedRows(value, key) {
686
- if (key === undefined || !isRow(value))
687
- return [];
688
- const rows = value[key];
689
- return Array.isArray(rows) ? rows.filter(isRow) : [];
690
- }
691
- /**
692
- * The reduction's own value, once {@link declaredRowsBranch}'s internal
693
- * branch is spliced back out: a bare count unwrapped from
694
- * {@link DECLARED_COUNT_BRANCH_KEY}, or a caller's own combine object with
695
- * {@link DECLARED_ROWS_BRANCH_KEY} removed. The caller never sees either
696
- * reserved key.
697
- */
698
- function publicReductionValue(raw, reduce) {
699
- if (!isRow(raw))
700
- return raw;
701
- if (reduce.kind === 'count')
702
- return raw[DECLARED_COUNT_BRANCH_KEY] ?? 0;
703
- const publicValue = { ...raw };
704
- delete publicValue[DECLARED_ROWS_BRANCH_KEY];
705
- return publicValue;
706
- }
707
- /** Write each reduced relation back, under the keys Field Visibility kept. */
708
- function restoreReductions(shown, source, plans) {
709
- for (const plan of plans) {
710
- if (plan.reduce !== undefined) {
711
- if (!(plan.relation in shown))
712
- continue;
713
- const raw = source[plan.relation];
714
- if (plan.declaredRows !== undefined && !hasRowsBranch(plan)) {
715
- shown[plan.relation] = publicReductionValue(raw, plan.reduce);
716
- continue;
717
- }
718
- const key = rowsKeyOf(plan);
719
- if (key === undefined || !isRow(raw)) {
720
- shown[plan.relation] = raw;
721
- continue;
722
- }
723
- const kept = shown[plan.relation];
724
- if (reduces(plan.includes) && Array.isArray(kept)) {
725
- const rows = combinedRows(raw, key);
726
- for (const [related, original] of pairRelatedRows(kept, rows)) {
727
- restoreReductions(related, original, plan.includes);
728
- }
729
- }
730
- const base = publicReductionValue(raw, plan.reduce);
731
- shown[plan.relation] = { ...(isRow(base) ? base : raw), [key]: kept };
732
- continue;
733
- }
734
- if (!reduces(plan.includes))
735
- continue;
736
- const kept = shown[plan.relation];
737
- const raw = source[plan.relation];
738
- if (Array.isArray(kept) && Array.isArray(raw)) {
739
- for (const [related, original] of pairRelatedRows(kept, raw)) {
740
- restoreReductions(related, original, plan.includes);
741
- }
742
- }
743
- else if (isRow(kept) && isRow(raw)) {
744
- restoreReductions(kept, raw, plan.includes);
745
- }
746
- }
747
- }
748
- async function unshapedDeclaredRows(binding, listName, rows, plans) {
749
- const byRelation = new Map();
750
- for (const plan of plans) {
751
- if (plan.reduce === undefined || plan.declaredRows === undefined || !hasRowsBranch(plan)) {
752
- continue;
753
- }
754
- const values = new Map();
755
- for (const row of rows)
756
- values.set(String(row['id']), declaredRowsOf(row[plan.relation]));
757
- byRelation.set(plan.relation, values);
758
- }
759
- const fetched = plans.filter((plan) => plan.declaredFetch !== undefined);
760
- const ids = rows
761
- .map((row) => row['id'])
762
- .filter((id) => typeof id === 'string' || typeof id === 'number');
763
- if (fetched.length === 0 || ids.length === 0)
764
- return byRelation;
765
- const ops = await whereCombinators();
766
- for (const plan of fetched) {
767
- const owners = await withOrigin('engine', () => collectionFor(binding.ormHandle, listName)
768
- .where((model) => identityIn(model, listName, ids))
769
- .select('id')
770
- .include(plan.relation, (child) => {
771
- let scoped = child;
772
- for (const predicate of plan.declaredFetch ?? []) {
773
- scoped = scoped.where((model) => lowerWhere(predicate, model, ops));
774
- }
775
- return scoped;
776
- })
777
- .all());
778
- const values = new Map();
779
- for (const owner of owners)
780
- values.set(String(owner['id']), owner[plan.relation]);
781
- byRelation.set(plan.relation, values);
782
- }
783
- return byRelation;
784
- }
785
- async function ruleViewTreeFor(binding, listName, rows, plans) {
786
- const unshaped = await unshapedDeclaredRows(binding, listName, rows, plans);
787
- const nested = {};
788
- for (const plan of plans) {
789
- if (plan.declared || plan.includes.length === 0)
790
- continue;
791
- const rowsKey = rowsKeyOf(plan);
792
- if (plan.reduce !== undefined && rowsKey === undefined)
793
- continue;
794
- const children = [];
795
- for (const row of rows) {
796
- const value = row[plan.relation];
797
- if (plan.reduce !== undefined)
798
- children.push(...combinedRows(value, rowsKey));
799
- else if (Array.isArray(value))
800
- children.push(...value.filter(isRow));
801
- else if (isRow(value))
802
- children.push(value);
803
- }
804
- if (children.length === 0)
805
- continue;
806
- nested[plan.relation] = await ruleViewTreeFor(binding, plan.relatedListName, children, plan.includes);
807
- }
808
- return { viewFor: (row) => ruleViewFor(row, plans, unshaped), nested };
809
- }
810
- function narrowsWhatARuleSees(plan) {
811
- if (plan.declared || !plan.callerShaped)
812
- return false;
813
- return plan.reduce === undefined || hasRowsBranch(plan);
814
- }
815
- function isRowlessReduction(plan) {
816
- return plan.reduce !== undefined && !hasRowsBranch(plan) && plan.declaredRows === undefined;
817
- }
818
- /** A to-one reading `null` for a row the session cannot see. Inverse sides cannot be told apart. */
819
- function isScopedAway(row, plan, value) {
820
- if (plan.arity !== 'one' || value !== null)
821
- return false;
822
- return plan.foreignKey === undefined ? plan.accessScoped : row[plan.foreignKey] !== null;
823
- }
824
- function ruleViewFor(row, plans, unshaped) {
825
- const removed = new Set();
826
- const declared = {};
827
- const scopedAway = new Set();
828
- for (const plan of plans) {
829
- const values = unshaped.get(plan.relation);
830
- const rows = values?.get(String(row['id']));
831
- if (isScopedAway(row, plan, rows ?? row[plan.relation]))
832
- scopedAway.add(plan.relation);
833
- if (rows !== undefined)
834
- declared[plan.relation] = rows;
835
- else if (values !== undefined || narrowsWhatARuleSees(plan) || isRowlessReduction(plan)) {
836
- removed.add(plan.relation);
837
- }
838
- }
839
- return { removed, declared, scopedAway };
840
- }
841
- /**
842
- * What every terminal returns rows through — the one place a row this engine
843
- * read becomes a row a caller may see, and the only place any foreign-key
844
- * pass runs. `all()`, `first()`, the `forUpdate()` lane and `nearest()` all
845
- * come through here, so none of them can drift apart on what a column reads
846
- * as, and neither can a hook or a `read` rule.
847
- *
848
- * The batch is what lets {@link narrowUnincludedForeignKeys} resolve a
849
- * to-one this read never named at all for a whole page in one query per
850
- * relation rather than one per row.
851
- */
852
- async function visibleRows(binding, rows, plan) {
853
- const { listConfig, context, config, listName } = binding;
854
- const ruleViews = await ruleViewTreeFor(binding, listName, rows, plan.includes);
855
- const results = await Promise.all(rows.map(async (row) => {
856
- const filtered = await filterReadableFields(maskReductions(row, plan.includes), listConfig.fields, { session: context.session, context }, config, 0, listName, plan.additions, plan.selection, plan.reducedDeclared, ruleViews);
857
- applyForeignKeys(filtered, plan.includes);
858
- restoreReductions(filtered, row, plan.includes);
859
- return filtered;
860
- }));
861
- await narrowUnincludedForeignKeys(binding, results, rows, listName, listConfig, plan.includes, ruleViews);
862
- return results;
863
- }
864
325
  /**
865
326
  * Whether the session's operation-level `query` access reaches the row with
866
327
  * this id, resolved as a read would: a filter rule is run as a scoped read of
@@ -889,12 +350,14 @@ async function writtenRowQueryable(binding, id) {
889
350
  }
890
351
  /** Whether a computed field on this list declares a relation a write's return must fetch. */
891
352
  export function writeReturnReadsRelations(binding) {
892
- return declaredRelations(binding).length > 0;
353
+ return declaredRelations(binding).size > 0;
893
354
  }
894
355
  /** Runs on the binding's own handle: the write's transaction, never a second connection. */
895
356
  export async function readWrittenRow(binding, id) {
896
357
  if ((typeof id !== 'string' && typeof id !== 'number') || !writeReturnReadsRelations(binding)) {
897
- return (await writtenRowQueryable(binding, id)) ? { kind: 'written' } : { kind: 'hidden' };
358
+ return (await writtenRowQueryable(binding, id))
359
+ ? { kind: 'written', root: planLevel(binding, undefined, []) }
360
+ : { kind: 'hidden' };
898
361
  }
899
362
  const plan = await resolvePlan(binding, {
900
363
  predicates: [{ id: { equals: id } }],
@@ -907,39 +370,14 @@ export async function readWrittenRow(binding, id) {
907
370
  return { kind: 'hidden' };
908
371
  const collection = scope(binding, plan, await whereCombinators(), FIRST_DISPOSITIONS, unreachableRefusal);
909
372
  const row = await engineRead(binding, () => collection.first());
910
- return row === null ? { kind: 'hidden' } : { kind: 'read', row, plan };
911
- }
912
- /**
913
- * What a write hands back: the row Field Visibility leaves, then the
914
- * foreign-key pass a read of the same row would give it, so `create()` and
915
- * `update()` never return an id `first()` hides. A row the session cannot
916
- * `query` comes back as the list's system fields alone.
917
- */
918
- export async function visibleWrittenRow(binding, row, read) {
919
- const { listConfig, context, config, listName } = binding;
920
- if (read.kind === 'read')
921
- return (await visibleRows(binding, [read.row], read.plan))[0];
922
- if (read.kind === 'hidden') {
923
- const kept = {};
924
- for (const key of getListDependencies(config, listName).systemFields) {
925
- if (key in row)
926
- kept[key] = row[key];
927
- }
928
- return kept;
929
- }
930
- const filtered = await filterReadableFields(row, listConfig.fields, { session: context.session, context: { ...context, _isSudo: context._isSudo } }, config, 0, listName);
931
- await narrowUnincludedForeignKeys(binding, [filtered], [row], listName, listConfig, [], NO_RULE_VIEWS);
932
- return filtered;
373
+ return row === null ? { kind: 'hidden' } : { kind: 'read', row, root: plan.root };
933
374
  }
934
375
  /** `all()` is the terminal every plan member was designed for. */
935
376
  const ALL_DISPOSITIONS = {
936
377
  predicates: 'applied',
937
378
  orders: 'applied',
938
- includes: 'applied',
379
+ root: 'applied',
939
380
  projection: 'applied',
940
- selection: 'applied',
941
- additions: 'applied',
942
- reducedDeclared: 'applied',
943
381
  limit: 'applied',
944
382
  offset: 'applied',
945
383
  distinct: 'applied',
@@ -1011,15 +449,6 @@ async function locked(binding, taken, plan, rows) {
1011
449
  const refreshed = scope(binding, { ...plan, limit: undefined, offset: undefined }, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal).where((model) => identityIn(model, listName, held, column));
1012
450
  return await engineRead(binding, () => refreshed.all());
1013
451
  }
1014
- async function engineRead(binding, run) {
1015
- try {
1016
- return await withOrigin('engine', run);
1017
- }
1018
- catch (error) {
1019
- binding.context._transactionOwner?.poison(error);
1020
- throw error;
1021
- }
1022
- }
1023
452
  async function runAll(binding, state) {
1024
453
  const plan = await resolvePlan(binding, state);
1025
454
  if (plan === null)
@@ -1028,7 +457,7 @@ async function runAll(binding, state) {
1028
457
  const collection = scope(binding, plan, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal);
1029
458
  const read = await engineRead(binding, () => collection.all());
1030
459
  const rows = taken === undefined ? read : await locked(binding, taken, plan, read);
1031
- return await visibleRows(binding, rows, plan);
460
+ return await visibleRows(binding, rows, plan.root);
1032
461
  }
1033
462
  async function runFirst(binding, state) {
1034
463
  const plan = await resolvePlan(binding, state);
@@ -1042,7 +471,7 @@ async function runFirst(binding, state) {
1042
471
  const rows = taken === undefined ? [read] : await locked(binding, taken, plan, [read]);
1043
472
  if (rows.length === 0)
1044
473
  return null;
1045
- return (await visibleRows(binding, rows, plan))[0];
474
+ return (await visibleRows(binding, rows, plan.root))[0];
1046
475
  }
1047
476
  function countOf(result, listName, key) {
1048
477
  const value = result[key];
@@ -1069,11 +498,8 @@ function countOf(result, listName, key) {
1069
498
  const AGGREGATE_DISPOSITIONS = {
1070
499
  predicates: 'applied',
1071
500
  orders: 'inapplicable',
1072
- includes: 'inapplicable',
501
+ root: 'inapplicable',
1073
502
  projection: 'inapplicable',
1074
- selection: 'inapplicable',
1075
- additions: 'inapplicable',
1076
- reducedDeclared: 'inapplicable',
1077
503
  limit: 'refused',
1078
504
  offset: 'refused',
1079
505
  distinct: 'refused',
@@ -1136,11 +562,8 @@ function present(near) {
1136
562
  const NEAREST_DISPOSITIONS = {
1137
563
  predicates: 'applied',
1138
564
  orders: 'inapplicable',
1139
- includes: 'applied',
565
+ root: 'applied',
1140
566
  projection: 'applied',
1141
- selection: 'applied',
1142
- additions: 'applied',
1143
- reducedDeclared: 'applied',
1144
567
  limit: 'inapplicable',
1145
568
  offset: 'refused',
1146
569
  distinct: 'refused',
@@ -1204,7 +627,7 @@ async function runNearest(binding, state, field, vector, options) {
1204
627
  }
1205
628
  const rows = await engineRead(binding, () => collection.limit(near.limit).all());
1206
629
  const scores = rows.map((row) => score(near, row));
1207
- const items = await visibleRows(binding, rows, plan);
630
+ const items = await visibleRows(binding, rows, plan.root);
1208
631
  return items.map((item, index) => ({ item, score: scores[index] }));
1209
632
  }
1210
633
  function score(near, row) {
@@ -1251,38 +674,28 @@ async function canOrderBy(binding, ctx, key) {
1251
674
  }
1252
675
  }
1253
676
  /**
1254
- * Which of `keys` a read of `listKey` may order by: the answer `orderBy` gives
1255
- * this session, read off the same access check, resolution and lowering with
1256
- * nothing executed. It never throws. A key it cannot answer for is left out, so
1257
- * a read-denied key, an absent one and a column with no `order` trait are
1258
- * indistinguishable to a caller (ADR-0031, ADR-0055), and a real fault is left
1259
- * for the read itself to report.
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.
1260
680
  */
1261
- export async function orderableKeys(context, config, listKey, keys) {
1262
- const listConfig = Object.hasOwn(config.lists, listKey) ? config.lists[listKey] : undefined;
1263
- if (listConfig === undefined)
681
+ export async function columnOrderedKeys(context, config, listKey, keys) {
682
+ const binding = readBindingFor(context, config, listKey);
683
+ if (binding === undefined)
1264
684
  return new Set();
1265
- const binding = {
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 {
1266
691
  listName: listKey,
1267
- listConfig,
692
+ listConfig: config.lists[listKey],
1268
693
  ormHandle: context.ormHandle,
1269
694
  context,
1270
695
  config,
1271
696
  };
1272
- const secured = context._isSudo !== true;
1273
- try {
1274
- const access = secured
1275
- ? await checkAccess(listConfig.access?.operation?.query, {
1276
- session: context.session,
1277
- context,
1278
- })
1279
- : true;
1280
- if (access === false)
1281
- return new Set();
1282
- }
1283
- catch {
1284
- return new Set();
1285
- }
697
+ }
698
+ async function orderableAmong(binding, secured, keys) {
1286
699
  const ctx = resolveContext(binding, secured);
1287
700
  const unique = [...new Set(keys)];
1288
701
  const answers = await Promise.all(unique.map((key) => canOrderBy(binding, ctx, key)));