cvx-kit 0.0.1 → 0.0.3

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.
@@ -0,0 +1,173 @@
1
+ # `cvx-kit/zod-table` — one zod shape per entity, masked into every boundary
2
+
3
+ The core idea: an entity's shape is declared **once** as a zod object, and every
4
+ boundary that shape crosses — storage, insert, update, command input, public
5
+ DTO, LLM tool input — is a *derived mask* of that single declaration. There is
6
+ no second source of truth to drift.
7
+
8
+ ```ts
9
+ import { z } from 'zod'
10
+ import { zodTable } from 'cvx-kit/zod-table'
11
+
12
+ export const documents = zodTable(
13
+ 'documents',
14
+ (id) => ({
15
+ title: z.string(),
16
+ ownerId: id('users'), // `id` is convex-helpers' zid — typed Id<'users'>
17
+ secretNote: z.string(),
18
+ reviewState: z.enum(['draft', 'published']),
19
+ }),
20
+ {
21
+ serverFields: ['reviewState'], // excluded from inserts — server assigns it
22
+ commandFields: ['title'], // the ONLY fields a command may carry
23
+ publicFields: ['title', 'ownerId'], // the DTO allowlist — everything else is redacted
24
+ },
25
+ )
26
+ ```
27
+
28
+ ## What you get back
29
+
30
+ | Property | What it is | Where you use it |
31
+ |---|---|---|
32
+ | `table` | `defineTable(...)` from the storage shape | `convex/schema.ts` |
33
+ | `storage` | zod object of the raw row (no `_id`/`_creationTime`) | validating rows |
34
+ | `schema` | `storage` + `_id` + `_creationTime` | full-document validation |
35
+ | `insertSchema` | storage minus timestamps minus `serverFields` | mutation args |
36
+ | `updateSchema` | `insertSchema.partial()` | patch mutation args |
37
+ | `commandInput` | strict pick of `commandFields` | command protocol inputs |
38
+ | `publicDto` | strict pick of `publicFields` | query return validators |
39
+ | `toPublicDto(row)` | projects **and re-parses** a row into the DTO | query handlers |
40
+ | `insert(omit?)` / `update(omit?)` | schema with per-callsite field omission | specialized mutations |
41
+ | `tools.insert` / `tools.update` / `tools.id` | ready-made LLM tool input schemas | agent tool definitions |
42
+ | `tableName` | the literal table name | indexes, triggers registration |
43
+
44
+ ## Redaction is runtime, not just types
45
+
46
+ `toPublicDto` does not merely `pick` at the type level — it re-parses the
47
+ projected object through the strict `publicDto` schema. A field that leaks into
48
+ the projection throws instead of shipping to the client. Use it on **every**
49
+ row a public query returns:
50
+
51
+ ```ts
52
+ export const get = authQuery({
53
+ args: { id: zid('documents') },
54
+ returns: documents.publicDto,
55
+ handler: async (ctx, { id }) => {
56
+ const row = await ctx.db.get(id)
57
+ if (!row) throw new KitError({ code: 'NOT_FOUND' })
58
+ return documents.toPublicDto(row) // secretNote can never leak
59
+ },
60
+ })
61
+ ```
62
+
63
+ ## Opinionated timestamps
64
+
65
+ Every `zodTable` bakes in three optional, **server-owned** fields:
66
+
67
+ - `createdAt` — set once on insert
68
+ - `updatedAt` — moved on every update
69
+ - `archivedAt` — soft-delete marker, under application control
70
+
71
+ They are always excluded from `insertSchema`, `updateSchema`, and
72
+ `commandInput` — a client or command can never write them. They are *not* in
73
+ `publicFields` by default; list them explicitly when a DTO needs them
74
+ (`publicFields: ['title', 'createdAt']`).
75
+
76
+ They maintain themselves only if you register the `timestamps` trigger once
77
+ per table (see `triggers.md`). Without the trigger they stay `undefined`.
78
+
79
+ Prefer `createdAt` over `_creationTime` in DTOs so the whole lifecycle
80
+ vocabulary is yours; `_creationTime` remains available for index ordering.
81
+
82
+ ## Field masks — who sees what
83
+
84
+ Think of the options as three independent allowlists over the same shape:
85
+
86
+ - `serverFields` — *the server assigns these.* Excluded from `insertSchema`
87
+ and `updateSchema`. Example: `reviewState`, denormalized counters.
88
+ - `commandFields` — *a command may say these.* The command protocol's input
89
+ is exactly this pick, strict. Everything else must be derived by the handler.
90
+ - `publicFields` — *a client may see these.* The DTO is exactly this pick;
91
+ may include timestamp fields.
92
+
93
+ All derived objects are `.strict()` — unknown keys are rejected, in both
94
+ directions.
95
+
96
+ ## `tenantTable` — tenant-owned rows
97
+
98
+ `tenantTable` is `zodTable` plus a server-owned `tenant` string field —
99
+ injected exactly like timestamps: present in storage, excluded from
100
+ `insertSchema`/`updateSchema`/`commandInput`, in `publicDto` only by explicit
101
+ allowlist. Handlers stamp it from `ctx.tenant`; row-level security matches on
102
+ it; the `tenantOwnership` trigger forbids reassigning it. See `tenancy.md`.
103
+
104
+ ```ts
105
+ export const <entities> = tenantTable('<entities>', (id) => ({ ... }), {
106
+ commandFields: [...], publicFields: [...],
107
+ })
108
+ ```
109
+
110
+ ## `createModule` — module table maps
111
+
112
+ Merges per-module table maps into the application schema, throwing when two
113
+ modules declare the same table — the combinator for `domain/table.ts`:
114
+
115
+ ```ts
116
+ export default defineSchema(createModule(<moduleA>Tables, <moduleB>Tables))
117
+ ```
118
+
119
+ ## `zodVariantTable` — discriminated unions
120
+
121
+ For tables whose rows are a discriminated union (events, polymorphic
122
+ resources), `zodTable`'s object-shape model doesn't apply. `zodVariantTable`
123
+ keeps the same "one owner per table" convention while accepting any zod type
124
+ as storage:
125
+
126
+ ```ts
127
+ const events = zodVariantTable('events', z.discriminatedUnion('kind', [
128
+ z.object({ kind: z.literal('created'), by: zid('users') }),
129
+ z.object({ kind: z.literal('archived'), reason: z.string() }),
130
+ ]))
131
+ // events.table → defineTable, events.insertSchema → the union itself
132
+ ```
133
+
134
+ Note: variant tables do not get the timestamp fields or boundary masks — the
135
+ union is the boundary.
136
+
137
+ ## `jsonSafeZid` — Ids in LLM tool schemas
138
+
139
+ JSON-schema generators turn `zid('users')` into an opaque custom type that
140
+ confuses LLM tool calling. `jsonSafeZid('users')` presents as a plain
141
+ described string in the generated JSON schema while keeping `Id<'users'>` at
142
+ the type level:
143
+
144
+ ```ts
145
+ const assignInput = z.object({
146
+ documentId: jsonSafeZid('documents'), // LLM sees: string, "Convex document id for table \"documents\""
147
+ })
148
+ ```
149
+
150
+ ## Wiring into `schema.ts`
151
+
152
+ ```ts
153
+ // convex/schema.ts
154
+ import { defineSchema } from 'convex/server'
155
+ import { documents } from './tables/documents'
156
+
157
+ export default defineSchema({
158
+ documents: documents.table
159
+ .index('by_owner', ['ownerId'])
160
+ .index('by_owner_archived', ['ownerId', 'archivedAt']),
161
+ })
162
+ ```
163
+
164
+ Indexes are chained on `.table` exactly like a hand-written `defineTable`.
165
+
166
+ ## Rules of thumb
167
+
168
+ 1. One `zodTable` per entity, in its own module, exported by name.
169
+ 2. Never define an inline `defineTable` or a second zod object for the same
170
+ entity — masks only.
171
+ 3. Every public query returns `publicDto`-shaped data via `toPublicDto`.
172
+ 4. Every command input is `commandInput` (or a documented composition of it).
173
+ 5. Timestamps are trigger-maintained; handlers never write them.
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "cvx-kit",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Reusable Convex application kit: foundation kernels, approvals component, zod table boundaries, WorkOS-backed auth function constructors, and audited command protocol.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
8
8
  "files": [
9
9
  "dist",
10
- "src"
10
+ "src",
11
+ "!src/docs",
12
+ "docs"
11
13
  ],
