@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
@@ -1,25 +1,28 @@
1
1
  import { checkAccess } from '../access/index.js';
2
- import { resolveSyntheticReverseRelation } from '../access/engine.js';
3
- import { ValidationError, DatabaseError } from '../hooks/index.js';
2
+ import { bindSessionReadOnly, sealSession } from '../access/actor.js';
3
+ import { ValidationError, DatabaseError, isPlainObject } from '../hooks/index.js';
4
4
  import { databaseErrorMessage, normalizeDatabaseError } from '../lib/prisma-errors.js';
5
5
  import { isClientSafeError } from '../lib/client-safe-error.js';
6
6
  import { nullPrototypeRegistry } from '../lib/null-prototype-registry.js';
7
7
  import { warnOnce } from '../lib/warn-once.js';
8
- import { createSecuredRead, orderableKeys } from '../secured/read.js';
9
- import { createLivenessProbe, createRowLockLane, RowLockUnavailableError, unusableRowLockLane, } from '../secured/lock.js';
8
+ import { createSecuredRead } from '../secured/read.js';
9
+ import { invalidatesSessionVocabularyOf, withOwnSessionVocabulary, } from '../access/session-vocabulary.js';
10
+ import { withSingletonProbe } from './write-target.js';
11
+ import { createLivenessProbe, createRowLockLane, RowLockUnavailableError, RowLockLaneUnavailableError, unusableRowLockLane, } from '../secured/lock.js';
10
12
  import { createUnsafeSurface, createUnsafeTransactionSurface, unavailableUnsafeSurface, } from '../unsafe.js';
11
13
  import { ENGINE_FACE, engineContextOf } from './engine-context.js';
12
14
  import { withOrigin } from '../origin.js';
13
15
  import { getRelationshipOptions, isRelationshipLabelReadable, relationshipSearchWhere, } from '../query/relationship-options.js';
14
16
  import { runWritePipeline, createWriteStrategy, updateWriteStrategy, deleteWriteStrategy, } from './write-pipeline.js';
15
- import { resolveJunctionEdge, ownsForeignKey } from './junction.js';
16
- import { isRelationshipField } from '../fields/index.js';
17
+ import { resolveJunctionEdge, resolveLinkableScope } from './junction.js';
18
+ import { assertListMembersResolve, resolveListMember, resolveListMembers, } from '../members/list-members.js';
17
19
  import { parseListId } from '../contract/id-boundary.js';
18
20
  import { reportAfterTransactionFailures } from './transaction-boundary.js';
19
21
  import { isRowActionAvailable } from '../config/row-action-availability.js';
20
22
  import { checkHasAccess } from '../config/has-access-gate.js';
21
23
  import { TransactionRegistry, TransactionRolledBackError, } from '../access/transaction-registry.js';
22
24
  export { TransactionRolledBackError };
