@opensaas/stack-core 0.43.0 → 0.44.0-alpha.1

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 (641) hide show
  1. package/CHANGELOG.md +2720 -0
  2. package/README.md +182 -115
  3. package/dist/.build-hash +1 -0
  4. package/dist/access/declared-dependencies.d.ts +102 -88
  5. package/dist/access/declared-dependencies.d.ts.map +1 -1
  6. package/dist/access/declared-dependencies.js +83 -71
  7. package/dist/access/declared-dependencies.js.map +1 -1
  8. package/dist/access/depth-limits.d.ts +4 -0
  9. package/dist/access/depth-limits.d.ts.map +1 -1
  10. package/dist/access/depth-limits.js +4 -0
  11. package/dist/access/depth-limits.js.map +1 -1
  12. package/dist/access/engine.d.ts +68 -20
  13. package/dist/access/engine.d.ts.map +1 -1
  14. package/dist/access/engine.js +151 -37
  15. package/dist/access/engine.js.map +1 -1
  16. package/dist/access/errors.d.ts +33 -48
  17. package/dist/access/errors.d.ts.map +1 -1
  18. package/dist/access/errors.js +73 -63
  19. package/dist/access/errors.js.map +1 -1
  20. package/dist/access/field-access.d.ts +76 -5
  21. package/dist/access/field-access.d.ts.map +1 -1
  22. package/dist/access/field-access.js +235 -61
  23. package/dist/access/field-access.js.map +1 -1
  24. package/dist/access/field-visibility.d.ts +50 -4
  25. package/dist/access/field-visibility.d.ts.map +1 -1
  26. package/dist/access/field-visibility.js +143 -160
  27. package/dist/access/field-visibility.js.map +1 -1
  28. package/dist/access/index.d.ts +5 -12
  29. package/dist/access/index.d.ts.map +1 -1
  30. package/dist/access/index.js +6 -19
  31. package/dist/access/index.js.map +1 -1
  32. package/dist/access/orm-client.d.ts +19 -0
  33. package/dist/access/orm-client.d.ts.map +1 -0
  34. package/dist/access/orm-client.js +37 -0
  35. package/dist/access/orm-client.js.map +1 -0
  36. package/dist/access/query-validation.d.ts +7 -129
  37. package/dist/access/query-validation.d.ts.map +1 -1
  38. package/dist/access/query-validation.js +12 -314
  39. package/dist/access/query-validation.js.map +1 -1
  40. package/dist/access/system-fields.d.ts +2 -0
  41. package/dist/access/system-fields.d.ts.map +1 -0
  42. package/dist/access/system-fields.js +7 -0
  43. package/dist/access/system-fields.js.map +1 -0
  44. package/dist/access/transaction-registry.d.ts +25 -2
  45. package/dist/access/transaction-registry.d.ts.map +1 -1
  46. package/dist/access/transaction-registry.js +34 -17
  47. package/dist/access/transaction-registry.js.map +1 -1
  48. package/dist/access/types.d.ts +217 -131
  49. package/dist/access/types.d.ts.map +1 -1
  50. package/dist/config/field-kind.d.ts +66 -0
  51. package/dist/config/field-kind.d.ts.map +1 -0
  52. package/dist/config/field-kind.js +63 -0
  53. package/dist/config/field-kind.js.map +1 -0
  54. package/dist/config/hook-only-fields.d.ts +3 -0
  55. package/dist/config/hook-only-fields.d.ts.map +1 -0
  56. package/dist/config/hook-only-fields.js +17 -0
  57. package/dist/config/hook-only-fields.js.map +1 -0
  58. package/dist/config/index.d.ts +3 -1
  59. package/dist/config/index.d.ts.map +1 -1
  60. package/dist/config/index.js +6 -3
  61. package/dist/config/index.js.map +1 -1
  62. package/dist/config/mcp-tool-names.d.ts +3 -0
  63. package/dist/config/mcp-tool-names.d.ts.map +1 -0
  64. package/dist/config/mcp-tool-names.js +25 -0
  65. package/dist/config/mcp-tool-names.js.map +1 -0
  66. package/dist/config/nav-count.d.ts.map +1 -1
  67. package/dist/config/nav-count.js +4 -4
  68. package/dist/config/nav-count.js.map +1 -1
  69. package/dist/config/plugin-engine.d.ts +8 -2
  70. package/dist/config/plugin-engine.d.ts.map +1 -1
  71. package/dist/config/plugin-engine.js +102 -38
  72. package/dist/config/plugin-engine.js.map +1 -1
  73. package/dist/config/types.d.ts +966 -614
  74. package/dist/config/types.d.ts.map +1 -1
  75. package/dist/context/apply-defaults.d.ts +18 -1
  76. package/dist/context/apply-defaults.d.ts.map +1 -1
  77. package/dist/context/apply-defaults.js +30 -4
  78. package/dist/context/apply-defaults.js.map +1 -1
  79. package/dist/context/engine-context.d.ts +73 -0
  80. package/dist/context/engine-context.d.ts.map +1 -0
  81. package/dist/context/engine-context.js +67 -0
  82. package/dist/context/engine-context.js.map +1 -0
  83. package/dist/context/hook-pipeline.d.ts +9 -2
  84. package/dist/context/hook-pipeline.d.ts.map +1 -1
  85. package/dist/context/hook-pipeline.js +5 -4
  86. package/dist/context/hook-pipeline.js.map +1 -1
  87. package/dist/context/index.d.ts +98 -89
  88. package/dist/context/index.d.ts.map +1 -1
  89. package/dist/context/index.js +917 -722
  90. package/dist/context/index.js.map +1 -1
  91. package/dist/context/junction.d.ts +59 -0
  92. package/dist/context/junction.d.ts.map +1 -0
  93. package/dist/context/junction.js +126 -0
  94. package/dist/context/junction.js.map +1 -0
  95. package/dist/context/plugin-field-read.d.ts +33 -0
  96. package/dist/context/plugin-field-read.d.ts.map +1 -0
  97. package/dist/context/plugin-field-read.js +41 -0
  98. package/dist/context/plugin-field-read.js.map +1 -0
  99. package/dist/context/plugin-field-write.d.ts +104 -0
  100. package/dist/context/plugin-field-write.d.ts.map +1 -0
  101. package/dist/context/plugin-field-write.js +184 -0
  102. package/dist/context/plugin-field-write.js.map +1 -0
  103. package/dist/context/relationship-input.d.ts +146 -0
  104. package/dist/context/relationship-input.d.ts.map +1 -0
  105. package/dist/context/relationship-input.js +453 -0
  106. package/dist/context/relationship-input.js.map +1 -0
  107. package/dist/context/transaction-boundary.d.ts +54 -28
  108. package/dist/context/transaction-boundary.d.ts.map +1 -1
  109. package/dist/context/transaction-boundary.js +87 -169
  110. package/dist/context/transaction-boundary.js.map +1 -1
  111. package/dist/context/write-pipeline.d.ts +33 -51
  112. package/dist/context/write-pipeline.d.ts.map +1 -1
  113. package/dist/context/write-pipeline.js +372 -185
  114. package/dist/context/write-pipeline.js.map +1 -1
  115. package/dist/contract/dependencies.d.ts +88 -0
  116. package/dist/contract/dependencies.d.ts.map +1 -0
  117. package/dist/contract/dependencies.js +237 -0
  118. package/dist/contract/dependencies.js.map +1 -0
  119. package/dist/contract/derive.d.ts +49 -0
  120. package/dist/contract/derive.d.ts.map +1 -0
  121. package/dist/contract/derive.js +540 -0
  122. package/dist/contract/derive.js.map +1 -0
  123. package/dist/contract/fixtures/contract-configs.d.ts +64 -0
  124. package/dist/contract/fixtures/contract-configs.d.ts.map +1 -0
  125. package/dist/contract/fixtures/contract-configs.js +345 -0
  126. package/dist/contract/fixtures/contract-configs.js.map +1 -0
  127. package/dist/contract/id-boundary.d.ts +53 -0
  128. package/dist/contract/id-boundary.d.ts.map +1 -0
  129. package/dist/contract/id-boundary.js +76 -0
  130. package/dist/contract/id-boundary.js.map +1 -0
  131. package/dist/contract/index.d.ts +8 -0
  132. package/dist/contract/index.d.ts.map +1 -0
  133. package/dist/contract/index.js +11 -0
  134. package/dist/contract/index.js.map +1 -0
  135. package/dist/contract/prisma.d.ts +58 -0
  136. package/dist/contract/prisma.d.ts.map +1 -0
  137. package/dist/contract/prisma.js +282 -0
  138. package/dist/contract/prisma.js.map +1 -0
  139. package/dist/contract/relation-graph.d.ts +70 -0
  140. package/dist/contract/relation-graph.d.ts.map +1 -0
  141. package/dist/contract/relation-graph.js +113 -0
  142. package/dist/contract/relation-graph.js.map +1 -0
  143. package/dist/contract/types.d.ts +135 -0
  144. package/dist/contract/types.d.ts.map +1 -0
  145. package/dist/contract/types.js +2 -0
  146. package/dist/contract/types.js.map +1 -0
  147. package/dist/db/client.d.ts +55 -0
  148. package/dist/db/client.d.ts.map +1 -0
  149. package/dist/db/client.js +73 -0
  150. package/dist/db/client.js.map +1 -0
  151. package/dist/db/dev-database.d.ts +124 -0
  152. package/dist/db/dev-database.d.ts.map +1 -0
  153. package/dist/db/dev-database.js +291 -0
  154. package/dist/db/dev-database.js.map +1 -0
  155. package/dist/db/password-front.d.ts +10 -0
  156. package/dist/db/password-front.d.ts.map +1 -0
  157. package/dist/db/password-front.js +147 -0
  158. package/dist/db/password-front.js.map +1 -0
  159. package/dist/db/process-identity.d.ts +36 -0
  160. package/dist/db/process-identity.d.ts.map +1 -0
  161. package/dist/db/process-identity.js +114 -0
  162. package/dist/db/process-identity.js.map +1 -0
  163. package/dist/db/state-file.d.ts +54 -0
  164. package/dist/db/state-file.d.ts.map +1 -0
  165. package/dist/db/state-file.js +91 -0
  166. package/dist/db/state-file.js.map +1 -0
  167. package/dist/db/url.d.ts +89 -0
  168. package/dist/db/url.d.ts.map +1 -0
  169. package/dist/db/url.js +118 -0
  170. package/dist/db/url.js.map +1 -0
  171. package/dist/extend.d.ts +13 -2
  172. package/dist/extend.d.ts.map +1 -1
  173. package/dist/extend.js +24 -1
  174. package/dist/extend.js.map +1 -1
  175. package/dist/fields/index.d.ts +87 -42
  176. package/dist/fields/index.d.ts.map +1 -1
  177. package/dist/fields/index.js +457 -677
  178. package/dist/fields/index.js.map +1 -1
  179. package/dist/filter/collect.d.ts +3 -4
  180. package/dist/filter/collect.d.ts.map +1 -1
  181. package/dist/filter/collect.js +2 -2
  182. package/dist/filter/collect.js.map +1 -1
  183. package/dist/filter/index.d.ts +1 -2
  184. package/dist/filter/index.d.ts.map +1 -1
  185. package/dist/filter/index.js +0 -1
  186. package/dist/filter/index.js.map +1 -1
  187. package/dist/filter/map.d.ts +12 -5
  188. package/dist/filter/map.d.ts.map +1 -1
  189. package/dist/filter/map.js +47 -19
  190. package/dist/filter/map.js.map +1 -1
  191. package/dist/filter/types.d.ts +10 -24
  192. package/dist/filter/types.d.ts.map +1 -1
  193. package/dist/filter/types.js +2 -11
  194. package/dist/filter/types.js.map +1 -1
  195. package/dist/hooks/index.d.ts +22 -31
  196. package/dist/hooks/index.d.ts.map +1 -1
  197. package/dist/hooks/index.js +33 -37
  198. package/dist/hooks/index.js.map +1 -1
  199. package/dist/hooks/transition-guard.d.ts +61 -0
  200. package/dist/hooks/transition-guard.d.ts.map +1 -0
  201. package/dist/hooks/transition-guard.js +93 -0
  202. package/dist/hooks/transition-guard.js.map +1 -0
  203. package/dist/index.d.ts +52 -16
  204. package/dist/index.d.ts.map +1 -1
  205. package/dist/index.js +105 -51
  206. package/dist/index.js.map +1 -1
  207. package/dist/internal.d.ts +4 -2
  208. package/dist/internal.d.ts.map +1 -1
  209. package/dist/internal.js +16 -0
  210. package/dist/internal.js.map +1 -1
  211. package/dist/lib/case-utils.d.ts +0 -1
  212. package/dist/lib/case-utils.d.ts.map +1 -1
  213. package/dist/lib/case-utils.js +0 -3
  214. package/dist/lib/case-utils.js.map +1 -1
  215. package/dist/lib/client-safe-error.d.ts +9 -0
  216. package/dist/lib/client-safe-error.d.ts.map +1 -0
  217. package/dist/lib/client-safe-error.js +23 -0
  218. package/dist/lib/client-safe-error.js.map +1 -0
  219. package/dist/lib/database-errors.d.ts +78 -0
  220. package/dist/lib/database-errors.d.ts.map +1 -0
  221. package/dist/lib/database-errors.js +148 -0
  222. package/dist/lib/database-errors.js.map +1 -0
  223. package/dist/lib/null-prototype-registry.d.ts +9 -0
  224. package/dist/lib/null-prototype-registry.d.ts.map +1 -0
  225. package/dist/lib/null-prototype-registry.js +11 -0
  226. package/dist/lib/null-prototype-registry.js.map +1 -0
  227. package/dist/lib/prisma-errors.d.ts +30 -32
  228. package/dist/lib/prisma-errors.d.ts.map +1 -1
  229. package/dist/lib/prisma-errors.js +86 -46
  230. package/dist/lib/prisma-errors.js.map +1 -1
  231. package/dist/lib/process-global.d.ts +23 -0
  232. package/dist/lib/process-global.d.ts.map +1 -0
  233. package/dist/lib/process-global.js +33 -0
  234. package/dist/lib/process-global.js.map +1 -0
  235. package/dist/lib/warn-once.d.ts +7 -0
  236. package/dist/lib/warn-once.d.ts.map +1 -0
  237. package/dist/lib/warn-once.js +13 -0
  238. package/dist/lib/warn-once.js.map +1 -0
  239. package/dist/mcp/advertise.d.ts +16 -0
  240. package/dist/mcp/advertise.d.ts.map +1 -0
  241. package/dist/mcp/advertise.js +25 -0
  242. package/dist/mcp/advertise.js.map +1 -0
  243. package/dist/mcp/arguments.d.ts +10 -0
  244. package/dist/mcp/arguments.d.ts.map +1 -0
  245. package/dist/mcp/arguments.js +143 -0
  246. package/dist/mcp/arguments.js.map +1 -0
  247. package/dist/mcp/field-schema.d.ts +50 -5
  248. package/dist/mcp/field-schema.d.ts.map +1 -1
  249. package/dist/mcp/field-schema.js +137 -14
  250. package/dist/mcp/field-schema.js.map +1 -1
  251. package/dist/mcp/handler.d.ts +16 -5
  252. package/dist/mcp/handler.d.ts.map +1 -1
  253. package/dist/mcp/handler.js +404 -126
  254. package/dist/mcp/handler.js.map +1 -1
  255. package/dist/mcp/index.d.ts +1 -0
  256. package/dist/mcp/index.d.ts.map +1 -1
  257. package/dist/mcp/index.js +1 -0
  258. package/dist/mcp/index.js.map +1 -1
  259. package/dist/mcp/projection.d.ts +42 -34
  260. package/dist/mcp/projection.d.ts.map +1 -1
  261. package/dist/mcp/projection.js +246 -200
  262. package/dist/mcp/projection.js.map +1 -1
  263. package/dist/mcp/tool-error.d.ts +8 -0
  264. package/dist/mcp/tool-error.d.ts.map +1 -0
  265. package/dist/mcp/tool-error.js +11 -0
  266. package/dist/mcp/tool-error.js.map +1 -0
  267. package/dist/mcp/types.d.ts +3 -2
  268. package/dist/mcp/types.d.ts.map +1 -1
  269. package/dist/mcp/where-id-boundary.d.ts +28 -0
  270. package/dist/mcp/where-id-boundary.d.ts.map +1 -0
  271. package/dist/mcp/where-id-boundary.js +187 -0
  272. package/dist/mcp/where-id-boundary.js.map +1 -0
  273. package/dist/origin.d.ts +147 -0
  274. package/dist/origin.d.ts.map +1 -0
  275. package/dist/origin.js +191 -0
  276. package/dist/origin.js.map +1 -0
  277. package/dist/query/relationship-options.d.ts +35 -6
  278. package/dist/query/relationship-options.d.ts.map +1 -1
  279. package/dist/query/relationship-options.js +33 -23
  280. package/dist/query/relationship-options.js.map +1 -1
  281. package/dist/secured/aggregate.d.ts +32 -0
  282. package/dist/secured/aggregate.d.ts.map +1 -0
  283. package/dist/secured/aggregate.js +31 -0
  284. package/dist/secured/aggregate.js.map +1 -0
  285. package/dist/secured/include.d.ts +320 -0
  286. package/dist/secured/include.d.ts.map +1 -0
  287. package/dist/secured/include.js +650 -0
  288. package/dist/secured/include.js.map +1 -0
  289. package/dist/secured/lock.d.ts +127 -0
  290. package/dist/secured/lock.d.ts.map +1 -0
  291. package/dist/secured/lock.js +275 -0
  292. package/dist/secured/lock.js.map +1 -0
  293. package/dist/secured/lower.d.ts +82 -0
  294. package/dist/secured/lower.d.ts.map +1 -0
  295. package/dist/secured/lower.js +162 -0
  296. package/dist/secured/lower.js.map +1 -0
  297. package/dist/secured/operators.d.ts +7 -0
  298. package/dist/secured/operators.d.ts.map +1 -0
  299. package/dist/secured/operators.js +18 -0
  300. package/dist/secured/operators.js.map +1 -0
  301. package/dist/secured/read.d.ts +208 -0
  302. package/dist/secured/read.d.ts.map +1 -0
  303. package/dist/secured/read.js +1245 -0
  304. package/dist/secured/read.js.map +1 -0
  305. package/dist/secured/select.d.ts +87 -0
  306. package/dist/secured/select.d.ts.map +1 -0
  307. package/dist/secured/select.js +250 -0
  308. package/dist/secured/select.js.map +1 -0
  309. package/dist/secured/vector.d.ts +58 -0
  310. package/dist/secured/vector.d.ts.map +1 -0
  311. package/dist/secured/vector.js +113 -0
  312. package/dist/secured/vector.js.map +1 -0
  313. package/dist/secured/vocabulary.d.ts +227 -0
  314. package/dist/secured/vocabulary.d.ts.map +1 -0
  315. package/dist/secured/vocabulary.js +578 -0
  316. package/dist/secured/vocabulary.js.map +1 -0
  317. package/dist/secured/write.d.ts +58 -0
  318. package/dist/secured/write.d.ts.map +1 -0
  319. package/dist/secured/write.js +87 -0
  320. package/dist/secured/write.js.map +1 -0
  321. package/dist/testing/context.d.ts +236 -0
  322. package/dist/testing/context.d.ts.map +1 -0
  323. package/dist/testing/context.js +457 -0
  324. package/dist/testing/context.js.map +1 -0
  325. package/dist/testing/escape.d.ts +83 -0
  326. package/dist/testing/escape.d.ts.map +1 -0
  327. package/dist/testing/escape.js +148 -0
  328. package/dist/testing/escape.js.map +1 -0
  329. package/dist/testing/extensions.d.ts +48 -0
  330. package/dist/testing/extensions.d.ts.map +1 -0
  331. package/dist/testing/extensions.js +107 -0
  332. package/dist/testing/extensions.js.map +1 -0
  333. package/dist/testing/index.d.ts +5 -0
  334. package/dist/testing/index.d.ts.map +1 -0
  335. package/dist/testing/index.js +11 -0
  336. package/dist/testing/index.js.map +1 -0
  337. package/dist/testing/plans.d.ts +64 -0
  338. package/dist/testing/plans.d.ts.map +1 -0
  339. package/dist/testing/plans.js +47 -0
  340. package/dist/testing/plans.js.map +1 -0
  341. package/dist/types/context.d.ts +110 -0
  342. package/dist/types/context.d.ts.map +1 -0
  343. package/dist/types/context.js +2 -0
  344. package/dist/types/context.js.map +1 -0
  345. package/dist/types/contract.d.ts +198 -0
  346. package/dist/types/contract.d.ts.map +1 -0
  347. package/dist/types/contract.js +2 -0
  348. package/dist/types/contract.js.map +1 -0
  349. package/dist/types/index.d.ts +6 -0
  350. package/dist/types/index.d.ts.map +1 -0
  351. package/dist/types/index.js +11 -0
  352. package/dist/types/index.js.map +1 -0
  353. package/dist/types/inputs.d.ts +83 -0
  354. package/dist/types/inputs.d.ts.map +1 -0
  355. package/dist/types/inputs.js +2 -0
  356. package/dist/types/inputs.js.map +1 -0
  357. package/dist/types/rows.d.ts +76 -0
  358. package/dist/types/rows.d.ts.map +1 -0
  359. package/dist/types/rows.js +2 -0
  360. package/dist/types/rows.js.map +1 -0
  361. package/dist/types/secured-list.d.ts +396 -0
  362. package/dist/types/secured-list.d.ts.map +1 -0
  363. package/dist/types/secured-list.js +2 -0
  364. package/dist/types/secured-list.js.map +1 -0
  365. package/dist/unsafe.d.ts +162 -0
  366. package/dist/unsafe.d.ts.map +1 -0
  367. package/dist/unsafe.js +177 -0
  368. package/dist/unsafe.js.map +1 -0
  369. package/dist/validation/config-refusal.d.ts +16 -0
  370. package/dist/validation/config-refusal.d.ts.map +1 -0
  371. package/dist/validation/config-refusal.js +2 -0
  372. package/dist/validation/config-refusal.js.map +1 -0
  373. package/dist/validation/database-config.d.ts +19 -0
  374. package/dist/validation/database-config.d.ts.map +1 -0
  375. package/dist/validation/database-config.js +221 -0
  376. package/dist/validation/database-config.js.map +1 -0
  377. package/dist/validation/default-value.d.ts +21 -0
  378. package/dist/validation/default-value.d.ts.map +1 -0
  379. package/dist/validation/default-value.js +72 -0
  380. package/dist/validation/default-value.js.map +1 -0
  381. package/dist/validation/extension-packs.d.ts +11 -0
  382. package/dist/validation/extension-packs.d.ts.map +1 -0
  383. package/dist/validation/extension-packs.js +53 -0
  384. package/dist/validation/extension-packs.js.map +1 -0
  385. package/dist/validation/field-config.d.ts +35 -21
  386. package/dist/validation/field-config.d.ts.map +1 -1
  387. package/dist/validation/field-config.js +51 -41
  388. package/dist/validation/field-config.js.map +1 -1
  389. package/dist/validation/field-names.d.ts +15 -0
  390. package/dist/validation/field-names.d.ts.map +1 -0
  391. package/dist/validation/field-names.js +155 -0
  392. package/dist/validation/field-names.js.map +1 -0
  393. package/dist/validation/identifier-names.d.ts +4 -0
  394. package/dist/validation/identifier-names.d.ts.map +1 -0
  395. package/dist/validation/identifier-names.js +39 -0
  396. package/dist/validation/identifier-names.js.map +1 -0
  397. package/dist/validation/needs-closure.d.ts +20 -30
  398. package/dist/validation/needs-closure.d.ts.map +1 -1
  399. package/dist/validation/needs-closure.js +29 -105
  400. package/dist/validation/needs-closure.js.map +1 -1
  401. package/dist/validation/relations.d.ts +17 -0
  402. package/dist/validation/relations.d.ts.map +1 -0
  403. package/dist/validation/relations.js +268 -0
  404. package/dist/validation/relations.js.map +1 -0
  405. package/dist/validation/schema.d.ts.map +1 -1
  406. package/dist/validation/schema.js +1 -3
  407. package/dist/validation/schema.js.map +1 -1
  408. package/dist/where/like.d.ts +32 -0
  409. package/dist/where/like.d.ts.map +1 -0
  410. package/dist/where/like.js +42 -0
  411. package/dist/where/like.js.map +1 -0
  412. package/package.json +70 -8
  413. package/.turbo/turbo-build.log +0 -4
  414. package/CLAUDE.md +0 -609
  415. package/dist/access/access-filter.d.ts +0 -241
  416. package/dist/access/access-filter.d.ts.map +0 -1
  417. package/dist/access/access-filter.js +0 -692
  418. package/dist/access/access-filter.js.map +0 -1
  419. package/dist/access/access-filter.test.d.ts +0 -2
  420. package/dist/access/access-filter.test.d.ts.map +0 -1
  421. package/dist/access/access-filter.test.js +0 -1256
  422. package/dist/access/access-filter.test.js.map +0 -1
  423. package/dist/access/declared-dependencies.test.d.ts +0 -2
  424. package/dist/access/declared-dependencies.test.d.ts.map +0 -1
  425. package/dist/access/declared-dependencies.test.js +0 -226
  426. package/dist/access/declared-dependencies.test.js.map +0 -1
  427. package/dist/access/denied-relation-visibility.test.d.ts +0 -2
  428. package/dist/access/denied-relation-visibility.test.d.ts.map +0 -1
  429. package/dist/access/denied-relation-visibility.test.js +0 -114
  430. package/dist/access/denied-relation-visibility.test.js.map +0 -1
  431. package/dist/access/engine.test.d.ts +0 -2
  432. package/dist/access/engine.test.d.ts.map +0 -1
  433. package/dist/access/engine.test.js +0 -56
  434. package/dist/access/engine.test.js.map +0 -1
  435. package/dist/access/field-access.test.d.ts +0 -2
  436. package/dist/access/field-access.test.d.ts.map +0 -1
  437. package/dist/access/field-access.test.js +0 -635
  438. package/dist/access/field-access.test.js.map +0 -1
  439. package/dist/access/multi-column-read-write.test.d.ts +0 -2
  440. package/dist/access/multi-column-read-write.test.d.ts.map +0 -1
  441. package/dist/access/multi-column-read-write.test.js +0 -183
  442. package/dist/access/multi-column-read-write.test.js.map +0 -1
  443. package/dist/access/relationship-count.d.ts +0 -98
  444. package/dist/access/relationship-count.d.ts.map +0 -1
  445. package/dist/access/relationship-count.js +0 -262
  446. package/dist/access/relationship-count.js.map +0 -1
  447. package/dist/access/relationship-count.test.d.ts +0 -2
  448. package/dist/access/relationship-count.test.d.ts.map +0 -1
  449. package/dist/access/relationship-count.test.js +0 -247
  450. package/dist/access/relationship-count.test.js.map +0 -1
  451. package/dist/access/relationship-label-filter.d.ts +0 -45
  452. package/dist/access/relationship-label-filter.d.ts.map +0 -1
  453. package/dist/access/relationship-label-filter.js +0 -22
  454. package/dist/access/relationship-label-filter.js.map +0 -1
  455. package/dist/access/relationship-label-filter.test.d.ts +0 -2
  456. package/dist/access/relationship-label-filter.test.d.ts.map +0 -1
  457. package/dist/access/relationship-label-filter.test.js +0 -89
  458. package/dist/access/relationship-label-filter.test.js.map +0 -1
  459. package/dist/access/synthetic-include-read.test.d.ts +0 -2
  460. package/dist/access/synthetic-include-read.test.d.ts.map +0 -1
  461. package/dist/access/synthetic-include-read.test.js +0 -121
  462. package/dist/access/synthetic-include-read.test.js.map +0 -1
  463. package/dist/context/nested-operations.d.ts +0 -74
  464. package/dist/context/nested-operations.d.ts.map +0 -1
  465. package/dist/context/nested-operations.js +0 -847
  466. package/dist/context/nested-operations.js.map +0 -1
  467. package/dist/fields/calendar-day.test.d.ts +0 -2
  468. package/dist/fields/calendar-day.test.d.ts.map +0 -1
  469. package/dist/fields/calendar-day.test.js +0 -246
  470. package/dist/fields/calendar-day.test.js.map +0 -1
  471. package/dist/fields/format-prisma-default.d.ts +0 -35
  472. package/dist/fields/format-prisma-default.d.ts.map +0 -1
  473. package/dist/fields/format-prisma-default.js +0 -49
  474. package/dist/fields/format-prisma-default.js.map +0 -1
  475. package/dist/fields/format-prisma-default.test.d.ts +0 -2
  476. package/dist/fields/format-prisma-default.test.d.ts.map +0 -1
  477. package/dist/fields/format-prisma-default.test.js +0 -54
  478. package/dist/fields/format-prisma-default.test.js.map +0 -1
  479. package/dist/fields/relationship-foreign-key.test.d.ts +0 -2
  480. package/dist/fields/relationship-foreign-key.test.d.ts.map +0 -1
  481. package/dist/fields/relationship-foreign-key.test.js +0 -18
  482. package/dist/fields/relationship-foreign-key.test.js.map +0 -1
  483. package/dist/fields/select.test.d.ts +0 -2
  484. package/dist/fields/select.test.d.ts.map +0 -1
  485. package/dist/fields/select.test.js +0 -279
  486. package/dist/fields/select.test.js.map +0 -1
  487. package/dist/fields/text-keystone-compat.test.d.ts +0 -2
  488. package/dist/fields/text-keystone-compat.test.d.ts.map +0 -1
  489. package/dist/fields/text-keystone-compat.test.js +0 -93
  490. package/dist/fields/text-keystone-compat.test.js.map +0 -1
  491. package/dist/filter/filter.test.d.ts +0 -2
  492. package/dist/filter/filter.test.d.ts.map +0 -1
  493. package/dist/filter/filter.test.js +0 -431
  494. package/dist/filter/filter.test.js.map +0 -1
  495. package/dist/filter/serialize.test.d.ts +0 -2
  496. package/dist/filter/serialize.test.d.ts.map +0 -1
  497. package/dist/filter/serialize.test.js +0 -83
  498. package/dist/filter/serialize.test.js.map +0 -1
  499. package/dist/index.test.d.ts +0 -2
  500. package/dist/index.test.d.ts.map +0 -1
  501. package/dist/index.test.js +0 -33
  502. package/dist/index.test.js.map +0 -1
  503. package/dist/lib/case-utils.test.d.ts +0 -2
  504. package/dist/lib/case-utils.test.d.ts.map +0 -1
  505. package/dist/lib/case-utils.test.js +0 -123
  506. package/dist/lib/case-utils.test.js.map +0 -1
  507. package/dist/query/index.d.ts +0 -127
  508. package/dist/query/index.d.ts.map +0 -1
  509. package/dist/query/index.js +0 -179
  510. package/dist/query/index.js.map +0 -1
  511. package/dist/query/index.test.d.ts +0 -2
  512. package/dist/query/index.test.d.ts.map +0 -1
  513. package/dist/query/index.test.js +0 -632
  514. package/dist/query/index.test.js.map +0 -1
  515. package/dist/query/relationship-options.test.d.ts +0 -2
  516. package/dist/query/relationship-options.test.d.ts.map +0 -1
  517. package/dist/query/relationship-options.test.js +0 -150
  518. package/dist/query/relationship-options.test.js.map +0 -1
  519. package/dist/validation/field-config.test.d.ts +0 -2
  520. package/dist/validation/field-config.test.d.ts.map +0 -1
  521. package/dist/validation/field-config.test.js +0 -159
  522. package/dist/validation/field-config.test.js.map +0 -1
  523. package/dist/validation/schema.test.d.ts +0 -2
  524. package/dist/validation/schema.test.d.ts.map +0 -1
  525. package/dist/validation/schema.test.js +0 -409
  526. package/dist/validation/schema.test.js.map +0 -1
  527. package/docs/type-distribution-fix.md +0 -136
  528. package/src/access/access-filter.test.ts +0 -1919
  529. package/src/access/access-filter.ts +0 -1041
  530. package/src/access/declared-dependencies.test.ts +0 -277
  531. package/src/access/declared-dependencies.ts +0 -220
  532. package/src/access/denied-relation-visibility.test.ts +0 -173
  533. package/src/access/depth-limits.ts +0 -27
  534. package/src/access/engine.test.ts +0 -67
  535. package/src/access/engine.ts +0 -193
  536. package/src/access/errors.ts +0 -242
  537. package/src/access/field-access.test.ts +0 -792
  538. package/src/access/field-access.ts +0 -353
  539. package/src/access/field-transforms.ts +0 -68
  540. package/src/access/field-visibility.ts +0 -569
  541. package/src/access/index.ts +0 -82
  542. package/src/access/multi-column-read-write.test.ts +0 -305
  543. package/src/access/query-validation.ts +0 -486
  544. package/src/access/relationship-count.test.ts +0 -344
  545. package/src/access/relationship-count.ts +0 -346
  546. package/src/access/relationship-label-filter.test.ts +0 -128
  547. package/src/access/relationship-label-filter.ts +0 -63
  548. package/src/access/synthetic-include-read.test.ts +0 -179
  549. package/src/access/transaction-registry.ts +0 -58
  550. package/src/access/types.ts +0 -391
  551. package/src/config/index.ts +0 -177
  552. package/src/config/label.ts +0 -51
  553. package/src/config/nav-count.ts +0 -77
  554. package/src/config/plugin-engine.ts +0 -261
  555. package/src/config/types.ts +0 -3270
  556. package/src/context/apply-defaults.ts +0 -47
  557. package/src/context/hook-pipeline.ts +0 -137
  558. package/src/context/index.ts +0 -1737
  559. package/src/context/nested-operations.ts +0 -1470
  560. package/src/context/transaction-boundary.ts +0 -485
  561. package/src/context/write-pipeline.ts +0 -732
  562. package/src/extend.ts +0 -28
  563. package/src/fields/calendar-day.test.ts +0 -291
  564. package/src/fields/format-prisma-default.test.ts +0 -64
  565. package/src/fields/format-prisma-default.ts +0 -64
  566. package/src/fields/index.ts +0 -1901
  567. package/src/fields/relationship-foreign-key.test.ts +0 -29
  568. package/src/fields/select.test.ts +0 -336
  569. package/src/fields/text-keystone-compat.test.ts +0 -126
  570. package/src/filter/collect.ts +0 -108
  571. package/src/filter/filter.test.ts +0 -538
  572. package/src/filter/index.ts +0 -26
  573. package/src/filter/map.ts +0 -63
  574. package/src/filter/parse.ts +0 -99
  575. package/src/filter/serialize.test.ts +0 -115
  576. package/src/filter/serialize.ts +0 -73
  577. package/src/filter/types.ts +0 -120
  578. package/src/hooks/index.ts +0 -752
  579. package/src/index.test.ts +0 -50
  580. package/src/index.ts +0 -192
  581. package/src/internal.ts +0 -61
  582. package/src/lib/case-utils.test.ts +0 -159
  583. package/src/lib/case-utils.ts +0 -68
  584. package/src/lib/prisma-errors.ts +0 -86
  585. package/src/mcp/constants.ts +0 -24
  586. package/src/mcp/field-schema.ts +0 -84
  587. package/src/mcp/handler.ts +0 -633
  588. package/src/mcp/index.ts +0 -7
  589. package/src/mcp/projection.ts +0 -497
  590. package/src/mcp/types.ts +0 -18
  591. package/src/query/index.test.ts +0 -830
  592. package/src/query/index.ts +0 -352
  593. package/src/query/relationship-options.test.ts +0 -194
  594. package/src/query/relationship-options.ts +0 -78
  595. package/src/utils/password.ts +0 -80
  596. package/src/validation/field-config.test.ts +0 -199
  597. package/src/validation/field-config.ts +0 -139
  598. package/src/validation/needs-closure.ts +0 -188
  599. package/src/validation/schema.test.ts +0 -487
  600. package/src/validation/schema.ts +0 -51
  601. package/tests/access-relationships.test.ts +0 -646
  602. package/tests/access.test.ts +0 -590
  603. package/tests/apply-defaults.test.ts +0 -119
  604. package/tests/bare-read-scalars.test.ts +0 -147
  605. package/tests/computed-field-selective-evaluation.test.ts +0 -418
  606. package/tests/config.test.ts +0 -242
  607. package/tests/context.test.ts +0 -3146
  608. package/tests/default-value-create.test.ts +0 -299
  609. package/tests/field-hooks.test.ts +0 -410
  610. package/tests/field-types.test.ts +0 -1291
  611. package/tests/hook-context-secured.test.ts +0 -424
  612. package/tests/hook-pipeline.test.ts +0 -233
  613. package/tests/hooks.test.ts +0 -481
  614. package/tests/interactive-transaction.test.ts +0 -444
  615. package/tests/label.test.ts +0 -135
  616. package/tests/mcp-fields-projection-access.test.ts +0 -250
  617. package/tests/mcp-handler.test.ts +0 -1411
  618. package/tests/multi-column-validation-ordering.test.ts +0 -305
  619. package/tests/nav-count.test.ts +0 -189
  620. package/tests/needs-declared-dependencies.test.ts +0 -503
  621. package/tests/nested-access-and-hooks.test.ts +0 -2566
  622. package/tests/nested-operation-registry.test.ts +0 -317
  623. package/tests/nested-write-hooks.test.ts +0 -864
  624. package/tests/nested-write-synthetic-relation.test.ts +0 -437
  625. package/tests/password-type-distribution.test.ts +0 -154
  626. package/tests/password-types.test.ts +0 -174
  627. package/tests/password.test.ts +0 -263
  628. package/tests/plugin-engine.test.ts +0 -1155
  629. package/tests/prisma-errors.test.ts +0 -141
  630. package/tests/resolve-chain.test.ts +0 -411
  631. package/tests/singleton.test.ts +0 -421
  632. package/tests/sudo.test.ts +0 -676
  633. package/tests/to-one-include-access-filter.test.ts +0 -172
  634. package/tests/transaction-boundary-hooks.test.ts +0 -710
  635. package/tests/transaction-boundary-outermost.test.ts +0 -530
  636. package/tests/virtual-fields.test.ts +0 -421
  637. package/tests/with-session.test.ts +0 -243
  638. package/tests/write-pipeline.test.ts +0 -617
  639. package/tsconfig.json +0 -12
  640. package/tsconfig.tsbuildinfo +0 -1
  641. package/vitest.config.ts +0 -69