12
14
  "exports": {
13
15
  "./package.json": "./package.json",
@@ -23,6 +25,10 @@
23
25
  "types": "./dist/auth.d.mts",
24
26
  "import": "./dist/auth.mjs"
25
27
  },
28
+ "./tenancy": {
29
+ "types": "./dist/tenancy.d.mts",
30
+ "import": "./dist/tenancy.mjs"
31
+ },
26
32
  "./triggers": {
27
33
  "types": "./dist/triggers.d.mts",
28
34
  "import": "./dist/triggers.mjs"
@@ -67,7 +73,8 @@
67
73
  "build": "vp pack",
68
74
  "typecheck": "tsc --noEmit",
69
75
  "test": "vp test run",
70
- "prepublishOnly": "bun run typecheck && bun run test && bun run build",
76
+ "docs:sync": "rm -f docs/*.md && cp src/docs/*.md docs/",
77
+ "prepublishOnly": "bun run typecheck && bun run test && bun run build && bun run docs:sync",
71
78
  "build:codegen": "convex codegen --component-dir ./src/components/foundation && convex codegen --component-dir ./src/components/approvals && vp pack",
72
79
  "release": "npm version patch && git push --follow-tags",
73
80
  "release:minor": "npm version minor && git push --follow-tags"
package/src/auth.ts CHANGED
@@ -22,7 +22,16 @@ import type {
22
22
  UserIdentity,
23
23
  } from 'convex/server'
24
24
  import type { Triggers } from 'convex-helpers/server/triggers'
25
+ import type { TableNamesInDataModel } from 'convex/server'
25
26
  import { defaultErrors, type ErrorFactory } from './errors'
27
+ import {
28
+ composeRules,
29
+ createTenantRules,
30
+ wrapDatabaseReader,
31
+ wrapDatabaseWriter,
32
+ type RLSConfig,
33
+ type Rules,
34
+ } from './tenancy'
26
35
 
27
36
  /** The default vocabulary; every app can supply its own via the Role generic. */
28
37
  export type DefaultRole = 'reader' | 'writer' | 'admin'
@@ -39,8 +48,43 @@ export type AuthBundle<Role extends string> = Readonly<{
39
48
  org: Readonly<{ organizationId: string; role: Role }>
40
49
  role: Role
41
50
  actor: Actor<Role>
51
+ /** The resolved tenant — security.tenancy.resolve, or the organization id. */
52
+ tenant: string
42
53
  }>
43
54
 
55
+ /**
56
+ * Row-level security configuration. Tenant isolation and role-level rules
57
+ * compose (AND) onto the same wrapped database: queries get a wrapped
58
+ * reader, mutations get triggers first, then a wrapped writer.
59
+ */
60
+ export type SecurityConfig<
61
+ DataModel extends GenericDataModel,
62
+ Role extends string,
63
+ > = {
64
+ /**
65
+ * Tenant isolation. `tables` is THE registry: every listed table gets
66
+ * read/insert/modify gated on row.tenant === ctx.tenant. Pair with
67
+ * tenantTable (schema) and tenantOwnership (triggers) on the same list.
68
+ */
69
+ tenancy?: {
70
+ tables: readonly TableNamesInDataModel<DataModel>[]
71
+ /** Maps the auth bundle to the tenant; defaults to org.organizationId. */
72
+ resolve?: (bundle: Omit<AuthBundle<Role>, 'tenant'>) => string
73
+ }
74
+ /**
75
+ * Role-level (or any policy) rules derived from the authenticated bundle,
76
+ * composed AND-wise with tenant isolation. E.g. gate `modify` on a table
77
+ * to admin roles.
78
+ */
79
+ rules?: (bundle: AuthBundle<Role>) => Rules<unknown, DataModel>
80
+ /**
81
+ * Policy for tables with no rule. Defaults to 'deny' when tenancy is
82
+ * configured (unlisted tables are unreachable — fail closed), 'allow'
83
+ * otherwise.
84
+ */
85
+ defaultPolicy?: RLSConfig['defaultPolicy']
86
+ }
87
+
44
88
  type AnyAuthContext<DataModel extends GenericDataModel> =
45
89
  | GenericQueryCtx<DataModel>
46
90
  | GenericMutationCtx<DataModel>
@@ -90,6 +134,12 @@ export type AuthFunctionsConfig<
90
134
  ) => GenericMutationCtx<DataModel>