25
+ const LINKED_TARGET_LIMIT = 1000;
23
26
  /**
24
27
  * Warn once per (list, operation) when a caller passes `select` to a read op
25
28
  * that ignores it. See "Narrowing Reads" in packages/core/CLAUDE.md.
@@ -32,30 +35,30 @@ function warnIfSelectIgnored(args, listName, operation) {
32
35
  `Narrow a read with \`include\`, or with \`.select()\` on the secured surface, instead. ` +
33
36
  `See https://stack.opensaas.au/docs/concepts/queries`);
34
37
  }
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
36
- function isSingletonList(listConfig) {
37
- return !!listConfig.isSingleton;
38
- }
39
- /**
40
- * Whether `fieldName` on `listKey` owns a foreign-key column — the property
41
- * `removeRelated`/`createRelated`/`linkRelated` actually need, rather than
42
- * `many`, which is only a proxy for it: a non-owning to-one (the inverse half
43
- * of a one-to-one) has `many` falsy exactly like an owning one, and a
44
- * synthetic `from_<List>_<field>` back-relation is never declared, so it is
45
- * never itself in `listConfig.fields` (#1442).
46
- */
47
- function classifyBackReference(listKey, fieldName,
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
49
- listConfig, config) {
50
- const field = listConfig.fields[fieldName];
51
- if (isRelationshipField(field)) {
52
- return ownsForeignKey(config, listKey, fieldName, field)
53
- ? { kind: 'owning', field }
54
- : { kind: 'nonOwning' };
38
+ function classifyBackReference(listKey, fieldName, config) {
39
+ const member = resolveListMember(config, listKey, fieldName);
40
+ if (member === undefined)
41
+ return { kind: 'notRelationship' };
42
+ switch (member.kind) {
43
+ case 'field': {
44
+ const { storage } = member;
45
+ if (storage.kind !== 'relation')
46
+ return { kind: 'notRelationship' };
47
+ return storage.foreignKey === undefined
48
+ ? { kind: 'nonOwning' }
49
+ : { kind: 'owning', relatedList: storage.relatedList, foreignKey: storage.foreignKey.name };
50
+ }
51
+ case 'syntheticBackRelation':
52
+ return { kind: 'nonOwning' };
53
+ case 'foreignKey':
54
+ case 'partColumn':
55
+ case 'systemField':
56
+ return { kind: 'notRelationship' };
57
+ default: {
58
+ const unhandled = member;
59
+ return unhandled;
60
+ }
55
61
  }
56
- return resolveSyntheticReverseRelation(fieldName, listKey, config) !== null
57
- ? { kind: 'nonOwning' }
58
- : { kind: 'notRelationship' };
59
62
  }
60
63
  function isRawRowCollection(value) {
61
64
  return (typeof value === 'object' &&
@@ -92,12 +95,8 @@ function assertIdentityWhere(where, listName, terminal) {
92
95
  ], {});
93
96
  }
94
97
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
95
- function shouldAutoCreate(listConfig) {
96
- if (!listConfig.isSingleton)
97
- return false;
98
- if (typeof listConfig.isSingleton === 'boolean')
99
- return true;
100
- return listConfig.isSingleton.autoCreate !== false;
98
+ function autoCreates(listConfig) {
99
+ return typeof listConfig.isSingleton !== 'object' || listConfig.isSingleton.autoCreate !== false;
101
100
  }
102
101
  /**
103
102
  * Thrown when `context.transaction()` can neither open a transaction nor join
@@ -344,13 +343,19 @@ function transactionFace(base, lock, owner) {
344
343
  advisoryLock: async (key) => {
345
344
  if (lock === undefined)
346
345
  throw new RowLockUnavailableError('advisoryLock()');
347
- await lock.advisory(key);
346
+ try {
347
+ await lock.advisory(key);
348
+ }
349
+ catch (error) {
350
+ owner.poison(error);
351
+ throw error;
352
+ }
348
353
  },
349
354
  sudo: () => transactionFace(base.sudo(), lock, owner),
350
355
  withSession: (session) => transactionFace(base.withSession(session), lock, owner),
351
356
  transaction: (fn) => joinNested(fn, face, owner),
352
357
  };
353
- return face;
358
+ return bindSessionReadOnly(face);
354
359
  }
355
360
  // A database failure reaches the client as the stack's own message (ADR-0042),
356
361
  // so the driver's diagnostic text — carried on `cause` — reaches no channel at
@@ -446,7 +451,7 @@ function assertValidSession(session) {
446
451
  if (undefinedKeys.length > 0)
447
452
  throw new InvalidSessionError(undefinedKeys);
448
453
  }
449
- export function getContext(config, ormHandle, session, storage, _isSudo = false,
454
+ export function getContext(config, ormHandle, givenSession, storage, _isSudo = false,
450
455
  // Internal: when rebuilding the context against a transaction client, reuse the
451
456
  // already-initialised plugin services rather than re-running plugin runtimes.
452
457
  _sharedPlugins,
@@ -474,7 +479,8 @@ _unsafeTransaction,
474
479
  // carried through by `sudo()`/`withSession()` unchanged; omitted for a
475
480
  // fresh, non-transactional context, which is its own base.
476
481
  _baseContext) {
477
- assertValidSession(session);
482
+ assertValidSession(givenSession);
483
+ const session = givenSession === null ? null : sealSession(givenSession);
478
484
  // Broad type to allow dynamic model access; populated by populateDbDelegate below.
479
485
  const db = {};
480
486
  const openTransaction = transactionOpenerFor(config, client, _unsafeTransaction);
@@ -516,8 +522,9 @@ _baseContext) {
516
522
  _rowLock: lock,
517
523
  _config: config,
518
524
  _baseContext,
519
- _orderableKeys: (listKey, keys) => orderableKeys(context, config, listKey, keys),
520
525
  };
526
+ withOwnSessionVocabulary(context, config);
527
+ withSingletonProbe(context);
521
528
  populateDbDelegate(db, config, ormHandle, context, lock);
522
529
  // Skipped when reusing shared plugins (transaction rebind) so runtimes — and
523
530
  // any side effects they carry — run exactly once per top-level context.
@@ -598,11 +605,11 @@ _baseContext) {
598
605
  return { bulkAction: false, error: 'Access denied' };
599
606
  }
600
607
  try {
601
- const result = await action.handler({
608
+ const result = await transaction(async (tx) => action.handler({
602
609
  listKey: props.listKey,
603
610
  ids: props.ids,
604
- context,
605
- });
611
+ context: engineContextOf(tx),
612
+ }));
606
613
  return { bulkAction: true, message: result?.message };
607
614
  }
608
615
  catch (error) {
@@ -686,7 +693,7 @@ _baseContext) {
686
693
  if (!props.field) {
687
694
  return { removed: false, error: 'Missing back-reference field for disconnect' };
688
695
  }
689
- const backRef = classifyBackReference(props.listKey, props.field, listConfig, config);
696
+ const backRef = classifyBackReference(props.listKey, props.field, config);
690
697
  if (backRef.kind === 'notRelationship') {
691
698
  return {
692
699
  removed: false,
@@ -712,9 +719,8 @@ _baseContext) {
712
719
  // owning foreign-key column `<field>Id` (relationship-input.ts),
713
720
  // which is the key a bare read (no `include`) returns it under.
714
721
  const fieldName = props.field;
715
- const foreignKeyColumn = `${fieldName}Id`;
716
- const parentListKey = backRef.field.ref.split('.')[0];
717
- const expectedParentId = parseId(parentListKey, props.parentId);
722
+ const foreignKeyColumn = backRef.foreignKey;
723
+ const expectedParentId = parseId(backRef.relatedList, props.parentId);
718
724
  if (expectedParentId === null) {
719
725
  return { removed: false, error: 'Access denied or operation failed' };
720
726
  }
@@ -811,7 +817,7 @@ _baseContext) {
811
817
  }
812
818
  const data = { ...props.data };
813
819
  if (props.field && props.parentId) {
814
- const backRef = classifyBackReference(props.listKey, props.field, listConfig, config);
820
+ const backRef = classifyBackReference(props.listKey, props.field, config);
815
821
  // The back-reference must name a relationship field on this list; a
816
822
  // non-relationship field would otherwise receive a nonsensical
817
823
  // { connect } value. Also hardening — the drawer only ever passes a
@@ -833,7 +839,7 @@ _baseContext) {
833
839
  `Link the parent from the side that holds the column.`,
834
840
  };
835
841
  }
836
- const parentId = parseId(backRef.field.ref.split('.')[0], props.parentId);
842
+ const parentId = parseId(backRef.relatedList, props.parentId);
837
843
  if (parentId === null) {
838
844
  return { created: false, error: 'Access denied or operation failed' };
839
845
  }
@@ -909,26 +915,26 @@ _baseContext) {
909
915
  // update returns `null`, surfaced as `{ linked: false }` with a generic
910
916
  // reason.
911
917
  if (props.action === 'linkRelated') {
912
- const backRef = classifyBackReference(props.listKey, props.field, listConfig, config);
913
- if (backRef.kind === 'notRelationship') {
914
- return {
915
- linked: false,
916
- error: `Field "${props.field}" on list "${props.listKey}" is not a relationship field`,
917
- };
918
- }
919
- if (backRef.kind === 'nonOwning') {
920
- return {
921
- linked: false,
922
- error: `Cannot link through "${props.field}": a non-owning back-reference owns no foreign key. ` +
923
- `Link the parent from the side that holds the column.`,
924
- };
925
- }
926
- const relatedId = parseId(props.listKey, props.id);
927
- const parentId = parseId(backRef.field.ref.split('.')[0], props.parentId);
928
- if (relatedId === null || parentId === null) {
929
- return { linked: false, error: 'Access denied or operation failed' };
930
- }
931
918
  try {
919
+ const backRef = classifyBackReference(props.listKey, props.field, config);
920
+ if (backRef.kind === 'notRelationship') {
921
+ return {
922
+ linked: false,
923
+ error: `Field "${props.field}" on list "${props.listKey}" is not a relationship field`,
924
+ };
925
+ }
926
+ if (backRef.kind === 'nonOwning') {
927
+ return {
928
+ linked: false,
929
+ error: `Cannot link through "${props.field}": a non-owning back-reference owns no foreign key. ` +
930
+ `Link the parent from the side that holds the column.`,
931
+ };
932
+ }
933
+ const relatedId = parseId(props.listKey, props.id);
934
+ const parentId = parseId(backRef.relatedList, props.parentId);
935
+ if (relatedId === null || parentId === null) {
936
+ return { linked: false, error: 'Access denied or operation failed' };
937
+ }
932
938
  const result = await model.update({
933
939
  where: { id: relatedId },
934
940
  data: { [props.field]: { connect: { id: parentId } } },
@@ -974,13 +980,38 @@ _baseContext) {
974
980
  error: `Field "${props.field}" on list "${props.listKey}" is not a relationship field`,
975
981
  };
976
982
  }
977
- const relatedListKey = fieldConfig.ref.split('.')[0];
983
+ const linkedTargetIds = async (linkable) => {
984
+ const rows = await context.db[linkable.junctionListKey]
985
+ .where(linkable.linkedRows)
986
+ .limit(LINKED_TARGET_LIMIT)
987
+ .all();
988
+ return rows.flatMap((row) => {
989
+ const id = row[linkable.targetForeignKey];
990
+ return typeof id === 'string' || typeof id === 'number' ? [id] : [];
991
+ });
992
+ };
993
+ let relatedListKey = fieldConfig.ref.split('.')[0];
994
+ let scope;
995
+ if (props.linkableFor !== undefined) {
996
+ const parentId = parseId(props.listKey, props.linkableFor);
997
+ if (parentId === null)
998
+ return { options: [], searched: false };
999
+ const linkable = resolveLinkableScope(config, props.listKey, props.field, parentId);
1000
+ if (linkable === null)
1001
+ return { options: [], searched: false };
1002
+ relatedListKey = linkable.listKey;
1003
+ scope =
1004
+ linkable.kind === 'where'
1005
+ ? linkable.where
1006
+ : { id: { notIn: await linkedTargetIds(linkable) } };
1007
+ }
978
1008
  const labelReadable = await isRelationshipLabelReadable(context, config, relatedListKey);
979
1009
  const options = await getRelationshipOptions(context, config, relatedListKey, {
980
1010
  labelReadable,
981
1011
  search: props.search,
982
1012
  take: props.take,
983
1013
  selectedIds: props.selectedIds,
1014
+ scope,
984
1015
  });
985
1016
  const searched = relationshipSearchWhere(config, relatedListKey, {
986
1017
  search: props.search,
@@ -1017,24 +1048,24 @@ _baseContext) {
1017
1048
  }
1018
1049
  // Bypasses access control; hooks and validation still run.
1019
1050
  function sudo() {
1020
- return getContext(config, ormHandle, session, context.storage, true, undefined,
1051
+ return vocabularyDerivedFrom(context, getContext(config, ormHandle, session, context.storage, true, undefined,
1021
1052
  // ADR-0028: a sudo write issued from inside an owned transaction (e.g.
1022
1053
  // `tx.sudo().db.x.create()`) must still defer to that owner.
1023
1054
  context._transactionOwner, client, _unsafeTransaction,
1024
1055
  // Carry the same base-client context forward (issue #1348) — sudo()
1025
1056
  // derives a new context, not a new transaction boundary.
1026
- context._baseContext);
1057
+ context._baseContext));
1027
1058
  }
1028
1059
  // Substitutes the session; access control and hooks still run against it
1029
1060
  // (orthogonal to `sudo`, so the receiver's sudo state is preserved).
1030
1061
  function withSession(newSession) {
1031
- return getContext(config, ormHandle, newSession, context.storage, _isSudo, undefined,
1062
+ return vocabularyDerivedFrom(context, getContext(config, ormHandle, newSession, context.storage, _isSudo, undefined,
1032
1063
  // ADR-0028: a write issued from inside an owned transaction (e.g.
1033
1064
  // `tx.withSession(s).db.x.create()`) must still defer to that owner.
1034
1065
  context._transactionOwner, client, _unsafeTransaction,
1035
1066
  // Carry the same base-client context forward (issue #1348) — withSession()
1036
1067
  // derives a new context, not a new transaction boundary.
1037
- context._baseContext);
1068
+ context._baseContext));
1038
1069
  }
1039
1070
  // Interactive, hook-firing transaction (#614). See the `transaction` doc on
1040
1071
  // `StackContext` above for the atomicity/isolation/retry contract.
@@ -1053,7 +1084,11 @@ _baseContext) {
1053
1084
  }
1054
1085
  const registry = new TransactionRegistry();
1055
1086
  const ormClient = ormHandle;
1056
- const settled = runTransactionBody(fn, registry, ormClient);
1087
+ const settled = runTransactionBody(fn, registry, ormClient).then((result) => {
1088
+ if (registry.hasWritten)
1089
+ context._invalidateSessionVocabulary?.();
1090
+ return result;
1091
+ });
1057
1092
  return settleTransactionOwner(settled, registry, config);
1058
1093
  }
1059
1094
  function runTransactionBody(fn, registry, ormClient) {
@@ -1118,7 +1153,12 @@ _baseContext) {
1118
1153
  _isSudo,
1119
1154
  [ENGINE_FACE]: context,
1120
1155
  };
1121
- return returned;
1156
+ bindSessionReadOnly(context);
1157
+ return bindSessionReadOnly(returned);
1158
+ }
1159
+ function vocabularyDerivedFrom(receiver, derived) {
1160
+ invalidatesSessionVocabularyOf(engineContextOf(derived), receiver);
1161
+ return derived;
1122
1162
  }
1123
1163
  /**
1124
1164
  * Complete the normalisation the engine terminal started, for a write.
@@ -1156,6 +1196,7 @@ function resolvingConstraints(operation, config) {
1156
1196
  export function populateDbDelegate(target, config, ormHandle, context,
1157
1197
  /** The row-lock lane, on a transaction-bound context alone (ADR-0047). */
1158
1198
  lock) {
1199
+ const listMembers = assertListMembersResolve(config);
1159
1200
  for (const [listName, listConfig] of Object.entries(config.lists)) {
1160
1201
  const createOp = resolvingConstraints(createCreate(listName, listConfig, ormHandle, context, config), config);
1161
1202
  const updateOp = resolvingConstraints(createUpdate(listName, listConfig, ormHandle, context, config), config);
@@ -1164,9 +1205,17 @@ lock) {
1164
1205
  update: updateOp,
1165
1206
  delete: resolvingConstraints(createDelete(listName, listConfig, ormHandle, context, config), config),
1166
1207
  };
1167
- const read = createSecuredRead({ listName, listConfig, ormHandle, context, config, lock });
1168
- if (isSingletonList(listConfig)) {
1169
- operations.get = resolvingConstraints(createGet(listName, listConfig, read, context, createOp), config);
1208
+ const read = createSecuredRead({
1209
+ listName,
1210
+ listConfig,
1211
+ ormHandle,
1212
+ context,
1213
+ config,
1214
+ rebindDb: rebindDb(config),
1215
+ lock,
1216
+ });
1217
+ if (listMembers.get(listName)?.singleton === true) {
1218
+ operations.get = resolvingConstraints(createGet(listName, listConfig, read, context, createOp, lock), config);
1170
1219
  }
1171
1220
  else {
1172
1221
  operations.where = read.where;
@@ -1202,6 +1251,10 @@ export function buildDbDelegate(config, ormHandle, context) {
1202
1251
  populateDbDelegate(db, config, ormHandle, context, context._rowLock);
1203
1252
  return db;
1204
1253
  }
1254
+ /** The read binding's `rebindDb`: a hook's `context.db`, built against the hook's own context. */
1255
+ export function rebindDb(config) {
1256
+ return (derived) => buildDbDelegate(config, derived.ormHandle, derived);
1257
+ }
1205
1258
  const WRITE_ARG_KEYS = {
1206
1259
  create: ['data'],
1207
1260
  update: ['where', 'data'],
@@ -1212,6 +1265,9 @@ function assertWriteArgs(args, listName, operation, isSingleton) {
1212
1265
  if (args === null || typeof args !== 'object') {
1213
1266
  throw new ValidationError([`${operation}() on "${listName}" takes an argument object with ${allowed.join(', ')}.`], {});
1214
1267
  }
1268
+ if (operation !== 'delete' && !isPlainObject(Reflect.get(args, 'data'))) {
1269
+ throw new ValidationError([`${operation}() on "${listName}" takes \`data\` as an object of field values.`], {});
1270
+ }
1215
1271
  for (const [key, value] of Object.entries(args)) {
1216
1272
  if (allowed.includes(key) || value === undefined)
1217
1273
  continue;
@@ -1231,7 +1287,7 @@ listConfig, ormHandle, context, config) {
1231
1287
  // Thin adapter over the Write Pipeline: pick the create strategy, run the
1232
1288
  // canonical secured write sequence, return its result.
1233
1289
  return async (args) => {
1234
- assertWriteArgs(args, listName, 'create', isSingletonList(listConfig));
1290
+ assertWriteArgs(args, listName, 'create', resolveListMembers(config, listName).singleton);
1235
1291
  return runWritePipeline({
1236
1292
  listName,
1237
1293
  listConfig,
@@ -1239,7 +1295,7 @@ listConfig, ormHandle, context, config) {
1239
1295
  context,
1240
1296
  config,
1241
1297
  inputData: args.data,
1242
- strategy: createWriteStrategy(listName, listConfig),
1298
+ strategy: createWriteStrategy(listName, listConfig, config),
1243
1299
  });
1244
1300
  };
1245
1301
  }
@@ -1251,7 +1307,7 @@ listConfig, ormHandle, context, config) {
1251
1307
  return async (args) => {
1252
1308
  // Runs before the pipeline's access gate — a `where` the engine cannot
1253
1309
  // lower is the caller's own shape error, not a denial.
1254
- assertWriteArgs(args, listName, 'update', isSingletonList(listConfig));
1310
+ assertWriteArgs(args, listName, 'update', resolveListMembers(config, listName).singleton);
1255
1311
  assertIdentityWhere(args.where, listName, 'update');
1256
1312
  return runWritePipeline({
1257
1313
  listName,
@@ -1270,7 +1326,7 @@ listConfig, ormHandle, context, config) {
1270
1326
  // Thin adapter over the Write Pipeline: pick the delete strategy, run the
1271
1327
  // canonical secured write sequence, return its result.
1272
1328
  return async (args) => {
1273
- assertWriteArgs(args, listName, 'delete', isSingletonList(listConfig));
1329
+ assertWriteArgs(args, listName, 'delete', resolveListMembers(config, listName).singleton);
1274
1330
  assertIdentityWhere(args.where, listName, 'delete');
1275
1331
  return runWritePipeline({
1276
1332
  listName,
@@ -1322,9 +1378,20 @@ function assertBooleanInclude(include, listName) {
1322
1378
  * so the create is refused and `get()` answers `null` — the same `null` a
1323
1379
  * denied or an absent row answers with.
1324
1380
  */
1381
+ async function tookSingletonLock(lock, listName) {
1382
+ try {
1383
+ await lock.advisory(`opensaas:singleton-create:${listName}`);
1384
+ return true;
1385
+ }
1386
+ catch (error) {
1387
+ if (error instanceof RowLockLaneUnavailableError)
1388
+ return false;
1389
+ throw error;
1390
+ }
1391
+ }
1325
1392
  function createGet(listName,
1326
1393
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- ListConfig must accept any TypeInfo
1327
- listConfig, read, context, createFn) {
1394
+ listConfig, read, context, createFn, lock) {
1328
1395
  return async (args) => {
1329
1396
  warnIfSelectIgnored(args, listName, 'get');
1330
1397
  assertBooleanInclude(args?.include, listName);
@@ -1332,7 +1399,7 @@ listConfig, read, context, createFn) {
1332
1399
  const item = await scoped.first();
1333
1400
  if (item)
1334
1401
  return item;
1335
- if (!shouldAutoCreate(listConfig))
1402
+ if (!autoCreates(listConfig))
1336
1403
  return null;
1337
1404
  // `first()` conflates denied with absent, and auto-create must fire only on
1338
1405
  // absent — a denied session that provoked a create would both write a row it
@@ -1349,6 +1416,11 @@ listConfig, read, context, createFn) {
1349
1416
  if (readable !== true)
1350
1417
  return null;
1351
1418
  }
1419
+ if (lock !== undefined && (await tookSingletonLock(lock, listName))) {
1420
+ const settled = await scoped.first();
1421
+ if (settled)
1422
+ return settled;
1423
+ }
1352
1424
  try {
1353
1425
  await createFn({ data: {} });
1354
1426
  }