@@ -1,1041 +0,0 @@
1
- import type { Session, AccessContext, PrismaFilter } from './types.js'
2
- import type { OpenSaasConfig, FieldConfig, ListConfig } from '../config/types.js'
3
- import {
4
- checkAccess,
5
- getRelatedListConfig,
6
- resolveSyntheticReverseRelation,
7
- listSyntheticReverseRelationNames,
8
- } from './engine.js'
9
- import { READ_INCLUDE_MAX_DEPTH } from './depth-limits.js'
10
- import {
11
- AccessScopeDepthExceededError,
12
- RelationFilterAccessDeniedError,
13
- UndeclaredCountKeyError,
14
- UndeclaredIncludeKeyError,
15
- } from './errors.js'
16
- import {
17
- LOGICAL_OPERATORS,
18
- RELATION_QUANTIFIERS,
19
- resolveQueryField,
20
- validateQueryFieldReadAccess,
21
- validateQueryKeys,
22
- walkWhereReadAccess,
23
- type ResolveSyntheticRelation,
24
- type SyntheticRelationTarget,
25
- } from './query-validation.js'
26
- import { isToManyRelationshipField, resolveCountAccessEntryForList } from './relationship-count.js'
27
- import { getDbKey } from '../lib/case-utils.js'
28
-
29
- /**
30
- * Access Filter — phase 1 of the two-phase read (pre-query).
31
- *
32
- * This module scopes which rows and relationships the database is allowed to
33
- * return, before the query runs. It evaluates *operation-level* `query` access
34
- * on related lists and turns the results into a Prisma `include`/`where` clause,
35
- * so denied rows and relations never leave the database.
36
- *
37
- * Phase 2 (post-query field stripping + `resolveOutput` + virtual computation)
38
- * lives in `field-visibility.ts`. The two phases cannot be merged: virtual
39
- * fields are computed in JavaScript and post-query field access can depend on
40
- * the fetched row, neither of which is expressible in SQL. See
41
- * `docs/adr/0001-access-control-is-a-two-phase-read.md` and the access-control
42
- * glossary in `CONTEXT.md`.
43
- *
44
- * **Caller-directed (ADR-0026).** `buildAccessScopedInclude` walks only the
45
- * branches `requestedInclude` names — the caller's own `include`, a fragment
46
- * `query`'s projection, or `foldDeclaredDependencies`'s fold of a field's
47
- * `needs` (`declared-dependencies.ts`), all resolved before this module ever
48
- * runs. Naming a relation fetches that relation's own columns and stops
49
- * (the "One hop" rule, see `CONTEXT.md`); reaching further means the request
50
- * named a nested `include` there too. A relation nobody named never has its
51
- * list's `query` access evaluated at all — there is no separate "build the
52
- * whole tree, then reconcile against what was asked for" pass to walk it.
53
- *
54
- * **To-one relations are scoped after the query, not inside it (issue #974).**
55
- * Prisma accepts a nested `where` on an `include` entry only for a to-**many**
56
- * relation; the same shape on a to-**one** relation raises
57
- * `PrismaClientValidationError`. So for a to-one relation whose related
58
- * list's `query` access resolves to a filter, `buildAccessScopedInclude`
59
- * does NOT attach that filter as `where` — it fetches the row unscoped and
60
- * records the filter in the returned `toOneAccessFilters` tree instead.
61
- * `resolveToOneAccessVisibility` (below) turns that tree into the set of
62
- * related ids the session may actually see, via ONE batched `id IN (...)`
63
- * existence check per (relation, nesting level) across every row in the
64
- * read — never a per-row query, and never a hand-rolled evaluation of the
65
- * access filter (it is handed to Prisma exactly as `checkAccess` produced
66
- * it). `field-visibility.ts`'s `filterReadableFields` is where the result
67
- * actually becomes `null` for a row the check excludes — see its module doc.
68
- * A to-one relation whose related list's `query` access is `true` (no
69
- * filter) or `false` (denied outright) needs no existence check at all: the
70
- * former is left as `true` in `include`, unchanged from before; the latter
71
- * is recorded as `{ kind: 'denied' }`, and `field-visibility.ts` forces the
72
- * key to `null` without ever asking Prisma for it.
73
- *
74
- * **A denied to-many relation is recorded the same way (issue #1103).** An
75
- * outright `query` denial (`=== false`) drops the relation from `include`
76
- * regardless of arity — a to-many relation has no more of a `where` shape
77
- * for "denied" than a to-one one does. Before this, only the to-one branch
78
- * recorded anything; a denied to-many key was simply absent from the raw
79
- * row, and `filterReadableFields`'s main loop — which only ever visits keys
80
- * `Object.entries(workingItem)` actually contains — had nothing to force it
81
- * with, so it stayed missing from the result instead of coming back `[]`.
82
- * Now both arities record `{ kind: 'denied' }` in `toOneAccessFilters`, and
83
- * `field-visibility.ts`'s post-query pass forces the key present using the
84
- * field's own declared arity: `null` for a to-one relation, `[]` for a
85
- * to-many one.
86
- *
87
- * **A synthetic back-relation is a declared relationship wherever a caller
88
- * can name one (issue #1082).** A list-only `ref` (`ref: 'Other'`,
89
- * no target field) makes schema generation synthesize a back-relation on
90
- * `Other` (`from_<List>_<field>`) because the ORM requires an opposite field
91
- * there — but no list config declares it, so it is absent from `Other`'s own
92
- * `fieldConfigs`. An include key that fails the declared-relationship test is
93
- * resolved via `resolveSyntheticReverseRelation` before being treated as
94
- * unrecognised; a hit is scoped exactly like the declared relationship field
95
- * it stands for (its owning list's `query` access, folded `where`, nested
96
- * recursion, depth), always as a to-many relation — a list-only ref has one
97
- * construction site and no arity branch. A key that resolves to neither a
98
- * declared relationship nor a synthetic one is **rejected** (`_count` is
99
- * handled separately, scoped per named relation rather than resolved as a
100
- * single relationship key — see `buildAccessScopedCountSelect` below and
101
- * issue #1087), restoring this module's own denial rule for the one key
102
- * shape that used to fail open.
103
- *
104
- * **A `where`/`orderBy` a caller nests inside an `include` entry is validated
105
- * against the RELATED list, before it is AND-folded (issue #1092).** Before
106
- * this, the AND-fold below merged a caller's nested `where` with the access
107
- * filter and passed the result through unexamined — a key naming a field the
108
- * session cannot read reached Prisma, a probing oracle over exactly the
109
- * fields #915 exists to close one level up. `validateQueryKeys`/
110
- * `validateQueryFieldReadAccess` (`query-validation.ts`, #912/#915) are
111
- * reused as-is, called here against `relatedConfig` instead of the current
112
- * list — the same two checks the top-level `where`/`orderBy` already gets.
113
- * The one addition this position needs: an unresolved key is tried against
114
- * `resolveSyntheticReverseRelation` (via `validateQueryKeys`'s
115
- * `resolveSyntheticRelation` hook) before being rejected, so a nested
116
- * predicate naming a synthetic back-relation resolves rather than throwing.
117
- *
118
- * Those two checks only reach the entry's OWN top-level keys. A relation
119
- * quantifier (`some`/`every`/`none`/`is`/`isNot`) nested inside that `where`
120
- * names a list one hop further away, which needs the same treatment the
121
- * top-level `where` already gets from `buildAccessScopedWhere` (#916,
122
- * below): scope it by THAT list's own `query` access and check its fields'
123
- * read access, recursing through every further hop. Skipping this for a
124
- * to-many entry would just move #1092's oracle one hop further out instead
125
- * of closing it — `buildAccessScopedWhere` is called on `requestedEntry.where`
126
- * before the AND-fold, with its own `resolveSyntheticRelation` param (below)
127
- * so a synthetic key stays resolved at this deeper level too.
128
- */
129
-
130
- /** The structured (object) form of a relation include entry — caller/fold-supplied or produced by this module. */
131
- type IncludeEntryObject = {
132
- where?: PrismaFilter
133
- include?: Record<string, unknown>
134
- take?: number
135
- orderBy?: PrismaFilter | PrismaFilter[]
136
- skip?: number
137
- }
138
-
139
- /** A plain object — excludes `null` and arrays, which `typeof x === 'object'` alone would admit. */
140
- function isPlainObject(value: unknown): value is Record<string, unknown> {
141
- return typeof value === 'object' && value !== null && !Array.isArray(value)
142
- }
143
-
144
- /**
145
- * Narrow an unknown include value to the structured object form (vs bare `true`
146
- * or any other primitive). Caller-supplied includes arrive untyped at the
147
- * runtime boundary: narrow to a plain object first, then validate each field's
148
- * own type before trusting it, rather than casting the whole value wholesale.
149
- *
150
- * A numeric `take` on a to-many relation include (a caller-supplied row bound,
151
- * issue #752), and a caller-supplied `orderBy`/`skip` (#851), are carried
152
- * through the same way: none of the three can ever widen the result past the
153
- * access `where` — they only narrow or reorder rows the access filter already
154
- * admits — so preserving them is access-neutral.
155
- */
156
- function asEntryObject(value: unknown): IncludeEntryObject | null {
157
- if (!isPlainObject(value)) return null
158
- const { where, include, take, orderBy, skip } = value
159
- const entry: IncludeEntryObject = {}
160
- if (isPlainObject(where)) entry.where = where
161
- if (isPlainObject(include)) entry.include = include
162
- if (typeof take === 'number') entry.take = take
163
- if (isPlainObject(orderBy) || Array.isArray(orderBy)) {
164
- entry.orderBy = orderBy as PrismaFilter | PrismaFilter[]
165
- }
166
- if (typeof skip === 'number') entry.skip = skip
167
- return entry
168
- }
169
-
170
- /**
171
- * AND-combine an access `where` with a caller-supplied nested `where`.
172
- *
173
- * The access filter is authoritative: the caller's filter may only NARROW the
174
- * result further, never widen past what access permits. We therefore wrap both
175
- * in a Prisma `AND` so neither can override the other.
176
- */
177
- function andWhere(
178
- accessWhere: PrismaFilter | undefined,
179
- callerWhere: PrismaFilter | undefined,
180
- ): PrismaFilter | undefined {
181
- if (accessWhere && callerWhere) {
182
- return { AND: [accessWhere, callerWhere] }
183
- }
184
- return accessWhere ?? callerWhere
185
- }
186
-
187
- /**
188
- * One relation's recorded access filter, or an outright denial — see the
189
- * module doc's "To-one relations" section. `kind: 'scoped'` is to-one only
190
- * (a to-many filter is attached as Prisma `where` instead, never recorded
191
- * here); `kind: 'denied'` is recorded for BOTH arities (issue #1103) — a
192
- * to-many relation has no `where`-based way to record "zero rows, and the
193
- * key itself absent" either, so it shares the same post-query mechanism a
194
- * denied to-one already used.
195
- */
196
- export type ToOneAccessFilterEntry =
197
- { kind: 'scoped'; relatedListName: string; accessWhere: PrismaFilter } | { kind: 'denied' }
198
-
199
- /**
200
- * Which relations, at which nesting level of an `include`, need a post-query
201
- * fixup rather than a Prisma-side `where` — a to-one relation whose related
202
- * list's `query` access resolved to a filter (`kind: 'scoped'`) or a denial
203
- * (`kind: 'denied'`), Prisma cannot express either as a nested `where` on a
204
- * to-one include; a to-many relation whose related list denies `query`
205
- * access outright (`kind: 'denied'` only — a to-many filter is attached as
206
- * `where` and never reaches this tree) is dropped from `include` entirely,
207
- * so nothing marks its key present in the raw row either (issue #1103).
208
- * `resolveToOneAccessVisibility` consumes this tree; `filterReadableFields`
209
- * (`field-visibility.ts`) applies its result — forcing a denied key to `null`
210
- * for a to-one relation, `[]` for a to-many one.
211
- */
212
- export type ToOneAccessFilterTree = {
213
- /** Relation keys at THIS level needing a post-query fixup. */
214
- filters: Record<string, ToOneAccessFilterEntry>
215
- /** Per-key trees for relations present in the include for other reasons, whose own nested include may contain further filters. */
216
- nested: Record<string, ToOneAccessFilterTree>
217
- }
218
-
219
- export function emptyToOneAccessFilterTree(): ToOneAccessFilterTree {
220
- return { filters: {}, nested: {} }
221
- }
222
-
223
- function isToOneAccessFilterTreeEmpty(tree: ToOneAccessFilterTree): boolean {
224
- return Object.keys(tree.filters).length === 0 && Object.keys(tree.nested).length === 0
225
- }
226
-
227
- /**
228
- * Whether a relationship field is to-one (at most one related row) rather
229
- * than to-many. Exported so `field-visibility.ts` can pick the same `null`
230
- * (to-one) vs `[]` (to-many) shape for a denied relation's forced value
231
- * (issue #1103) that this module used to decide whether to record the
232
- * denial in the first place — one source of truth for arity, not two.
233
- */
234
- export function isToOneRelationship(fieldConfig: FieldConfig): boolean {
235
- return !('many' in fieldConfig && fieldConfig.many === true)
236
- }
237
-
238
- /**
239
- * Which `_count.select` keys, at which nesting level of an `include`, were
240
- * denied outright by their related list's `query` access — omitted from the
241
- * `_count.select` sent to Prisma (issue #1087), so the row Prisma returns
242
- * either lacks the key entirely or lacks a `_count` object at all. Consumed
243
- * post-query by `filterReadableFields` (`field-visibility.ts`), which injects
244
- * `0` for each — a count is a session-relative value, and `0` is what "no
245
- * visible rows" means for it, never an absent key (mirroring the to-one
246
- * `null` injection this module already does for issue #974, though a denied
247
- * count needs no existence check: `0` requires no query at all).
248
- */
249
- export type CountAccessDenialTree = {
250
- /** `_count.select` keys denied at THIS level. */
251
- keys: Set<string>
252
- /** Per-relation trees for relations present in the include for other reasons, whose own nested include may contain a further `_count`. */
253
- nested: Record<string, CountAccessDenialTree>
254
- }
255
-
256
- export function emptyCountAccessDenialTree(): CountAccessDenialTree {
257
- return { keys: new Set(), nested: {} }
258
- }
259
-
260
- function isCountAccessDenialTreeEmpty(tree: CountAccessDenialTree): boolean {
261
- return tree.keys.size === 0 && Object.keys(tree.nested).length === 0
262
- }
263
-
264
- /**
265
- * Normalize a caller's `_count` include value to the `_count.select` map it
266
- * names. `true` (Prisma's "count every relation" shorthand) expands to every
267
- * countable relation Prisma itself carries on this model — every DECLARED
268
- * to-many relationship, plus every synthetic back-relation a list-only `ref`
269
- * elsewhere in the config synthesizes onto it (issue #1082): Prisma's own
270
- * `_count: true` has always counted both, and dropping the synthetic ones
271
- * here would silently stop counting a relation the caller used to get a
272
- * (previously unscoped) count for. Returns `null` for a shape that requests
273
- * nothing countable (`false`, or an object with no usable `select`).
274
- */
275
- function normalizeCountSelect(
276
- requestedValue: unknown,
277
- fieldConfigs: Record<string, FieldConfig>,
278
- listKey: string,
279
- config: OpenSaasConfig,
280
- ): Record<string, unknown> | null {
281
- if (requestedValue === true) {
282
- const expanded: Record<string, unknown> = {}
283
- for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
284
- if (isToManyRelationshipField(fieldConfig)) expanded[fieldName] = true
285
- }
286
- for (const syntheticName of listSyntheticReverseRelationNames(listKey, config)) {
287
- expanded[syntheticName] = true
288
- }
289
- return expanded
290
- }
291
- if (isPlainObject(requestedValue) && isPlainObject(requestedValue.select)) {
292
- return requestedValue.select
293
- }
294
- return null
295
- }
296
-
297
- /**
298
- * Scope a caller-supplied `_count` include value by each named relation's own
299
- * `query` access — the `_count` counterpart to the rest of this module's
300
- * relation scoping (issue #1087, closing the one key `buildAccessScopedInclude`
301
- * used to allowlist through unscoped, #1082's "Out of scope").
302
- *
303
- * For each key in the caller's `_count.select` (or, for bare `_count: true`,
304
- * every declared to-many relation — see `normalizeCountSelect`):
305
- * - Not a declared to-many relationship and not a synthetic back-relation
306
- * (#1082 — always genuinely countable) → THROWN as
307
- * `UndeclaredCountKeyError`, matching `buildAccessScopedInclude`'s own
308
- * rejection for the ordinary walk. A declared to-many relationship whose
309
- * `ref` cannot be resolved is skipped instead, matching that same walk's
310
- * handling of a config-level dangling ref (not a caller error).
311
- * - The counting list's OWN field-level `read` access on the relationship
312
- * field being counted denies it, or the related list's `query` access
313
- * denies it (`=== false`) → omitted from the select sent to Prisma and
314
- * added to the returned `deniedKeys`, so `filterReadableFields` can inject
315
- * `0` post-query (a count is session-relative; denial doesn't mean "no such
316
- * relation"). The field-level check has nothing to evaluate for a synthetic
317
- * back-relation (no field of its own on this list) and is skipped for it —
318
- * `resolveCountAccessEntryForList` (issue #1111). Both checks run BEFORE
319
- * any caller-supplied nested `where` is validated — validating first would
320
- * let a caller who cannot read a single row of the related list learn its
321
- * field names and field-level read rules from a thrown `ValidationError`
322
- * alone, reopening the exact oracle #915/ADR-0031 closed for a top-level
323
- * predicate.
324
- * - Otherwise → the caller-supplied nested `where` at that key (if any) is
325
- * key- and read-access-validated against the RELATED list via the same
326
- * `validateQueryKeys`/`validateQueryFieldReadAccess` primitives
327
- * `createFindMany` already runs on a top-level `where` (#912/#915), then
328
- * run through `buildAccessScopedWhere` — the same fold `createFindMany`
329
- * applies to a top-level `where` (#916) — so a relation filter nested
330
- * inside IT (e.g. `_count.select.posts.where.comments.some`) is scoped by
331
- * THAT further list's own `query` access too, not just the counted
332
- * relation's. The result is AND-combined with the counted relation's own
333
- * access filter (if any) — reusing `andWhere`, never replacing the
334
- * caller's condition, mirroring `buildAccessScopedInclude` itself.
335
- */
336
- async function buildAccessScopedCountSelect(
337
- requestedValue: unknown,
338
- fieldConfigs: Record<string, FieldConfig>,
339
- args: { session: Session | null; context: AccessContext },
340
- config: OpenSaasConfig,
341
- listKey: string,
342
- ): Promise<{ select: Record<string, unknown> | undefined; deniedKeys: Set<string> }> {
343
- const requestedSelect = normalizeCountSelect(requestedValue, fieldConfigs, listKey, config)
344
- const deniedKeys = new Set<string>()
345
- if (!requestedSelect) return { select: undefined, deniedKeys }
346
-
347
- const select: Record<string, unknown> = {}
348
-
349
- for (const [key, entryValue] of Object.entries(requestedSelect)) {
350
- // A caller can explicitly exclude a key from the `true`-expanded set the
351
- // same way Prisma's own `select` excludes a field.
352
- if (entryValue === false) continue
353
-
354
- const fieldConfig = fieldConfigs[key]
355
- const isDeclaredToMany = isToManyRelationshipField(fieldConfig)
356
-
357
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
358
- let relatedConfig: { listName: string; listConfig: ListConfig<any> } | null = null
359
- if (isDeclaredToMany && fieldConfig && 'ref' in fieldConfig) {
360
- relatedConfig = getRelatedListConfig(fieldConfig.ref as string, config)
361
- }
362
- if (!relatedConfig && !isDeclaredToMany) {
363
- const synthetic = resolveSyntheticReverseRelation(key, listKey, config)
364
- if (synthetic) {
365
- relatedConfig = {
366
- listName: synthetic.sourceListName,
367
- listConfig: synthetic.sourceListConfig,
368
- }
369
- }
370
- }
371
- if (!relatedConfig) {
372
- // A declared to-many field whose `ref` didn't resolve is a config
373
- // issue, not a caller error — skip it exactly like the ordinary
374
- // include walk does for the same case.
375
- if (isDeclaredToMany) continue
376
- throw new UndeclaredCountKeyError(listKey, key)
377
- }
378
-
379
- // `fieldConfig` is undefined for a synthetic back-relation (#1082) — it has
380
- // no field of its own on THIS list, so there is no field-level `read`
381
- // access to fold in; `resolveCountAccessEntryForList` treats a missing
382
- // `fieldAccess` as exempt, matching the ordinary include path's own lack
383
- // of a field-level gate for a synthetic key (issue #1111).
384
- const accessEntry = await resolveCountAccessEntryForList(
385
- relatedConfig.listConfig,
386
- args,
387
- fieldConfig?.access,
388
- )
389
-
390
- // Denial is checked BEFORE the caller's nested `where` is validated —
391
- // mirroring `buildAccessScopedWhere`'s own ordering below. A fully denied
392
- // relation counts `0` no matter what `where` the caller supplied, so
393
- // validating it first would let a caller who cannot read a single row of
394
- // the related list learn its field names and field-level read rules from
395
- // a `ValidationError`'s message alone — the exact oracle #915/ADR-0031
396
- // closed for a top-level predicate, reopened here if this ran first.
397
- if (accessEntry.kind === 'denied') {
398
- deniedKeys.add(key)
399
- continue
400
- }
401
-
402
- const requestedWhere =
403
- isPlainObject(entryValue) && isPlainObject(entryValue.where)
404
- ? (entryValue.where as Record<string, unknown>)
405
- : undefined
406
-
407
- let scopedRequestedWhere: Record<string, unknown> | undefined
408
- if (requestedWhere) {
409
- // Mirrors `buildAccessScopedInclude`'s own `resolveSyntheticRelation`
410
- // closure (#1092/#916) — a synthetic back-relation (#1082) named inside
411
- // this count entry's own `where` resolves against its source list
412
- // rather than being rejected as undeclared, at every hop.
413
- const resolveSyntheticRelation = (
414
- syntheticKey: string,
415
- fromListName: string,
416
- ): SyntheticRelationTarget | null => {
417
- const synthetic = resolveSyntheticReverseRelation(syntheticKey, fromListName, config)
418
- return synthetic
419
- ? { listConfig: synthetic.sourceListConfig, listName: synthetic.sourceListName }
420
- : null
421
- }
422
- validateQueryKeys({
423
- where: requestedWhere,
424
- listConfig: relatedConfig.listConfig,
425
- listName: relatedConfig.listName,
426
- config,
427
- isSudo: false,
428
- resolveSyntheticRelation,
429
- })
430
- await validateQueryFieldReadAccess({
431
- where: requestedWhere,
432
- listConfig: relatedConfig.listConfig,
433
- listName: relatedConfig.listName,
434
- session: args.session,
435
- context: args.context,
436
- isSudo: false,
437
- })
438
- // A relation filter (`some`/`every`/`none`/`is`/`isNot`) nested inside
439
- // the caller's own `where` names a THIRD list one hop further out —
440
- // e.g. `_count.select.posts.where.comments.some`. Validation above only
441
- // checked keys against the counted relation's OWN fields; without this,
442
- // that nested relation would reach Prisma unscoped by ITS list's
443
- // `query` access, letting the resulting count reveal whether
444
- // inaccessible rows over there exist. `buildAccessScopedWhere` is the
445
- // same fold `createFindMany` runs on an ordinary top-level `where`
446
- // (#916) — reused here rather than re-derived.
447
- scopedRequestedWhere = (await buildAccessScopedWhere(
448
- requestedWhere,
449
- relatedConfig.listConfig,
450
- relatedConfig.listName,
451
- config,
452
- args,
453
- resolveSyntheticRelation,
454
- )) as Record<string, unknown> | undefined
455
- }
456
-
457
- const scopedWhere = andWhere(
458
- accessEntry.kind === 'scoped' ? accessEntry.where : undefined,
459
- scopedRequestedWhere,
460
- )
461
- select[key] = scopedWhere ? { where: scopedWhere } : true
462
- }
463
-
464
- return { select: Object.keys(select).length > 0 ? select : undefined, deniedKeys }
465
- }
466
-
467
- /**
468
- * Build the access-scoped `include` for exactly the relations a read
469
- * requested, recursing only into branches `requestedInclude` itself names.
470
- *
471
- * For each key in `requestedInclude`:
472
- * - A declared field that isn't a relationship (scalar, virtual, …) → access
473
- * control does not govern it; passed through unchanged (a virtual key is
474
- * stripped later by `stripVirtualFieldsFromInclude`, #628).
475
- * - Not declared at all → resolved via `resolveSyntheticReverseRelation`
476
- * (the synthetic-back-relation case above); `_count` is scoped by
477
- * `buildAccessScopedCountSelect` (issue #1087 — each named relation's own
478
- * `query` access, exactly like any other relation this walk scopes; a
479
- * denied one is recorded for `filterReadableFields` to inject `0` for,
480
- * post-query, since Prisma cannot be asked for a guaranteed `0`); anything
481
- * else throws `UndeclaredIncludeKeyError` rather than reaching the
482
- * database unscoped.
483
- * - A declared relationship whose related list's `query` access denies it
484
- * (`=== false`) → dropped entirely, no matter what the request asked for
485
- * nested beneath it (#566): the caller chooses *which* relations, access
486
- * control chooses *whether* and *with what filter*. This denial is also
487
- * recorded in `toOneAccessFilters` (`kind: 'denied'`), for either arity, so
488
- * `filterReadableFields` can still surface an explicit `null` (to-one,
489
- * issue #974) or `[]` (to-many, issue #1103) for it rather than an absent
490
- * key.
491
- * - Otherwise, for a to-**many** relation → the access `where` is
492
- * AND-combined with any caller-supplied nested `where` (never replaced —
493
- * the other half of #566), and a caller-supplied `take` rides through
494
- * unchanged (#752). For a to-**one** relation → the access filter (if any)
495
- * is recorded in `toOneAccessFilters` instead of attached as `where`,
496
- * because Prisma only accepts a nested `where` on a to-many include
497
- * (issue #974) — the entry itself never carries a `where` for a to-one key.
498
- * - Either way — the "One hop" rule (ADR-0026) — nested relations are scoped
499
- * ONLY if `requestedInclude` itself named a nested `include` here. A bare
500
- * relation (or one with no nested `include`) fetches its own columns and
501
- * stops: no recursive call, no access evaluation on anything beneath it.
502
- *
503
- * **Depth is a cost limit, not a cycle guard (ADR-0026).** A `requestedInclude`
504
- * is always a finite literal — the caller's own object, or
505
- * `foldDeclaredDependencies`'s already-cycle-guarded fold — so this recursion
506
- * cannot loop unboundedly on its own; nothing here walks the relationship
507
- * graph unprompted. `READ_INCLUDE_MAX_DEPTH` still bounds how deep a request
508
- * may reach, fail-closed per ADR-0022: a request naming anything at or past
509
- * the cap throws `AccessScopeDepthExceededError` rather than silently
510
- * returning less than what was asked for.
511
- */
512
- export async function buildAccessScopedInclude(
513
- requestedInclude: Record<string, unknown>,
514
- fieldConfigs: Record<string, FieldConfig>,
515
- args: {
516
- session: Session | null
517
- context: AccessContext
518
- },
519
- config: OpenSaasConfig,
520
- listKey: string,
521
- depth: number = 0,
522
- ): Promise<{
523
- include: Record<string, unknown>
524
- toOneAccessFilters: ToOneAccessFilterTree
525
- countDenials: CountAccessDenialTree
526
- }> {
527
- const requestedKeys = Object.keys(requestedInclude)
528
- if (depth >= READ_INCLUDE_MAX_DEPTH && requestedKeys.length > 0) {
529
- throw new AccessScopeDepthExceededError(listKey, requestedKeys[0], depth)
530
- }
531
-
532
- const result: Record<string, unknown> = {}
533
- const toOneAccessFilters = emptyToOneAccessFilterTree()
534
- const countDenials = emptyCountAccessDenialTree()
535
-
536
- for (const [relationName, requestedValue] of Object.entries(requestedInclude)) {
537
- // A caller can explicitly opt a relation OUT of a Prisma include with
538
- // `false` (its `Include` type allows a bare `boolean`), or end up with an
539
- // `undefined` value from a conditionally-built object
540
- // (`{ posts: cond ? true : undefined }`) — Prisma treats both the same as
541
- // the key being absent. Treat them identically here too, before this key
542
- // ever reaches access evaluation: an explicitly-disabled relation was
543
- // never actually requested, so it must not be evaluated, dropped as
544
- // "denied", or recorded as needing a post-query fixup (issue #1103 code
545
- // review) — any of those would surface a value (`[]`/`null`) for a key
546
- // the caller deliberately excluded.
547
- if (requestedValue === false || requestedValue === undefined) continue
548
-
549
- const fieldConfig = fieldConfigs[relationName]
550
- const isDeclaredRelationship =
551
- fieldConfig?.type === 'relationship' && 'ref' in fieldConfig && !!fieldConfig.ref
552
-
553
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
554
- let relatedConfig: { listName: string; listConfig: ListConfig<any> } | null
555
- let isToOne: boolean
556
-
557
- if (isDeclaredRelationship) {
558
- relatedConfig = getRelatedListConfig(fieldConfig.ref as string, config)
559
- if (!relatedConfig) continue
560
- isToOne = isToOneRelationship(fieldConfig)
561
- } else if (fieldConfig) {
562
- // A declared field that is not a relationship — access control does
563
- // not govern it here; passed through unchanged.
564
- result[relationName] = requestedValue
565
- continue
566
- } else if (relationName === '_count') {
567
- const { select, deniedKeys } = await buildAccessScopedCountSelect(
568
- requestedValue,
569
- fieldConfigs,
570
- args,
571
- config,
572
- listKey,
573
- )
574
- if (select) result[relationName] = { select }
575
- if (deniedKeys.size > 0) countDenials.keys = deniedKeys
576
- continue
577
- } else {
578
- const synthetic = resolveSyntheticReverseRelation(relationName, listKey, config)
579
- if (!synthetic) {
580
- throw new UndeclaredIncludeKeyError(listKey, relationName)
581
- }
582
- relatedConfig = { listName: synthetic.sourceListName, listConfig: synthetic.sourceListConfig }
583
- // Always to-many — a list-only ref has one construction site and no
584
- // arity branch, and one-to-one is structurally impossible for it.
585
- isToOne = false
586
- }
587
-
588
- const queryAccess = relatedConfig.listConfig.access?.operation?.query
589
- const accessResult = await checkAccess(queryAccess, {
590
- session: args.session,
591
- context: args.context,
592
- })
593
-
594
- if (accessResult === false) {
595
- // Recorded for either arity (issue #1103) — a to-many relation is
596
- // dropped from `include` here exactly like a to-one one, and needs the
597
- // same post-query fixup so its key comes back `[]`, not silently
598
- // absent from the row.
599
- toOneAccessFilters.filters[relationName] = { kind: 'denied' }
600
- continue
601
- }
602
-
603
- const accessWhere = typeof accessResult === 'object' ? accessResult : undefined
604
- const requestedEntry = asEntryObject(requestedValue)
605
-
606
- // #1092 — a nested `where`/`orderBy` gets the same #912/#915 checks the
607
- // top-level `where`/`orderBy` already gets, resolved against the RELATED
608
- // list (`relatedConfig`) rather than the current one, and run only once
609
- // the relation is known to be accessible at all (same ordering reason as
610
- // the top-level checks: don't leak a field's name/read-gating status to a
611
- // caller who has zero access to the relation to begin with).
612
- const resolveSyntheticRelation = (
613
- key: string,
614
- fromListName: string,
615
- ): SyntheticRelationTarget | null => {
616
- const synthetic = resolveSyntheticReverseRelation(key, fromListName, config)
617
- return synthetic
618
- ? { listConfig: synthetic.sourceListConfig, listName: synthetic.sourceListName }
619
- : null
620
- }
621
- validateQueryKeys({
622
- where: requestedEntry?.where,
623
- orderBy: requestedEntry?.orderBy,
624
- listConfig: relatedConfig.listConfig,
625
- listName: relatedConfig.listName,
626
- config,
627
- isSudo: false,
628
- resolveSyntheticRelation,
629
- })
630
- await validateQueryFieldReadAccess({
631
- where: requestedEntry?.where,
632
- orderBy: requestedEntry?.orderBy,
633
- listConfig: relatedConfig.listConfig,
634
- listName: relatedConfig.listName,
635
- session: args.session,
636
- context: args.context,
637
- isSudo: false,
638
- })
639
-
640
- // The two checks above only reach the entry's own top-level keys — a
641
- // relation quantifier (`some`/`every`/`none`/`is`/`isNot`) nested inside
642
- // this `where` names a DEEPER related list, which needs the same
643
- // treatment the top-level `where` already gets from `buildAccessScopedWhere`
644
- // (#916): scope it by that deeper list's own `query` access and check ITS
645
- // fields' read access, recursing through every further hop. Only for
646
- // to-many — a to-one entry never carries `requestedEntry.where` through to
647
- // Prisma at all (see below), so there is nothing here to scope.
648
- const scopedRequestedWhere =
649
- !isToOne && requestedEntry?.where !== undefined
650
- ? ((await buildAccessScopedWhere(
651
- requestedEntry.where,
652
- relatedConfig.listConfig,
653
- relatedConfig.listName,
654
- config,
655
- args,
656
- resolveSyntheticRelation,
657
- )) as PrismaFilter)
658
- : requestedEntry?.where
659
-
660
- let nestedInclude: Record<string, unknown> | undefined
661
- let nestedToOneFilters: ToOneAccessFilterTree | undefined
662
- let nestedCountDenials: CountAccessDenialTree | undefined
663
- if (requestedEntry?.include) {
664
- const nested = await buildAccessScopedInclude(
665
- requestedEntry.include,
666
- relatedConfig.listConfig.fields,
667
- args,
668
- config,
669
- relatedConfig.listName,
670
- depth + 1,
671
- )
672
- nestedInclude = nested.include
673
- nestedToOneFilters = nested.toOneAccessFilters
674
- nestedCountDenials = nested.countDenials
675
- }
676
-
677
- const entry: IncludeEntryObject = {}
678
- if (isToOne) {
679
- if (accessWhere) {
680
- toOneAccessFilters.filters[relationName] = {
681
- kind: 'scoped',
682
- relatedListName: relatedConfig.listName,
683
- accessWhere,
684
- }
685
- }
686
- } else {
687
- const mergedWhere = andWhere(accessWhere, scopedRequestedWhere)
688
- if (mergedWhere) entry.where = mergedWhere
689
- if (requestedEntry?.take !== undefined) entry.take = requestedEntry.take
690
- if (requestedEntry?.orderBy !== undefined) entry.orderBy = requestedEntry.orderBy
691
- if (requestedEntry?.skip !== undefined) entry.skip = requestedEntry.skip
692
- }
693
- if (nestedInclude && Object.keys(nestedInclude).length > 0) entry.include = nestedInclude
694
- if (nestedToOneFilters && !isToOneAccessFilterTreeEmpty(nestedToOneFilters)) {
695
- toOneAccessFilters.nested[relationName] = nestedToOneFilters
696
- }
697
- if (nestedCountDenials && !isCountAccessDenialTreeEmpty(nestedCountDenials)) {
698
- countDenials.nested[relationName] = nestedCountDenials
699
- }
700
-
701
- result[relationName] = Object.keys(entry).length > 0 ? entry : true
702
- }
703
-
704
- return { include: result, toOneAccessFilters, countDenials }
705
- }
706
-
707
- /**
708
- * One relation's resolved post-query visibility — see
709
- * `resolveToOneAccessVisibility`. `kind: 'visible'` (an existence check
710
- * against a set of ids) only ever arises for a to-one relation, since only a
711
- * to-one `kind: 'scoped'` filter entry produces one; `kind: 'denied'` passes
712
- * straight through unresolved for either arity.
713
- */
714
- export type ToOneVisibility = { kind: 'denied' } | { kind: 'visible'; ids: ReadonlySet<string> }
715
-
716
- /** The resolved counterpart to {@link ToOneAccessFilterTree}, produced by `resolveToOneAccessVisibility`. */
717
- export type ToOneAccessVisibilityTree = {
718
- filters: Record<string, ToOneVisibility>
719
- nested: Record<string, ToOneAccessVisibilityTree>
720
- }
721
-
722
- export function emptyToOneAccessVisibilityTree(): ToOneAccessVisibilityTree {
723
- return { filters: {}, nested: {} }
724
- }
725
-
726
- /**
727
- * Resolve a `ToOneAccessFilterTree` against the RAW rows Prisma already
728
- * fetched (unscoped for the flagged to-one relations — see
729
- * `buildAccessScopedInclude`) into the set of related ids the session may
730
- * actually see, one batched `id IN (...)` existence check per (relation,
731
- * nesting level) across every row in `items` — never once per row.
732
- *
733
- * For each `filters` entry at a level:
734
- * - `kind: 'denied'` → carried straight through; no query, nothing to check.
735
- * - `kind: 'scoped'` → every id present at this key across ALL of `items` is
736
- * collected first (an empty set skips the query entirely — nothing to
737
- * check), then ONE `findMany` through the RAW `prisma` client (not
738
- * `context.db`, which would re-evaluate the same access-control function a
739
- * second time) asks which of those ids also satisfy `accessWhere` — the
740
- * exact `PrismaFilter` `checkAccess` already produced, handed to Prisma
741
- * unmodified rather than interpreted by hand.
742
- *
743
- * Recurses into `nested` by flattening the related items reached through
744
- * each key across every row in `items` (a to-many hop contributes every one
745
- * of its rows; a to-one hop contributes its single row, if any) into the
746
- * next level's own `items` array, so a to-one relation nested arbitrarily
747
- * deep is still resolved with one batched query per node, not per parent row.
748
- */
749
- export async function resolveToOneAccessVisibility(
750
- items: readonly unknown[],
751
- tree: ToOneAccessFilterTree,
752
- args: {
753
- session: Session | null
754
- context: AccessContext
755
- },
756
- ): Promise<ToOneAccessVisibilityTree> {
757
- const resolved = emptyToOneAccessVisibilityTree()
758
-
759
- for (const [key, entry] of Object.entries(tree.filters)) {
760
- if (entry.kind === 'denied') {
761
- resolved.filters[key] = { kind: 'denied' }
762
- continue
763
- }
764
-
765
- const ids = new Set<string>()
766
- for (const item of items) {
767
- if (!item || typeof item !== 'object') continue
768
- const value = (item as Record<string, unknown>)[key]
769
- if (value && typeof value === 'object' && 'id' in value) {
770
- ids.add(String((value as Record<string, unknown>).id))
771
- }
772
- }
773
-
774
- if (ids.size === 0) {
775
- resolved.filters[key] = { kind: 'visible', ids: new Set() }
776
- continue
777
- }
778
-
779
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic model access by list name, mirroring the rest of the read pipeline
780
- const model = (args.context.prisma as any)[getDbKey(entry.relatedListName)]
781
- const visibleRows = await model.findMany({
782
- where: { AND: [entry.accessWhere, { id: { in: [...ids] } }] },
783
- select: { id: true },
784
- })
785
- const visibleIds = new Set<string>(
786
- Array.isArray(visibleRows) ? visibleRows.map((row: { id: unknown }) => String(row.id)) : [],
787
- )
788
- resolved.filters[key] = { kind: 'visible', ids: visibleIds }
789
- }
790
-
791
- for (const [key, nestedTree] of Object.entries(tree.nested)) {
792
- const nestedItems: unknown[] = []
793
- for (const item of items) {
794
- if (!item || typeof item !== 'object') continue
795
- const value = (item as Record<string, unknown>)[key]
796
- if (Array.isArray(value)) nestedItems.push(...value)
797
- else if (value && typeof value === 'object') nestedItems.push(value)
798
- }
799
- resolved.nested[key] = await resolveToOneAccessVisibility(nestedItems, nestedTree, args)
800
- }
801
-
802
- return resolved
803
- }
804
-
805
- /**
806
- * Scope every relation filter (`some`/`every`/`none`/`is`/`isNot`) nested in a
807
- * caller's `where` by the related list's own `query` access — the `where`
808
- * counterpart to `buildAccessScopedInclude` above, closing #916 (the
809
- * "unclosed half of ADR-0022"). `include` and `where` are fundamentally
810
- * different requests (which relations come back, vs. which parent rows
811
- * match), so this is a distinct function, but it shares every primitive that
812
- * matters: `checkAccess`/`getRelatedListConfig` (the same access-evaluation
813
- * calls `buildAccessScopedInclude` makes), `andWhere` (the same AND-fold), and
814
- * `resolveQueryField`/`LOGICAL_OPERATORS`/`RELATION_QUANTIFIERS`/
815
- * `walkWhereReadAccess` (the same shape-recognition and field-read check
816
- * `query-validation.ts` already uses for #912/#915) — there is no second,
817
- * parallel implementation of any of those decisions.
818
- *
819
- * For each relationship key found (at any depth — the walk recurses through
820
- * `AND`/`OR`/`NOT` and through every hop of a chain):
821
- * - Not a declared relationship, or the key #912 already rejected (this walk
822
- * runs strictly after that check) → passed through unchanged.
823
- * - The related list's `query` access denies it (`=== false`) → THROWS
824
- * `RelationFilterAccessDeniedError`. Unlike `buildAccessScopedInclude`'s
825
- * silent drop, this is a loud failure: a `where` predicate has no neutral
826
- * "not requested" outcome the way a missing `include` key does, so a
827
- * silently-empty match would itself be a distinguishable signal (ADR-0022).
828
- * - Otherwise → the access filter (if any) is AND-combined into the relation
829
- * quantifier's nested clause (never replacing the caller's own condition,
830
- * mirroring `andWhere`'s include-side contract), keys inside that nested
831
- * clause are checked against the RELATED list's field-level `read` access
832
- * (`walkWhereReadAccess`, closing #915's stated gap for this path), and the
833
- * walk recurses into the related list's own fields for a further hop.
834
- *
835
- * One quantifier is deliberately conservative rather than exactly precise:
836
- * folding the access filter into `every`'s nested clause with a plain AND
837
- * makes `every` require every related row to be BOTH access-visible AND
838
- * matching, not "every access-visible row matches" (the latter needs a
839
- * `NOT`/`OR` transform this does not attempt). The conservative version can
840
- * only reject a query that the precise version would allow — it never
841
- * widens what a caller can learn — so it is the safe direction to ship; a
842
- * caller can observe that an inaccessible related row exists (an `every`
843
- * that "should" pass instead fails), but never that row's field values,
844
- * which is the property this ticket exists to close.
845
- *
846
- * A quantifier's value of literal `null` (`is: null`/`isNot: null`, a to-one
847
- * relation's existence check) is passed through untouched rather than folded:
848
- * it names no fields to read-check or scope, and AND-folding an access filter
849
- * into it would silently invert the caller's own predicate (see the inline
850
- * comment at that branch).
851
- *
852
- * `resolveSyntheticRelation` (#1092/#1108) extends this to a key that
853
- * resolves to a synthetic back-relation (#1082) rather than a declared
854
- * field, recursing against its SOURCE list. Only `buildAccessScopedInclude`
855
- * passes it, for the include-nested `where` position; the top-level `where`
856
- * this function was originally built for (`context/index.ts`) omits it, so
857
- * a synthetic key there is unaffected — matching #1092's own scope, which
858
- * deliberately left the top-level checks unchanged.
859
- */
860
- export async function buildAccessScopedWhere(
861
- where: unknown,
862
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
863
- listConfig: ListConfig<any>,
864
- listName: string,
865
- config: OpenSaasConfig,
866
- args: {
867
- session: Session | null
868
- context: AccessContext
869
- },
870
- // #1092/#1108 — the include-nested position's own addition, exactly
871
- // mirroring `validateQueryKeys`'s `resolveSyntheticRelation` (see that
872
- // module's doc comment): every top-level `where` caller omits this, so
873
- // top-level behavior is unchanged.
874
- resolveSyntheticRelation?: ResolveSyntheticRelation,
875
- ): Promise<unknown> {
876
- if (where === null || typeof where !== 'object') return where
877
-
878
- if (Array.isArray(where)) {
879
- return Promise.all(
880
- where.map((entry) =>
881
- buildAccessScopedWhere(entry, listConfig, listName, config, args, resolveSyntheticRelation),
882
- ),
883
- )
884
- }
885
-
886
- const result: Record<string, unknown> = {}
887
-
888
- for (const [key, value] of Object.entries(where as Record<string, unknown>)) {
889
- if (LOGICAL_OPERATORS.has(key)) {
890
- result[key] = await buildAccessScopedWhere(
891
- value,
892
- listConfig,
893
- listName,
894
- config,
895
- args,
896
- resolveSyntheticRelation,
897
- )
898
- continue
899
- }
900
-
901
- const resolved = resolveQueryField(key, listConfig.fields)
902
- // A synthetic back-relation (#1082) carries no `ref` of its own to
903
- // follow — its "related" list for recursion is the SOURCE list it
904
- // stands for, given directly by the resolver, not `getRelatedListConfig`.
905
- const related = resolved?.isRelationship
906
- ? getRelatedListConfig(resolved.fieldConfig.ref, config)
907
- : !resolved
908
- ? (() => {
909
- const synthetic = resolveSyntheticRelation?.(key, listName)
910
- return synthetic
911
- ? { listConfig: synthetic.listConfig, listName: synthetic.listName }
912
- : null
913
- })()
914
- : null
915
-
916
- if (!related || value === null || typeof value !== 'object' || Array.isArray(value)) {
917
- result[key] = value
918
- continue
919
- }
920
-
921
- const queryAccess = related.listConfig.access?.operation?.query
922
- const accessResult = await checkAccess(queryAccess, {
923
- session: args.session,
924
- context: args.context,
925
- })
926
-
927
- if (accessResult === false) {
928
- throw new RelationFilterAccessDeniedError(listName, key, related.listName)
929
- }
930
-
931
- const accessWhere = typeof accessResult === 'object' ? accessResult : undefined
932
- const relationEntries = Object.entries(value as Record<string, unknown>)
933
- const hasQuantifier = relationEntries.some(([k]) => RELATION_QUANTIFIERS.has(k))
934
-
935
- if (hasQuantifier) {
936
- const nestedEntry: Record<string, unknown> = {}
937
- for (const [quantifier, quantifierValue] of relationEntries) {
938
- if (!RELATION_QUANTIFIERS.has(quantifier)) {
939
- nestedEntry[quantifier] = quantifierValue
940
- continue
941
- }
942
- if (quantifierValue === null) {
943
- // `is: null` / `isNot: null` tests EXISTENCE of a to-one relation,
944
- // not its fields — there is nothing to read-check or scope, and
945
- // AND-folding the access filter in here would silently invert the
946
- // caller's predicate: `is: null` ("has no related row") would
947
- // become `is: <accessWhere>` ("has a related row matching the
948
- // filter"), the opposite of what was asked. Passed through as-is.
949
- nestedEntry[quantifier] = null
950
- continue
951
- }
952
- await walkWhereReadAccess(quantifierValue, related.listConfig, related.listName, args)
953
- const scopedNested = await buildAccessScopedWhere(
954
- quantifierValue,
955
- related.listConfig,
956
- related.listName,
957
- config,
958
- args,
959
- resolveSyntheticRelation,
960
- )
961
- nestedEntry[quantifier] = accessWhere
962
- ? andWhere(accessWhere, scopedNested as PrismaFilter | undefined)
963
- : scopedNested
964
- }
965
- result[key] = nestedEntry
966
- } else {
967
- // Prisma's direct-nesting to-one form: the whole value IS the nested
968
- // WHERE clause, with no `is` wrapper. Only wrap it in `is` when there is
969
- // an access filter to fold in — an unwrapped value that needs no fold
970
- // (fully-allowed related list) is returned exactly as the caller wrote
971
- // it, so an already-permitted query is not perturbed by this pass.
972
- await walkWhereReadAccess(value, related.listConfig, related.listName, args)
973
- const scopedNested = await buildAccessScopedWhere(
974
- value,
975
- related.listConfig,
976
- related.listName,
977
- config,
978
- args,
979
- resolveSyntheticRelation,
980
- )
981
- result[key] = accessWhere
982
- ? { is: andWhere(accessWhere, scopedNested as PrismaFilter | undefined) }
983
- : scopedNested
984
- }
985
- }
986
-
987
- return result
988
- }
989
-
990
- /**
991
- * Remove keys that correspond to `virtual` fields from a Prisma `include`
992
- * object, recursing into nested relationship includes using the related
993
- * list's field configs.
994
- *
995
- * Virtual fields are computed in JavaScript (via `resolveOutput` in
996
- * `field-visibility.ts`) and have no database column. Naming one in `include`
997
- * type-checks — the generated `Include` type lists every field the config
998
- * declares — but Prisma throws `Unknown field '<name>' for include statement`
999
- * at runtime. This is applied as the final step on whatever `include` a read
1000
- * op ends up with (fragment-built, access-controlled merge, or sudo
1001
- * passthrough) so a virtual key never reaches the Prisma client, regardless
1002
- * of which path produced it. The virtual value is still computed
1003
- * unconditionally by `filterReadableFields`, so stripping the include key has
1004
- * no effect on whether the value appears in the result (#628).
1005
- */
1006
- export function stripVirtualFieldsFromInclude(
1007
- include: Record<string, unknown> | undefined,
1008
- fieldConfigs: Record<string, FieldConfig>,
1009
- config: OpenSaasConfig,
1010
- ): Record<string, unknown> | undefined {
1011
- if (!include) return include
1012
-
1013
- const result: Record<string, unknown> = {}
1014
-
1015
- for (const [key, value] of Object.entries(include)) {
1016
- const fieldConfig = fieldConfigs[key]
1017
-
1018
- if (fieldConfig?.virtual) continue
1019
-
1020
- const isDeclaredRelationship =
1021
- fieldConfig?.type === 'relationship' && 'ref' in fieldConfig && !!fieldConfig.ref
1022
- const entry = asEntryObject(value)
1023
-
1024
- if (isDeclaredRelationship && entry?.include) {
1025
- const relatedConfig = getRelatedListConfig(fieldConfig.ref as string, config)
1026
- if (relatedConfig) {
1027
- const nestedInclude = stripVirtualFieldsFromInclude(
1028
- entry.include,
1029
- relatedConfig.listConfig.fields,
1030
- config,
1031
- )
1032
- result[key] = { ...(value as Record<string, unknown>), include: nestedInclude }
1033
- continue
1034
- }
1035
- }
1036
-
1037
- result[key] = value
1038
- }
1039
-
1040
- return result
1041
- }