cvx-kit 0.2.0 → 2.0.0-beta.0

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 (372) hide show
  1. package/README.md +556 -99
  2. package/dist/agent-tools.d.mts +39 -26
  3. package/dist/agent-tools.mjs +1 -1
  4. package/dist/agent-tools.mjs.map +1 -1
  5. package/dist/auth.d.mts +120 -308
  6. package/dist/auth.mjs +1 -1
  7. package/dist/auth.mjs.map +1 -1
  8. package/dist/components/approvals/_generated/api.d.mts +7 -9
  9. package/dist/components/approvals/_generated/api.mjs.map +1 -1
  10. package/dist/components/approvals/_generated/component.d.mts +1 -4
  11. package/dist/components/approvals/_generated/dataModel.d.mts +5 -8
  12. package/dist/components/approvals/_generated/server.d.mts +12 -15
  13. package/dist/components/approvals/_generated/server.mjs.map +1 -1
  14. package/dist/components/approvals/audit.d.mts +8 -9
  15. package/dist/components/approvals/audit.mjs +1 -1
  16. package/dist/components/approvals/audit.mjs.map +1 -1
  17. package/dist/components/approvals/client.d.mts +11 -12
  18. package/dist/components/approvals/client.mjs +1 -1
  19. package/dist/components/approvals/client.mjs.map +1 -1
  20. package/dist/components/approvals/constants.d.mts +30 -31
  21. package/dist/components/approvals/constants.mjs.map +1 -1
  22. package/dist/components/approvals/convex.config.d.mts +0 -2
  23. package/dist/components/approvals/convex.config.mjs.map +1 -1
  24. package/dist/components/approvals/decisions.d.mts +13 -17
  25. package/dist/components/approvals/decisions.mjs +1 -1
  26. package/dist/components/approvals/decisions.mjs.map +1 -1
  27. package/dist/components/approvals/functions.d.mts +3 -4
  28. package/dist/components/approvals/functions.mjs +1 -1
  29. package/dist/components/approvals/functions.mjs.map +1 -1
  30. package/dist/components/approvals/health.d.mts +2 -3
  31. package/dist/components/approvals/health.mjs.map +1 -1
  32. package/dist/components/approvals/requests.d.mts +114 -131
  33. package/dist/components/approvals/requests.mjs +1 -1
  34. package/dist/components/approvals/requests.mjs.map +1 -1
  35. package/dist/components/approvals/schema.d.ts +106 -315
  36. package/dist/components/approvals/schema.js +1 -1
  37. package/dist/components/approvals/validators.d.mts +323 -395
  38. package/dist/components/approvals/validators.mjs +1 -1
  39. package/dist/components/approvals/validators.mjs.map +1 -1
  40. package/dist/components/approvals/workflow.d.mts +4 -5
  41. package/dist/components/approvals/workflow.mjs +1 -1
  42. package/dist/components/approvals/workflow.mjs.map +1 -1
  43. package/dist/components/approvals/workflow_steps.d.mts +16 -17
  44. package/dist/components/approvals/workflow_steps.mjs +1 -1
  45. package/dist/components/approvals/workflow_steps.mjs.map +1 -1
  46. package/dist/components/foundation/_generated/api.d.mts +6 -8
  47. package/dist/components/foundation/_generated/api.mjs.map +1 -1
  48. package/dist/components/foundation/_generated/component.d.mts +1 -4
  49. package/dist/components/foundation/_generated/dataModel.d.mts +5 -8
  50. package/dist/components/foundation/_generated/server.d.mts +12 -15
  51. package/dist/components/foundation/_generated/server.mjs.map +1 -1
  52. package/dist/components/foundation/client.d.mts +45 -38
  53. package/dist/components/foundation/client.mjs +1 -1
  54. package/dist/components/foundation/client.mjs.map +1 -1
  55. package/dist/components/foundation/convex.config.d.mts +0 -2
  56. package/dist/components/foundation/convex.config.mjs.map +1 -1
  57. package/dist/components/foundation/functions.d.mts +2 -3
  58. package/dist/components/foundation/functions.mjs.map +1 -1
  59. package/dist/components/foundation/modules/command/command.d.mts +65 -17
  60. package/dist/components/foundation/modules/command/command.mjs +1 -1
  61. package/dist/components/foundation/modules/command/command.mjs.map +1 -1
  62. package/dist/components/foundation/modules/command/lifecycle.d.mts +78 -0
  63. package/dist/components/foundation/modules/command/lifecycle.mjs +2 -0
  64. package/dist/components/foundation/modules/command/lifecycle.mjs.map +1 -0
  65. package/dist/components/foundation/modules/observability/observability.d.mts +13 -6
  66. package/dist/components/foundation/modules/observability/observability.mjs +1 -1
  67. package/dist/components/foundation/modules/observability/observability.mjs.map +1 -1
  68. package/dist/components/foundation/modules/query/query.d.mts +11 -12
  69. package/dist/components/foundation/modules/query/query.mjs +1 -1
  70. package/dist/components/foundation/modules/query/query.mjs.map +1 -1
  71. package/dist/components/foundation/operation.d.mts +72 -0
  72. package/dist/components/foundation/operation.mjs +2 -0
  73. package/dist/components/foundation/operation.mjs.map +1 -0
  74. package/dist/components/foundation/result.d.mts +7 -8
  75. package/dist/components/foundation/result.mjs.map +1 -1
  76. package/dist/components/foundation/schema.d.ts +0 -2
  77. package/dist/components/foundation/telemetry.d.mts +2 -3
  78. package/dist/components/foundation/telemetry.mjs +1 -1
  79. package/dist/components/foundation/telemetry.mjs.map +1 -1
  80. package/dist/contracts.d.mts +5 -0
  81. package/dist/contracts.mjs +1 -0
  82. package/dist/crud.d.mts +185 -27
  83. package/dist/crud.mjs +1 -1
  84. package/dist/crud.mjs.map +1 -1
  85. package/dist/effect.d.mts +16 -0
  86. package/dist/effect.mjs +1 -0
  87. package/dist/errors.d.mts +6 -6
  88. package/dist/errors.mjs +1 -1
  89. package/dist/errors.mjs.map +1 -1
  90. package/dist/filters.d.mts +4 -0
  91. package/dist/filters.mjs +1 -0
  92. package/dist/idempotency.d.mts +2 -0
  93. package/dist/idempotency.mjs +1 -0
  94. package/dist/index.d.mts +14 -7
  95. package/dist/index.mjs +1 -1
  96. package/dist/json-schema.d.mts +40 -0
  97. package/dist/json-schema.mjs +2 -0
  98. package/dist/json-schema.mjs.map +1 -0
  99. package/dist/k/compile.d.mts +17 -0
  100. package/dist/k/compile.mjs +2 -0
  101. package/dist/k/compile.mjs.map +1 -0
  102. package/dist/k/dates.d.mts +23 -0
  103. package/dist/k/dates.mjs +2 -0
  104. package/dist/k/dates.mjs.map +1 -0
  105. package/dist/k/formats.d.mts +24 -0
  106. package/dist/k/formats.mjs +2 -0
  107. package/dist/k/formats.mjs.map +1 -0
  108. package/dist/k/index.d.mts +32 -0
  109. package/dist/k/index.mjs +2 -0
  110. package/dist/k/index.mjs.map +1 -0
  111. package/dist/k/issues.d.mts +31 -0
  112. package/dist/k/issues.mjs +2 -0
  113. package/dist/k/issues.mjs.map +1 -0
  114. package/dist/k/kinds.d.mts +81 -0
  115. package/dist/k/kinds.mjs +2 -0
  116. package/dist/k/kinds.mjs.map +1 -0
  117. package/dist/k/object.d.mts +43 -0
  118. package/dist/k/object.mjs +2 -0
  119. package/dist/k/object.mjs.map +1 -0
  120. package/dist/k/parse.d.mts +29 -0
  121. package/dist/k/parse.mjs +2 -0
  122. package/dist/k/parse.mjs.map +1 -0
  123. package/dist/k/schema.d.mts +100 -0
  124. package/dist/k/schema.mjs +2 -0
  125. package/dist/k/schema.mjs.map +1 -0
  126. package/dist/k/standard.d.mts +8 -0
  127. package/dist/k/standard.mjs +2 -0
  128. package/dist/k/standard.mjs.map +1 -0
  129. package/dist/k/types.d.mts +64 -0
  130. package/dist/k/types.mjs +1 -0
  131. package/dist/k.d.mts +8 -0
  132. package/dist/k.mjs +1 -0
  133. package/dist/middleware.d.mts +8 -11
  134. package/dist/middleware.mjs +1 -1
  135. package/dist/middleware.mjs.map +1 -1
  136. package/dist/modules/contracts/binding.d.mts +23 -0
  137. package/dist/modules/contracts/binding.mjs +2 -0
  138. package/dist/modules/contracts/binding.mjs.map +1 -0
  139. package/dist/modules/contracts/contract.d.mts +67 -0
  140. package/dist/modules/contracts/contract.mjs +2 -0
  141. package/dist/modules/contracts/contract.mjs.map +1 -0
  142. package/dist/modules/contracts/convex.d.mts +18 -0
  143. package/dist/modules/contracts/convex.mjs +2 -0
  144. package/dist/modules/contracts/convex.mjs.map +1 -0
  145. package/dist/modules/contracts/domain.d.mts +62 -0
  146. package/dist/modules/contracts/domain.mjs +2 -0
  147. package/dist/modules/contracts/domain.mjs.map +1 -0
  148. package/dist/modules/contracts/errors.d.mts +737 -0
  149. package/dist/modules/contracts/errors.mjs +2 -0
  150. package/dist/modules/contracts/errors.mjs.map +1 -0
  151. package/dist/modules/contracts/exposure.d.mts +103 -0
  152. package/dist/modules/contracts/exposure.mjs +2 -0
  153. package/dist/modules/contracts/exposure.mjs.map +1 -0
  154. package/dist/modules/contracts/idempotency.d.mts +85 -0
  155. package/dist/modules/contracts/idempotency.mjs +2 -0
  156. package/dist/modules/contracts/idempotency.mjs.map +1 -0
  157. package/dist/modules/contracts/lazy.d.mts +6 -0
  158. package/dist/modules/contracts/lazy.mjs +2 -0
  159. package/dist/modules/contracts/lazy.mjs.map +1 -0
  160. package/dist/modules/contracts/standard.d.mts +17 -0
  161. package/dist/modules/contracts/standard.mjs +2 -0
  162. package/dist/modules/contracts/standard.mjs.map +1 -0
  163. package/dist/modules/contracts/workflow.d.mts +165 -0
  164. package/dist/modules/contracts/workflow.mjs +2 -0
  165. package/dist/modules/contracts/workflow.mjs.map +1 -0
  166. package/dist/modules/effect/action.d.mts +63 -0
  167. package/dist/modules/effect/action.mjs +2 -0
  168. package/dist/modules/effect/action.mjs.map +1 -0
  169. package/dist/modules/effect/agent-tools.d.mts +2 -0
  170. package/dist/modules/effect/agent-tools.mjs +1 -0
  171. package/dist/modules/effect/api-runtime.d.mts +36 -0
  172. package/dist/modules/effect/api-runtime.mjs +2 -0
  173. package/dist/modules/effect/api-runtime.mjs.map +1 -0
  174. package/dist/modules/effect/api.d.mts +46 -0
  175. package/dist/modules/effect/api.mjs +2 -0
  176. package/dist/modules/effect/api.mjs.map +1 -0
  177. package/dist/modules/effect/auth.d.mts +46 -0
  178. package/dist/modules/effect/auth.mjs +2 -0
  179. package/dist/modules/effect/auth.mjs.map +1 -0
  180. package/dist/modules/effect/client.d.mts +15 -0
  181. package/dist/modules/effect/client.mjs +1 -0
  182. package/dist/modules/effect/command.d.mts +108 -0
  183. package/dist/modules/effect/command.mjs +2 -0
  184. package/dist/modules/effect/command.mjs.map +1 -0
  185. package/dist/modules/effect/crud.d.mts +885 -0
  186. package/dist/modules/effect/crud.mjs +2 -0
  187. package/dist/modules/effect/crud.mjs.map +1 -0
  188. package/dist/modules/effect/domain.d.mts +82 -0
  189. package/dist/modules/effect/domain.mjs +2 -0
  190. package/dist/modules/effect/domain.mjs.map +1 -0
  191. package/dist/modules/effect/errors.d.mts +13 -0
  192. package/dist/modules/effect/errors.mjs +2 -0
  193. package/dist/modules/effect/errors.mjs.map +1 -0
  194. package/dist/modules/effect/failure-audit.d.mts +48 -0
  195. package/dist/modules/effect/failure-audit.mjs +2 -0
  196. package/dist/modules/effect/failure-audit.mjs.map +1 -0
  197. package/dist/modules/effect/failures.d.mts +21 -0
  198. package/dist/modules/effect/failures.mjs +2 -0
  199. package/dist/modules/effect/failures.mjs.map +1 -0
  200. package/dist/modules/effect/foundation.d.mts +23 -0
  201. package/dist/modules/effect/foundation.mjs +2 -0
  202. package/dist/modules/effect/foundation.mjs.map +1 -0
  203. package/dist/modules/effect/idempotency.d.mts +29 -0
  204. package/dist/modules/effect/idempotency.mjs +2 -0
  205. package/dist/modules/effect/idempotency.mjs.map +1 -0
  206. package/dist/modules/effect/input.d.mts +10 -0
  207. package/dist/modules/effect/input.mjs +2 -0
  208. package/dist/modules/effect/input.mjs.map +1 -0
  209. package/dist/modules/effect/normalize.d.mts +7 -0
  210. package/dist/modules/effect/normalize.mjs +2 -0
  211. package/dist/modules/effect/normalize.mjs.map +1 -0
  212. package/dist/modules/effect/operation.d.mts +148 -0
  213. package/dist/modules/effect/operation.mjs +2 -0
  214. package/dist/modules/effect/operation.mjs.map +1 -0
  215. package/dist/modules/effect/query.d.mts +90 -0
  216. package/dist/modules/effect/query.mjs +2 -0
  217. package/dist/modules/effect/query.mjs.map +1 -0
  218. package/dist/modules/effect/schema.d.mts +30 -0
  219. package/dist/modules/effect/schema.mjs +2 -0
  220. package/dist/modules/effect/schema.mjs.map +1 -0
  221. package/dist/modules/effect/scope.d.mts +25 -0
  222. package/dist/modules/effect/scope.mjs +2 -0
  223. package/dist/modules/effect/scope.mjs.map +1 -0
  224. package/dist/modules/effect/workflow.d.mts +75 -0
  225. package/dist/modules/effect/workflow.mjs +2 -0
  226. package/dist/modules/effect/workflow.mjs.map +1 -0
  227. package/dist/modules/foundation/foundation.d.mts +108 -0
  228. package/dist/modules/foundation/foundation.mjs +2 -0
  229. package/dist/modules/foundation/foundation.mjs.map +1 -0
  230. package/dist/modules/foundation/operation.d.mts +139 -0
  231. package/dist/modules/foundation/operation.mjs +2 -0
  232. package/dist/modules/foundation/operation.mjs.map +1 -0
  233. package/dist/namespace.d.mts +104 -0
  234. package/dist/namespace.mjs +2 -0
  235. package/dist/namespace.mjs.map +1 -0
  236. package/dist/oxlint/architecture.d.mts +23 -0
  237. package/dist/oxlint/architecture.mjs +2 -0
  238. package/dist/oxlint/architecture.mjs.map +1 -0
  239. package/dist/oxlint/ast.d.mts +51 -0
  240. package/dist/oxlint/ast.mjs +2 -0
  241. package/dist/oxlint/ast.mjs.map +1 -0
  242. package/dist/oxlint/components.d.mts +4 -0
  243. package/dist/oxlint/components.mjs +2 -0
  244. package/dist/oxlint/components.mjs.map +1 -0
  245. package/dist/oxlint/ownership.d.mts +59 -0
  246. package/dist/oxlint/ownership.mjs +2 -0
  247. package/dist/oxlint/ownership.mjs.map +1 -0
  248. package/dist/oxlint/reserved-files.d.mts +4 -0
  249. package/dist/oxlint/reserved-files.mjs +2 -0
  250. package/dist/oxlint/reserved-files.mjs.map +1 -0
  251. package/dist/oxlint.d.mts +9 -0
  252. package/dist/oxlint.mjs +2 -0
  253. package/dist/oxlint.mjs.map +1 -0
  254. package/dist/querying/cursors.d.mts +94 -0
  255. package/dist/querying/cursors.mjs +2 -0
  256. package/dist/querying/cursors.mjs.map +1 -0
  257. package/dist/querying/execution.d.mts +40 -0
  258. package/dist/querying/execution.mjs +2 -0
  259. package/dist/querying/execution.mjs.map +1 -0
  260. package/dist/querying/failures.d.mts +11 -0
  261. package/dist/querying/failures.mjs +2 -0
  262. package/dist/querying/failures.mjs.map +1 -0
  263. package/dist/querying/filter-boundary.d.mts +25 -0
  264. package/dist/querying/filter-boundary.mjs +2 -0
  265. package/dist/querying/filter-boundary.mjs.map +1 -0
  266. package/dist/querying/filters.d.mts +180 -0
  267. package/dist/querying/filters.mjs +2 -0
  268. package/dist/querying/filters.mjs.map +1 -0
  269. package/dist/querying/planner.d.mts +61 -0
  270. package/dist/querying/planner.mjs +2 -0
  271. package/dist/querying/planner.mjs.map +1 -0
  272. package/dist/querying/relationships.d.mts +58 -0
  273. package/dist/querying/relationships.mjs +2 -0
  274. package/dist/querying/relationships.mjs.map +1 -0
  275. package/dist/querying/search.d.mts +69 -0
  276. package/dist/querying/search.mjs +2 -0
  277. package/dist/querying/search.mjs.map +1 -0
  278. package/dist/querying/security.d.mts +123 -0
  279. package/dist/querying/security.mjs +2 -0
  280. package/dist/querying/security.mjs.map +1 -0
  281. package/dist/querying/shared/ast.d.mts +26 -0
  282. package/dist/querying/shared/ast.mjs +2 -0
  283. package/dist/querying/shared/ast.mjs.map +1 -0
  284. package/dist/querying/shared/contract.d.mts +98 -0
  285. package/dist/querying/shared/contract.mjs +2 -0
  286. package/dist/querying/shared/contract.mjs.map +1 -0
  287. package/dist/querying/shared/evaluate.d.mts +5 -0
  288. package/dist/querying/shared/evaluate.mjs +2 -0
  289. package/dist/querying/shared/evaluate.mjs.map +1 -0
  290. package/dist/querying/shared/filters.d.mts +65 -0
  291. package/dist/querying/shared/filters.mjs +2 -0
  292. package/dist/querying/shared/filters.mjs.map +1 -0
  293. package/dist/querying/shared/native-json.d.mts +7 -0
  294. package/dist/querying/shared/native-json.mjs +2 -0
  295. package/dist/querying/shared/native-json.mjs.map +1 -0
  296. package/dist/querying/shared/page.d.mts +20 -0
  297. package/dist/querying/shared/page.mjs +2 -0
  298. package/dist/querying/shared/page.mjs.map +1 -0
  299. package/dist/querying.d.mts +82 -0
  300. package/dist/querying.mjs +2 -0
  301. package/dist/querying.mjs.map +1 -0
  302. package/dist/react/failures.d.mts +24 -0
  303. package/dist/react/failures.mjs +2 -0
  304. package/dist/react/failures.mjs.map +1 -0
  305. package/dist/react/optimistic.d.mts +46 -0
  306. package/dist/react/optimistic.mjs +2 -0
  307. package/dist/react/optimistic.mjs.map +1 -0
  308. package/dist/react/pagination-state.d.mts +39 -0
  309. package/dist/react/pagination-state.mjs +2 -0
  310. package/dist/react/pagination-state.mjs.map +1 -0
  311. package/dist/react/use-command.d.mts +22 -0
  312. package/dist/react/use-command.mjs +2 -0
  313. package/dist/react/use-command.mjs.map +1 -0
  314. package/dist/react/use-paginated-list.d.mts +34 -0
  315. package/dist/react/use-paginated-list.mjs +2 -0
  316. package/dist/react/use-paginated-list.mjs.map +1 -0
  317. package/dist/react.d.mts +6 -0
  318. package/dist/react.mjs +1 -0
  319. package/dist/registration.d.mts +65 -0
  320. package/dist/registration.mjs +2 -0
  321. package/dist/registration.mjs.map +1 -0
  322. package/dist/relations-public.d.mts +2 -0
  323. package/dist/relations-public.mjs +1 -0
  324. package/dist/relations.d.mts +274 -0
  325. package/dist/relations.mjs +2 -0
  326. package/dist/relations.mjs.map +1 -0
  327. package/dist/state-machine.d.mts +1 -4
  328. package/dist/state-machine.mjs.map +1 -1
  329. package/dist/table.d.mts +222 -0
  330. package/dist/table.mjs +2 -0
  331. package/dist/table.mjs.map +1 -0
  332. package/dist/tenancy.d.mts +6 -8
  333. package/dist/tenancy.mjs +1 -1
  334. package/dist/tenancy.mjs.map +1 -1
  335. package/dist/triggers.d.mts +12 -10
  336. package/dist/triggers.mjs +1 -1
  337. package/dist/triggers.mjs.map +1 -1
  338. package/dist/webhooks.d.mts +22 -112
  339. package/dist/webhooks.mjs +1 -1
  340. package/dist/webhooks.mjs.map +1 -1
  341. package/dist/workflow.d.mts +2 -0
  342. package/dist/workflow.mjs +1 -0
  343. package/dist/zod.d.mts +30 -0
  344. package/dist/zod.mjs +2 -0
  345. package/dist/zod.mjs.map +1 -0
  346. package/package.json +145 -33
  347. package/src/test.ts +9 -17
  348. package/CHANGELOG.md +0 -145
  349. package/ROADMAP.md +0 -126
  350. package/dist/zod-table.d.mts +0 -175
  351. package/dist/zod-table.mjs +0 -2
  352. package/dist/zod-table.mjs.map +0 -1
  353. package/docs/agent-tools.md +0 -41
  354. package/docs/approvals.md +0 -200
  355. package/docs/architecture.md +0 -124
  356. package/docs/auth.md +0 -218
  357. package/docs/commands.md +0 -278
  358. package/docs/conventions.md +0 -291
  359. package/docs/crud.md +0 -62
  360. package/docs/llms.md +0 -220
  361. package/docs/maintainability.md +0 -124
  362. package/docs/migration.md +0 -351
  363. package/docs/plans/2026-08-14-001-feat-v0.1.0-feature-set-plan.md +0 -461
  364. package/docs/plans/2026-08-21-0930-feat-resolve-organization-hook-plan.md +0 -178
  365. package/docs/tenancy.md +0 -179
  366. package/docs/triggers.md +0 -75
  367. package/docs/upgrading.md +0 -175
  368. package/docs/webhooks.md +0 -58
  369. package/docs/zod-table.md +0 -173
  370. package/scripts/convex-config-bridge.mjs +0 -55
  371. package/scripts/extract-changelog.mjs +0 -29
  372. package/scripts/smoke-packed-components.mjs +0 -374
