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.
- package/README.md +556 -99
- package/dist/agent-tools.d.mts +39 -26
- package/dist/agent-tools.mjs +1 -1
- package/dist/agent-tools.mjs.map +1 -1
- package/dist/auth.d.mts +120 -308
- package/dist/auth.mjs +1 -1
- package/dist/auth.mjs.map +1 -1
- package/dist/components/approvals/_generated/api.d.mts +7 -9
- package/dist/components/approvals/_generated/api.mjs.map +1 -1
- package/dist/components/approvals/_generated/component.d.mts +1 -4
- package/dist/components/approvals/_generated/dataModel.d.mts +5 -8
- package/dist/components/approvals/_generated/server.d.mts +12 -15
- package/dist/components/approvals/_generated/server.mjs.map +1 -1
- package/dist/components/approvals/audit.d.mts +8 -9
- package/dist/components/approvals/audit.mjs +1 -1
- package/dist/components/approvals/audit.mjs.map +1 -1
- package/dist/components/approvals/client.d.mts +11 -12
- package/dist/components/approvals/client.mjs +1 -1
- package/dist/components/approvals/client.mjs.map +1 -1
- package/dist/components/approvals/constants.d.mts +30 -31
- package/dist/components/approvals/constants.mjs.map +1 -1
- package/dist/components/approvals/convex.config.d.mts +0 -2
- package/dist/components/approvals/convex.config.mjs.map +1 -1
- package/dist/components/approvals/decisions.d.mts +13 -17
- package/dist/components/approvals/decisions.mjs +1 -1
- package/dist/components/approvals/decisions.mjs.map +1 -1
- package/dist/components/approvals/functions.d.mts +3 -4
- package/dist/components/approvals/functions.mjs +1 -1
- package/dist/components/approvals/functions.mjs.map +1 -1
- package/dist/components/approvals/health.d.mts +2 -3
- package/dist/components/approvals/health.mjs.map +1 -1
- package/dist/components/approvals/requests.d.mts +114 -131
- package/dist/components/approvals/requests.mjs +1 -1
- package/dist/components/approvals/requests.mjs.map +1 -1
- package/dist/components/approvals/schema.d.ts +106 -315
- package/dist/components/approvals/schema.js +1 -1
- package/dist/components/approvals/validators.d.mts +323 -395
- package/dist/components/approvals/validators.mjs +1 -1
- package/dist/components/approvals/validators.mjs.map +1 -1
- package/dist/components/approvals/workflow.d.mts +4 -5
- package/dist/components/approvals/workflow.mjs +1 -1
- package/dist/components/approvals/workflow.mjs.map +1 -1
- package/dist/components/approvals/workflow_steps.d.mts +16 -17
- package/dist/components/approvals/workflow_steps.mjs +1 -1
- package/dist/components/approvals/workflow_steps.mjs.map +1 -1
- package/dist/components/foundation/_generated/api.d.mts +6 -8
- package/dist/components/foundation/_generated/api.mjs.map +1 -1
- package/dist/components/foundation/_generated/component.d.mts +1 -4
- package/dist/components/foundation/_generated/dataModel.d.mts +5 -8
- package/dist/components/foundation/_generated/server.d.mts +12 -15
- package/dist/components/foundation/_generated/server.mjs.map +1 -1
- package/dist/components/foundation/client.d.mts +45 -38
- package/dist/components/foundation/client.mjs +1 -1
- package/dist/components/foundation/client.mjs.map +1 -1
- package/dist/components/foundation/convex.config.d.mts +0 -2
- package/dist/components/foundation/convex.config.mjs.map +1 -1
- package/dist/components/foundation/functions.d.mts +2 -3
- package/dist/components/foundation/functions.mjs.map +1 -1
- package/dist/components/foundation/modules/command/command.d.mts +65 -17
- package/dist/components/foundation/modules/command/command.mjs +1 -1
- package/dist/components/foundation/modules/command/command.mjs.map +1 -1
- package/dist/components/foundation/modules/command/lifecycle.d.mts +78 -0
- package/dist/components/foundation/modules/command/lifecycle.mjs +2 -0
- package/dist/components/foundation/modules/command/lifecycle.mjs.map +1 -0
- package/dist/components/foundation/modules/observability/observability.d.mts +13 -6
- package/dist/components/foundation/modules/observability/observability.mjs +1 -1
- package/dist/components/foundation/modules/observability/observability.mjs.map +1 -1
- package/dist/components/foundation/modules/query/query.d.mts +11 -12
- package/dist/components/foundation/modules/query/query.mjs +1 -1
- package/dist/components/foundation/modules/query/query.mjs.map +1 -1
- package/dist/components/foundation/operation.d.mts +72 -0
- package/dist/components/foundation/operation.mjs +2 -0
- package/dist/components/foundation/operation.mjs.map +1 -0
- package/dist/components/foundation/result.d.mts +7 -8
- package/dist/components/foundation/result.mjs.map +1 -1
- package/dist/components/foundation/schema.d.ts +0 -2
- package/dist/components/foundation/telemetry.d.mts +2 -3
- package/dist/components/foundation/telemetry.mjs +1 -1
- package/dist/components/foundation/telemetry.mjs.map +1 -1
- package/dist/contracts.d.mts +5 -0
- package/dist/contracts.mjs +1 -0
- package/dist/crud.d.mts +185 -27
- package/dist/crud.mjs +1 -1
- package/dist/crud.mjs.map +1 -1
- package/dist/effect.d.mts +16 -0
- package/dist/effect.mjs +1 -0
- package/dist/errors.d.mts +6 -6
- package/dist/errors.mjs +1 -1
- package/dist/errors.mjs.map +1 -1
- package/dist/filters.d.mts +4 -0
- package/dist/filters.mjs +1 -0
- package/dist/idempotency.d.mts +2 -0
- package/dist/idempotency.mjs +1 -0
- package/dist/index.d.mts +14 -7
- package/dist/index.mjs +1 -1
- package/dist/json-schema.d.mts +40 -0
- package/dist/json-schema.mjs +2 -0
- package/dist/json-schema.mjs.map +1 -0
- package/dist/k/compile.d.mts +17 -0
- package/dist/k/compile.mjs +2 -0
- package/dist/k/compile.mjs.map +1 -0
- package/dist/k/dates.d.mts +23 -0
- package/dist/k/dates.mjs +2 -0
- package/dist/k/dates.mjs.map +1 -0
- package/dist/k/formats.d.mts +24 -0
- package/dist/k/formats.mjs +2 -0
- package/dist/k/formats.mjs.map +1 -0
- package/dist/k/index.d.mts +32 -0
- package/dist/k/index.mjs +2 -0
- package/dist/k/index.mjs.map +1 -0
- package/dist/k/issues.d.mts +31 -0
- package/dist/k/issues.mjs +2 -0
- package/dist/k/issues.mjs.map +1 -0
- package/dist/k/kinds.d.mts +81 -0
- package/dist/k/kinds.mjs +2 -0
- package/dist/k/kinds.mjs.map +1 -0
- package/dist/k/object.d.mts +43 -0
- package/dist/k/object.mjs +2 -0
- package/dist/k/object.mjs.map +1 -0
- package/dist/k/parse.d.mts +29 -0
- package/dist/k/parse.mjs +2 -0
- package/dist/k/parse.mjs.map +1 -0
- package/dist/k/schema.d.mts +100 -0
- package/dist/k/schema.mjs +2 -0
- package/dist/k/schema.mjs.map +1 -0
- package/dist/k/standard.d.mts +8 -0
- package/dist/k/standard.mjs +2 -0
- package/dist/k/standard.mjs.map +1 -0
- package/dist/k/types.d.mts +64 -0
- package/dist/k/types.mjs +1 -0
- package/dist/k.d.mts +8 -0
- package/dist/k.mjs +1 -0
- package/dist/middleware.d.mts +8 -11
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/modules/contracts/binding.d.mts +23 -0
- package/dist/modules/contracts/binding.mjs +2 -0
- package/dist/modules/contracts/binding.mjs.map +1 -0
- package/dist/modules/contracts/contract.d.mts +67 -0
- package/dist/modules/contracts/contract.mjs +2 -0
- package/dist/modules/contracts/contract.mjs.map +1 -0
- package/dist/modules/contracts/convex.d.mts +18 -0
- package/dist/modules/contracts/convex.mjs +2 -0
- package/dist/modules/contracts/convex.mjs.map +1 -0
- package/dist/modules/contracts/domain.d.mts +62 -0
- package/dist/modules/contracts/domain.mjs +2 -0
- package/dist/modules/contracts/domain.mjs.map +1 -0
- package/dist/modules/contracts/errors.d.mts +737 -0
- package/dist/modules/contracts/errors.mjs +2 -0
- package/dist/modules/contracts/errors.mjs.map +1 -0
- package/dist/modules/contracts/exposure.d.mts +103 -0
- package/dist/modules/contracts/exposure.mjs +2 -0
- package/dist/modules/contracts/exposure.mjs.map +1 -0
- package/dist/modules/contracts/idempotency.d.mts +85 -0
- package/dist/modules/contracts/idempotency.mjs +2 -0
- package/dist/modules/contracts/idempotency.mjs.map +1 -0
- package/dist/modules/contracts/lazy.d.mts +6 -0
- package/dist/modules/contracts/lazy.mjs +2 -0
- package/dist/modules/contracts/lazy.mjs.map +1 -0
- package/dist/modules/contracts/standard.d.mts +17 -0
- package/dist/modules/contracts/standard.mjs +2 -0
- package/dist/modules/contracts/standard.mjs.map +1 -0
- package/dist/modules/contracts/workflow.d.mts +165 -0
- package/dist/modules/contracts/workflow.mjs +2 -0
- package/dist/modules/contracts/workflow.mjs.map +1 -0
- package/dist/modules/effect/action.d.mts +63 -0
- package/dist/modules/effect/action.mjs +2 -0
- package/dist/modules/effect/action.mjs.map +1 -0
- package/dist/modules/effect/agent-tools.d.mts +2 -0
- package/dist/modules/effect/agent-tools.mjs +1 -0
- package/dist/modules/effect/api-runtime.d.mts +36 -0
- package/dist/modules/effect/api-runtime.mjs +2 -0
- package/dist/modules/effect/api-runtime.mjs.map +1 -0
- package/dist/modules/effect/api.d.mts +46 -0
- package/dist/modules/effect/api.mjs +2 -0
- package/dist/modules/effect/api.mjs.map +1 -0
- package/dist/modules/effect/auth.d.mts +46 -0
- package/dist/modules/effect/auth.mjs +2 -0
- package/dist/modules/effect/auth.mjs.map +1 -0
- package/dist/modules/effect/client.d.mts +15 -0
- package/dist/modules/effect/client.mjs +1 -0
- package/dist/modules/effect/command.d.mts +108 -0
- package/dist/modules/effect/command.mjs +2 -0
- package/dist/modules/effect/command.mjs.map +1 -0
- package/dist/modules/effect/crud.d.mts +885 -0
- package/dist/modules/effect/crud.mjs +2 -0
- package/dist/modules/effect/crud.mjs.map +1 -0
- package/dist/modules/effect/domain.d.mts +82 -0
- package/dist/modules/effect/domain.mjs +2 -0
- package/dist/modules/effect/domain.mjs.map +1 -0
- package/dist/modules/effect/errors.d.mts +13 -0
- package/dist/modules/effect/errors.mjs +2 -0
- package/dist/modules/effect/errors.mjs.map +1 -0
- package/dist/modules/effect/failure-audit.d.mts +48 -0
- package/dist/modules/effect/failure-audit.mjs +2 -0
- package/dist/modules/effect/failure-audit.mjs.map +1 -0
- package/dist/modules/effect/failures.d.mts +21 -0
- package/dist/modules/effect/failures.mjs +2 -0
- package/dist/modules/effect/failures.mjs.map +1 -0
- package/dist/modules/effect/foundation.d.mts +23 -0
- package/dist/modules/effect/foundation.mjs +2 -0
- package/dist/modules/effect/foundation.mjs.map +1 -0
- package/dist/modules/effect/idempotency.d.mts +29 -0
- package/dist/modules/effect/idempotency.mjs +2 -0
- package/dist/modules/effect/idempotency.mjs.map +1 -0
- package/dist/modules/effect/input.d.mts +10 -0
- package/dist/modules/effect/input.mjs +2 -0
- package/dist/modules/effect/input.mjs.map +1 -0
- package/dist/modules/effect/normalize.d.mts +7 -0
- package/dist/modules/effect/normalize.mjs +2 -0
- package/dist/modules/effect/normalize.mjs.map +1 -0
- package/dist/modules/effect/operation.d.mts +148 -0
- package/dist/modules/effect/operation.mjs +2 -0
- package/dist/modules/effect/operation.mjs.map +1 -0
- package/dist/modules/effect/query.d.mts +90 -0
- package/dist/modules/effect/query.mjs +2 -0
- package/dist/modules/effect/query.mjs.map +1 -0
- package/dist/modules/effect/schema.d.mts +30 -0
- package/dist/modules/effect/schema.mjs +2 -0
- package/dist/modules/effect/schema.mjs.map +1 -0
- package/dist/modules/effect/scope.d.mts +25 -0
- package/dist/modules/effect/scope.mjs +2 -0
- package/dist/modules/effect/scope.mjs.map +1 -0
- package/dist/modules/effect/workflow.d.mts +75 -0
- package/dist/modules/effect/workflow.mjs +2 -0
- package/dist/modules/effect/workflow.mjs.map +1 -0
- package/dist/modules/foundation/foundation.d.mts +108 -0
- package/dist/modules/foundation/foundation.mjs +2 -0
- package/dist/modules/foundation/foundation.mjs.map +1 -0
- package/dist/modules/foundation/operation.d.mts +139 -0
- package/dist/modules/foundation/operation.mjs +2 -0
- package/dist/modules/foundation/operation.mjs.map +1 -0
- package/dist/namespace.d.mts +104 -0
- package/dist/namespace.mjs +2 -0
- package/dist/namespace.mjs.map +1 -0
- package/dist/oxlint/architecture.d.mts +23 -0
- package/dist/oxlint/architecture.mjs +2 -0
- package/dist/oxlint/architecture.mjs.map +1 -0
- package/dist/oxlint/ast.d.mts +51 -0
- package/dist/oxlint/ast.mjs +2 -0
- package/dist/oxlint/ast.mjs.map +1 -0
- package/dist/oxlint/components.d.mts +4 -0
- package/dist/oxlint/components.mjs +2 -0
- package/dist/oxlint/components.mjs.map +1 -0
- package/dist/oxlint/ownership.d.mts +59 -0
- package/dist/oxlint/ownership.mjs +2 -0
- package/dist/oxlint/ownership.mjs.map +1 -0
- package/dist/oxlint/reserved-files.d.mts +4 -0
- package/dist/oxlint/reserved-files.mjs +2 -0
- package/dist/oxlint/reserved-files.mjs.map +1 -0
- package/dist/oxlint.d.mts +9 -0
- package/dist/oxlint.mjs +2 -0
- package/dist/oxlint.mjs.map +1 -0
- package/dist/querying/cursors.d.mts +94 -0
- package/dist/querying/cursors.mjs +2 -0
- package/dist/querying/cursors.mjs.map +1 -0
- package/dist/querying/execution.d.mts +40 -0
- package/dist/querying/execution.mjs +2 -0
- package/dist/querying/execution.mjs.map +1 -0
- package/dist/querying/failures.d.mts +11 -0
- package/dist/querying/failures.mjs +2 -0
- package/dist/querying/failures.mjs.map +1 -0
- package/dist/querying/filter-boundary.d.mts +25 -0
- package/dist/querying/filter-boundary.mjs +2 -0
- package/dist/querying/filter-boundary.mjs.map +1 -0
- package/dist/querying/filters.d.mts +180 -0
- package/dist/querying/filters.mjs +2 -0
- package/dist/querying/filters.mjs.map +1 -0
- package/dist/querying/planner.d.mts +61 -0
- package/dist/querying/planner.mjs +2 -0
- package/dist/querying/planner.mjs.map +1 -0
- package/dist/querying/relationships.d.mts +58 -0
- package/dist/querying/relationships.mjs +2 -0
- package/dist/querying/relationships.mjs.map +1 -0
- package/dist/querying/search.d.mts +69 -0
- package/dist/querying/search.mjs +2 -0
- package/dist/querying/search.mjs.map +1 -0
- package/dist/querying/security.d.mts +123 -0
- package/dist/querying/security.mjs +2 -0
- package/dist/querying/security.mjs.map +1 -0
- package/dist/querying/shared/ast.d.mts +26 -0
- package/dist/querying/shared/ast.mjs +2 -0
- package/dist/querying/shared/ast.mjs.map +1 -0
- package/dist/querying/shared/contract.d.mts +98 -0
- package/dist/querying/shared/contract.mjs +2 -0
- package/dist/querying/shared/contract.mjs.map +1 -0
- package/dist/querying/shared/evaluate.d.mts +5 -0
- package/dist/querying/shared/evaluate.mjs +2 -0
- package/dist/querying/shared/evaluate.mjs.map +1 -0
- package/dist/querying/shared/filters.d.mts +65 -0
- package/dist/querying/shared/filters.mjs +2 -0
- package/dist/querying/shared/filters.mjs.map +1 -0
- package/dist/querying/shared/native-json.d.mts +7 -0
- package/dist/querying/shared/native-json.mjs +2 -0
- package/dist/querying/shared/native-json.mjs.map +1 -0
- package/dist/querying/shared/page.d.mts +20 -0
- package/dist/querying/shared/page.mjs +2 -0
- package/dist/querying/shared/page.mjs.map +1 -0
- package/dist/querying.d.mts +82 -0
- package/dist/querying.mjs +2 -0
- package/dist/querying.mjs.map +1 -0
- package/dist/react/failures.d.mts +24 -0
- package/dist/react/failures.mjs +2 -0
- package/dist/react/failures.mjs.map +1 -0
- package/dist/react/optimistic.d.mts +46 -0
- package/dist/react/optimistic.mjs +2 -0
- package/dist/react/optimistic.mjs.map +1 -0
- package/dist/react/pagination-state.d.mts +39 -0
- package/dist/react/pagination-state.mjs +2 -0
- package/dist/react/pagination-state.mjs.map +1 -0
- package/dist/react/use-command.d.mts +22 -0
- package/dist/react/use-command.mjs +2 -0
- package/dist/react/use-command.mjs.map +1 -0
- package/dist/react/use-paginated-list.d.mts +34 -0
- package/dist/react/use-paginated-list.mjs +2 -0
- package/dist/react/use-paginated-list.mjs.map +1 -0
- package/dist/react.d.mts +6 -0
- package/dist/react.mjs +1 -0
- package/dist/registration.d.mts +65 -0
- package/dist/registration.mjs +2 -0
- package/dist/registration.mjs.map +1 -0
- package/dist/relations-public.d.mts +2 -0
- package/dist/relations-public.mjs +1 -0
- package/dist/relations.d.mts +274 -0
- package/dist/relations.mjs +2 -0
- package/dist/relations.mjs.map +1 -0
- package/dist/state-machine.d.mts +1 -4
- package/dist/state-machine.mjs.map +1 -1
- package/dist/table.d.mts +222 -0
- package/dist/table.mjs +2 -0
- package/dist/table.mjs.map +1 -0
- package/dist/tenancy.d.mts +6 -8
- package/dist/tenancy.mjs +1 -1
- package/dist/tenancy.mjs.map +1 -1
- package/dist/triggers.d.mts +12 -10
- package/dist/triggers.mjs +1 -1
- package/dist/triggers.mjs.map +1 -1
- package/dist/webhooks.d.mts +22 -112
- package/dist/webhooks.mjs +1 -1
- package/dist/webhooks.mjs.map +1 -1
- package/dist/workflow.d.mts +2 -0
- package/dist/workflow.mjs +1 -0
- package/dist/zod.d.mts +30 -0
- package/dist/zod.mjs +2 -0
- package/dist/zod.mjs.map +1 -0
- package/package.json +145 -33
- package/src/test.ts +9 -17
- package/CHANGELOG.md +0 -145
- package/ROADMAP.md +0 -126
- package/dist/zod-table.d.mts +0 -175
- package/dist/zod-table.mjs +0 -2
- package/dist/zod-table.mjs.map +0 -1
- package/docs/agent-tools.md +0 -41
- package/docs/approvals.md +0 -200
- package/docs/architecture.md +0 -124
- package/docs/auth.md +0 -218
- package/docs/commands.md +0 -278
- package/docs/conventions.md +0 -291
- package/docs/crud.md +0 -62
- package/docs/llms.md +0 -220
- package/docs/maintainability.md +0 -124
- package/docs/migration.md +0 -351
- package/docs/plans/2026-08-14-001-feat-v0.1.0-feature-set-plan.md +0 -461
- package/docs/plans/2026-08-21-0930-feat-resolve-organization-hook-plan.md +0 -178
- package/docs/tenancy.md +0 -179
- package/docs/triggers.md +0 -75
- package/docs/upgrading.md +0 -175
- package/docs/webhooks.md +0 -58
- package/docs/zod-table.md +0 -173
- package/scripts/convex-config-bridge.mjs +0 -55
- package/scripts/extract-changelog.mjs +0 -29
- package/scripts/smoke-packed-components.mjs +0 -374
package/docs/approvals.md
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
# `cvx-kit/components/approvals` — declarative approval workflows
|
|
2
|
-
|
|
3
|
-
A self-contained Convex component for human-in-the-loop approval flows:
|
|
4
|
-
declarative step lists (decision / branch / mutation / action / notify) with
|
|
5
|
-
count quorum, maker-checker, expiry, compatibility keys, durable execution via
|
|
6
|
-
a **nested** `@convex-dev/workflow`, and its own audit trail via a **nested**
|
|
7
|
-
`convex-audit-log`. The component never imports host code: tenancy is an
|
|
8
|
-
opaque `scopeRef`, resources are opaque `resourceType`/`resourceRef` strings,
|
|
9
|
-
and callbacks cross the boundary as **function handles**.
|
|
10
|
-
|
|
11
|
-
## Mounting
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
// convex/convex.config.ts
|
|
15
|
-
import approvals from 'cvx-kit/components/approvals/convex.config'
|
|
16
|
-
app.use(approvals)
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
After installation or upgrade, deploy once and run the component health query
|
|
20
|
-
from a host wrapper:
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
return ctx.runQuery(components.approvals.health.check, {})
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
It returns the installed approvals schema version and required index names. It
|
|
27
|
-
also executes reads through
|
|
28
|
-
`approvalDecisions.by_runId_and_decidedAt` and
|
|
29
|
-
`approvalDecisions.by_runId_and_stepKey_and_actor_actorRef`, failing with an
|
|
30
|
-
installation-specific diagnostic if the packaged component schema was not
|
|
31
|
-
deployed.
|
|
32
|
-
|
|
33
|
-
The component mounts its own private `workflow` and `auditLog` children. Do
|
|
34
|
-
**not** re-register those children at the app root under the same identity,
|
|
35
|
-
and never reach into `components.approvals.workflow` / `.auditLog` from host
|
|
36
|
-
code — the `Approvals` client class is the only contract.
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
// convex/approvals.ts — host facade, declared once
|
|
40
|
-
import { Approvals } from 'cvx-kit/components/approvals'
|
|
41
|
-
import { components } from './_generated/api'
|
|
42
|
-
|
|
43
|
-
export const approvals = new Approvals(components.approvals)
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Defining a workflow — in the owning domain
|
|
47
|
-
|
|
48
|
-
Workflow *definitions* are business logic and live in the domain that owns the
|
|
49
|
-
resource, not in the root facade:
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
// convex/domain/documents/approval.ts
|
|
53
|
-
import { approvals } from '../../approvals'
|
|
54
|
-
import { internal } from '../../_generated/api'
|
|
55
|
-
|
|
56
|
-
export const publishApproval = approvals.define({
|
|
57
|
-
name: 'documentPublish',
|
|
58
|
-
steps: [
|
|
59
|
-
approvals.decision('managerDecision', {
|
|
60
|
-
decisions: ['approved', 'rejected'],
|
|
61
|
-
quorum: { kind: 'count', approvals: 1 },
|
|
62
|
-
makerChecker: true, // requester cannot decide
|
|
63
|
-
expiresAfterMs: 7 * 24 * 60 * 60 * 1000,
|
|
64
|
-
}),
|
|
65
|
-
approvals.branch('applyDecision', {
|
|
66
|
-
approvedStepKey: 'publishDocument',
|
|
67
|
-
rejectedStepKey: 'notifyRejected',
|
|
68
|
-
}),
|
|
69
|
-
approvals.mutation('publishDocument', {
|
|
70
|
-
handler: internal.domain.documents.approval_functions.applyDecision,
|
|
71
|
-
}),
|
|
72
|
-
approvals.notify('notifyRejected', {
|
|
73
|
-
handler: internal.domain.documents.approval_functions.notifyRejected,
|
|
74
|
-
retry: true,
|
|
75
|
-
}),
|
|
76
|
-
],
|
|
77
|
-
})
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### Step kinds
|
|
81
|
-
|
|
82
|
-
| Step | Runs | Notes |
|
|
83
|
-
|---|---|---|
|
|
84
|
-
| `decision(key, { decisions, quorum, makerChecker?, expiresAfterMs? })` | waits for humans | quorum is `{ kind: 'count', approvals: n }`; expiry resolves the run as `expired` |
|
|
85
|
-
| `branch(key, { approvedStepKey, rejectedStepKey })` | routing | jumps by the preceding decision's outcome |
|
|
86
|
-
| `mutation(key, { handler, retry? })` | internal mutation handle | transactional apply step |
|
|
87
|
-
| `action(key, { handler, retry? })` | internal action handle | side effects (third parties) |
|
|
88
|
-
| `notify(key, { handler, retry? })` | internal action handle | semantically a notification; same contract as action |
|
|
89
|
-
|
|
90
|
-
Limits (enforced by the component's validators): ≤ 32 steps per workflow,
|
|
91
|
-
≤ 8 decisions per decision step, names ≤ 96 chars, references ≤ 256 chars,
|
|
92
|
-
metadata ≤ 32 entries (keys ≤ 64, values ≤ 512 chars).
|
|
93
|
-
|
|
94
|
-
### Compatibility keys
|
|
95
|
-
|
|
96
|
-
`compatibilityKey` (defaults to `name`) versions the workflow shape. Every
|
|
97
|
-
`decide`/`cancel`/`status`/`restart` call sends it, so a run started under an
|
|
98
|
-
old definition cannot be driven by an incompatible new one. When you change a
|
|
99
|
-
workflow's steps in a breaking way, change the key.
|
|
100
|
-
|
|
101
|
-
## Driving a run
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
// start — from an authMutation, with ctx.actor as the requester
|
|
105
|
-
const { runId } = await publishApproval.start(ctx, {
|
|
106
|
-
scopeRef: ctx.org.organizationId, // opaque tenancy
|
|
107
|
-
resourceType: 'document',
|
|
108
|
-
resourceRef: documentId, // opaque to the component
|
|
109
|
-
requester: ctx.actor,
|
|
110
|
-
metadata: { title }, // bounded string map
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// decide — a manager approves or rejects
|
|
114
|
-
await publishApproval.decide(ctx, { runId, decision: 'approved', reason }, ctx.actor)
|
|
115
|
-
|
|
116
|
-
// observe from a host query
|
|
117
|
-
await publishApproval.status(ctx, runId) // pending | approved | rejected | expired | canceled
|
|
118
|
-
await publishApproval.evidence(ctx, runId) // per-step decision evidence
|
|
119
|
-
await publishApproval.list(ctx, {
|
|
120
|
-
scopeRef,
|
|
121
|
-
state: 'pending',
|
|
122
|
-
paginationOpts: { cursor: null, numItems: 20 },
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
// manage
|
|
126
|
-
await publishApproval.cancel(ctx, runId, ctx.actor)
|
|
127
|
-
await publishApproval.restart(ctx, runId)
|
|
128
|
-
await approvals.cleanupAudit(ctx, { olderThanDays: 365, batchSize: 100 })
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
## Callback handlers — validate everything coming back
|
|
132
|
-
|
|
133
|
-
Step handlers are **internal** functions (build them with `systemMutation` /
|
|
134
|
-
`systemAction`). They receive `ApprovalCallbackInput`: `{ runId, scopeRef,
|
|
135
|
-
resourceType, resourceRef, metadata?, decision? }` — all opaque strings.
|
|
136
|
-
Because the component cannot know your tables, the handler must re-establish
|
|
137
|
-
trust:
|
|
138
|
-
|
|
139
|
-
```ts
|
|
140
|
-
import { approvalCallbackArgs } from 'cvx-kit/components/approvals'
|
|
141
|
-
|
|
142
|
-
export const applyDecision = systemMutation({
|
|
143
|
-
args: approvalCallbackArgs,
|
|
144
|
-
handler: async (ctx, input) => {
|
|
145
|
-
if (input.resourceType !== 'document') throw new Error('WRONG_RESOURCE')
|
|
146
|
-
const id = ctx.db.normalizeId('documents', input.resourceRef)
|
|
147
|
-
if (!id) throw new Error('BAD_REF')
|
|
148
|
-
const document = await ctx.db.get(id)
|
|
149
|
-
// guard against stale runs: the doc must still point at this run
|
|
150
|
-
if (document?.approvalRunId !== input.runId) return
|
|
151
|
-
await ctx.db.patch(id, { reviewState: 'published' })
|
|
152
|
-
},
|
|
153
|
-
})
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
That `approvalRunId` check is the idempotency/staleness guard from the origin
|
|
157
|
-
app — copy it.
|
|
158
|
-
|
|
159
|
-
For branch callbacks, `decision.evidence` contains
|
|
160
|
-
`{ stepKey, actorRef, decision, reason?, decidedAt }` items. Use the exported
|
|
161
|
-
`approvalCallbackArgs` validator instead of duplicating that protocol boundary.
|
|
162
|
-
|
|
163
|
-
### Paginated inboxes
|
|
164
|
-
|
|
165
|
-
Expose the configured workflow through a host query; component pagination uses
|
|
166
|
-
`convex-helpers` internally because Convex's built-in `.paginate()` is not
|
|
167
|
-
available inside components:
|
|
168
|
-
|
|
169
|
-
```ts
|
|
170
|
-
export const approvalInbox = authQuery({
|
|
171
|
-
args: {
|
|
172
|
-
state: z.enum(['pending', 'approved', 'rejected', 'expired', 'canceled']).optional(),
|
|
173
|
-
paginationOpts: convexToZod(paginationOptsValidator),
|
|
174
|
-
},
|
|
175
|
-
handler: (ctx, args) => publishApproval.list(ctx, {
|
|
176
|
-
scopeRef: ctx.org.organizationId,
|
|
177
|
-
...args,
|
|
178
|
-
}),
|
|
179
|
-
})
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
The result keeps Convex's standard pagination shape, so React consumers can
|
|
183
|
-
call `usePaginatedQuery(api.approvals.approvalInbox, { state: 'pending' },
|
|
184
|
-
{ initialNumItems: 20 })`. Pass each returned cursor only back to the same
|
|
185
|
-
scope/state query.
|
|
186
|
-
|
|
187
|
-
## Rules and footguns
|
|
188
|
-
|
|
189
|
-
1. Root facade (`convex/approvals.ts`) instantiates the client; workflow
|
|
190
|
-
*definitions* live in the owning domain.
|
|
191
|
-
2. Handlers get opaque strings — always `normalizeId`, re-check
|
|
192
|
-
`resourceType`, and verify the resource still references the run.
|
|
193
|
-
3. `makerChecker: true` means the requester's decisions are rejected — design
|
|
194
|
-
your UI accordingly.
|
|
195
|
-
4. The component keeps its own audit trail (retention category
|
|
196
|
-
`approval-protocol`, cleanup via `cleanupAudit`, retention 1–3650 days,
|
|
197
|
-
batch ≤ 100). Host-side business audit still goes through your command
|
|
198
|
-
protocol — they are different trails.
|
|
199
|
-
5. Changing steps without changing `compatibilityKey` lets old runs be driven
|
|
200
|
-
by a new incompatible shape; bump the key on breaking changes.
|
package/docs/architecture.md
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
# Architecture — the shape of a cvx-kit application
|
|
2
|
-
|
|
3
|
-
cvx-kit was extracted from a production Convex application (~12k LOC backend).
|
|
4
|
-
This document describes the architecture that application follows and that the
|
|
5
|
-
kit is designed to reproduce: strict ownership boundaries, injected policy,
|
|
6
|
-
portable components, and conventions that are *executable* (enforced by tests)
|
|
7
|
-
rather than aspirational.
|
|
8
|
-
|
|
9
|
-
## The layers
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
convex/
|
|
13
|
-
convex.config.ts # component mounting — each app.use(x) exactly once
|
|
14
|
-
schema.ts # defineSchema(domainTables) — nothing else
|
|
15
|
-
functions.ts # createAuthFunctions(...) — the ONLY place raw builders appear
|
|
16
|
-
foundation.ts # const { Command, Query, observability } = new Foundation(...)
|
|
17
|
-
approvals.ts # const approvals = new Approvals(components.approvals)
|
|
18
|
-
audit.ts # writeAuditEntry over convex-audit-log + admin readers
|
|
19
|
-
triggers.ts # the single Triggers registry + helper registrations
|
|
20
|
-
auth.ts / auth.config.ts / http.ts # identity provider wiring (e.g. WorkOS AuthKit)
|
|
21
|
-
api/ # PUBLIC surface — thin adapters only
|
|
22
|
-
<documents>.ts # authQuery/authMutation wrapping domain executors
|
|
23
|
-
domain/
|
|
24
|
-
table.ts # merges per-domain table maps into domainTables
|
|
25
|
-
shared/ # genuinely cross-entity helpers only
|
|
26
|
-
<entity>/ # one directory per entity/aggregate
|
|
27
|
-
schema.ts # zodTable declaration (shapes only)
|
|
28
|
-
table.ts # indexes attached to <zodTable>.table
|
|
29
|
-
constants.ts # finite vocabularies (UPPER_SNAKE readonly tuples)
|
|
30
|
-
commands.ts # operation registry + executors (new Command(...))
|
|
31
|
-
queries.ts # domain read logic
|
|
32
|
-
approval.ts # approvals.define(...) workflow definitions
|
|
33
|
-
*_functions.ts # internal callbacks (systemMutation/systemAction)
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Layer rules
|
|
37
|
-
|
|
38
|
-
1. **`api/` is the only public surface.** It contains no bare `query(` or
|
|
39
|
-
`mutation(` — only `auth*`/`role*`/`admin*` constructors delegating to
|
|
40
|
-
domain `execute*` functions. Public functions are adapters: parse, pass
|
|
41
|
-
`ctx.actor`, project DTOs.
|
|
42
|
-
2. **`domain/<entity>/` owns everything about the entity** — its zod shape,
|
|
43
|
-
indexes, vocabularies, commands, queries, workflows. Cross-domain reuse
|
|
44
|
-
goes through `domain/shared/`, never sibling imports of another domain's
|
|
45
|
-
internals.
|
|
46
|
-
3. **Root files are facades over mounted components.** `foundation.ts`,
|
|
47
|
-
`approvals.ts`, `audit.ts` configure infrastructure; business definitions
|
|
48
|
-
stay in the owning domain.
|
|
49
|
-
4. **Internal functions use `systemMutation`/`systemAction`** — never raw
|
|
50
|
-
`internalMutation(`/`internalAction(` — so triggers and `include` apply to
|
|
51
|
-
scheduled/callback work too.
|
|
52
|
-
|
|
53
|
-
## Injection, not import
|
|
54
|
-
|
|
55
|
-
The kit's central design rule: **library code never imports app singletons**.
|
|
56
|
-
Every kit surface takes its policy as a parameter:
|
|
57
|
-
|
|
58
|
-
- `createAuthFunctions` receives the generated builders, `getAuthUser`,
|
|
59
|
-
`mapRole`, `verifyMembership`, `triggers`, `errors`.
|
|
60
|
-
- `Foundation` receives `classifyError`, `writeAudit`, the `enabled` flag.
|
|
61
|
-
- `Approvals` receives only its component reference; host data crosses as
|
|
62
|
-
opaque validated strings.
|
|
63
|
-
|
|
64
|
-
The reverse rule holds for components: a portable component must not import
|
|
65
|
-
host domains, generated host APIs, environment configuration, or vendor SDKs
|
|
66
|
-
(WorkOS, Drizzle, Redis…). Host values cross its public API only as
|
|
67
|
-
**validated, bounded, opaque data** — `scopeRef`, `resourceRef`, function
|
|
68
|
-
handles. Component schemas contain only their own records and never mirror a
|
|
69
|
-
private child component's tables.
|
|
70
|
-
|
|
71
|
-
## Boundaries as schemas
|
|
72
|
-
|
|
73
|
-
Data crossing any boundary is a zod schema derived from one `zodTable`
|
|
74
|
-
declaration per entity (see `zod-table.md`):
|
|
75
|
-
|
|
76
|
-
- storage → `defineTable` via `zodToConvex`
|
|
77
|
-
- writes → `insertSchema`/`updateSchema` (server fields and timestamps
|
|
78
|
-
excluded)
|
|
79
|
-
- commands → `commandInput` (explicit allowlist)
|
|
80
|
-
- clients → `publicDto` + `toPublicDto` (runtime redaction)
|
|
81
|
-
- LLM tools → `tools.*` and `jsonSafeZid`
|
|
82
|
-
|
|
83
|
-
Everything is `.strict()`. There is exactly one source of truth per entity and
|
|
84
|
-
no boundary that "just trusts" its input.
|
|
85
|
-
|
|
86
|
-
## Writes as commands
|
|
87
|
-
|
|
88
|
-
State changes flow through the audited command protocol (see `commands.md`):
|
|
89
|
-
parse input → observe → run handler → parse output → write audit — one
|
|
90
|
-
transaction, rollback-consistent. Classifications and audit derivation are
|
|
91
|
-
type-required per operation. Human-gated changes use the approvals component;
|
|
92
|
-
its apply steps are commands too.
|
|
93
|
-
|
|
94
|
-
## Conventions worth enforcing with tests
|
|
95
|
-
|
|
96
|
-
The origin app enforces its architecture with a test suite that raw-globs the
|
|
97
|
-
source and asserts violations — each rule proved against a synthetic fixture
|
|
98
|
-
first so the checker can't silently pass. Highly recommended for consumers:
|
|
99
|
-
|
|
100
|
-
- **No inline tables/schemas** — `defineTable`/`defineSchema` only in
|
|
101
|
-
`table.ts`/`schema.ts`.
|
|
102
|
-
- **No inline enums** — `z.enum([...])` with an inline array literal is
|
|
103
|
-
banned; vocabularies are named `UPPER_SNAKE` tuples in `constants.ts`.
|
|
104
|
-
- **No deep server imports** — components consumed only through their
|
|
105
|
-
`client.ts` facade; explicit allowlist for exceptions.
|
|
106
|
-
- **No handwritten references** — `makeFunctionReference` banned; use
|
|
107
|
-
generated `internal`/`api`.
|
|
108
|
-
- **No sibling-domain imports** — a domain reaches another only via
|
|
109
|
-
`domain/shared/` or the public API.
|
|
110
|
-
- **Facades mount once** — every `app.use(x)` appears exactly once; facades
|
|
111
|
-
never reach a component's private children.
|
|
112
|
-
- **Component boundary tests** — no host imports, no
|
|
113
|
-
`process.env`/vendor-SDK references inside `components/*`.
|
|
114
|
-
- **Copyability** — copy the component to a tmpdir with only its package
|
|
115
|
-
deps, run `tsc`: proves portability continuously.
|
|
116
|
-
|
|
117
|
-
## Component mounting order
|
|
118
|
-
|
|
119
|
-
In `convex.config.ts`, mount infrastructure before consumers: auth kit →
|
|
120
|
-
foundation → audit log → approvals → named workflow/workpool instances. Use
|
|
121
|
-
`app.use(x, { name })` when mounting the same component twice; `components.
|
|
122
|
-
<name>` keys off that name. Required env vars can be declared on
|
|
123
|
-
`defineApp({ env: { ... } })` so deploys fail fast — remember to stub them in
|
|
124
|
-
test setup.
|
package/docs/auth.md
DELETED
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
# `cvx-kit/auth` — auth-aware function constructors from injected policy
|
|
2
|
-
|
|
3
|
-
`createAuthFunctions` builds the function constructors your whole backend uses
|
|
4
|
-
in place of raw `query`/`mutation`/`action`. Nothing in the kit imports your
|
|
5
|
-
app's singletons — identity resolution, role vocabulary, membership
|
|
6
|
-
verification, trigger wrapping, and error policy are all **injected** by the
|
|
7
|
-
host. The kit provides the structure; the app provides the policy.
|
|
8
|
-
|
|
9
|
-
## Setup — declared once per app
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
// convex/functions.ts (the single place constructors are built)
|
|
13
|
-
import { createAuthFunctions, defaultRoleMap } from 'cvx-kit/auth'
|
|
14
|
-
import {
|
|
15
|
-
action, internalAction, internalMutation, internalQuery, mutation, query,
|
|
16
|
-
} from './_generated/server'
|
|
17
|
-
import type { DataModel } from './_generated/dataModel'
|
|
18
|
-
import { triggers } from './triggers'
|
|
19
|
-
import { authKit } from './auth'
|
|
20
|
-
|
|
21
|
-
export const {
|
|
22
|
-
authQuery, authMutation, authAction,
|
|
23
|
-
roleQuery, roleMutation, roleAction,
|
|
24
|
-
adminQuery, adminMutation, adminAction,
|
|
25
|
-
systemQuery, systemMutation, systemAction,
|
|
26
|
-
include, authenticatedUser,
|
|
27
|
-
} = createAuthFunctions<DataModel>({
|
|
28
|
-
query, mutation, action, internalQuery, internalMutation, internalAction,
|
|
29
|
-
getAuthUser: (ctx) => authKit.getAuthUser(ctx),
|
|
30
|
-
mapRole: defaultRoleMap, // or your own vocabulary (see below)
|
|
31
|
-
adminRoles: ['admin'],
|
|
32
|
-
triggers, // every mutation write runs through wrapDB
|
|
33
|
-
verifyMembership: async ({ userId, organizationId }) => {
|
|
34
|
-
const memberships = await authKit.workos.userManagement
|
|
35
|
-
.listOrganizationMemberships({ organizationId, userId, statuses: ['active'] })
|
|
36
|
-
const m = memberships.data.find((c) => c.userId === userId)
|
|
37
|
-
return m ? { organizationId: m.organizationId, roleSlug: m.role.slug } : null
|
|
38
|
-
},
|
|
39
|
-
})
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Every function in the app is then built from these — raw `query`/`mutation`
|
|
43
|
-
imports from `_generated/server` appear **only** in this file.
|
|
44
|
-
|
|
45
|
-
## The constructor families
|
|
46
|
-
|
|
47
|
-
| Constructor | Visibility | Auth | Extra |
|
|
48
|
-
|---|---|---|---|
|
|
49
|
-
| `authQuery` / `authMutation` / `authAction` | public | any authenticated org member | actions live-verify membership |
|
|
50
|
-
| `roleQuery(...roles)` / `roleMutation(...)` / `roleAction(...)` | public | listed roles only | factory — call with your roles |
|
|
51
|
-
| `adminQuery` / `adminMutation` / `adminAction` | public | `config.adminRoles` | pre-built `role*(...adminRoles)` |
|
|
52
|
-
| `systemQuery` | internal | none (trusted caller) | include-equipped, RLS-unwrapped |
|
|
53
|
-
| `systemMutation` | internal | none (trusted caller) | still trigger-wrapped |
|
|
54
|
-
| `systemAction` | internal | none | plain internal action |
|
|
55
|
-
|
|
56
|
-
All are `zCustom*` constructors from convex-helpers, so `args` and `returns`
|
|
57
|
-
take zod schemas directly and compose with `zodTable` masks.
|
|
58
|
-
|
|
59
|
-
## What handlers receive on `ctx`
|
|
60
|
-
|
|
61
|
-
Authenticated constructors extend the ctx with a frozen auth bundle:
|
|
62
|
-
|
|
63
|
-
- `ctx.identity` — the raw `UserIdentity` (JWT claims)
|
|
64
|
-
- `ctx.user.id` — the synchronized principal id (from `getAuthUser`)
|
|
65
|
-
- `ctx.org` — `{ organizationId, role }`
|
|
66
|
-
- `ctx.role` — the mapped role
|
|
67
|
-
- `ctx.actor` — `{ userId, organizationId, role }` — pass this to audits,
|
|
68
|
-
approvals, and commands as the canonical actor reference
|
|
69
|
-
- `ctx.include` — the bounded query builder (below)
|
|
70
|
-
|
|
71
|
-
`systemQuery` and `systemMutation` get `include` (plus trigger wrapping for the
|
|
72
|
-
mutation) but no auth bundle.
|
|
73
|
-
|
|
74
|
-
## Role vocabulary is per-app
|
|
75
|
-
|
|
76
|
-
`mapRole` maps the identity's role slug onto **your** role union; returning
|
|
77
|
-
`null` rejects with `FORBIDDEN`, so unknown roles never pass. The default
|
|
78
|
-
(`defaultRoleMap`) is WorkOS-flavored: `member → writer`, and
|
|
79
|
-
`reader`/`writer`/`admin` pass through.
|
|
80
|
-
|
|
81
|
-
Supply your own vocabulary via the generic:
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
createAuthFunctions<DataModel, 'viewer' | 'editor' | 'owner'>({
|
|
85
|
-
mapRole: (slug) => (slug === 'owner' ? 'owner' : slug === 'editor' ? 'editor' : slug ? 'viewer' : null),
|
|
86
|
-
adminRoles: ['owner'],
|
|
87
|
-
...
|
|
88
|
-
})
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
## Queries/mutations trust the JWT; actions re-verify
|
|
92
|
-
|
|
93
|
-
Queries and mutations read org and role from the identity token. Actions —
|
|
94
|
-
which can run long and call third parties — additionally call
|
|
95
|
-
`verifyMembership` **live** before the handler runs, and the fresher
|
|
96
|
-
role/organization from that check replaces the JWT's. Verification **fails
|
|
97
|
-
closed**: any error or org mismatch throws `FORBIDDEN`. Omit
|
|
98
|
-
`verifyMembership` to let actions trust the JWT like queries do.
|
|
99
|
-
|
|
100
|
-
## Resolving org/role from the database (`resolveOrganization`)
|
|
101
|
-
|
|
102
|
-
Some identity providers issue tokens that carry **no org claims** — custom
|
|
103
|
-
credentials, magic codes, or apps that keep memberships in their own tables.
|
|
104
|
-
For those, configure `resolveOrganization` to derive organization authority
|
|
105
|
-
from the app's own data instead of the JWT.
|
|
106
|
-
|
|
107
|
-
When configured, the hook is the organization authority — claim parsing is
|
|
108
|
-
skipped entirely. Returning `null` **or throwing** rejects with `FORBIDDEN`
|
|
109
|
-
(fail closed); a missing identity or user is still `UNAUTHENTICATED`; the
|
|
110
|
-
returned `roleSlug` still passes through `mapRole` like a claim would.
|
|
111
|
-
|
|
112
|
-
The handler must be **action-safe**: in actions the ctx has no `db`, so branch
|
|
113
|
-
on its presence and fall back to an internal query:
|
|
114
|
-
|
|
115
|
-
```ts
|
|
116
|
-
import { internal } from './_generated/api'
|
|
117
|
-
|
|
118
|
-
resolveOrganization: async ({ ctx, user }) => {
|
|
119
|
-
const membership =
|
|
120
|
-
'db' in ctx
|
|
121
|
-
? await ctx.db
|
|
122
|
-
.query('companyUsers')
|
|
123
|
-
.withIndex('by_user', (q) => q.eq('userId', user.id))
|
|
124
|
-
.first()
|
|
125
|
-
: await ctx.runQuery(internal.memberships.byUser, { userId: user.id })
|
|
126
|
-
if (!membership || !membership.active) return null // revoked ⇒ FORBIDDEN
|
|
127
|
-
return { organizationId: membership.orgId, roleSlug: membership.roleSlug }
|
|
128
|
-
},
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
Resolver obligations:
|
|
132
|
-
|
|
133
|
-
- Bind the lookup to the **verified** user — `user.id` (or
|
|
134
|
-
`identity.subject`), never a caller-influenced or non-unique attribute.
|
|
135
|
-
- Resolve revoked/inactive/expired memberships to `null`, so revocation
|
|
136
|
-
actually takes effect fail-closed.
|
|
137
|
-
|
|
138
|
-
Composition: `mapRole` still applies to the returned slug; with tenancy
|
|
139
|
-
configured, `ctx.tenant` derives from the hook's org
|
|
140
|
-
(`security.tenancy.resolve` is unchanged); `verifyMembership` for actions is
|
|
141
|
-
unchanged and receives the hook-resolved `organizationId` — in actions its
|
|
142
|
-
live result supersedes the hook's role/org, exactly as it supersedes claims.
|
|
143
|
-
|
|
144
|
-
Cost: one DB read per authenticated call (claims are free); actions with
|
|
145
|
-
`verifyMembership` configured also pay the live check. When debugging, note
|
|
146
|
-
that a misconfigured hook surfaces as blanket `FORBIDDEN` — errors are
|
|
147
|
-
swallowed fail-closed, never rethrown.
|
|
148
|
-
|
|
149
|
-
## Mutations are structurally trigger-wrapped
|
|
150
|
-
|
|
151
|
-
When `triggers` (or `wrapDB`) is configured, every `authMutation`,
|
|
152
|
-
`roleMutation`, and `systemMutation` runs its writes through
|
|
153
|
-
`triggers.wrapDB(ctx)`. Trigger enforcement (timestamps, append-only,
|
|
154
|
-
no-delete, denormalization) is therefore **structural** — a handler cannot
|
|
155
|
-
forget it, because it never sees an unwrapped `ctx.db`. This is the reason raw
|
|
156
|
-
`mutation` must not be used outside `functions.ts`.
|
|
157
|
-
|
|
158
|
-
## `include()` — every read is bounded
|
|
159
|
-
|
|
160
|
-
`include` wraps a table query in a fluent selector that (a) picks the first
|
|
161
|
-
matching indexed query and (b) refuses unbounded reads. `execute(limit)`
|
|
162
|
-
rejects limits outside `1..maxRows` (default 100, configurable via
|
|
163
|
-
`maxIncludedQueryRows`).
|
|
164
|
-
|
|
165
|
-
```ts
|
|
166
|
-
export const list = authQuery({
|
|
167
|
-
args: { ownerId: zid('users').optional(), limit: z.number() },
|
|
168
|
-
returns: z.array(documents.publicDto),
|
|
169
|
-
handler: (ctx, args) =>
|
|
170
|
-
ctx.include(ctx.db.query('documents'))
|
|
171
|
-
.when(args.ownerId, (q, ownerId) =>
|
|
172
|
-
q.withIndex('by_owner', (ix) => ix.eq('ownerId', ownerId)))
|
|
173
|
-
.otherwise((q) => q.withIndex('by_owner'))
|
|
174
|
-
.execute(args.limit, (rows) => rows.map(documents.toPublicDto)),
|
|
175
|
-
})
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
Selector methods:
|
|
179
|
-
|
|
180
|
-
- `.when(value, select)` — if `value` is non-null and nothing selected yet,
|
|
181
|
-
use `select(query, value)`.
|
|
182
|
-
- `.matching(indexName, range?, shouldMatch?)` — index selection with an
|
|
183
|
-
optional guard boolean.
|
|
184
|
-
- `.otherwise(select)` — fallback, returns the raw `Query`.
|
|
185
|
-
- `.resolve()` — selected query or full table scan (escape hatch).
|
|
186
|
-
- `.execute(limit, transform?)` — bounded `take` + optional projection. This
|
|
187
|
-
is the normal terminal.
|
|
188
|
-
|
|
189
|
-
## Row-level security and tenancy (optional)
|
|
190
|
-
|
|
191
|
-
The `security` config adds structural RLS to every `auth*`/`role*`/`admin*`
|
|
192
|
-
constructor — role-level rules (`security.rules`, standalone), tenant
|
|
193
|
-
isolation from a table registry (`security.tenancy`, opt-in), or both
|
|
194
|
-
AND-composed. Queries get a wrapped reader; mutations wrap triggers first,
|
|
195
|
-
then the RLS writer; `system*` stays unwrapped. With tenancy configured, ctx
|
|
196
|
-
gains `ctx.tenant` (actions re-derive it from live verification). Full
|
|
197
|
-
treatment: `tenancy.md`.
|
|
198
|
-
|
|
199
|
-
## Errors
|
|
200
|
-
|
|
201
|
-
All rejections go through the injected `ErrorFactory` (default: throws
|
|
202
|
-
`KitError` with codes `UNAUTHENTICATED`, `FORBIDDEN`,
|
|
203
|
-
`INVALID_REQUEST_BOUNDARY`). Hosts with their own error taxonomy adapt it:
|
|
204
|
-
|
|
205
|
-
```ts
|
|
206
|
-
errors: { throw: (input) => { throw App.errors.from(input) } }
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
## Rules of thumb
|
|
210
|
-
|
|
211
|
-
1. Build constructors once in `convex/functions.ts`; import them everywhere
|
|
212
|
-
else. Raw `_generated/server` builders appear nowhere else.
|
|
213
|
-
2. Public functions: `auth*`/`role*`/`admin*`. Scheduled/internal work:
|
|
214
|
-
`system*`. Never expose an unauthenticated public function unless it is
|
|
215
|
-
deliberately anonymous (and then document why).
|
|
216
|
-
3. Reads go through `ctx.include(...)` with an explicit limit.
|
|
217
|
-
4. Use `ctx.actor` as the actor reference in audits and approvals — never
|
|
218
|
-
re-derive identity in a handler.
|