91
135
  /** Upper bound for include() reads. */
92
136
  maxIncludedQueryRows?: number
137
+ /**
138
+ * Row-level security: tenant isolation (from a table registry) and
139
+ * role-level rules, applied structurally inside every auth*, role*, and
140
+ * admin* constructor. system* constructors stay unwrapped (trusted).
141
+ */
142
+ security?: SecurityConfig<DataModel, Role>
93
143
  }
94
144
 
95
145
  /** WorkOS-style default: `member` writes; reader/writer/admin pass through. */
@@ -223,15 +273,68 @@ export function createAuthFunctions<
223
273
  : undefined
224
274
  const role = config.mapRole(organizationRole)
225
275
  if (!role) return errors.throw({ code: 'FORBIDDEN' })
226
- return Object.freeze({
276
+ const base = {
227
277
  identity,
228
278
  user: Object.freeze({ id: user.id }),
229
279
  org: Object.freeze({ organizationId, role }),
230
280
  role,
231
281
  actor: Object.freeze({ userId: user.id, organizationId, role }),
282
+ }
283
+ return Object.freeze({
284
+ ...base,
285
+ tenant: config.security?.tenancy?.resolve?.(base) ?? organizationId,
232
286
  })
233
287
  }
234
288
 
289
+ const securityConfig: RLSConfig | undefined = config.security
290
+ ? {
291
+ defaultPolicy:
292
+ config.security.defaultPolicy ??
293
+ (config.security.tenancy ? 'deny' : 'allow'),
294
+ }
295
+ : undefined
296
+
297
+ function securityRules(
298
+ bundle: AuthBundle<Role>,
299
+ ): Rules<unknown, DataModel> | undefined {
300
+ const security = config.security
301
+ if (!security) return undefined
302
+ const sets: Rules<unknown, DataModel>[] = []
303
+ if (security.tenancy) {
304
+ sets.push(
305
+ createTenantRules<DataModel>(bundle.tenant, security.tenancy.tables),
306
+ )
307
+ }
308
+ if (security.rules) sets.push(security.rules(bundle))
309
+ return sets.length === 1 ? sets[0] : composeRules(...sets)
310
+ }
311
+
312
+ function secureReader(
313
+ ctx: GenericQueryCtx<DataModel>,
314
+ bundle: AuthBundle<Role>,
315
+ ): GenericQueryCtx<DataModel> {
316
+ const rules = securityRules(bundle)
317
+ if (!rules) return ctx
318
+ return {
319
+ ...ctx,
320
+ db: wrapDatabaseReader({}, ctx.db, rules as never, securityConfig),
321
+ }
322
+ }
323
+
324
+ function secureWriter(
325
+ ctx: GenericMutationCtx<DataModel>,
326
+ bundle: AuthBundle<Role>,
327
+ ): GenericMutationCtx<DataModel> {
328
+ const rules = securityRules(bundle)
329
+ if (!rules) return ctx
330
+ // Triggers wrap first (ctx already trigger-wrapped by the caller),
331
+ // then row-level security wraps the triggered db — order matters.
332
+ return {
333
+ ...ctx,
334
+ db: wrapDatabaseWriter({}, ctx.db, rules as never, securityConfig),
335
+ }
336
+ }
337
+
235
338
  async function verifiedMembership(userId: string, organizationId: string) {
236
339
  if (!config.verifyMembership) return null
237
340
  try {
@@ -253,15 +356,17 @@ export function createAuthFunctions<
253
356
  }
254
357
 
255
358
  async function queryInput(ctx: GenericQueryCtx<DataModel>) {
359
+ const bundle = await authenticatedUser(ctx)
256
360
  return {
257
- ctx: { ...ctx, include, ...(await authenticatedUser(ctx)) },
361
+ ctx: { ...secureReader(ctx, bundle), include, ...bundle },
258
362
  args: {},
259
363
  }
260
364
  }
261
365
 
262
366
  async function mutationInput(ctx: GenericMutationCtx<DataModel>) {
367
+ const bundle = await authenticatedUser(ctx)
263
368
  return {
264
- ctx: { ...wrapDB(ctx), include, ...(await authenticatedUser(ctx)) },
369
+ ctx: { ...secureWriter(wrapDB(ctx), bundle), include, ...bundle },
265
370
  args: {},
266
371
  }
267
372
  }
@@ -275,20 +380,28 @@ export function createAuthFunctions<
275
380
  if (!membership) return { ctx: { ...ctx, ...authenticated }, args: {} }
276
381
  const role = config.mapRole(membership.roleSlug)
277
382
  if (!role) return errors.throw({ code: 'FORBIDDEN' })
383
+ const verified = {
384
+ identity: authenticated.identity,
385
+ user: authenticated.user,
386
+ org: Object.freeze({
387
+ organizationId: membership.organizationId,
388
+ role,
389
+ }),
390
+ role,
391
+ actor: Object.freeze({
392
+ userId: authenticated.user.id,
393
+ organizationId: membership.organizationId,
394
+ role,
395
+ }),
396
+ }
278
397
  return {
279
398
  ctx: {
280
399
  ...ctx,
281
- ...authenticated,
282
- org: Object.freeze({
283
- organizationId: membership.organizationId,
284
- role,
285
- }),
286
- role,
287
- actor: Object.freeze({
288
- userId: authenticated.user.id,
289
- organizationId: membership.organizationId,
290
- role,
291
- }),
400
+ ...verified,
401
+ // Tenant re-derives from the live-verified organization.
402
+ tenant:
403
+ config.security?.tenancy?.resolve?.(verified) ??
404
+ membership.organizationId,
292
405
  },
293
406
  args: {},
294
407
  }
@@ -35,12 +35,41 @@ export type AuditedOperation = Readonly<{
35
35
  command: Readonly<{ parse: (value: unknown) => unknown }>
36
36
  result: Readonly<{ parse: (value: unknown) => unknown }>
37
37
  classification: string
38
+ /**
39
+ * Permission slug required to execute this operation. Checked through the
40
+ * Foundation's injected checkPermission BEFORE guards and the handler;
41
+ * declaring a permission without injecting a checker fails closed.
42
+ */
43
+ permission?: string
44
+ /**
45
+ * Per-operation precondition, after the permission check and the registry
46
+ * default guard, before the handler. Throw to deny — nothing has run yet.
47
+ */
48
+ guard?: (context: never, command: never) => MaybePromise<void>
49
+ /**
50
+ * Allowlist of aggregate types this operation's audit may reference. When
51
+ * declared, an audit entry whose aggregate.type is not listed throws —
52
+ * the audit vocabulary is enforced, not advisory.
53
+ */
54
+ aggregates?: readonly string[]
38
55
  audit: (
39
56
  resolution: Readonly<{ command: never; result: never }>,
40
57
  context: never,
41
58
  ) => MaybePromise<Omit<AuditEntryInput, 'classification'> | null>
42
59
  }>
43
60
 
61
+ /** Registry-wide defaults applied to every operation of one Command. */
62
+ export type CommandDefaults = Readonly<{
63
+ /** Runs before every operation's own guard. Throw to deny. */
64
+ guard?: (context: never) => MaybePromise<void>
65
+ }>
66
+
67
+ /** Injected permission policy: throw to deny. Host semantics, kit ordering. */
68
+ export type PermissionChecker = (
69
+ context: never,
70
+ input: Readonly<{ permission: string; operation: string }>,
71
+ ) => MaybePromise<void>
72
+
44
73
  export type AuditedRegistry = Readonly<Record<string, AuditedOperation>>
45
74
 
46
75
  export type FoundationOptions = Readonly<{
@@ -52,6 +81,12 @@ export type FoundationOptions = Readonly<{
52
81
  */
53
82
  writeAudit?: AuditWriter
54
83
  }>
84
+ /**
85
+ * Permission policy for operations that declare a `permission`. Runs
86
+ * before guards and the handler; throw to deny. An operation with a
87
+ * permission but no injected checker fails closed.
88
+ */
89
+ checkPermission?: PermissionChecker
55
90
  }>
56
91
 
57
92
  type FoundationComponentApi = Readonly<{
@@ -68,6 +103,13 @@ class BoundCommand<Context, const Operations extends AuditedRegistry> {
68
103
  readonly #kernel: CommandKernelClass<Context, Operations>
69
104
  readonly #observability: Observability
70
105
  readonly #writeAudit: AuditWriter
106
+ readonly #checkPermission: PermissionChecker | undefined
107
+ readonly #defaults: CommandDefaults
108
+
109
+ /** Declared aggregate types per operation — introspectable for tests. */
110
+ readonly aggregates: Readonly<{
111
+ [Key in OperationKey<Operations>]: Operations[Key]['aggregates']
112
+ }>
71
113
 
72
114
  static operation<const Definition extends AuditedOperation>(
73
115
  definition: Definition,
@@ -77,10 +119,25 @@ class BoundCommand<Context, const Operations extends AuditedRegistry> {
77
119
 
78
120
  constructor(
79
121
  operations: Operations,
80
- deps: { observability: Observability; writeAudit: AuditWriter },
122
+ deps: {
123
+ observability: Observability
124
+ writeAudit: AuditWriter
125
+ checkPermission?: PermissionChecker
126
+ defaults?: CommandDefaults
127
+ },
81
128
  ) {
82
129
  this.#observability = deps.observability
83
130
  this.#writeAudit = deps.writeAudit
131
+ this.#checkPermission = deps.checkPermission
132
+ this.#defaults = deps.defaults ?? {}
133
+ this.aggregates = Object.freeze(
134
+ Object.fromEntries(
135
+ Object.entries(operations).map(([operation, definition]) => [
136
+ operation,
137
+ definition.aggregates,
138
+ ]),
139
+ ),
140
+ ) as this['aggregates']
84
141
  this.#kernel = new CommandKernelClass<Context, Operations>({
85
142
  operations,
86
143
  execute: (execution) => this.#execute(execution),
@@ -106,12 +163,35 @@ class BoundCommand<Context, const Operations extends AuditedRegistry> {
106
163
  classification: execution.definition.classification,
107
164
  },
108
165
  async () => {
166
+ // Guards run before the handler: permission → default → operation.
167
+ const permission = execution.definition.permission
168
+ if (permission !== undefined) {
169
+ if (!this.#checkPermission) {
170
+ throw new CommandPermissionError(execution.operation)
171
+ }
172
+ await this.#checkPermission(execution.context as never, {
173
+ permission,
174
+ operation: execution.operation,
175
+ })
176
+ }
177
+ await this.#defaults.guard?.(execution.context as never)
178
+ await execution.definition.guard?.(
179
+ execution.context as never,
180
+ execution.command as never,
181
+ )
109
182
  const result = await execution.run()
110
183
  const audit = await execution.definition.audit(
111
184
  { command: execution.command, result } as never,
112
185
  execution.context as never,
113
186
  )
114
187
  if (audit) {
188
+ const allowed = execution.definition.aggregates
189
+ if (allowed && !allowed.includes(audit.aggregate.type)) {
190
+ throw new CommandAggregateError(
191
+ execution.operation,
192
+ audit.aggregate.type,
193
+ )
194
+ }
115
195
  await this.#writeAudit(execution.context as never, {
116
196
  ...audit,
117
197
  classification: execution.definition.classification,
@@ -123,6 +203,30 @@ class BoundCommand<Context, const Operations extends AuditedRegistry> {
123
203
  }
124
204
  }
125
205
 
206
+ /** The audit referenced an aggregate type outside the operation's allowlist. */
207
+ class CommandAggregateError extends Error {
208
+ readonly code = 'COMMAND_AGGREGATE_NOT_DECLARED'
209
+ readonly name = 'CommandAggregateError'
210
+
211
+ constructor(operation: string, aggregateType: string) {
212
+ super(
213
+ `Operation "${operation}" audited aggregate type "${aggregateType}" outside its declared aggregates`,
214
+ )
215
+ }
216
+ }
217
+
218
+ /** Fails closed: an operation declared a permission but no checker exists. */
219
+ class CommandPermissionError extends Error {
220
+ readonly code = 'COMMAND_PERMISSION_NOT_CONFIGURED'
221
+ readonly name = 'CommandPermissionError'
222
+
223
+ constructor(operation: string) {
224
+ super(
225
+ `Operation "${operation}" declares a permission but the Foundation has no checkPermission`,
226
+ )
227
+ }
228
+ }
229
+
126
230
  export type ApplicationCommand<
127
231
  Context,
128
232
  Operations extends AuditedRegistry,
@@ -131,6 +235,7 @@ export type ApplicationCommand<
131
235
  type CommandConstructor = {
132
236
  new <Context, const Operations extends AuditedRegistry>(
133
237
  operations: Operations,
238
+ defaults?: CommandDefaults,
134
239
  ): BoundCommand<Context, Operations>
135
240
  operation: (typeof BoundCommand)['operation']
136
241
  }
@@ -153,12 +258,18 @@ export class Foundation<
153
258
  const observability = new Observability(options.observability)
154
259
  this.observability = observability
155
260
  const writeAudit = options.observability.writeAudit ?? (() => undefined)
261
+ const checkPermission = options.checkPermission
156
262
  this.Command = class <
157
263
  Context,
158
264
  const Operations extends AuditedRegistry,
159
265
  > extends BoundCommand<Context, Operations> {
160
- constructor(operations: Operations) {
161
- super(operations, { observability, writeAudit })
266
+ constructor(operations: Operations, defaults?: CommandDefaults) {
267
+ super(operations, {
268
+ observability,
269
+ writeAudit,
270
+ checkPermission,
271
+ defaults,
272
+ })
162
273
  }
163
274
  }
164
275
  }
package/src/index.ts CHANGED
@@ -4,12 +4,23 @@ export {
4
4
  type ErrorFactory,
5
5
  } from './errors'
6
6
  export {
7
+ createModule,
7
8
  jsonSafeZid,
9
+ tenantTable,
8
10
  TIMESTAMP_FIELDS,
9
11
  zodTable,
10
12
  zodVariantTable,
11
13
  type TableBoundaryOptions,
12
14
  } from './zod-table'
15
+ export {
16
+ assertTenantOwned,
17
+ composeRules,
18
+ createTenantRules,
19
+ requireTenantReference,
20
+ TENANT_FIELD,
21
+ type RLSConfig,
22
+ type Rules,
23
+ } from './tenancy'
13
24
  export {
14
25
  createAuthFunctions,
15
26
  createInclude,
@@ -20,9 +31,11 @@ export {
20
31
  type DefaultRole,
21
32
  type Include,
22
33
  type IncludedQuery,
34
+ type SecurityConfig,
23
35
  } from './auth'
24
36
  export {
25
37
  appendOnly,
38
+ tenantOwnership,
26
39
  createTriggers,
27
40
  noDelete,
28
41
  timestamps,
@@ -37,5 +50,7 @@ export {
37
50
  type AuditedRegistry,
38
51
  type AuditEntryInput,
39
52
  type AuditWriter,
53
+ type CommandDefaults,
40
54
  type FoundationOptions,
55
+ type PermissionChecker,
41
56
  } from './components/foundation/client'