package/docs/commands.md DELETED
@@ -1,278 +0,0 @@
1
- # `cvx-kit/components/foundation` — the audited command protocol
2
-
3
- The Foundation is a Convex component that **owns zero tables and zero
4
- capability** — its schema is empty by design. What it provides is the
5
- application kernel: the `Command` protocol (validate → observe → execute →
6
- audit, in one transaction), the `Query` kernel, and payload-free
7
- `Observability`. It is declared **once** per app and everything destructures
8
- from it; there is no separate command import.
9
-
10
- ## Mounting and declaring
11
-
12
- ```ts
13
- // convex/convex.config.ts
14
- import { defineApp } from 'convex/server'
15
- import foundation from 'cvx-kit/components/foundation/convex.config'
16
-
17
- const app = defineApp()
18
- app.use(foundation)
19
- export default app
20
- ```
21
-
22
- ```ts
23
- // convex/foundation.ts — declared once, the single kernel source
24
- import { Foundation } from 'cvx-kit/components/foundation'
25
- import { components } from './_generated/api'
26
- import { writeAuditEntry } from './audit'
27
-
28
- export const { Command, Query, observability } = new Foundation(
29
- components.foundation,
30
- {
31
- observability: {
32
- enabled: () => process.env.COMMAND_OBSERVABILITY_ENABLED === 'true',
33
- classifyError: (error) => classify(error), // → { outcome: 'denied' | 'failed', errorCode }
34
- writeAudit: (ctx, entry) => writeAuditEntry(ctx, entry),
35
- },
36
- },
37
- )
38
- ```
39
-
40
- Host code consumes the Foundation **only through this facade** — never deep
41
- imports of the component's internal modules.
42
-
43
- ## Defining commands — per domain
44
-
45
- Each domain declares a frozen registry of operations and derives typed
46
- executors from it. `classification` and `audit()` are **mandatory per
47
- operation** — auditing is type-enforced, not opt-in.
48
-
49
- ```ts
50
- // convex/domain/documents/commands.ts
51
- import { z } from 'zod'
52
- import { Command } from '../../foundation'
53
- import { documents } from './schema'
54
-
55
- const operations = {
56
- 'documents.rename': Command.operation({
57
- command: documents.commandInput.extend({ id: zid('documents') }),
58
- result: z.object({ ok: z.literal(true) }).strict(),
59
- classification: 'business',
60
- audit: ({ command }) => ({
61
- operation: 'documents.rename',
62
- actorId: command.actorId,
63
- aggregate: { type: 'document', id: command.id },
64
- metadata: { title: command.title },
65
- }),
66
- }),
67
- } as const
68
-
69
- const commands = new Command<MutationCtx, typeof operations>(operations)
70
-
71
- export const executeRename = commands.exec({
72
- operation: 'documents.rename',
73
- handler: async (ctx, command) => {
74
- await ctx.db.patch(command.id, { title: command.title })
75
- return { ok: true }
76
- },
77
- })
78
- ```
79
-
80
- The public API layer then wraps the executor in an `authMutation` and passes
81
- `ctx.actor` in. Public functions stay thin adapters; domain logic lives behind
82
- `execute*` functions.
83
-
84
- ## What one execution does, in order
85
-
86
- 1. **Parse the command** through the operation's `command` schema (strict zod).
87
- 2. **Observe**: start the observation clock.
88
- 3. **Permission check** — if the operation declares `permission`, the
89
- Foundation's injected `checkPermission(ctx, { permission, operation })`
90
- runs; throw to deny. Declaring a permission with no injected checker
91
- **fails closed** (`COMMAND_PERMISSION_NOT_CONFIGURED`).
92
- 4. **Default guard** — the registry-wide guard passed as the Command's second
93
- argument (`new Command(operations, { guard })`), if any.
94
- 5. **Operation guard** — the operation's own `guard(ctx, command)`:
95
- preconditions like state-machine legality, ownership beyond roles, or
96
- invariants over the parsed command. Throw to deny — nothing has run yet,
97
- so a denial is always clean.
98
- 6. **Run the handler**; its return value is parsed through the `result` schema
99
- — outputs are validated too.
100
- 7. **Audit**: call the operation's `audit({ command, result }, ctx)`. If it
101
- returns non-null, the entry (plus the operation's `classification`) is
102
- written through the injected `writeAudit` — **in the same transaction** as
103
- the handler's writes. Returning `null` skips the audit (the ontology
104
- convention: failures are not audited). When the operation declares
105
- `aggregates: [...]`, an audit whose `aggregate.type` is not in that
106
- allowlist throws (`COMMAND_AGGREGATE_NOT_DECLARED`) — the audit
107
- vocabulary is enforced, not advisory. The per-operation catalog is
108
- introspectable as `commands.aggregates` for tests.
109
- 8. **Emit the observation**: `{ operation, classification, outcome,
110
- errorCode?, durationMs }` — completed, denied, or failed per
111
- `classifyError`.
112
-
113
- ### Guards and permissions
114
-
115
- ```ts
116
- // convex/foundation.ts — permission semantics are host policy, injected once
117
- export const { Command, Query, observability } = new Foundation(
118
- components.foundation,
119
- {
120
- observability: { ... },
121
- checkPermission: (ctx, { permission }) =>
122
- requirePermission(ctx.identity, permission), // throw to deny
123
- },
124
- )
125
-
126
- // domain/<module>/commands.ts
127
- const operations = {
128
- '<entities>.publish': Command.operation({
129
- command: <entities>.commandInput.extend({ id: zid('<entities>') }),
130
- result: z.object({ ok: z.literal(true) }).strict(),
131
- classification: 'business',
132
- permission: '<domain>.manage', // checked first
133
- aggregates: ['<aggregate-type>'], // audit vocabulary allowlist
134
- guard: async (ctx, command) => { // precondition, pre-handler
135
- const row = await requireTenantReference(ctx.tenant, () => ctx.db.get(command.id))
136
- if (row.state !== 'draft') errors.throw({ code: 'INVALID_STATE' })
137
- },
138
- audit: ({ command }) => ({ ... }),
139
- }),
140
- } as const
141
-
142
- const commands = new Command<MutationCtx, typeof operations>(operations, {
143
- guard: (ctx) => assertNotReadonlyWindow(ctx), // registry-wide default
144
- })
145
- ```
146
-
147
- Guards deny by throwing; a denial before the handler never needs rollback
148
- because nothing has executed. Keep guards read-only — a guard that writes is
149
- a handler in disguise.
150
-
151
- ### Middleware — composable, next()-based
152
-
153
- For wrap-around concerns (timing, tracing, context enrichment, replay
154
- wrappers) that two disconnected callbacks can't express, both kernels take
155
- Express/TanStack-style middleware. The chain runs **inside** the pipeline's
156
- invariants: after the permission check, around [guards → handler], before the
157
- result-schema re-parse, aggregate allowlist, and audit — so middleware can
158
- never skip authorization, return an invalid result, or desynchronize audit
159
- from effects.
160
-
161
- ```ts
162
- const timing = Command.middleware(async ({ operation, next }) => {
163
- const started = Date.now()
164
- const result = await next() // inner middleware → guards → handler
165
- observability.observe // (or your own sink)
166
- console.info(`${operation} took ${Date.now() - started}ms`)
167
- return result
168
- })
169
-
170
- const withVendor = Command.middleware(async ({ context, next }) =>
171
- next({ context: { vendor: await loadVendor(context) } })) // enriches downstream ctx
172
-
173
- const commands = new Command<Ctx, typeof operations>(operations, {
174
- guard: (ctx) => assertNotReadonlyWindow(ctx),
175
- middleware: [timing], // registry-wide, outermost
176
- })
177
-
178
- '<entities>.publish': Command.operation({
179
- ...,
180
- middleware: [withVendor], // per-operation, inside registry chain
181
- guard: (ctx, command) => { /* ctx.vendor available here */ },
182
- })
183
- ```
184
-
185
- Order: registry middleware (array order) → operation middleware → default
186
- guard → operation guard → handler. `next()` returns the downstream result;
187
- `next({ context })` merges enrichment into the context handed to inner
188
- middleware, guards, and the handler. Skipping `next()` short-circuits
189
- (guards and handler never run — the returned value still must satisfy the
190
- result schema); calling it twice throws `COMMAND_MIDDLEWARE_NEXT_REUSED`.
191
- Whatever leaves the chain is re-parsed through the operation's strict result
192
- schema — a middleware cannot fabricate an invalid result.
193
-
194
- The Query kernel takes the same shape: `new Query({ defaults, middleware,
195
- execute })` plus per-executor `middleware: [...]`, with `Query.middleware`
196
- as the typing helper. Kernel middleware runs before executor middleware,
197
- inside the host's injected `execute` policy.
198
-
199
- If any step throws, the Convex transaction rolls back — handler writes and
200
- audit entry together. Audit and effects can never disagree.
201
-
202
- ### Dynamic dispatch
203
-
204
- `exec` has a second overload for operation selection at runtime:
205
-
206
- ```ts
207
- export const executeAny = commands.exec({
208
- dispatcher: z.object({ kind: z.enum(OPERATION_KINDS) /* from constants.ts */ }),
209
- select: (input) => `documents.${input.kind}` as const,
210
- handler,
211
- })
212
- ```
213
-
214
- An unconfigured operation throws `COMMAND_OPERATION_NOT_CONFIGURED`.
215
-
216
- ## Observability — payload-free by construction
217
-
218
- Observations carry identifiers and a duration, never payloads. Events are
219
- silently dropped unless:
220
-
221
- - `enabled` resolves to `true`,
222
- - `operation` and `classification` match `/^[A-Za-z][A-Za-z0-9_.-]{0,159}$/`,
223
- - `errorCode` (when present) matches `/^[A-Z][A-Z0-9_]{0,95}$/`.
224
-
225
- Practical consequence: **dotted lowercase operation names**
226
- (`documents.rename`) and **UPPER_SNAKE error codes** are effectively
227
- mandatory. Every telemetry path is wrapped in try/catch — observability is
228
- behaviorally inert and can never change command behavior or mask the original
229
- failure. Default sink is one JSON line on `console.info`
230
- (`event: 'command.execution'`); inject `emit` to redirect.
231
-
232
- ## `executeResultBoundary` — typed failures, but only while it's safe
233
-
234
- ```ts
235
- // destructured from the Foundation facade, like everything else
236
- const { executeResultBoundary, projectResult } = new Foundation(...)
237
-
238
- const result = await executeResultBoundary(ctx, () => run(), boundary)
239
- // Result<Value, Failure> = { ok: true, value } | { ok: false, error }
240
- ```
241
-
242
- Semantics — read carefully, they are decided by **transaction state**, not by
243
- the error:
244
-
245
- - Success → `{ ok: true, value }`.
246
- - A failure the boundary recognizes (`dataOf(error)` non-undefined) is
247
- returned as `{ ok: false, error }` **only if the transaction has produced no
248
- effects yet** (nothing written, nothing scheduled — checked via
249
- `ctx.meta.getTransactionMetrics()`).
250
- - If anything was already written or scheduled, the error is **rethrown** so
251
- Convex rolls the transaction back — a returned failure after effects would
252
- commit those effects.
253
- - Unrecognized errors and unreadable metrics always rethrow.
254
-
255
- Use it at the outermost edge of a mutation when the client needs a typed
256
- `Result` over the wire instead of a thrown error. `projectResult(result,
257
- onFailure)` unwraps on the caller side.
258
-
259
- ## The `Query` kernel
260
-
261
- `Query` mirrors the pattern for reads: an injected `execute` receives
262
- `{ context, metadata, run }`, letting the host apply uniform read policy
263
- (authorization checks, tracing) around every domain query without the domains
264
- knowing. Metadata defaults are set at construction and merged per-executor.
265
-
266
- ## Rules and footguns
267
-
268
- 1. One `new Foundation(...)` per app, in `convex/foundation.ts`. Everything
269
- else destructures from it.
270
- 2. Never deep-import the component's `modules/*` or `result.ts` paths from app
271
- code — the `client.ts` facade is the contract.
272
- 3. The audit callback receives the ctx untyped (`never`); wiring a query ctx
273
- into an audited command fails at runtime, not compile time. Commands run in
274
- mutations.
275
- 4. Name operations `domain.verb` and error codes `UPPER_SNAKE` or your
276
- telemetry silently disappears (see regexes above).
277
- 5. Keep operation registries frozen (`as const`) in the domain's
278
- `commands.ts`; vocabulary tuples live in `constants.ts`.
@@ -1,291 +0,0 @@
1
- # cvx-kit conventions — folder structure, file anatomy, and naming
2
-
3
- This is the kit's doctrine. Not a style suggestion: the structure below is
4
- what the kit's structural guarantees (auth, triggers, bounded reads, audited
5
- commands) assume, and what its recommended architecture tests enforce. Follow
6
- it as written; deviate only with a documented reason and a matching change to
7
- the tests.
8
-
9
- Names in `<angle brackets>` are placeholders for your vocabulary. Everything
10
- else — the folder roles, the file anatomy, the naming grammar — is fixed.
11
-
12
- ---
13
-
14
- ## 1. Folder structure
15
-
16
- ```
17
- convex/
18
- convex.config.ts
19
- schema.ts
20
- functions.ts
21
- triggers.ts
22
- foundation.ts
23
- <component>.ts (one per mounted component: audit.ts, approvals.ts, ...)
24
- auth.ts auth.config.ts http.ts crons.ts
25
- api/
26
- <entity>.ts
27
- domain/
28
- table.ts
29
- shared/
30
- <provider>.ts
31
- <entity>/
32
- schema.ts table.ts constants.ts commands.ts queries.ts
33
- rules.ts shared.ts actions.ts
34
- <workflow>.ts <workflow>_functions.ts
35
- __tests__/
36
- components/ (only if you author portable components)
37
- <name>/
38
- __tests__/
39
- setup.ts architecture.test.ts facades.test.ts
40
- ```
41
-
42
- Three zones, three privileges:
43
-
44
- - **Root** — configuration and facades. May import kit modules, generated
45
- code, and component clients. May NOT export public functions or contain
46
- business decisions.
47
- - **`api/`** — the public surface. May import `functions.ts` constructors and
48
- its entity's domain modules. May NOT contain business logic, private
49
- helpers, or reads/writes beyond delegation.
50
- - **`domain/`** — all business logic. May import `functions.ts`,
51
- `foundation.ts`, facades, `domain/shared/`, and its own entity directory.
52
- May NOT import `api/`, sibling entities, `_generated/server` builders, or
53
- component internals.
54
-
55
- Dependency direction is one-way: `api → domain → shared → kit`. Anything
56
- pointing the other way is a violation.
57
-
58
- ### Module grain
59
-
60
- `domain/<entity>/` is really `domain/<module>/`: the directory is a **bounded
61
- context that may own several related tables**, not necessarily one. Two
62
- tables belong in the same module when a single command routinely writes both,
63
- or when one's vocabulary is meaningless without the other; otherwise they are
64
- separate modules. The file anatomy in §2 is unchanged — a module's
65
- `schema.ts` simply declares several `zodTable`s and its `table.ts` exports
66
- one merged map (combine maps in `domain/table.ts` with `createModule`, which
67
- rejects duplicate owners). A one-table module is the degenerate case, so
68
- small apps look exactly like before.
69
-
70
- Cross-module rules:
71
-
72
- - Sibling modules may import each other's `rules.ts` and `queries.ts`
73
- **only** — never `commands.ts`: one module never mutates another's state.
74
- - A workflow that must atomically write across modules lives in an optional
75
- `application/` layer above `domain/` (`application/<workflow>.ts`); `api/`
76
- adapters then import `application/*` and their own module, never a second
77
- module.
78
-
79
- ---
80
-
81
- ## 2. File anatomy — what each file contains, exactly
82
-
83
- Every file has one job. The lists below are exhaustive: if content isn't
84
- listed for a file, it doesn't belong there.
85
-
86
- ### Universal file order
87
-
88
- Every source file reads from public purpose to private implementation:
89
-
90
- 1. Imports.
91
- 2. Exported API — types, schemas, constants, functions.
92
- 3. File-local types and constants supporting the implementation.
93
- 4. Private helpers as **named function declarations**, at the bottom.
94
-
95
- No helper implementations above the exported API. No helper
96
- arrow-function constants where a `function` declaration works. No barrel
97
- files (`index.ts` re-exports) anywhere inside `convex/`. No re-exporting
98
- another file's function to shorten an import.
99
-
100
- ### Root files
101
-
102
- | File | Contains | Never contains |
103
- |---|---|---|
104
- | `convex.config.ts` | `defineApp()`, one `app.use(...)` per component, `defineApp({ env })` declarations, `export default app` | anything else |
105
- | `schema.ts` | `defineSchema(domainTables)` | table definitions, validators |
106
- | `functions.ts` | the single `createAuthFunctions<DataModel>()` call and its exported constructors | handlers, business policy beyond the injected config |
107
- | `triggers.ts` | `createTriggers()`, `timestamps`/`appendOnly`/`noDelete` registrations, calls to per-entity `register<Entity>Triggers` | trigger *logic* for a specific entity (that lives in the entity) |
108
- | `foundation.ts` | the single `new Foundation(...)`, destructured exports | command definitions |
109
- | `<component>.ts` | `new <Client>(components.<name>)` + minimal admin plumbing | workflow/business definitions |
110
- | `http.ts` | routes: verify signature → parse payload → delegate to a domain function | webhook business logic |
111
- | `crons.ts` | `cronJobs()` declarations targeting `internal.domain.<entity>...` | handler logic |
112
-
113
- ### `api/<entity>.ts` — the adapter file
114
-
115
- One file per entity exposed to clients. Each exported function follows one
116
- template and stays within ~10 lines of glue:
117
-
118
- ```ts
119
- export const <verb> = authMutation({
120
- args: <entity>.commandInput.extend({ /* ids, etc. */ }),
121
- returns: <resultSchema>,
122
- handler: (ctx, args) => execute<Verb><Entity>(ctx, { ...args, actorId: ctx.actor.userId }),
123
- })
124
-
125
- export const list = authQuery({
126
- args: { limit: z.number() },
127
- returns: z.array(<entity>.publicDto),
128
- handler: (ctx, { limit }) =>
129
- ctx.include(ctx.db.query('<entities>'))
130
- .matching('by_<field>', (ix) => ix.eq('<field>', ctx.actor.userId))
131
- .execute(limit, (rows) => rows.map(<entity>.toPublicDto)),
132
- })
133
- ```
134
-
135
- Adapters do four things — parse (boundary schemas), authorize (constructor
136
- choice), delegate (`execute*` with `ctx.actor`), project (`toPublicDto`).
137
- The moment an adapter needs a private helper or a business `if`, that code
138
- moves to the domain.
139
-
140
- ### `domain/<entity>/schema.ts` — shapes only
141
-
142
- - The entity's single `zodTable(...)` call, with `serverFields`,
143
- `commandFields`, `publicFields` decided deliberately.
144
- - DTO compositions derived from the table's boundaries
145
- (`z.object({ ...<entity>.publicDto.shape, ... }).strict()`).
146
- - Inferred types (`export type <Entity> = z.infer<typeof <entity>.schema>`).
147
- - NO indexes, NO `defineTable`, NO enum literals (consume `constants.ts`).
148
-
149
- ### `domain/<entity>/table.ts` — topology only
150
-
151
- ```ts
152
- export const <entity>Tables = {
153
- <entities>: <entity>.table
154
- .index('by_<field>', ['<field>'])
155
- .index('by_<field>_and_<field2>', ['<field>', '<field2>']),
156
- }
157
- ```
158
-
159
- Registered in `domain/table.ts`, which merges all entity maps into
160
- `domainTables`. Shapes change with the data model; indexes change with query
161
- patterns — that's why they are separate files.
162
-
163
- ### `domain/<entity>/constants.ts` — vocabularies
164
-
165
- Every finite value set the entity owns, one owner per vocabulary:
166
-
167
- ```ts
168
- export const <ENTITY>_STATES = ['<a>', '<b>', '<c>'] as const
169
- export type <Entity>State = (typeof <ENTITY>_STATES)[number]
170
- ```
171
-
172
- Schemas, validators, filters, transition maps, and tests consume the tuple.
173
- Transition maps are typed against the value type so an invalid literal cannot
174
- compile. Numeric limits and durations the entity owns also live here.
175
-
176
- ### `domain/<entity>/commands.ts` — every state change
177
-
178
- 1. The frozen operation registry: `Command.operation({ command, result,
179
- classification, audit })` per operation, keys `'<entity>.<verb>'`.
180
- 2. One `new Command<Ctx, typeof operations>(operations)`.
181
- 3. Exported executors: `export const execute<Verb><Entity> = commands.exec(...)`.
182
- 4. Private handler helpers at the bottom.
183
-
184
- Audit derivation lives here, next to the operation it describes. No reads
185
- that belong in `queries.ts`; no pure predicates that belong in `rules.ts`.
186
-
187
- ### `domain/<entity>/queries.ts` — domain reads
188
-
189
- Read logic worth reusing beyond one adapter: multi-step lookups, DTO
190
- assembly, cross-index selection. Bounded like everything else.
191
-
192
- ### `domain/<entity>/rules.ts` — pure invariants
193
-
194
- Predicates and transition checks with **no ctx and no db**: unit-testable
195
- business truth (`can<Verb>(doc, actor)`, `isTransitionLegal(from, to)`).
196
- Handlers call rules; rules never call handlers.
197
-
198
- ### `domain/<entity>/shared.ts` — entity-private helpers
199
-
200
- Code shared by two or more files *of this entity*. If a second entity needs
201
- it, it moves to `domain/shared/` — never a sibling import.
202
-
203
- ### `domain/<entity>/actions.ts` — external side effects
204
-
205
- Action-side logic using the shared provider clients from
206
- `domain/shared/<provider>.ts`. Domains import the shared client; they never
207
- instantiate their own vendor SDK.
208
-
209
- ### `domain/<entity>/<workflow>.ts` + `<workflow>_functions.ts`
210
-
211
- The workflow/approval definition the entity owns, and its internal callback
212
- targets built with `system*` constructors. Callbacks re-validate everything:
213
- `normalizeId`, `resourceType` check, staleness guard.
214
-
215
- ### `domain/shared/` — cross-entity, and only cross-entity
216
-
217
- One configured client per external provider (`<provider>.ts`), plus zod/util
218
- helpers genuinely used by multiple entities. This directory is not a dumping
219
- ground: code with one consumer moves back to that consumer.
220
-
221
- ### Tests
222
-
223
- - `domain/<entity>/__tests__/` — behavioral tests for the entity, driven
224
- through the real constructors (`t.withIdentity(...)`).
225
- - `convex/__tests__/architecture.test.ts` — the executable conventions;
226
- every rule proved against a synthetic violating fixture first.
227
- - `convex/__tests__/facades.test.ts` — every `app.use` exactly once; no
228
- private-child access.
229
- - `convex/__tests__/setup.ts` — env stubs for `defineApp({ env })` vars.
230
- - Test files are named `<subject>.test.ts` and live in a `__tests__/`
231
- directory beside the code they test — never in a parallel top-level tree.
232
-
233
- ---
234
-
235
- ## 3. Naming grammar
236
-
237
- | Thing | Convention | Example shape |
238
- |---|---|---|
239
- | Entity directory | plural camelCase, matches table name | `domain/<entities>/` |
240
- | Table | plural camelCase | `<entities>` |
241
- | Fields | camelCase; foreign keys `<other>Id`; timestamps only via the kit | `ownerId`, `archivedAt` |
242
- | Index | `by_<field>` / `by_<field>_and_<field>`, declaration order, nested paths flattened with `_` | `by_ownerId_and_state_status` |
243
- | Command operation | `<entity>.<verb>` lowercase dotted; must match `/^[A-Za-z][A-Za-z0-9_.-]{0,159}$/` | `<entities>.approve` |
244
- | Error code | `UPPER_SNAKE`, matching `/^[A-Z][A-Z0-9_]{0,95}$/`, defined in one owner | `INVALID_REQUEST_BOUNDARY` |
245
- | Vocabulary tuple | `UPPER_SNAKE_CASE` `as const`; derived type PascalCase singular | `<ENTITY>_STATES` / `<Entity>State` |
246
- | Domain executor | `execute<Verb><Entity>` | `executeApprove<Entity>` |
247
- | Rule predicate | `can<Verb>` / `is<Condition>` | `canApprove`, `isTransitionLegal` |
248
- | Trigger registration | `register<Entity>Triggers(triggers)` | exported from the entity, called in root `triggers.ts` |
249
- | Internal-callback file | `<purpose>_functions.ts`, sibling of `<purpose>.ts` | `approval_functions.ts` |
250
- | zodTable export | camelCase plural, same as table | `export const <entities> = zodTable(...)` |
251
- | DTO schema | `<entity><Purpose>Dto` | `<entity>SummaryDto` |
252
- | Zod schema value | camelCase noun | `commandInput`, `publicDto` |
253
- | Type | PascalCase; no `I`/`T` prefixes | `<Entity>State` |
254
- | Test file | `<subject>.test.ts` in `__tests__/` | `commands.test.ts` |
255
- | Public function path | `api/<entity>:<fn>` — clients never call a root path | — |
256
-
257
- The two regexes are enforced at runtime: observability silently drops events
258
- whose operation/classification/errorCode don't match. The rest is enforced by
259
- the architecture tests — a naming rule without a test is a wish.
260
-
261
- ---
262
-
263
- ## 4. Import rules
264
-
265
- Allowed, per zone (anything not listed is forbidden):
266
-
267
- | From | May import |
268
- |---|---|
269
- | `api/<entity>.ts` | `functions.ts`, `domain/<entity>/*`, `domain/shared/*`, zod |
270
- | `domain/<entity>/*` | `functions.ts`, `foundation.ts`, component facades, `domain/shared/*`, own directory, generated `internal`/`api` (references only), kit modules |
271
- | `domain/shared/*` | kit modules, vendor SDKs (this is the ONLY home for vendor clients) |
272
- | root facades | kit modules, `components` from generated api |
273
- | `functions.ts` | `_generated/server` (the only file allowed to) |
274
- | `components/<name>/**` | its own directory + npm deps only — no host imports, no `process.env` |
275
-
276
- Forbidden everywhere: sibling-entity imports, `api/` from `domain/`,
277
- component internals (anything below a component's client facade),
278
- `makeFunctionReference`, barrel files.
279
-
280
- ---
281
-
282
- ## 5. Formatting and language
283
-
284
- - TypeScript strict; no `any` in exported signatures; `as never` only at kit
285
- seams that document it.
286
- - Zod objects that cross a boundary are `.strict()` — always.
287
- - `Object.freeze` for registries and long-lived configuration objects.
288
- - Comments state constraints the code can't (`// Optional only for rows
289
- created before <epoch>.`) — never narration of the next line.
290
- - One entity concept per file; when a file serves two purposes, split it
291
- along the anatomy in §2.
package/docs/crud.md DELETED
@@ -1,62 +0,0 @@
1
- # `cvx-kit/crud` — the CRUD command factory
2
-
3
- `createCrudCommands` generates the three standard operations every entity
4
- starts with — create, update, archive — from one zodTable declaration,
5
- **fully inside the audited command pipeline**: strict command inputs from
6
- `commandFields`, mandatory audit with a declared aggregate type (enforced by
7
- the aggregates allowlist), observability, guards, and middleware.
8
-
9
- > convex-helpers ships a same-named `crud` helper. It was deliberately not
10
- > used here: it bypasses the command pipeline — no audits, no guards, and
11
- > hard deletes. This factory is the kit-doctrine equivalent.
12
-
13
- ## Usage
14
-
15
- ```ts
16
- import { createCrudCommands } from 'cvx-kit/crud'
17
- import { Command } from '../foundation' // the destructured facade
18
-
19
- const crud = createCrudCommands({
20
- Command,
21
- table: <entities>, // a zodTable / tenantTable
22
- aggregateType: '<entity>',
23
- classification: 'business', // default
24
- actor: (ctx) => ctx.actor.userId, // audited actor
25
- enrich: (ctx) => ({ tenant: ctx.tenant }), // server-owned stamps on create
26
- guards: { archive: (ctx, command) => machine.assert(...) }, // optional
27
- middleware: [rateLimit({ limiter, name: '<entities>.write' })], // optional
28
- })
29
-
30
- // thin api adapters:
31
- export const create = authMutation({
32
- args: <entities>.commandInput,
33
- handler: (ctx, args) => crud.executeCreate(ctx, args),
34
- })
35
- ```
36
-
37
- ## What gets generated
38
-
39
- | Operation | Input | Effect | Audit aggregate id |
40
- |---|---|---|---|
41
- | `<table>.create` | `commandInput` (strict) + `enrich` stamps | insert | the new row id |
42
- | `<table>.update` | `{ id, data: commandInput.partial() }` (strict) | patch | the row id |
43
- | `<table>.archive` | `{ id }` | sets `archivedAt` (soft delete ONLY) | the row id |
44
-
45
- No hard-delete operation is generated — kit doctrine (`noDelete` +
46
- `archivedAt`).
47
-
48
- ## Tenancy
49
-
50
- - `enrich` is **required for tenantTables** — the factory fails fast with
51
- `CRUD_ENRICH_REQUIRED` instead of letting untenanted rows ship. Stamp
52
- `tenant: ctx.tenant` there.
53
- - Generated update/archive take client-supplied ids: with `security.tenancy`
54
- configured, the RLS-wrapped writer rejects cross-tenant writes (this is
55
- covered by the kit's own tests) — but keep `requireTenantReference` in
56
- bespoke guards that load rows.
57
-
58
- ## Wiring agents
59
-
60
- The executors are the natural handlers for `createAgentTools` mutation
61
- kinds — agents get audited commands, never raw db access. See
62
- `agent-tools.md`.