cvx-kit 0.1.15 → 0.1.16

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 (45) hide show
  1. package/README.md +59 -38
  2. package/dist/relations.mjs +1 -1
  3. package/dist/relations.mjs.map +1 -1
  4. package/dist/zod-table.d.mts +59 -2
  5. package/dist/zod-table.mjs +1 -1
  6. package/dist/zod-table.mjs.map +1 -1
  7. package/package.json +10 -3
  8. package/AGENTS.md +0 -47
  9. package/CHANGELOG.md +0 -337
  10. package/ROADMAP.md +0 -126
  11. package/docs/agent-tools.md +0 -41
  12. package/docs/approvals.md +0 -201
  13. package/docs/architecture.md +0 -124
  14. package/docs/auth.md +0 -443
  15. package/docs/command-context.md +0 -150
  16. package/docs/commands-legacy.md +0 -513
  17. package/docs/commands.md +0 -173
  18. package/docs/conventions.md +0 -345
  19. package/docs/crud.md +0 -66
  20. package/docs/domain-contracts.md +0 -148
  21. package/docs/domain-operations.md +0 -615
  22. package/docs/effect.md +0 -403
  23. package/docs/llms.md +0 -324
  24. package/docs/maintainability.md +0 -124
  25. package/docs/migration.md +0 -351
  26. package/docs/oxlint.md +0 -260
  27. package/docs/querying.md +0 -329
  28. package/docs/tenancy.md +0 -176
  29. package/docs/triggers.md +0 -77
  30. package/docs/upgrading.md +0 -287
  31. package/docs/webhooks.md +0 -58
  32. package/docs/zod-table.md +0 -278
  33. package/scripts/convex-config-bridge.mjs +0 -44
  34. package/scripts/convex-smoke-env.mjs +0 -33
  35. package/scripts/extract-changelog.mjs +0 -29
  36. package/scripts/owned-process.mjs +0 -140
  37. package/scripts/owned-process.test.mjs +0 -107
  38. package/scripts/smoke-packed-components.mjs +0 -341
  39. package/scripts/smoke-packed-effect.mjs +0 -439
  40. package/scripts/smoke-packed-querying.mjs +0 -298
  41. package/scripts/test-packed-auth.mjs +0 -72
  42. package/scripts/test-packed-effect.mjs +0 -482
  43. package/scripts/test-packed-ids.mjs +0 -150
  44. package/scripts/test-packed-oxlint.mjs +0 -167
  45. package/scripts/test-packed-querying.mjs +0 -185
@@ -161,13 +161,70 @@ export declare function paginated<Dto extends z.ZodType>(dto: Dto): {
161
161
  }>>>;
162
162
  }, z.core.$strip>;
163
163
  };
164
- /** Preserves discriminated-union storage while retaining the same table owner. */
164
+ type VariantSchema = z.ZodDiscriminatedUnion<readonly StrictObject<FieldSchemas>[], string>;
165
+ type VariantDiscriminator<Schema extends VariantSchema> = Schema['_zod']['def']['discriminator'];
166
+ type VariantTags<Schema extends VariantSchema> = Extract<z.output<Schema>[VariantDiscriminator<Schema>], string>;
167
+ type VariantBranch<Schema extends VariantSchema, Tag extends string> = Extract<Schema['options'][number], z.ZodObject<{ [Key in VariantDiscriminator<Schema>]: z.ZodLiteral<Tag>; }, z.core.$strict>>;
168
+ type VariantKeys<Schema extends VariantSchema, Tag extends string> = FieldKey<VariantBranch<Schema, Tag>['shape']>;
169
+ /** Boundary mode supports strict object branches with one string literal tag each. */
170
+ export type VariantBoundaryOptions<Schema extends VariantSchema> = {
171
+ discriminator: VariantDiscriminator<Schema>;
172
+ timestamps?: boolean;
173
+ variants: { [Tag in VariantTags<Schema>]: {
174
+ serverFields?: readonly Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>[];
175
+ commandFields?: readonly Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>[];
176
+ publicFields?: readonly (Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>> | keyof TimestampFields)[];
177
+ }; };
178
+ };
179
+ type VariantMaskKeys<Config, Mask extends string> = Config extends { [Key in Mask]?: readonly (infer Field)[]; } ? Extract<Field, string> : never;
180
+ type CheckedVariantOptions<Schema extends VariantSchema, Options extends VariantBoundaryOptions<Schema>> = {
181
+ variants: { [Tag in keyof Options['variants']]: Tag extends VariantTags<Schema> ? {
182
+ commandFields?: readonly Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema> | VariantMaskKeys<Options['variants'][Tag], 'serverFields'>>[];
183
+ publicFields?: readonly (Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>> | (Options extends {
184
+ timestamps: true;
185
+ } ? keyof TimestampFields : never))[];
186
+ } : never; };
187
+ };
188
+ type VariantMode = 'storage' | 'document' | 'insert' | 'update' | 'command' | 'commandUpdate' | 'public';
189
+ type VariantFields<Table extends string, Branch extends StrictObject<FieldSchemas>, Discriminator extends string, Config, Timestamps extends boolean, Mode extends VariantMode> = Mode extends 'storage' ? Branch['shape'] & (true extends Timestamps ? TimestampFields : {}) : Mode extends 'document' ? VariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'> & SystemFields<Table> : Mode extends 'public' | 'command' ? Pick<VariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'>, Extract<Discriminator | VariantMaskKeys<Config, Mode extends 'public' ? 'publicFields' : 'commandFields'>, keyof VariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'>>> : Mode extends 'insert' ? Omit<Branch['shape'], VariantMaskKeys<Config, 'serverFields'>> : PartialFields<Omit<VariantFields<Table, Branch, Discriminator, Config, Timestamps, Mode extends 'update' ? 'insert' : 'command'>, Discriminator>> & Pick<Branch['shape'], Extract<Discriminator, keyof Branch['shape']>>;
190
+ type DerivedVariantBranches<Table extends string, Schema extends VariantSchema, Options extends VariantBoundaryOptions<Schema>, Mode extends VariantMode, IncludeId extends boolean, Branches extends readonly StrictObject<FieldSchemas>[]> = { [Index in keyof Branches]: Branches[Index] extends StrictObject<FieldSchemas> ? StrictObject<VariantFields<Table, Branches[Index], VariantDiscriminator<Schema>, Options['variants'][Extract<z.output<Branches[Index]>[VariantDiscriminator<Schema>], keyof Options['variants']>], Options extends {
191
+ timestamps: infer Enabled;
192
+ } ? true extends Enabled ? true : false : false, Mode> & (IncludeId extends true ? {
193
+ _id: Zid<Table>;
194
+ } : {})> : never; };
195
+ type DerivedVariant<Table extends string, Schema extends VariantSchema, Options extends VariantBoundaryOptions<Schema>, Mode extends VariantMode, IncludeId extends boolean = false> = IncludeId extends true ? z.ZodDiscriminatedUnion<DerivedVariantBranches<Table, Schema, Options, Mode, true, Schema['options']>, VariantDiscriminator<Schema>> : z.ZodDiscriminatedUnion<DerivedVariantBranches<Table, Schema, Options, Mode, false, Schema['options']>, VariantDiscriminator<Schema>>;
196
+ export type VariantTableBoundary<Table extends string, Schema extends VariantSchema, Options extends VariantBoundaryOptions<Schema>> = {
197
+ tableName: Table;
198
+ storage: DerivedVariant<Table, Schema, Options, 'storage'>;
199
+ schema: DerivedVariant<Table, Schema, Options, 'document'>;
200
+ insertSchema: DerivedVariant<Table, Schema, Options, 'insert'>;
201
+ updateSchema: DerivedVariant<Table, Schema, Options, 'update'>;
202
+ commandInput: DerivedVariant<Table, Schema, Options, 'command'>;
203
+ publicDto: DerivedVariant<Table, Schema, Options, 'public'>;
204
+ table: TableDefinition<ConvexValidatorFromZod<DerivedVariant<Table, Schema, Options, 'storage'>, 'required'>>;
205
+ dto(): DerivedVariant<Table, Schema, Options, 'public'>;
206
+ dto<const IncludeId extends boolean>(includeId: IncludeId): DerivedVariant<Table, Schema, Options, 'public', IncludeId>;
207
+ toPublicDto(row: z.input<DerivedVariant<Table, Schema, Options, 'storage'>>): z.output<DerivedVariant<Table, Schema, Options, 'public'>>;
208
+ toPublicDto<const IncludeId extends boolean>(row: DtoRow<z.input<DerivedVariant<Table, Schema, Options, 'storage'>>, IncludeId>, includeId: IncludeId): z.output<DerivedVariant<Table, Schema, Options, 'public', IncludeId>>;
209
+ tools: {
210
+ id: StrictObject<{
211
+ id: z.ZodType<GenericId<Table>, string>;
212
+ }>;
213
+ insert: z.ZodType<z.output<DerivedVariant<Table, Schema, Options, 'command'>>, z.input<DerivedVariant<Table, Schema, Options, 'command'>>>;
214
+ update: StrictObject<{
215
+ id: z.ZodType<GenericId<Table>, string>;
216
+ data: z.ZodType<z.output<DerivedVariant<Table, Schema, Options, 'commandUpdate'>>, z.input<DerivedVariant<Table, Schema, Options, 'commandUpdate'>>>;
217
+ }>;
218
+ };
219
+ };
220
+ /** Legacy mode preserves arbitrary storage; the third argument opts into derived boundaries. */
165
221
  export declare function zodVariantTable<Table extends string, Schema extends z.ZodType>(tableName: Table, storage: Schema): {
166
222
  tableName: Table;
167
223
  storage: Schema;
168
224
  insertSchema: Schema;
169
- table: TableDefinition<ConvexValidatorFromZod<Schema, "required">, {}, {}, {}>;
225
+ table: TableDefinition<ConvexValidatorFromZod<Schema, 'required'>>;
170
226
  };
227
+ export declare function zodVariantTable<Table extends string, Schema extends VariantSchema, const Options extends VariantBoundaryOptions<Schema>>(tableName: Table, storage: Schema, options: Options & CheckedVariantOptions<Schema, NoInfer<Options>>): VariantTableBoundary<Table, Schema, Options>;
171
228
  /**
172
229
  * A zid that presents as a plain string in generated JSON schemas so
173
230
  * LLM-tool inputs stay primitive, while keeping the Id type at compile time.
@@ -1,2 +1,2 @@
1
- import{zodToConvex as e}from"./zod-convex.mjs";import{scalarPaginationSchema as t}from"./querying/filters.mjs";import{convexToZod as n,convexToZod as r,zid as i,zid as a}from"convex-helpers/server/zod4";import{defineTable as o,paginationOptsValidator as s}from"convex/server";import{z as c}from"zod";const l={createdAt:c.number().optional(),updatedAt:c.number().optional(),archivedAt:c.number().optional()},u=[`createdAt`,`updatedAt`,`archivedAt`];function d(n,r,i={}){let s=r(a);S(s,[`_id`,`_creationTime`,...u]);let d={...s,...l};C(d,i.serverFields??[],i.commandFields??[],i.publicFields??[]);let f=c.object(d).strict(),p=v(f,n),m=[...u,...i.serverFields??[]],h=i.commandFields??[],g=i.publicFields??[],w=c.object(b(d,m)).strict(),T=w.partial(),E=c.object(y(d,h)).strict(),D=c.object(y(d,g)).strict(),O=/* @__PURE__ */ new Set([`_id`,`_creationTime`,`tenant`,`__proto__`,...u]);function k(e){return A(e)}function A(e){let t=e===!0||e===!1?{_id:e}:e,r=t?.extend??{};for(let e of Object.keys(r)){if(O.has(e)||Object.hasOwn(D.shape,e))throw Error(`DTO extension collision for field "${e}" on table "${n}"`);x(r[e])}if(!t?._id&&Object.keys(r).length===0)return D;let i=c.object({...D.shape,...r}).strict();return t?._id?i.extend({_id:p.shape._id}):i}function j(e,t){let n=t===!0||t===!1?{_id:t}:t;A(t);let r=D.parse(Object.fromEntries(Object.keys(D.shape).map(t=>[t,e[t]]))),i=c.object(n?.extend??{}).strict().parse(n?.values??{}),a={...r,...i};return n?._id&&(a._id=p.shape._id.parse(`_id`in e?e._id:void 0)),a}function M(e){return e?w.omit(e):w}function N(e){return e?T.omit(e):T}let P=e(f),F=o(P);function I(n){return t(e(p).fields,n)}return{tableName:n,schema:p,storage:f,insertSchema:w,updateSchema:T,commandInput:E,publicDto:D,dto:k,toPublicDto:j,paginationOpts:I,table:F,insert:M,update:N,tools:{insert:E,update:c.object({data:E.partial(),id:_(n)}).strict(),id:c.object({id:_(n)}).strict()}}}const f={tenant:c.string().min(1)};function p(e,t,n={}){return d(e,e=>{let n=t(e);return S(n,[`tenant`,`_id`,`_creationTime`,...u]),{...n,...f}},{...n,serverFields:[...n.serverFields??[],`tenant`]})}function m(...e){let t={};for(let n of e)for(let[e,r]of Object.entries(n)){if(e in t)throw Error(`Table "${e}" is declared by more than one module`);t[e]=r}return t}function h(e){return{args:{paginationOpts:r(s)},result:c.object({page:c.array(e),isDone:c.boolean(),continueCursor:c.string(),splitCursor:c.string().nullable().optional(),pageStatus:c.enum([`SplitRecommended`,`SplitRequired`]).nullable().optional()})}}function g(t,n){return{tableName:t,storage:n,insertSchema:n,table:o(e(n))}}function _(e){let t=a(e);return c.string().refine(e=>t.safeParse(e).success).describe(`Convex document id for table "${e}"; validated against the table by Convex`)}function v(e,t){return e.extend({_id:a(t),_creationTime:c.number()})}function y(e,t){return Object.fromEntries(t.map(t=>[t,e[t]]))}function b(e,t){let n=new Set(t);return Object.fromEntries(Object.entries(e).filter(([e])=>!n.has(e)))}function x(e,t=/* @__PURE__ */ new Set){if(t.has(e))return;t.add(e);let n=e._zod.def,r=()=>{throw Error(`DTO extensions require supported value-preserving validation schemas`)};`coerce`in n&&n.coerce&&r(),`normalize`in n&&n.normalize&&r();let i=e=>x(e,t);for(let e of n.checks??[])if(e._zod.def.check===`overwrite`&&r(),`normalize`in e._zod.def&&e._zod.def.normalize&&r(),e._zod.def.check===`property`&&i(e._zod.def.schema),e._zod.def.check===`properties`)for(let t of Object.values(e._zod.def.shape))i(t);switch(n.type){case`string`:case`number`:case`boolean`:case`bigint`:case`date`:case`symbol`:case`null`:case`undefined`:case`void`:case`never`:case`literal`:case`enum`:return;case`object`:for(let e of Object.values(n.shape))i(e);n.catchall&&i(n.catchall);return;case`array`:i(n.element);return;case`optional`:case`nullable`:case`nonoptional`:i(n.innerType);return;case`union`:for(let e of n.options)i(e);return;case`intersection`:i(n.left),i(n.right);return;case`tuple`:for(let e of n.items)i(e);n.rest&&i(n.rest);return;case`record`:i(n.keyType),i(n.valueType);return;default:r()}}function S(e,t){for(let n of t)if(Object.hasOwn(e,n))throw Error(`Reserved table field "${n}" cannot be declared`)}function C(e,t,n,r){for(let i of[...t,...n,...r])if(!Object.hasOwn(e,i))throw Error(`Unknown table field "${i}"`);let i=/* @__PURE__ */ new Set([...u,...t]);for(let e of n)if(i.has(e))throw Error(`Server-owned field "${e}" cannot be a command input`)}export{u as TIMESTAMP_FIELDS,n as convexToZod,m as createModule,_ as jsonSafeZid,h as paginated,p as tenantTable,i as zid,d as zodTable,e as zodToConvex,g as zodVariantTable};
1
+ import{zodToConvex as e}from"./zod-convex.mjs";import{scalarPaginationSchema as t}from"./querying/filters.mjs";import{convexToZod as n,convexToZod as r,zid as i,zid as a}from"convex-helpers/server/zod4";import{defineTable as o,paginationOptsValidator as s}from"convex/server";import{z as c}from"zod";const l={createdAt:c.number().optional(),updatedAt:c.number().optional(),archivedAt:c.number().optional()},u=[`createdAt`,`updatedAt`,`archivedAt`];function d(n,r,i={}){let s=r(a);w(s,[`_id`,`_creationTime`,...u]);let d={...s,...l};T(d,i.serverFields??[],i.commandFields??[],i.publicFields??[]);let f=c.object(d).strict(),p=b(f,n),m=[...u,...i.serverFields??[]],h=i.commandFields??[],g=i.publicFields??[],v=c.object(S(d,m)).strict(),y=v.partial(),E=c.object(x(d,h)).strict(),D=c.object(x(d,g)).strict(),O=/* @__PURE__ */ new Set([`_id`,`_creationTime`,`tenant`,`__proto__`,...u]);function k(e){return A(e)}function A(e){let t=e===!0||e===!1?{_id:e}:e,r=t?.extend??{};for(let e of Object.keys(r)){if(O.has(e)||Object.hasOwn(D.shape,e))throw Error(`DTO extension collision for field "${e}" on table "${n}"`);C(r[e])}if(!t?._id&&Object.keys(r).length===0)return D;let i=c.object({...D.shape,...r}).strict();return t?._id?i.extend({_id:p.shape._id}):i}function j(e,t){let n=t===!0||t===!1?{_id:t}:t;A(t);let r=D.parse(Object.fromEntries(Object.keys(D.shape).map(t=>[t,e[t]]))),i=c.object(n?.extend??{}).strict().parse(n?.values??{}),a={...r,...i};return n?._id&&(a._id=p.shape._id.parse(`_id`in e?e._id:void 0)),a}function M(e){return e?v.omit(e):v}function N(e){return e?y.omit(e):y}let P=e(f),F=o(P);function I(n){return t(e(p).fields,n)}return{tableName:n,schema:p,storage:f,insertSchema:v,updateSchema:y,commandInput:E,publicDto:D,dto:k,toPublicDto:j,paginationOpts:I,table:F,insert:M,update:N,tools:{insert:E,update:c.object({data:E.partial(),id:_(n)}).strict(),id:c.object({id:_(n)}).strict()}}}const f={tenant:c.string().min(1)};function p(e,t,n={}){return d(e,e=>{let n=t(e);return w(n,[`tenant`,`_id`,`_creationTime`,...u]),{...n,...f}},{...n,serverFields:[...n.serverFields??[],`tenant`]})}function m(...e){let t={};for(let n of e)for(let[e,r]of Object.entries(n)){if(e in t)throw Error(`Table "${e}" is declared by more than one module`);t[e]=r}return t}function h(e){return{args:{paginationOpts:r(s)},result:c.object({page:c.array(e),isDone:c.boolean(),continueCursor:c.string(),splitCursor:c.string().nullable().optional(),pageStatus:c.enum([`SplitRecommended`,`SplitRequired`]).nullable().optional()})}}function g(t,n,r){return r?v(t,n,r):{tableName:t,storage:n,insertSchema:n,table:o(e(n))}}function _(e){let t=a(e);return c.string().refine(e=>t.safeParse(e).success).describe(`Convex document id for table "${e}"; validated against the table by Convex`)}function v(t,n,r){if(!(n instanceof c.ZodDiscriminatedUnion)||n.def.discriminator!==r.discriminator)throw Error(`Variant boundaries require a discriminated union with the configured discriminator`);if(n.def.checks?.length)throw Error(`Variant boundaries do not support union-level refinements`);let i=r.discriminator;if([`_id`,`_creationTime`,...u].includes(i))throw Error(`Variant discriminator cannot be a reserved field`);let s=/* @__PURE__ */ new Map;for(let e of n.options){if(!(e instanceof c.ZodObject)||!(e.def.catchall instanceof c.ZodNever))throw Error(`Variant boundaries require strict object branches`);if(e.def.checks?.length)throw Error(`Variant boundaries do not support object-level refinements`);try{C(e,/* @__PURE__ */ new Set,!0)}catch(e){throw Error(`Variant boundaries require supported value-preserving field validators without object catchalls`,{cause:e})}let n=e.shape[i];if(!(n instanceof c.ZodLiteral)||n.values.size!==1)throw Error(`Variant branches require one string literal discriminator`);let a=[...n.values][0];if(!c.string().safeParse(a).success)throw Error(`Variant branches require a string discriminator`);let o=a;if(s.has(o))throw Error(`Duplicate variant discriminator "${o}"`);if(!Object.hasOwn(r.variants,o))throw Error(`Missing variant configuration "${o}"`);let d=r.variants[o];w(e.shape,[`_id`,`_creationTime`,...u]);let f={...e.shape};r.timestamps&&Object.assign(f,l),T(f,d.serverFields??[],d.commandFields??[],d.publicFields??[]);for(let e of[d.serverFields,d.commandFields,d.publicFields])if(e?.includes(i))throw Error(`The variant discriminator is retained automatically and cannot appear in masks`);let p=c.strictObject(f),m=c.strictObject(S(f,[...u,...d.serverFields??[]])),h=c.strictObject(x(f,[i,...d.commandFields??[]])),g=e=>e.partial().extend({[i]:n});s.set(o,{storage:p,document:b(p,t),insert:m,update:g(m),command:h,commandUpdate:g(h),public:c.strictObject(x(f,[i,...d.publicFields??[]]))})}for(let e of Object.keys(r.variants))if(!s.has(e))throw Error(`Unknown variant configuration "${e}"`);function d(e,n=!1,r=!1){let o=[...s.values()].map(i=>{let o=i[e];return r?c.strictObject(Object.fromEntries(Object.entries(o.shape).map(([e,t])=>[e,y(t)]))):n?o.extend({_id:a(t)}):o});if(!o.length)throw Error(`Variant boundaries require at least one branch`);return c.discriminatedUnion(i,o)}let f=d(`storage`),p=d(`document`),m=d(`insert`),h=d(`update`),g=d(`command`),v=d(`public`);function E(e=!1){return e?d(`public`,!0):v}function D(e,n=!1){let r=c.string().parse(e[i]),o=s.get(r);if(!o)throw Error(`Unknown variant discriminator "${r}"`);let l=n?o.public.extend({_id:a(t)}):o.public;return l.parse(Object.fromEntries(Object.keys(l.shape).filter(t=>Object.hasOwn(e,t)).map(t=>[t,e[t]])))}return{tableName:t,storage:f,schema:p,insertSchema:m,updateSchema:h,commandInput:g,publicDto:v,dto:E,toPublicDto:D,table:o(e(f)),tools:{id:c.strictObject({id:_(t)}),insert:d(`command`,!1,!0),update:c.strictObject({id:_(t),data:d(`commandUpdate`,!1,!0)})}}}function y(t){if(!(t instanceof c.ZodType))throw Error(`Variant tools require classic Zod field schemas`);if(t instanceof c.ZodBigInt||t instanceof c.ZodLiteral&&[...t.values].some(e=>c.bigint().safeParse(e).success))throw Error(`Variant tool command fields cannot contain bigint; JSON Schema cannot represent it`);let n=e(t);if(n.kind===`id`){let e=_(n.tableName);return n.isOptional===`optional`?e.optional().refine(e=>t.safeParse(e).success):e.refine(e=>t.safeParse(e).success)}if(t instanceof c.ZodOptional||t instanceof c.ZodNullable||t instanceof c.ZodNonOptional)return t.clone({...t.def,innerType:y(t.unwrap())});if(t instanceof c.ZodArray)return t.clone({...t.def,element:y(t.element)});if(t instanceof c.ZodObject){let e=Object.fromEntries(Object.entries(t.shape).map(([e,t])=>[e,y(t)]));return t.clone(Object.assign({},t.def,{shape:e}))}return t instanceof c.ZodUnion?t.clone({...t.def,options:t.options.map(y)}):t instanceof c.ZodRecord?t.clone({...t.def,keyType:y(t.keyType),valueType:y(t.valueType)}):t instanceof c.ZodTuple?t.clone({...t.def,items:t.def.items.map(y),rest:t.def.rest?y(t.def.rest):null}):t instanceof c.ZodIntersection?t.clone({...t.def,left:y(t.def.left),right:y(t.def.right)}):t}function b(e,t){return e.extend({_id:a(t),_creationTime:c.number()})}function x(e,t){return Object.fromEntries(t.map(t=>[t,e[t]]))}function S(e,t){let n=new Set(t);return Object.fromEntries(Object.entries(e).filter(([e])=>!n.has(e)))}function C(t,n=/* @__PURE__ */ new Set,r=!1){if(n.has(t))return;n.add(t);let i=t._zod.def,a=()=>{throw Error(`DTO extensions require supported value-preserving validation schemas`)};`coerce`in i&&i.coerce&&a(),`normalize`in i&&i.normalize&&a();let o=e=>C(e,n,r);for(let e of i.checks??[])if(e._zod.def.check===`overwrite`&&a(),`normalize`in e._zod.def&&e._zod.def.normalize&&a(),e._zod.def.check===`property`&&o(e._zod.def.schema),e._zod.def.check===`properties`)for(let t of Object.values(e._zod.def.shape))o(t);switch(i.type){case`custom`:if(r&&e(t).kind===`id`)return;a();return;case`string`:case`number`:case`boolean`:case`bigint`:case`date`:case`symbol`:case`null`:case`undefined`:case`void`:case`never`:case`literal`:case`enum`:return;case`object`:if(r&&i.catchall&&i.catchall._zod.def.type!==`never`)throw Error(`Variant fields cannot use object catchalls; use a record field instead`);for(let e of Object.values(i.shape))o(e);i.catchall&&o(i.catchall);return;case`array`:o(i.element);return;case`optional`:case`nullable`:case`nonoptional`:o(i.innerType);return;case`union`:for(let e of i.options)o(e);return;case`intersection`:o(i.left),o(i.right);return;case`tuple`:for(let e of i.items)o(e);i.rest&&o(i.rest);return;case`record`:o(i.keyType),o(i.valueType);return;default:a()}}function w(e,t){for(let n of t)if(Object.hasOwn(e,n))throw Error(`Reserved table field "${n}" cannot be declared`)}function T(e,t,n,r){for(let i of[...t,...n,...r])if(!Object.hasOwn(e,i))throw Error(`Unknown table field "${i}"`);let i=/* @__PURE__ */ new Set([...u,...t]);for(let e of n)if(i.has(e))throw Error(`Server-owned field "${e}" cannot be a command input`)}export{u as TIMESTAMP_FIELDS,n as convexToZod,m as createModule,_ as jsonSafeZid,h as paginated,p as tenantTable,i as zid,d as zodTable,e as zodToConvex,g as zodVariantTable};
2
2
  //# sourceMappingURL=zod-table.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"zod-table.mjs","names":["zid","convexToZod","convexToZod$1","zid$1","timestampFields","z","TIMESTAMP_FIELDS","zodTable","declared","fields","assertOwnedFields","columns","assertFieldLists","options","storage","schema","documentSchema","tableName","omittedKeys","commandKeys","publicKeys","insertSchema","omitFields","updateSchema","commandInput","pickFields","publicDto","reservedDtoFields","dto","deriveDto","normalized","extension","key","assertDtoValidationSchema","resultSchema","toPublicDto","projected","row","result","insert","omit","update","nativeStorage","zodToConvex","table","defineTable","paginationOpts","scalarPaginationSchema","selection","jsonSafeZid","tenantFields","tenantTable","id","createModule","maps","combined","map","definition","paginated","paginationOptsValidator","zodVariantTable","value","keys","omitted","seen","def","reject","visit","child","check","reserved","server","command","managed","convexToZod","zid"],"sources":["../src/zod-table.ts"],"sourcesContent":["import { convexToZod, zid, type ConvexValidatorFromZod, type Zid } from 'convex-helpers/server/zod4'\nimport { defineTable, paginationOptsValidator, type TableDefinition } from 'convex/server'\nimport type { GenericId } from 'convex/values'\nimport { z } from 'zod'\n\nimport { zodToConvex } from './zod-convex'\n\nimport {\n\tscalarPaginationSchema,\n\ttype ScalarPaginationSchema,\n\ttype ScalarSelection,\n\ttype ExactScalarSelection,\n} from './querying/filters'\n\n// Use this entry point when deriving validators from kit-owned schemas so\n// the producer and converter consult the same convex-helpers ID registry.\nexport { zid, convexToZod } from 'convex-helpers/server/zod4'\nexport { zodToConvex } from './zod-convex'\n\ntype FieldSchemas = Record<string, z.ZodType>\ntype FieldKey<Fields extends FieldSchemas> = Extract<keyof Fields, string>\n\n/**\n * Opinionated lifecycle timestamps — every table has them, server-owned,\n * always excluded from insert/command boundaries. Maintained by the\n * `timestamps` trigger helper (see cvx-kit/triggers).\n */\nconst timestampFields = {\n\tcreatedAt: z.number().optional(),\n\tupdatedAt: z.number().optional(),\n\tarchivedAt: z.number().optional(),\n} satisfies FieldSchemas\n\ntype TimestampFields = typeof timestampFields\ntype ReservedStorageField = '_id' | '_creationTime' | keyof TimestampFields\ntype OwnedFields<Fields> = {\n\t[Key in keyof Fields]: Key extends ReservedStorageField ? never : Fields[Key]\n}\ntype SystemFields<Table extends string> = { _id: Zid<Table>; _creationTime: z.ZodNumber }\n\nexport const TIMESTAMP_FIELDS = ['createdAt', 'updatedAt', 'archivedAt'] as const\n\nexport type TableBoundaryOptions<\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n\tCommandFields extends readonly FieldKey<Fields>[],\n\tPublicFields extends readonly FieldKey<Fields & TimestampFields>[],\n> = {\n\tserverFields?: ServerFields\n\tcommandFields?: CommandFields &\n\t\treadonly Exclude<FieldKey<Fields>, ServerFields[number] | keyof TimestampFields>[]\n\tpublicFields?: PublicFields\n}\n\ntype StrictObject<Fields extends FieldSchemas> = z.ZodObject<Fields, z.core.$strict>\ntype WriteFields<\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n> = Omit<Fields & TimestampFields, ServerFields[number] | (typeof TIMESTAMP_FIELDS)[number]>\ntype PartialFields<Fields extends FieldSchemas> = {\n\t[Key in keyof Fields]: z.ZodOptional<Fields[Key]>\n}\n\n/** Options shared by the DTO return schema and row projector.\n * Promised contributions must be present, including in reusable options.\n * New extension schemas support validation only: no transforms, pipes,\n * coercion, preprocess, defaults, catches, or overwrites, even when nested.\n * Refinements must validate without mutating their input.\n */\nexport type DtoOptions<IncludeId extends boolean = false, Extension extends FieldSchemas = {}> = ([\n\tIncludeId,\n] extends [true]\n\t? { _id: IncludeId }\n\t: { _id?: IncludeId }) &\n\t(keyof Extension extends never ? { extend?: Extension } : { extend: Extension })\n\ntype DtoProjectionOptions<IncludeId extends boolean> = DtoOptions<IncludeId> & {\n\textend?: Record<string, never>\n\tvalues?: never\n}\n\ntype ReservedDtoField =\n\t| '_id'\n\t| '_creationTime'\n\t| 'tenant'\n\t| '__proto__'\n\t| (typeof TIMESTAMP_FIELDS)[number]\ntype DtoExtension<Public extends FieldSchemas, Extension extends FieldSchemas> = {\n\t[Key in keyof Extension]: Key extends keyof Public | ReservedDtoField\n\t\t? never\n\t\t: Extension[Key] extends z.ZodPipe | z.ZodTransform\n\t\t\t? never\n\t\t\t: unknown\n}\n// Broad dictionaries cannot promise any particular extension field.\ntype KnownExtension<Extension> = string extends keyof Extension ? {} : Extension\n\ntype DtoSchema<\n\tTable extends string,\n\tPublic extends FieldSchemas,\n\tIncludeId extends boolean,\n\tExtension extends FieldSchemas = {},\n> = IncludeId extends true\n\t? StrictObject<Public & { _id: Zid<Table> } & KnownExtension<Extension>>\n\t: StrictObject<Public & KnownExtension<Extension>>\ntype DtoRow<Row, IncludeId extends boolean> = Row & (true extends IncludeId ? { _id: string } : {})\ntype DtoValues<Extension extends FieldSchemas> = keyof Extension extends never\n\t? { values?: never }\n\t: { values: z.input<StrictObject<NoInfer<Extension>>> }\n\ntype DtoBoundary<Table extends string, Fields extends FieldSchemas, Public extends FieldSchemas> = {\n\tdto(): StrictObject<Public>\n\tdto<const IncludeId extends boolean>(includeId: IncludeId): DtoSchema<Table, Public, IncludeId>\n\tdto<const IncludeId extends boolean = false>(\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): DtoSchema<Table, Public, IncludeId>\n\tdto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\toptions: DtoOptions<IncludeId, Extension> & { extend?: DtoExtension<Public, Extension> },\n\t): DtoSchema<Table, Public, IncludeId, Extension>\n\ttoPublicDto(row: z.input<StrictObject<Fields>>): z.output<StrictObject<Public>>\n\ttoPublicDto<const IncludeId extends boolean>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\tincludeId: IncludeId,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\ttoPublicDto<const IncludeId extends boolean = false>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\ttoPublicDto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\toptions: DtoOptions<IncludeId, Extension> & {\n\t\t\textend?: DtoExtension<Public, Extension>\n\t\t} & DtoValues<Extension>,\n\t): z.output<DtoSchema<Table, Public, IncludeId, Extension>>\n}\n\n/** Named schema relationships keep generated declarations bounded and consumers fully typed. */\nexport type TableBoundary<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n\tCommandFields extends readonly FieldKey<Fields>[],\n\tPublicFields extends readonly FieldKey<Fields & TimestampFields>[],\n> = DtoBoundary<\n\tTable,\n\tFields & TimestampFields,\n\tPick<Fields & TimestampFields, PublicFields[number]>\n> & {\n\tpaginationOpts<\n\t\tconst Selection extends ScalarSelection<Fields & TimestampFields & SystemFields<Table>>,\n\t>(\n\t\tselection: ExactScalarSelection<Fields & TimestampFields & SystemFields<Table>, Selection>,\n\t): ScalarPaginationSchema<Fields & TimestampFields & SystemFields<Table>, Selection>\n\ttableName: Table\n\tstorage: StrictObject<Fields & TimestampFields>\n\tschema: ReturnType<typeof documentSchema<Fields & TimestampFields, Table>>\n\tinsertSchema: StrictObject<WriteFields<Fields, ServerFields>>\n\tupdateSchema: StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>\n\tcommandInput: StrictObject<Pick<Fields & TimestampFields, CommandFields[number]>>\n\tpublicDto: StrictObject<Pick<Fields & TimestampFields, PublicFields[number]>>\n\ttable: TableDefinition<ConvexValidatorFromZod<StrictObject<Fields & TimestampFields>, 'required'>>\n\tinsert(): StrictObject<WriteFields<Fields, ServerFields>>\n\tinsert<Mask extends z.util.Mask<keyof WriteFields<Fields, ServerFields>>>(\n\t\tomit: Mask,\n\t): ZodObjectOmit<StrictObject<WriteFields<Fields, ServerFields>>, Mask>\n\tupdate(): StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>\n\tupdate<Mask extends z.util.Mask<keyof WriteFields<Fields, ServerFields>>>(\n\t\tomit: Mask,\n\t): ZodObjectOmit<StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>, Mask>\n\ttools: {\n\t\tinsert: StrictObject<Pick<Fields & TimestampFields, CommandFields[number]>>\n\t\tupdate: StrictObject<{\n\t\t\tdata: StrictObject<PartialFields<Pick<Fields & TimestampFields, CommandFields[number]>>>\n\t\t\tid: z.ZodType<GenericId<Table>, string>\n\t\t}>\n\t\tid: StrictObject<{ id: z.ZodType<GenericId<Table>, string> }>\n\t}\n}\n\n/** Defines one table and derives its storage, document, write, and DTO boundaries. */\nexport function zodTable<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tconst ServerFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst CommandFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst PublicFields extends readonly FieldKey<Fields & TimestampFields>[] = readonly [],\n>(\n\ttableName: Table,\n\tfields: (id: typeof zid) => Fields & OwnedFields<Fields>,\n\toptions: TableBoundaryOptions<Fields, ServerFields, CommandFields, PublicFields> = {},\n): TableBoundary<Table, Fields, ServerFields, CommandFields, PublicFields> {\n\tconst declared = fields(zid)\n\tassertOwnedFields(declared, ['_id', '_creationTime', ...TIMESTAMP_FIELDS])\n\tconst columns: Fields & TimestampFields = { ...declared, ...timestampFields }\n\tassertFieldLists(\n\t\tcolumns,\n\t\toptions.serverFields ?? [],\n\t\toptions.commandFields ?? [],\n\t\toptions.publicFields ?? [],\n\t)\n\tconst storage: z.ZodObject<Fields & TimestampFields, z.core.$strict> = z.object(columns).strict()\n\tconst schema = documentSchema(storage, tableName)\n\tconst omittedKeys: readonly (ServerFields[number] | (typeof TIMESTAMP_FIELDS)[number])[] = [\n\t\t...TIMESTAMP_FIELDS,\n\t\t...(options.serverFields ?? []),\n\t]\n\tconst commandKeys: readonly CommandFields[number][] = options.commandFields ?? []\n\tconst publicKeys: readonly PublicFields[number][] = options.publicFields ?? []\n\tconst insertSchema: z.ZodObject<WriteFields<Fields, ServerFields>, z.core.$strict> = z\n\t\t.object(omitFields(columns, omittedKeys))\n\t\t.strict()\n\tconst updateSchema = insertSchema.partial()\n\tconst commandInput: z.ZodObject<\n\t\tPick<Fields & TimestampFields, CommandFields[number]>,\n\t\tz.core.$strict\n\t> = z.object(pickFields(columns, commandKeys)).strict()\n\tconst publicDto: z.ZodObject<\n\t\tPick<Fields & TimestampFields, PublicFields[number]>,\n\t\tz.core.$strict\n\t> = z.object(pickFields(columns, publicKeys)).strict()\n\ttype Public = Pick<Fields & TimestampFields, PublicFields[number]>\n\ttype Row = z.input<typeof storage>\n\ttype RuntimeDtoFields = Public & FieldSchemas\n\ttype RuntimeDto = z.output<StrictObject<RuntimeDtoFields>> & { _id?: GenericId<Table> }\n\ttype RuntimeOptions = { _id?: boolean; extend?: FieldSchemas; values?: unknown }\n\tconst reservedDtoFields = new Set<string>([\n\t\t'_id',\n\t\t'_creationTime',\n\t\t'tenant',\n\t\t'__proto__',\n\t\t...TIMESTAMP_FIELDS,\n\t])\n\n\tfunction dto(): typeof publicDto\n\tfunction dto<const IncludeId extends boolean>(\n\t\tincludeId: IncludeId,\n\t): DtoSchema<Table, Public, IncludeId>\n\tfunction dto<const IncludeId extends boolean = false>(\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): DtoSchema<Table, Public, IncludeId>\n\tfunction dto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\toptions: DtoOptions<IncludeId, Extension> & { extend?: DtoExtension<Public, Extension> },\n\t): DtoSchema<Table, Public, IncludeId, Extension>\n\tfunction dto(options?: boolean | RuntimeOptions) {\n\t\treturn deriveDto(options)\n\t}\n\n\tfunction deriveDto(options?: boolean | RuntimeOptions) {\n\t\tconst normalized = options === true || options === false ? { _id: options } : options\n\t\tconst extension = normalized?.extend ?? {}\n\t\tfor (const key of Object.keys(extension)) {\n\t\t\tif (reservedDtoFields.has(key) || Object.hasOwn(publicDto.shape, key)) {\n\t\t\t\tthrow new Error(`DTO extension collision for field \"${key}\" on table \"${tableName}\"`)\n\t\t\t}\n\t\t\tassertDtoValidationSchema(extension[key])\n\t\t}\n\t\tif (!normalized?._id && Object.keys(extension).length === 0) return publicDto\n\t\tconst resultSchema = z.object({ ...publicDto.shape, ...extension }).strict()\n\t\treturn normalized?._id ? resultSchema.extend({ _id: schema.shape._id }) : resultSchema\n\t}\n\n\tfunction toPublicDto(row: Row): z.output<typeof publicDto>\n\tfunction toPublicDto<const IncludeId extends boolean>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\tincludeId: IncludeId,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\tfunction toPublicDto<const IncludeId extends boolean = false>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\tfunction toPublicDto<\n\t\tconst IncludeId extends boolean = false,\n\t\tconst Extension extends FieldSchemas = {},\n\t>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\toptions: DtoOptions<IncludeId, Extension> & {\n\t\t\textend?: DtoExtension<Public, Extension>\n\t\t} & DtoValues<Extension>,\n\t): z.output<DtoSchema<Table, Public, IncludeId, Extension>>\n\tfunction toPublicDto(row: Row, options?: boolean | RuntimeOptions): RuntimeDto {\n\t\tconst normalized = options === true || options === false ? { _id: options } : options\n\t\t// Enforce extension collisions and validation-only schemas before parsing values.\n\t\tderiveDto(options)\n\t\tconst projected = publicDto.parse(\n\t\t\tObject.fromEntries(\n\t\t\t\tObject.keys(publicDto.shape).map((key) => {\n\t\t\t\t\t// SAFETY: publicDto contains only keys selected from the storage fields.\n\t\t\t\t\treturn [key, row[key as keyof typeof row]]\n\t\t\t\t}),\n\t\t\t),\n\t\t)\n\t\tconst extension = z\n\t\t\t.object(normalized?.extend ?? {})\n\t\t\t.strict()\n\t\t\t.parse(normalized?.values ?? {})\n\t\tconst result: RuntimeDto = { ...projected, ...extension }\n\t\tif (normalized?._id) {\n\t\t\tresult._id = schema.shape._id.parse('_id' in row ? row._id : undefined)\n\t\t}\n\t\treturn result\n\t}\n\n\ttype InsertSchema = typeof insertSchema\n\ttype UpdateSchema = typeof updateSchema\n\ttype InsertMask = z.util.Mask<keyof InsertSchema['shape']>\n\ttype UpdateMask = z.util.Mask<keyof UpdateSchema['shape']>\n\n\tfunction insert(): InsertSchema\n\tfunction insert<OmitMask extends InsertMask>(\n\t\tomit: OmitMask,\n\t): ZodObjectOmit<InsertSchema, OmitMask>\n\tfunction insert(omit?: InsertMask) {\n\t\t// SAFETY: InsertMask contains only insertSchema keys; the overload retains Zod's exact mask relationship.\n\t\treturn omit ? insertSchema.omit(omit as never) : insertSchema\n\t}\n\n\tfunction update(): UpdateSchema\n\tfunction update<OmitMask extends UpdateMask>(\n\t\tomit: OmitMask,\n\t): ZodObjectOmit<UpdateSchema, OmitMask>\n\tfunction update(omit?: UpdateMask) {\n\t\t// SAFETY: UpdateMask contains only updateSchema keys; the overload retains Zod's exact mask relationship.\n\t\treturn omit ? updateSchema.omit(omit as never) : updateSchema\n\t}\n\n\tconst nativeStorage = zodToConvex(storage)\n\tconst table: TableDefinition<ConvexValidatorFromZod<typeof storage, 'required'>> =\n\t\tdefineTable(nativeStorage)\n\ttype EntityFields = Fields & TimestampFields & SystemFields<Table>\n\tfunction paginationOpts<const Selection extends ScalarSelection<EntityFields>>(\n\t\tselection: ExactScalarSelection<EntityFields, Selection>,\n\t): ScalarPaginationSchema<EntityFields, Selection> {\n\t\treturn scalarPaginationSchema<EntityFields, Selection>(zodToConvex(schema).fields, selection)\n\t}\n\treturn {\n\t\ttableName,\n\t\tschema,\n\t\tstorage,\n\t\tinsertSchema,\n\t\tupdateSchema,\n\t\tcommandInput,\n\t\tpublicDto,\n\t\tdto,\n\t\ttoPublicDto,\n\t\tpaginationOpts,\n\t\ttable,\n\t\tinsert,\n\t\tupdate,\n\t\ttools: {\n\t\t\t// jsonSafeZid (not zid): tool masks feed generated JSON schemas, so\n\t\t\t// ids must present as plain strings while keeping the Id<...> type.\n\t\t\tinsert: commandInput,\n\t\t\tupdate: z.object({ data: commandInput.partial(), id: jsonSafeZid(tableName) }).strict(),\n\t\t\tid: z.object({ id: jsonSafeZid(tableName) }).strict(),\n\t\t},\n\t}\n}\n\nconst tenantFields = {\n\ttenant: z.string().min(1),\n} satisfies FieldSchemas\n\ntype TenantFields = typeof tenantFields\n\n/**\n * A zodTable whose rows are tenant-owned: injects the server-owned `tenant`\n * field (the tenancy boundary — see cvx-kit/tenancy). Like timestamps, it is\n * excluded from insert/update/command boundaries; handlers stamp it from\n * ctx.tenant, row-level security matches on it, and the tenantOwnership\n * trigger forbids reassigning it. Expose it via publicFields only when a DTO\n * genuinely needs it.\n */\nexport function tenantTable<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tconst ServerFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst CommandFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst PublicFields extends readonly FieldKey<Fields & TenantFields & TimestampFields>[] =\n\t\treadonly [],\n>(\n\ttableName: Table,\n\tfields: (\n\t\tid: typeof zid,\n\t) => Fields &\n\t\tOwnedFields<Fields> & { [Key in keyof Fields]: Key extends 'tenant' ? never : Fields[Key] },\n\toptions: TableBoundaryOptions<\n\t\tFields & TenantFields,\n\t\treadonly (ServerFields[number] | 'tenant')[],\n\t\tCommandFields,\n\t\tPublicFields\n\t> = {},\n) {\n\treturn zodTable<\n\t\tTable,\n\t\tFields & TenantFields,\n\t\treadonly (ServerFields[number] | 'tenant')[],\n\t\tCommandFields,\n\t\tPublicFields\n\t>(\n\t\ttableName,\n\t\t(id) => {\n\t\t\tconst declared = fields(id)\n\t\t\tassertOwnedFields(declared, ['tenant', '_id', '_creationTime', ...TIMESTAMP_FIELDS])\n\t\t\t// SAFETY: reserved keys were rejected before injecting tenant; the generic\n\t\t\t// wrapper forwards the same checked fields to the ordinary table constructor.\n\t\t\treturn { ...declared, ...tenantFields } as Fields &\n\t\t\t\tTenantFields &\n\t\t\t\tOwnedFields<Fields & TenantFields>\n\t\t},\n\t\t{\n\t\t\t...options,\n\t\t\tserverFields: [...(options.serverFields ?? []), 'tenant'],\n\t\t},\n\t)\n}\n\n/**\n * Merges per-module table maps into the application schema map, rejecting\n * duplicate table names across modules — the module-registry combinator for\n * domain/table.ts: `defineSchema(createModule(catalogTables, salesTables))`.\n */\nexport function createModule<const Maps extends readonly Record<string, TableDefinition<any>>[]>(\n\t...maps: Maps\n): UnionToIntersection<Maps[number]> {\n\tconst combined: Record<string, TableDefinition<any>> = {}\n\tfor (const map of maps) {\n\t\tfor (const [tableName, definition] of Object.entries(map)) {\n\t\t\tif (tableName in combined) {\n\t\t\t\tthrow new Error(`Table \"${tableName}\" is declared by more than one module`)\n\t\t\t}\n\t\t\tcombined[tableName] = definition\n\t\t}\n\t}\n\t// SAFETY: every input entry is copied, and duplicate keys are rejected above.\n\treturn combined as UnionToIntersection<Maps[number]>\n}\n\ntype UnionToIntersection<Union> = (\n\tUnion extends unknown ? (member: Union) => void : never\n) extends (member: infer Intersection) => void\n\t? Intersection\n\t: never\n\n/**\n * Cursor-pagination boundary schemas bridged from Convex's own validators:\n * `args.paginationOpts` for function args, `result` for the page shape.\n * Pair with `include(...).paginate(opts)` (cvx-kit/auth). The result object\n * is deliberately non-strict — Convex may attach engine fields (splitCursor,\n * pageStatus); the page items themselves are validated against the dto.\n * Note: under row-level security, pages may be SHORTER than numItems when\n * rules reject rows mid-page; cursors remain correct.\n */\nexport function paginated<Dto extends z.ZodType>(dto: Dto) {\n\treturn {\n\t\targs: { paginationOpts: convexToZod(paginationOptsValidator) },\n\t\tresult: z.object({\n\t\t\tpage: z.array(dto),\n\t\t\tisDone: z.boolean(),\n\t\t\tcontinueCursor: z.string(),\n\t\t\tsplitCursor: z.string().nullable().optional(),\n\t\t\tpageStatus: z.enum(['SplitRecommended', 'SplitRequired']).nullable().optional(),\n\t\t}),\n\t}\n}\n\n/** Preserves discriminated-union storage while retaining the same table owner. */\nexport function zodVariantTable<Table extends string, Schema extends z.ZodType>(\n\ttableName: Table,\n\tstorage: Schema,\n) {\n\treturn {\n\t\ttableName,\n\t\tstorage,\n\t\tinsertSchema: storage,\n\t\ttable: defineTable(zodToConvex(storage)),\n\t}\n}\n\n/**\n * A zid that presents as a plain string in generated JSON schemas so\n * LLM-tool inputs stay primitive, while keeping the Id type at compile time.\n */\nexport function jsonSafeZid<Table extends string>(\n\ttableName: Table,\n): z.ZodType<GenericId<Table>, string> {\n\tconst id = zid(tableName)\n\treturn z\n\t\t.string()\n\t\t.refine((value): value is GenericId<Table> => id.safeParse(value).success)\n\t\t.describe(`Convex document id for table \"${tableName}\"; validated against the table by Convex`)\n}\n\ntype ZodObjectOmit<\n\tSchema extends z.ZodObject<any, any>,\n\tOmitMask extends z.util.Mask<keyof Schema['shape']>,\n> =\n\tSchema extends z.ZodObject<infer ObjectFields, infer Config>\n\t\t? z.ZodObject<\n\t\t\t\tz.util.Flatten<Omit<ObjectFields, Extract<keyof ObjectFields, keyof OmitMask>>>,\n\t\t\t\tConfig\n\t\t\t>\n\t\t: never\n\nfunction documentSchema<Fields extends FieldSchemas, Table extends string>(\n\tstorage: StrictObject<Fields>,\n\ttableName: Table,\n) {\n\treturn storage.extend({ _id: zid(tableName), _creationTime: z.number() })\n}\n\nfunction pickFields<Fields extends FieldSchemas, const Keys extends readonly FieldKey<Fields>[]>(\n\tfields: Fields,\n\tkeys: Keys,\n) {\n\t// SAFETY: each entry uses a requested key and its original validator, preserving Pick.\n\treturn Object.fromEntries(keys.map((key) => [key, fields[key]])) as Pick<Fields, Keys[number]>\n}\n\nfunction omitFields<Fields extends FieldSchemas, const Keys extends readonly FieldKey<Fields>[]>(\n\tfields: Fields,\n\tkeys: Keys,\n) {\n\tconst omitted = new Set<string>(keys)\n\t// SAFETY: filtering removes exactly the supplied keys and keeps every remaining validator.\n\treturn Object.fromEntries(Object.entries(fields).filter(([key]) => !omitted.has(key))) as Omit<\n\t\tFields,\n\t\tKeys[number]\n\t>\n}\n\ntype DtoPropertiesCheck = z.core.$ZodCheck & {\n\treadonly _zod: {\n\t\treadonly def: { readonly check: 'properties' } & Record<\n\t\t\t'shape',\n\t\t\tRecord<string, z.core.$ZodType>\n\t\t>\n\t}\n}\n\n/** Inspect Zod 4's discriminated definitions, failing closed for unsupported nodes.\n * This restriction applies only to new DTO extensions; storage/publicDto are unchanged.\n */\nfunction assertDtoValidationSchema(\n\tschema: z.core.$ZodType,\n\tseen = new Set<z.core.$ZodType>(),\n): void {\n\tif (seen.has(schema)) return\n\tseen.add(schema)\n\t// SAFETY: Zod 4 exposes its built-in schema definitions as this discriminated union.\n\tconst def = (schema as z.core.$ZodTypes)._zod.def\n\tconst reject = () => {\n\t\tthrow new Error('DTO extensions require supported value-preserving validation schemas')\n\t}\n\tif ('coerce' in def && def.coerce) reject()\n\tif ('normalize' in def && def.normalize) reject()\n\tconst visit = (child: z.core.$ZodType) => assertDtoValidationSchema(child, seen)\n\tfor (const check of def.checks ?? []) {\n\t\tif (check._zod.def.check === 'overwrite') reject()\n\t\tif ('normalize' in check._zod.def && check._zod.def.normalize) reject()\n\t\tif (check._zod.def.check === 'property') {\n\t\t\t// SAFETY: the property discriminator owns this embedded schema in Zod 4.\n\t\t\tvisit((check as z.core.$ZodCheckProperty)._zod.def.schema)\n\t\t}\n\t\tif (check._zod.def.check === 'properties') {\n\t\t\t// SAFETY: the properties discriminator owns this schema shape in Zod 4.\n\t\t\tfor (const child of Object.values((check as DtoPropertiesCheck)._zod.def.shape)) {\n\t\t\t\tvisit(child)\n\t\t\t}\n\t\t}\n\t}\n\tswitch (def.type) {\n\t\tcase 'string':\n\t\tcase 'number':\n\t\tcase 'boolean':\n\t\tcase 'bigint':\n\t\tcase 'date':\n\t\tcase 'symbol':\n\t\tcase 'null':\n\t\tcase 'undefined':\n\t\tcase 'void':\n\t\tcase 'never':\n\t\tcase 'literal':\n\t\tcase 'enum':\n\t\t\treturn\n\t\tcase 'object':\n\t\t\tfor (const child of Object.values(def.shape)) visit(child)\n\t\t\tif (def.catchall) visit(def.catchall)\n\t\t\treturn\n\t\tcase 'array':\n\t\t\tvisit(def.element)\n\t\t\treturn\n\t\tcase 'optional':\n\t\tcase 'nullable':\n\t\tcase 'nonoptional':\n\t\t\tvisit(def.innerType)\n\t\t\treturn\n\t\tcase 'union':\n\t\t\tfor (const child of def.options) visit(child)\n\t\t\treturn\n\t\tcase 'intersection':\n\t\t\tvisit(def.left)\n\t\t\tvisit(def.right)\n\t\t\treturn\n\t\tcase 'tuple':\n\t\t\tfor (const child of def.items) visit(child)\n\t\t\tif (def.rest) visit(def.rest)\n\t\t\treturn\n\t\tcase 'record':\n\t\t\tvisit(def.keyType)\n\t\t\tvisit(def.valueType)\n\t\t\treturn\n\t\tdefault:\n\t\t\treject()\n\t}\n}\n\nfunction assertOwnedFields(fields: FieldSchemas, reserved: readonly string[]): void {\n\tfor (const key of reserved) {\n\t\tif (Object.hasOwn(fields, key))\n\t\t\tthrow new Error(`Reserved table field \"${key}\" cannot be declared`)\n\t}\n}\n\nfunction assertFieldLists(\n\tfields: FieldSchemas,\n\tserver: readonly string[],\n\tcommand: readonly string[],\n\tpublicKeys: readonly string[],\n): void {\n\tfor (const key of [...server, ...command, ...publicKeys]) {\n\t\tif (!Object.hasOwn(fields, key)) throw new Error(`Unknown table field \"${key}\"`)\n\t}\n\tconst managed = new Set<string>([...TIMESTAMP_FIELDS, ...server])\n\tfor (const key of command) {\n\t\tif (managed.has(key)) throw new Error(`Server-owned field \"${key}\" cannot be a command input`)\n\t}\n}\n"],"mappings":"4SA2BA,MAAMI,EAAkB,CACvB,UAAWC,EAAE,OAAO,CAAC,CAAC,SAAS,EAC/B,UAAWA,EAAE,OAAO,CAAC,CAAC,SAAS,EAC/B,WAAYA,EAAE,OAAO,CAAC,CAAC,SAAS,CACjC,EASaC,EAAmB,C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AAK,IAAMoB,IAAO,CAAC,GAAGuC,EAAQ,GAAGC,EAAS,GAAGpD,CAAU,EACtD,GAAI,CAAC,OAAO,OAAOX,EAAQuB,CAAG,EAAG,MAAU,MAAM,wBAAwBA,EAAI,EAAE,EAEhF,IAAMyC,kBAAU,IAAI,IAAY,CAAC,GAAGnE,EAAkB,GAAGiE,CAAM,CAAC,EAChE,IAAK,IAAMvC,KAAOwC,EACjB,GAAIC,EAAQ,IAAIzC,CAAG,EAAG,MAAU,MAAM,uBAAuBA,EAAI,4BAA4B,CAE/F"}
1
+ {"version":3,"file":"zod-table.mjs","names":["zid","convexToZod","convexToZod$1","zid$1","timestampFields","z","TIMESTAMP_FIELDS","zodTable","declared","fields","assertOwnedFields","columns","assertFieldLists","options","storage","schema","documentSchema","tableName","omittedKeys","commandKeys","publicKeys","insertSchema","omitFields","updateSchema","commandInput","pickFields","publicDto","reservedDtoFields","dto","deriveDto","normalized","extension","key","assertDtoValidationSchema","resultSchema","toPublicDto","projected","row","result","insert","omit","update","nativeStorage","zodToConvex","table","defineTable","paginationOpts","scalarPaginationSchema","selection","jsonSafeZid","tenantFields","tenantTable","id","createModule","maps","combined","map","definition","paginated","paginationOptsValidator","zodVariantTable","deriveVariantTable","value","discriminator","branches","branch","cause","tagSchema","tag","tagValue","config","mask","stored","command","partial","union","schemas","mode","tool","field","variantToolField","includeId","projection","native","child","\"shape\"","keys","omitted","seen","def","reject","visit","allowNativeIds","check","reserved","server","managed","convexToZod","zid"],"sources":["../src/zod-table.ts"],"sourcesContent":["import { convexToZod, zid, type ConvexValidatorFromZod, type Zid } from 'convex-helpers/server/zod4'\nimport { defineTable, paginationOptsValidator, type TableDefinition } from 'convex/server'\nimport type { GenericId } from 'convex/values'\nimport { z } from 'zod'\n\nimport { zodToConvex } from './zod-convex'\n\nimport {\n\tscalarPaginationSchema,\n\ttype ScalarPaginationSchema,\n\ttype ScalarSelection,\n\ttype ExactScalarSelection,\n} from './querying/filters'\n\n// Use this entry point when deriving validators from kit-owned schemas so\n// the producer and converter consult the same convex-helpers ID registry.\nexport { zid, convexToZod } from 'convex-helpers/server/zod4'\nexport { zodToConvex } from './zod-convex'\n\ntype FieldSchemas = Record<string, z.ZodType>\ntype FieldKey<Fields extends FieldSchemas> = Extract<keyof Fields, string>\n\n/**\n * Opinionated lifecycle timestamps — every table has them, server-owned,\n * always excluded from insert/command boundaries. Maintained by the\n * `timestamps` trigger helper (see cvx-kit/triggers).\n */\nconst timestampFields = {\n\tcreatedAt: z.number().optional(),\n\tupdatedAt: z.number().optional(),\n\tarchivedAt: z.number().optional(),\n} satisfies FieldSchemas\n\ntype TimestampFields = typeof timestampFields\ntype ReservedStorageField = '_id' | '_creationTime' | keyof TimestampFields\ntype OwnedFields<Fields> = {\n\t[Key in keyof Fields]: Key extends ReservedStorageField ? never : Fields[Key]\n}\ntype SystemFields<Table extends string> = { _id: Zid<Table>; _creationTime: z.ZodNumber }\n\nexport const TIMESTAMP_FIELDS = ['createdAt', 'updatedAt', 'archivedAt'] as const\n\nexport type TableBoundaryOptions<\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n\tCommandFields extends readonly FieldKey<Fields>[],\n\tPublicFields extends readonly FieldKey<Fields & TimestampFields>[],\n> = {\n\tserverFields?: ServerFields\n\tcommandFields?: CommandFields &\n\t\treadonly Exclude<FieldKey<Fields>, ServerFields[number] | keyof TimestampFields>[]\n\tpublicFields?: PublicFields\n}\n\ntype StrictObject<Fields extends FieldSchemas> = z.ZodObject<Fields, z.core.$strict>\ntype WriteFields<\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n> = Omit<Fields & TimestampFields, ServerFields[number] | (typeof TIMESTAMP_FIELDS)[number]>\ntype PartialFields<Fields extends FieldSchemas> = {\n\t[Key in keyof Fields]: z.ZodOptional<Fields[Key]>\n}\n\n/** Options shared by the DTO return schema and row projector.\n * Promised contributions must be present, including in reusable options.\n * New extension schemas support validation only: no transforms, pipes,\n * coercion, preprocess, defaults, catches, or overwrites, even when nested.\n * Refinements must validate without mutating their input.\n */\nexport type DtoOptions<IncludeId extends boolean = false, Extension extends FieldSchemas = {}> = ([\n\tIncludeId,\n] extends [true]\n\t? { _id: IncludeId }\n\t: { _id?: IncludeId }) &\n\t(keyof Extension extends never ? { extend?: Extension } : { extend: Extension })\n\ntype DtoProjectionOptions<IncludeId extends boolean> = DtoOptions<IncludeId> & {\n\textend?: Record<string, never>\n\tvalues?: never\n}\n\ntype ReservedDtoField =\n\t| '_id'\n\t| '_creationTime'\n\t| 'tenant'\n\t| '__proto__'\n\t| (typeof TIMESTAMP_FIELDS)[number]\ntype DtoExtension<Public extends FieldSchemas, Extension extends FieldSchemas> = {\n\t[Key in keyof Extension]: Key extends keyof Public | ReservedDtoField\n\t\t? never\n\t\t: Extension[Key] extends z.ZodPipe | z.ZodTransform\n\t\t\t? never\n\t\t\t: unknown\n}\n// Broad dictionaries cannot promise any particular extension field.\ntype KnownExtension<Extension> = string extends keyof Extension ? {} : Extension\n\ntype DtoSchema<\n\tTable extends string,\n\tPublic extends FieldSchemas,\n\tIncludeId extends boolean,\n\tExtension extends FieldSchemas = {},\n> = IncludeId extends true\n\t? StrictObject<Public & { _id: Zid<Table> } & KnownExtension<Extension>>\n\t: StrictObject<Public & KnownExtension<Extension>>\ntype DtoRow<Row, IncludeId extends boolean> = Row & (true extends IncludeId ? { _id: string } : {})\ntype DtoValues<Extension extends FieldSchemas> = keyof Extension extends never\n\t? { values?: never }\n\t: { values: z.input<StrictObject<NoInfer<Extension>>> }\n\ntype DtoBoundary<Table extends string, Fields extends FieldSchemas, Public extends FieldSchemas> = {\n\tdto(): StrictObject<Public>\n\tdto<const IncludeId extends boolean>(includeId: IncludeId): DtoSchema<Table, Public, IncludeId>\n\tdto<const IncludeId extends boolean = false>(\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): DtoSchema<Table, Public, IncludeId>\n\tdto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\toptions: DtoOptions<IncludeId, Extension> & { extend?: DtoExtension<Public, Extension> },\n\t): DtoSchema<Table, Public, IncludeId, Extension>\n\ttoPublicDto(row: z.input<StrictObject<Fields>>): z.output<StrictObject<Public>>\n\ttoPublicDto<const IncludeId extends boolean>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\tincludeId: IncludeId,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\ttoPublicDto<const IncludeId extends boolean = false>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\ttoPublicDto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\trow: DtoRow<z.input<StrictObject<Fields>>, IncludeId>,\n\t\toptions: DtoOptions<IncludeId, Extension> & {\n\t\t\textend?: DtoExtension<Public, Extension>\n\t\t} & DtoValues<Extension>,\n\t): z.output<DtoSchema<Table, Public, IncludeId, Extension>>\n}\n\n/** Named schema relationships keep generated declarations bounded and consumers fully typed. */\nexport type TableBoundary<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tServerFields extends readonly FieldKey<Fields>[],\n\tCommandFields extends readonly FieldKey<Fields>[],\n\tPublicFields extends readonly FieldKey<Fields & TimestampFields>[],\n> = DtoBoundary<\n\tTable,\n\tFields & TimestampFields,\n\tPick<Fields & TimestampFields, PublicFields[number]>\n> & {\n\tpaginationOpts<\n\t\tconst Selection extends ScalarSelection<Fields & TimestampFields & SystemFields<Table>>,\n\t>(\n\t\tselection: ExactScalarSelection<Fields & TimestampFields & SystemFields<Table>, Selection>,\n\t): ScalarPaginationSchema<Fields & TimestampFields & SystemFields<Table>, Selection>\n\ttableName: Table\n\tstorage: StrictObject<Fields & TimestampFields>\n\tschema: ReturnType<typeof documentSchema<Fields & TimestampFields, Table>>\n\tinsertSchema: StrictObject<WriteFields<Fields, ServerFields>>\n\tupdateSchema: StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>\n\tcommandInput: StrictObject<Pick<Fields & TimestampFields, CommandFields[number]>>\n\tpublicDto: StrictObject<Pick<Fields & TimestampFields, PublicFields[number]>>\n\ttable: TableDefinition<ConvexValidatorFromZod<StrictObject<Fields & TimestampFields>, 'required'>>\n\tinsert(): StrictObject<WriteFields<Fields, ServerFields>>\n\tinsert<Mask extends z.util.Mask<keyof WriteFields<Fields, ServerFields>>>(\n\t\tomit: Mask,\n\t): ZodObjectOmit<StrictObject<WriteFields<Fields, ServerFields>>, Mask>\n\tupdate(): StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>\n\tupdate<Mask extends z.util.Mask<keyof WriteFields<Fields, ServerFields>>>(\n\t\tomit: Mask,\n\t): ZodObjectOmit<StrictObject<PartialFields<WriteFields<Fields, ServerFields>>>, Mask>\n\ttools: {\n\t\tinsert: StrictObject<Pick<Fields & TimestampFields, CommandFields[number]>>\n\t\tupdate: StrictObject<{\n\t\t\tdata: StrictObject<PartialFields<Pick<Fields & TimestampFields, CommandFields[number]>>>\n\t\t\tid: z.ZodType<GenericId<Table>, string>\n\t\t}>\n\t\tid: StrictObject<{ id: z.ZodType<GenericId<Table>, string> }>\n\t}\n}\n\n/** Defines one table and derives its storage, document, write, and DTO boundaries. */\nexport function zodTable<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tconst ServerFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst CommandFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst PublicFields extends readonly FieldKey<Fields & TimestampFields>[] = readonly [],\n>(\n\ttableName: Table,\n\tfields: (id: typeof zid) => Fields & OwnedFields<Fields>,\n\toptions: TableBoundaryOptions<Fields, ServerFields, CommandFields, PublicFields> = {},\n): TableBoundary<Table, Fields, ServerFields, CommandFields, PublicFields> {\n\tconst declared = fields(zid)\n\tassertOwnedFields(declared, ['_id', '_creationTime', ...TIMESTAMP_FIELDS])\n\tconst columns: Fields & TimestampFields = { ...declared, ...timestampFields }\n\tassertFieldLists(\n\t\tcolumns,\n\t\toptions.serverFields ?? [],\n\t\toptions.commandFields ?? [],\n\t\toptions.publicFields ?? [],\n\t)\n\tconst storage: z.ZodObject<Fields & TimestampFields, z.core.$strict> = z.object(columns).strict()\n\tconst schema = documentSchema(storage, tableName)\n\tconst omittedKeys: readonly (ServerFields[number] | (typeof TIMESTAMP_FIELDS)[number])[] = [\n\t\t...TIMESTAMP_FIELDS,\n\t\t...(options.serverFields ?? []),\n\t]\n\tconst commandKeys: readonly CommandFields[number][] = options.commandFields ?? []\n\tconst publicKeys: readonly PublicFields[number][] = options.publicFields ?? []\n\tconst insertSchema: z.ZodObject<WriteFields<Fields, ServerFields>, z.core.$strict> = z\n\t\t.object(omitFields(columns, omittedKeys))\n\t\t.strict()\n\tconst updateSchema = insertSchema.partial()\n\tconst commandInput: z.ZodObject<\n\t\tPick<Fields & TimestampFields, CommandFields[number]>,\n\t\tz.core.$strict\n\t> = z.object(pickFields(columns, commandKeys)).strict()\n\tconst publicDto: z.ZodObject<\n\t\tPick<Fields & TimestampFields, PublicFields[number]>,\n\t\tz.core.$strict\n\t> = z.object(pickFields(columns, publicKeys)).strict()\n\ttype Public = Pick<Fields & TimestampFields, PublicFields[number]>\n\ttype Row = z.input<typeof storage>\n\ttype RuntimeDtoFields = Public & FieldSchemas\n\ttype RuntimeDto = z.output<StrictObject<RuntimeDtoFields>> & { _id?: GenericId<Table> }\n\ttype RuntimeOptions = { _id?: boolean; extend?: FieldSchemas; values?: unknown }\n\tconst reservedDtoFields = new Set<string>([\n\t\t'_id',\n\t\t'_creationTime',\n\t\t'tenant',\n\t\t'__proto__',\n\t\t...TIMESTAMP_FIELDS,\n\t])\n\n\tfunction dto(): typeof publicDto\n\tfunction dto<const IncludeId extends boolean>(\n\t\tincludeId: IncludeId,\n\t): DtoSchema<Table, Public, IncludeId>\n\tfunction dto<const IncludeId extends boolean = false>(\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): DtoSchema<Table, Public, IncludeId>\n\tfunction dto<const IncludeId extends boolean = false, const Extension extends FieldSchemas = {}>(\n\t\toptions: DtoOptions<IncludeId, Extension> & { extend?: DtoExtension<Public, Extension> },\n\t): DtoSchema<Table, Public, IncludeId, Extension>\n\tfunction dto(options?: boolean | RuntimeOptions) {\n\t\treturn deriveDto(options)\n\t}\n\n\tfunction deriveDto(options?: boolean | RuntimeOptions) {\n\t\tconst normalized = options === true || options === false ? { _id: options } : options\n\t\tconst extension = normalized?.extend ?? {}\n\t\tfor (const key of Object.keys(extension)) {\n\t\t\tif (reservedDtoFields.has(key) || Object.hasOwn(publicDto.shape, key)) {\n\t\t\t\tthrow new Error(`DTO extension collision for field \"${key}\" on table \"${tableName}\"`)\n\t\t\t}\n\t\t\tassertDtoValidationSchema(extension[key])\n\t\t}\n\t\tif (!normalized?._id && Object.keys(extension).length === 0) return publicDto\n\t\tconst resultSchema = z.object({ ...publicDto.shape, ...extension }).strict()\n\t\treturn normalized?._id ? resultSchema.extend({ _id: schema.shape._id }) : resultSchema\n\t}\n\n\tfunction toPublicDto(row: Row): z.output<typeof publicDto>\n\tfunction toPublicDto<const IncludeId extends boolean>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\tincludeId: IncludeId,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\tfunction toPublicDto<const IncludeId extends boolean = false>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\toptions: DtoProjectionOptions<IncludeId>,\n\t): z.output<DtoSchema<Table, Public, IncludeId>>\n\tfunction toPublicDto<\n\t\tconst IncludeId extends boolean = false,\n\t\tconst Extension extends FieldSchemas = {},\n\t>(\n\t\trow: DtoRow<Row, IncludeId>,\n\t\toptions: DtoOptions<IncludeId, Extension> & {\n\t\t\textend?: DtoExtension<Public, Extension>\n\t\t} & DtoValues<Extension>,\n\t): z.output<DtoSchema<Table, Public, IncludeId, Extension>>\n\tfunction toPublicDto(row: Row, options?: boolean | RuntimeOptions): RuntimeDto {\n\t\tconst normalized = options === true || options === false ? { _id: options } : options\n\t\t// Enforce extension collisions and validation-only schemas before parsing values.\n\t\tderiveDto(options)\n\t\tconst projected = publicDto.parse(\n\t\t\tObject.fromEntries(\n\t\t\t\tObject.keys(publicDto.shape).map((key) => {\n\t\t\t\t\t// SAFETY: publicDto contains only keys selected from the storage fields.\n\t\t\t\t\treturn [key, row[key as keyof typeof row]]\n\t\t\t\t}),\n\t\t\t),\n\t\t)\n\t\tconst extension = z\n\t\t\t.object(normalized?.extend ?? {})\n\t\t\t.strict()\n\t\t\t.parse(normalized?.values ?? {})\n\t\tconst result: RuntimeDto = { ...projected, ...extension }\n\t\tif (normalized?._id) {\n\t\t\tresult._id = schema.shape._id.parse('_id' in row ? row._id : undefined)\n\t\t}\n\t\treturn result\n\t}\n\n\ttype InsertSchema = typeof insertSchema\n\ttype UpdateSchema = typeof updateSchema\n\ttype InsertMask = z.util.Mask<keyof InsertSchema['shape']>\n\ttype UpdateMask = z.util.Mask<keyof UpdateSchema['shape']>\n\n\tfunction insert(): InsertSchema\n\tfunction insert<OmitMask extends InsertMask>(\n\t\tomit: OmitMask,\n\t): ZodObjectOmit<InsertSchema, OmitMask>\n\tfunction insert(omit?: InsertMask) {\n\t\t// SAFETY: InsertMask contains only insertSchema keys; the overload retains Zod's exact mask relationship.\n\t\treturn omit ? insertSchema.omit(omit as never) : insertSchema\n\t}\n\n\tfunction update(): UpdateSchema\n\tfunction update<OmitMask extends UpdateMask>(\n\t\tomit: OmitMask,\n\t): ZodObjectOmit<UpdateSchema, OmitMask>\n\tfunction update(omit?: UpdateMask) {\n\t\t// SAFETY: UpdateMask contains only updateSchema keys; the overload retains Zod's exact mask relationship.\n\t\treturn omit ? updateSchema.omit(omit as never) : updateSchema\n\t}\n\n\tconst nativeStorage = zodToConvex(storage)\n\tconst table: TableDefinition<ConvexValidatorFromZod<typeof storage, 'required'>> =\n\t\tdefineTable(nativeStorage)\n\ttype EntityFields = Fields & TimestampFields & SystemFields<Table>\n\tfunction paginationOpts<const Selection extends ScalarSelection<EntityFields>>(\n\t\tselection: ExactScalarSelection<EntityFields, Selection>,\n\t): ScalarPaginationSchema<EntityFields, Selection> {\n\t\treturn scalarPaginationSchema<EntityFields, Selection>(zodToConvex(schema).fields, selection)\n\t}\n\treturn {\n\t\ttableName,\n\t\tschema,\n\t\tstorage,\n\t\tinsertSchema,\n\t\tupdateSchema,\n\t\tcommandInput,\n\t\tpublicDto,\n\t\tdto,\n\t\ttoPublicDto,\n\t\tpaginationOpts,\n\t\ttable,\n\t\tinsert,\n\t\tupdate,\n\t\ttools: {\n\t\t\t// jsonSafeZid (not zid): tool masks feed generated JSON schemas, so\n\t\t\t// ids must present as plain strings while keeping the Id<...> type.\n\t\t\tinsert: commandInput,\n\t\t\tupdate: z.object({ data: commandInput.partial(), id: jsonSafeZid(tableName) }).strict(),\n\t\t\tid: z.object({ id: jsonSafeZid(tableName) }).strict(),\n\t\t},\n\t}\n}\n\nconst tenantFields = {\n\ttenant: z.string().min(1),\n} satisfies FieldSchemas\n\ntype TenantFields = typeof tenantFields\n\n/**\n * A zodTable whose rows are tenant-owned: injects the server-owned `tenant`\n * field (the tenancy boundary — see cvx-kit/tenancy). Like timestamps, it is\n * excluded from insert/update/command boundaries; handlers stamp it from\n * ctx.tenant, row-level security matches on it, and the tenantOwnership\n * trigger forbids reassigning it. Expose it via publicFields only when a DTO\n * genuinely needs it.\n */\nexport function tenantTable<\n\tTable extends string,\n\tFields extends FieldSchemas,\n\tconst ServerFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst CommandFields extends readonly FieldKey<Fields>[] = readonly [],\n\tconst PublicFields extends readonly FieldKey<Fields & TenantFields & TimestampFields>[] =\n\t\treadonly [],\n>(\n\ttableName: Table,\n\tfields: (\n\t\tid: typeof zid,\n\t) => Fields &\n\t\tOwnedFields<Fields> & { [Key in keyof Fields]: Key extends 'tenant' ? never : Fields[Key] },\n\toptions: TableBoundaryOptions<\n\t\tFields & TenantFields,\n\t\treadonly (ServerFields[number] | 'tenant')[],\n\t\tCommandFields,\n\t\tPublicFields\n\t> = {},\n) {\n\treturn zodTable<\n\t\tTable,\n\t\tFields & TenantFields,\n\t\treadonly (ServerFields[number] | 'tenant')[],\n\t\tCommandFields,\n\t\tPublicFields\n\t>(\n\t\ttableName,\n\t\t(id) => {\n\t\t\tconst declared = fields(id)\n\t\t\tassertOwnedFields(declared, ['tenant', '_id', '_creationTime', ...TIMESTAMP_FIELDS])\n\t\t\t// SAFETY: reserved keys were rejected before injecting tenant; the generic\n\t\t\t// wrapper forwards the same checked fields to the ordinary table constructor.\n\t\t\treturn { ...declared, ...tenantFields } as Fields &\n\t\t\t\tTenantFields &\n\t\t\t\tOwnedFields<Fields & TenantFields>\n\t\t},\n\t\t{\n\t\t\t...options,\n\t\t\tserverFields: [...(options.serverFields ?? []), 'tenant'],\n\t\t},\n\t)\n}\n\n/**\n * Merges per-module table maps into the application schema map, rejecting\n * duplicate table names across modules — the module-registry combinator for\n * domain/table.ts: `defineSchema(createModule(catalogTables, salesTables))`.\n */\nexport function createModule<const Maps extends readonly Record<string, TableDefinition<any>>[]>(\n\t...maps: Maps\n): UnionToIntersection<Maps[number]> {\n\tconst combined: Record<string, TableDefinition<any>> = {}\n\tfor (const map of maps) {\n\t\tfor (const [tableName, definition] of Object.entries(map)) {\n\t\t\tif (tableName in combined) {\n\t\t\t\tthrow new Error(`Table \"${tableName}\" is declared by more than one module`)\n\t\t\t}\n\t\t\tcombined[tableName] = definition\n\t\t}\n\t}\n\t// SAFETY: every input entry is copied, and duplicate keys are rejected above.\n\treturn combined as UnionToIntersection<Maps[number]>\n}\n\ntype UnionToIntersection<Union> = (\n\tUnion extends unknown ? (member: Union) => void : never\n) extends (member: infer Intersection) => void\n\t? Intersection\n\t: never\n\n/**\n * Cursor-pagination boundary schemas bridged from Convex's own validators:\n * `args.paginationOpts` for function args, `result` for the page shape.\n * Pair with `include(...).paginate(opts)` (cvx-kit/auth). The result object\n * is deliberately non-strict — Convex may attach engine fields (splitCursor,\n * pageStatus); the page items themselves are validated against the dto.\n * Note: under row-level security, pages may be SHORTER than numItems when\n * rules reject rows mid-page; cursors remain correct.\n */\nexport function paginated<Dto extends z.ZodType>(dto: Dto) {\n\treturn {\n\t\targs: { paginationOpts: convexToZod(paginationOptsValidator) },\n\t\tresult: z.object({\n\t\t\tpage: z.array(dto),\n\t\t\tisDone: z.boolean(),\n\t\t\tcontinueCursor: z.string(),\n\t\t\tsplitCursor: z.string().nullable().optional(),\n\t\t\tpageStatus: z.enum(['SplitRecommended', 'SplitRequired']).nullable().optional(),\n\t\t}),\n\t}\n}\n\ntype VariantSchema = z.ZodDiscriminatedUnion<readonly StrictObject<FieldSchemas>[], string>\ntype VariantDiscriminator<Schema extends VariantSchema> = Schema['_zod']['def']['discriminator']\ntype VariantTags<Schema extends VariantSchema> = Extract<\n\tz.output<Schema>[VariantDiscriminator<Schema>],\n\tstring\n>\ntype VariantBranch<Schema extends VariantSchema, Tag extends string> = Extract<\n\tSchema['options'][number],\n\tz.ZodObject<{ [Key in VariantDiscriminator<Schema>]: z.ZodLiteral<Tag> }, z.core.$strict>\n>\ntype VariantKeys<Schema extends VariantSchema, Tag extends string> = FieldKey<\n\tVariantBranch<Schema, Tag>['shape']\n>\n\n/** Boundary mode supports strict object branches with one string literal tag each. */\nexport type VariantBoundaryOptions<Schema extends VariantSchema> = {\n\tdiscriminator: VariantDiscriminator<Schema>\n\ttimestamps?: boolean\n\tvariants: {\n\t\t[Tag in VariantTags<Schema>]: {\n\t\t\tserverFields?: readonly Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>[]\n\t\t\tcommandFields?: readonly Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>[]\n\t\t\tpublicFields?: readonly (\n\t\t\t\t| Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>\n\t\t\t\t| keyof TimestampFields\n\t\t\t)[]\n\t\t}\n\t}\n}\ntype VariantMaskKeys<Config, Mask extends string> = Config extends {\n\t[Key in Mask]?: readonly (infer Field)[]\n}\n\t? Extract<Field, string>\n\t: never\ntype CheckedVariantOptions<\n\tSchema extends VariantSchema,\n\tOptions extends VariantBoundaryOptions<Schema>,\n> = {\n\tvariants: {\n\t\t[Tag in keyof Options['variants']]: Tag extends VariantTags<Schema>\n\t\t\t? {\n\t\t\t\t\tcommandFields?: readonly Exclude<\n\t\t\t\t\t\tVariantKeys<Schema, Tag>,\n\t\t\t\t\t\tVariantDiscriminator<Schema> | VariantMaskKeys<Options['variants'][Tag], 'serverFields'>\n\t\t\t\t\t>[]\n\t\t\t\t\tpublicFields?: readonly (\n\t\t\t\t\t\t| Exclude<VariantKeys<Schema, Tag>, VariantDiscriminator<Schema>>\n\t\t\t\t\t\t| (Options extends { timestamps: true } ? keyof TimestampFields : never)\n\t\t\t\t\t)[]\n\t\t\t\t}\n\t\t\t: never\n\t}\n}\ntype VariantMode =\n\t| 'storage'\n\t| 'document'\n\t| 'insert'\n\t| 'update'\n\t| 'command'\n\t| 'commandUpdate'\n\t| 'public'\ntype VariantFields<\n\tTable extends string,\n\tBranch extends StrictObject<FieldSchemas>,\n\tDiscriminator extends string,\n\tConfig,\n\tTimestamps extends boolean,\n\tMode extends VariantMode,\n> = Mode extends 'storage'\n\t? Branch['shape'] & (true extends Timestamps ? TimestampFields : {})\n\t: Mode extends 'document'\n\t\t? VariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'> &\n\t\t\t\tSystemFields<Table>\n\t\t: Mode extends 'public' | 'command'\n\t\t\t? Pick<\n\t\t\t\t\tVariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'>,\n\t\t\t\t\tExtract<\n\t\t\t\t\t\t| Discriminator\n\t\t\t\t\t\t| VariantMaskKeys<Config, Mode extends 'public' ? 'publicFields' : 'commandFields'>,\n\t\t\t\t\t\tkeyof VariantFields<Table, Branch, Discriminator, Config, Timestamps, 'storage'>\n\t\t\t\t\t>\n\t\t\t\t>\n\t\t\t: Mode extends 'insert'\n\t\t\t\t? Omit<Branch['shape'], VariantMaskKeys<Config, 'serverFields'>>\n\t\t\t\t: PartialFields<\n\t\t\t\t\t\tOmit<\n\t\t\t\t\t\t\tVariantFields<\n\t\t\t\t\t\t\t\tTable,\n\t\t\t\t\t\t\t\tBranch,\n\t\t\t\t\t\t\t\tDiscriminator,\n\t\t\t\t\t\t\t\tConfig,\n\t\t\t\t\t\t\t\tTimestamps,\n\t\t\t\t\t\t\t\tMode extends 'update' ? 'insert' : 'command'\n\t\t\t\t\t\t\t>,\n\t\t\t\t\t\t\tDiscriminator\n\t\t\t\t\t\t>\n\t\t\t\t\t> &\n\t\t\t\t\t\tPick<Branch['shape'], Extract<Discriminator, keyof Branch['shape']>>\ntype DerivedVariantBranches<\n\tTable extends string,\n\tSchema extends VariantSchema,\n\tOptions extends VariantBoundaryOptions<Schema>,\n\tMode extends VariantMode,\n\tIncludeId extends boolean,\n\tBranches extends readonly StrictObject<FieldSchemas>[],\n> = {\n\t[Index in keyof Branches]: Branches[Index] extends StrictObject<FieldSchemas>\n\t\t? StrictObject<\n\t\t\t\tVariantFields<\n\t\t\t\t\tTable,\n\t\t\t\t\tBranches[Index],\n\t\t\t\t\tVariantDiscriminator<Schema>,\n\t\t\t\t\tOptions['variants'][Extract<\n\t\t\t\t\t\tz.output<Branches[Index]>[VariantDiscriminator<Schema>],\n\t\t\t\t\t\tkeyof Options['variants']\n\t\t\t\t\t>],\n\t\t\t\t\tOptions extends { timestamps: infer Enabled }\n\t\t\t\t\t\t? true extends Enabled\n\t\t\t\t\t\t\t? true\n\t\t\t\t\t\t\t: false\n\t\t\t\t\t\t: false,\n\t\t\t\t\tMode\n\t\t\t\t> &\n\t\t\t\t\t(IncludeId extends true ? { _id: Zid<Table> } : {})\n\t\t\t>\n\t\t: never\n}\ntype DerivedVariant<\n\tTable extends string,\n\tSchema extends VariantSchema,\n\tOptions extends VariantBoundaryOptions<Schema>,\n\tMode extends VariantMode,\n\tIncludeId extends boolean = false,\n> = IncludeId extends true\n\t? z.ZodDiscriminatedUnion<\n\t\t\tDerivedVariantBranches<Table, Schema, Options, Mode, true, Schema['options']>,\n\t\t\tVariantDiscriminator<Schema>\n\t\t>\n\t: z.ZodDiscriminatedUnion<\n\t\t\tDerivedVariantBranches<Table, Schema, Options, Mode, false, Schema['options']>,\n\t\t\tVariantDiscriminator<Schema>\n\t\t>\n\nexport type VariantTableBoundary<\n\tTable extends string,\n\tSchema extends VariantSchema,\n\tOptions extends VariantBoundaryOptions<Schema>,\n> = {\n\ttableName: Table\n\tstorage: DerivedVariant<Table, Schema, Options, 'storage'>\n\tschema: DerivedVariant<Table, Schema, Options, 'document'>\n\tinsertSchema: DerivedVariant<Table, Schema, Options, 'insert'>\n\tupdateSchema: DerivedVariant<Table, Schema, Options, 'update'>\n\tcommandInput: DerivedVariant<Table, Schema, Options, 'command'>\n\tpublicDto: DerivedVariant<Table, Schema, Options, 'public'>\n\ttable: TableDefinition<\n\t\tConvexValidatorFromZod<DerivedVariant<Table, Schema, Options, 'storage'>, 'required'>\n\t>\n\tdto(): DerivedVariant<Table, Schema, Options, 'public'>\n\tdto<const IncludeId extends boolean>(\n\t\tincludeId: IncludeId,\n\t): DerivedVariant<Table, Schema, Options, 'public', IncludeId>\n\ttoPublicDto(\n\t\trow: z.input<DerivedVariant<Table, Schema, Options, 'storage'>>,\n\t): z.output<DerivedVariant<Table, Schema, Options, 'public'>>\n\ttoPublicDto<const IncludeId extends boolean>(\n\t\trow: DtoRow<z.input<DerivedVariant<Table, Schema, Options, 'storage'>>, IncludeId>,\n\t\tincludeId: IncludeId,\n\t): z.output<DerivedVariant<Table, Schema, Options, 'public', IncludeId>>\n\ttools: {\n\t\tid: StrictObject<{ id: z.ZodType<GenericId<Table>, string> }>\n\t\tinsert: z.ZodType<\n\t\t\tz.output<DerivedVariant<Table, Schema, Options, 'command'>>,\n\t\t\tz.input<DerivedVariant<Table, Schema, Options, 'command'>>\n\t\t>\n\t\tupdate: StrictObject<{\n\t\t\tid: z.ZodType<GenericId<Table>, string>\n\t\t\tdata: z.ZodType<\n\t\t\t\tz.output<DerivedVariant<Table, Schema, Options, 'commandUpdate'>>,\n\t\t\t\tz.input<DerivedVariant<Table, Schema, Options, 'commandUpdate'>>\n\t\t\t>\n\t\t}>\n\t}\n}\n\n/** Legacy mode preserves arbitrary storage; the third argument opts into derived boundaries. */\nexport function zodVariantTable<Table extends string, Schema extends z.ZodType>(\n\ttableName: Table,\n\tstorage: Schema,\n): {\n\ttableName: Table\n\tstorage: Schema\n\tinsertSchema: Schema\n\ttable: TableDefinition<ConvexValidatorFromZod<Schema, 'required'>>\n}\nexport function zodVariantTable<\n\tTable extends string,\n\tSchema extends VariantSchema,\n\tconst Options extends VariantBoundaryOptions<Schema>,\n>(\n\ttableName: Table,\n\tstorage: Schema,\n\toptions: Options & CheckedVariantOptions<Schema, NoInfer<Options>>,\n): VariantTableBoundary<Table, Schema, Options>\nexport function zodVariantTable(\n\ttableName: string,\n\tstorage: z.ZodType,\n\toptions?: {\n\t\tdiscriminator: string\n\t\ttimestamps?: boolean\n\t\tvariants: Record<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\tserverFields?: readonly string[]\n\t\t\t\tcommandFields?: readonly string[]\n\t\t\t\tpublicFields?: readonly string[]\n\t\t\t}\n\t\t>\n\t},\n) {\n\tif (options) return deriveVariantTable(tableName, storage, options)\n\treturn {\n\t\ttableName,\n\t\tstorage,\n\t\tinsertSchema: storage,\n\t\ttable: defineTable(zodToConvex(storage)),\n\t}\n}\n\n/**\n * A zid that presents as a plain string in generated JSON schemas so\n * LLM-tool inputs stay primitive, while keeping the Id type at compile time.\n */\nexport function jsonSafeZid<Table extends string>(\n\ttableName: Table,\n): z.ZodType<GenericId<Table>, string> {\n\tconst id = zid(tableName)\n\treturn z\n\t\t.string()\n\t\t.refine((value): value is GenericId<Table> => id.safeParse(value).success)\n\t\t.describe(`Convex document id for table \"${tableName}\"; validated against the table by Convex`)\n}\n\nfunction deriveVariantTable(\n\ttableName: string,\n\tdeclared: z.ZodType,\n\toptions: {\n\t\tdiscriminator: string\n\t\ttimestamps?: boolean\n\t\tvariants: Record<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\tserverFields?: readonly string[]\n\t\t\t\tcommandFields?: readonly string[]\n\t\t\t\tpublicFields?: readonly string[]\n\t\t\t}\n\t\t>\n\t},\n) {\n\tif (\n\t\t!(declared instanceof z.ZodDiscriminatedUnion) ||\n\t\tdeclared.def.discriminator !== options.discriminator\n\t)\n\t\tthrow new Error(\n\t\t\t'Variant boundaries require a discriminated union with the configured discriminator',\n\t\t)\n\tif (declared.def.checks?.length)\n\t\tthrow new Error('Variant boundaries do not support union-level refinements')\n\tconst discriminator = options.discriminator\n\tif (['_id', '_creationTime', ...TIMESTAMP_FIELDS].includes(discriminator))\n\t\tthrow new Error('Variant discriminator cannot be a reserved field')\n\tconst branches = new Map<\n\t\tstring,\n\t\t{\n\t\t\tstorage: StrictObject<FieldSchemas>\n\t\t\tdocument: StrictObject<FieldSchemas>\n\t\t\tinsert: StrictObject<FieldSchemas>\n\t\t\tupdate: StrictObject<FieldSchemas>\n\t\t\tcommand: StrictObject<FieldSchemas>\n\t\t\tcommandUpdate: StrictObject<FieldSchemas>\n\t\t\tpublic: StrictObject<FieldSchemas>\n\t\t}\n\t>()\n\tfor (const branch of declared.options) {\n\t\tif (!(branch instanceof z.ZodObject) || !(branch.def.catchall instanceof z.ZodNever))\n\t\t\tthrow new Error('Variant boundaries require strict object branches')\n\t\tif (branch.def.checks?.length)\n\t\t\tthrow new Error('Variant boundaries do not support object-level refinements')\n\t\ttry {\n\t\t\tassertDtoValidationSchema(branch, new Set(), true)\n\t\t} catch (cause) {\n\t\t\tthrow new Error(\n\t\t\t\t'Variant boundaries require supported value-preserving field validators without object catchalls',\n\t\t\t\t{ cause },\n\t\t\t)\n\t\t}\n\t\tconst tagSchema = branch.shape[discriminator]\n\t\tif (!(tagSchema instanceof z.ZodLiteral) || tagSchema.values.size !== 1)\n\t\t\tthrow new Error('Variant branches require one string literal discriminator')\n\t\tconst tag = [...tagSchema.values][0]\n\t\tif (!z.string().safeParse(tag).success)\n\t\t\tthrow new Error('Variant branches require a string discriminator')\n\t\t// SAFETY: a single literal value was checked as a string above.\n\t\tconst tagValue = tag as string\n\t\tif (branches.has(tagValue)) throw new Error(`Duplicate variant discriminator \"${tagValue}\"`)\n\t\tif (!Object.hasOwn(options.variants, tagValue))\n\t\t\tthrow new Error(`Missing variant configuration \"${tagValue}\"`)\n\t\tconst config = options.variants[tagValue]\n\t\tassertOwnedFields(branch.shape, ['_id', '_creationTime', ...TIMESTAMP_FIELDS])\n\t\tconst columns = { ...branch.shape } satisfies FieldSchemas\n\t\tif (options.timestamps) Object.assign(columns, timestampFields)\n\t\tassertFieldLists(\n\t\t\tcolumns,\n\t\t\tconfig.serverFields ?? [],\n\t\t\tconfig.commandFields ?? [],\n\t\t\tconfig.publicFields ?? [],\n\t\t)\n\t\tfor (const mask of [config.serverFields, config.commandFields, config.publicFields]) {\n\t\t\tif (mask?.includes(discriminator))\n\t\t\t\tthrow new Error(\n\t\t\t\t\t'The variant discriminator is retained automatically and cannot appear in masks',\n\t\t\t\t)\n\t\t}\n\t\tconst stored = z.strictObject(columns)\n\t\tconst insert = z.strictObject(\n\t\t\tomitFields(columns, [...TIMESTAMP_FIELDS, ...(config.serverFields ?? [])]),\n\t\t)\n\t\tconst command = z.strictObject(\n\t\t\tpickFields(columns, [discriminator, ...(config.commandFields ?? [])]),\n\t\t)\n\t\tconst partial = (schema: StrictObject<FieldSchemas>) =>\n\t\t\tschema.partial().extend({ [discriminator]: tagSchema })\n\t\tbranches.set(tagValue, {\n\t\t\tstorage: stored,\n\t\t\tdocument: documentSchema(stored, tableName),\n\t\t\tinsert,\n\t\t\tupdate: partial(insert),\n\t\t\tcommand,\n\t\t\tcommandUpdate: partial(command),\n\t\t\tpublic: z.strictObject(pickFields(columns, [discriminator, ...(config.publicFields ?? [])])),\n\t\t})\n\t}\n\tfor (const tag of Object.keys(options.variants)) {\n\t\tif (!branches.has(tag)) throw new Error(`Unknown variant configuration \"${tag}\"`)\n\t}\n\tfunction union(mode: VariantMode, includeId = false, tool = false) {\n\t\tconst schemas = [...branches.values()].map((branch) => {\n\t\t\tconst schema = branch[mode]\n\t\t\tif (tool)\n\t\t\t\treturn z.strictObject(\n\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\tObject.entries(schema.shape).map(([key, field]) => [key, variantToolField(field)]),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\treturn includeId ? schema.extend({ _id: zid(tableName) }) : schema\n\t\t})\n\t\tif (!schemas.length) throw new Error('Variant boundaries require at least one branch')\n\t\t// SAFETY: all entries are checked strict objects with a unique required literal tag; nonempty checked above.\n\t\treturn z.discriminatedUnion(\n\t\t\tdiscriminator,\n\t\t\tschemas as [StrictObject<FieldSchemas>, ...StrictObject<FieldSchemas>[]],\n\t\t)\n\t}\n\tconst storage = union('storage')\n\tconst schema = union('document')\n\tconst insertSchema = union('insert')\n\tconst updateSchema = union('update')\n\tconst commandInput = union('command')\n\tconst publicDto = union('public')\n\tfunction dto(includeId = false) {\n\t\treturn includeId ? union('public', true) : publicDto\n\t}\n\tfunction toPublicDto(row: z.input<StrictObject<FieldSchemas>>, includeId = false) {\n\t\tconst tag = z.string().parse(row[discriminator])\n\t\tconst branch = branches.get(tag)\n\t\tif (!branch) throw new Error(`Unknown variant discriminator \"${tag}\"`)\n\t\tconst projection = includeId ? branch.public.extend({ _id: zid(tableName) }) : branch.public\n\t\treturn projection.parse(\n\t\t\tObject.fromEntries(\n\t\t\t\tObject.keys(projection.shape)\n\t\t\t\t\t.filter((key) => Object.hasOwn(row, key))\n\t\t\t\t\t.map((key) => [key, row[key]]),\n\t\t\t),\n\t\t)\n\t}\n\treturn {\n\t\ttableName,\n\t\tstorage,\n\t\tschema,\n\t\tinsertSchema,\n\t\tupdateSchema,\n\t\tcommandInput,\n\t\tpublicDto,\n\t\tdto,\n\t\ttoPublicDto,\n\t\ttable: defineTable(zodToConvex(storage)),\n\t\ttools: {\n\t\t\tid: z.strictObject({ id: jsonSafeZid(tableName) }),\n\t\t\tinsert: union('command', false, true),\n\t\t\tupdate: z.strictObject({\n\t\t\t\tid: jsonSafeZid(tableName),\n\t\t\t\tdata: union('commandUpdate', false, true),\n\t\t\t}),\n\t\t},\n\t}\n}\n\nfunction variantToolField(schema: z.core.$ZodType): z.ZodType {\n\tif (!(schema instanceof z.ZodType))\n\t\tthrow new Error('Variant tools require classic Zod field schemas')\n\tif (\n\t\tschema instanceof z.ZodBigInt ||\n\t\t(schema instanceof z.ZodLiteral &&\n\t\t\t[...schema.values].some((value) => z.bigint().safeParse(value).success))\n\t)\n\t\tthrow new Error(\n\t\t\t'Variant tool command fields cannot contain bigint; JSON Schema cannot represent it',\n\t\t)\n\tconst native = zodToConvex(schema)\n\tif (native.kind === 'id') {\n\t\tconst id = jsonSafeZid(native.tableName)\n\t\tif (native.isOptional === 'optional')\n\t\t\treturn id.optional().refine((value) => schema.safeParse(value).success)\n\t\treturn id.refine((value) => schema.safeParse(value).success)\n\t}\n\tif (schema instanceof z.ZodOptional)\n\t\treturn schema.clone({ ...schema.def, innerType: variantToolField(schema.unwrap()) })\n\tif (schema instanceof z.ZodNullable)\n\t\treturn schema.clone({ ...schema.def, innerType: variantToolField(schema.unwrap()) })\n\tif (schema instanceof z.ZodNonOptional)\n\t\treturn schema.clone({ ...schema.def, innerType: variantToolField(schema.unwrap()) })\n\tif (schema instanceof z.ZodArray)\n\t\treturn schema.clone({ ...schema.def, element: variantToolField(schema.element) })\n\tif (schema instanceof z.ZodObject) {\n\t\tconst fields = Object.fromEntries(\n\t\t\tObject.entries(schema.shape).map(([key, child]) => [key, variantToolField(child)]),\n\t\t)\n\t\treturn schema.clone(Object.assign({}, schema.def, { ['shape']: fields }))\n\t}\n\tif (schema instanceof z.ZodUnion)\n\t\treturn schema.clone({ ...schema.def, options: schema.options.map(variantToolField) })\n\tif (schema instanceof z.ZodRecord)\n\t\treturn schema.clone({\n\t\t\t...schema.def,\n\t\t\t// SAFETY: conversion preserves the original record key output; IDs remain strings.\n\t\t\tkeyType: variantToolField(schema.keyType) as z.core.$ZodRecordKey,\n\t\t\tvalueType: variantToolField(schema.valueType),\n\t\t})\n\tif (schema instanceof z.ZodTuple)\n\t\treturn schema.clone({\n\t\t\t...schema.def,\n\t\t\titems: schema.def.items.map(variantToolField),\n\t\t\trest: schema.def.rest ? variantToolField(schema.def.rest) : null,\n\t\t})\n\tif (schema instanceof z.ZodIntersection)\n\t\treturn schema.clone({\n\t\t\t...schema.def,\n\t\t\tleft: variantToolField(schema.def.left),\n\t\t\tright: variantToolField(schema.def.right),\n\t\t})\n\treturn schema\n}\n\ntype ZodObjectOmit<\n\tSchema extends z.ZodObject<any, any>,\n\tOmitMask extends z.util.Mask<keyof Schema['shape']>,\n> =\n\tSchema extends z.ZodObject<infer ObjectFields, infer Config>\n\t\t? z.ZodObject<\n\t\t\t\tz.util.Flatten<Omit<ObjectFields, Extract<keyof ObjectFields, keyof OmitMask>>>,\n\t\t\t\tConfig\n\t\t\t>\n\t\t: never\n\nfunction documentSchema<Fields extends FieldSchemas, Table extends string>(\n\tstorage: StrictObject<Fields>,\n\ttableName: Table,\n) {\n\treturn storage.extend({ _id: zid(tableName), _creationTime: z.number() })\n}\n\nfunction pickFields<Fields extends FieldSchemas, const Keys extends readonly FieldKey<Fields>[]>(\n\tfields: Fields,\n\tkeys: Keys,\n) {\n\t// SAFETY: each entry uses a requested key and its original validator, preserving Pick.\n\treturn Object.fromEntries(keys.map((key) => [key, fields[key]])) as Pick<Fields, Keys[number]>\n}\n\nfunction omitFields<Fields extends FieldSchemas, const Keys extends readonly FieldKey<Fields>[]>(\n\tfields: Fields,\n\tkeys: Keys,\n) {\n\tconst omitted = new Set<string>(keys)\n\t// SAFETY: filtering removes exactly the supplied keys and keeps every remaining validator.\n\treturn Object.fromEntries(Object.entries(fields).filter(([key]) => !omitted.has(key))) as Omit<\n\t\tFields,\n\t\tKeys[number]\n\t>\n}\n\ntype DtoPropertiesCheck = z.core.$ZodCheck & {\n\treadonly _zod: {\n\t\treadonly def: { readonly check: 'properties' } & Record<\n\t\t\t'shape',\n\t\t\tRecord<string, z.core.$ZodType>\n\t\t>\n\t}\n}\n\n/** Inspect Zod 4's discriminated definitions, failing closed for unsupported nodes.\n * Used by DTO extensions and opt-in variant boundaries; legacy schemas are unchanged.\n */\nfunction assertDtoValidationSchema(\n\tschema: z.core.$ZodType,\n\tseen = new Set<z.core.$ZodType>(),\n\tallowNativeIds = false,\n): void {\n\tif (seen.has(schema)) return\n\tseen.add(schema)\n\t// SAFETY: Zod 4 exposes its built-in schema definitions as this discriminated union.\n\tconst def = (schema as z.core.$ZodTypes)._zod.def\n\tconst reject = () => {\n\t\tthrow new Error('DTO extensions require supported value-preserving validation schemas')\n\t}\n\tif ('coerce' in def && def.coerce) reject()\n\tif ('normalize' in def && def.normalize) reject()\n\tconst visit = (child: z.core.$ZodType) => assertDtoValidationSchema(child, seen, allowNativeIds)\n\tfor (const check of def.checks ?? []) {\n\t\tif (check._zod.def.check === 'overwrite') reject()\n\t\tif ('normalize' in check._zod.def && check._zod.def.normalize) reject()\n\t\tif (check._zod.def.check === 'property') {\n\t\t\t// SAFETY: the property discriminator owns this embedded schema in Zod 4.\n\t\t\tvisit((check as z.core.$ZodCheckProperty)._zod.def.schema)\n\t\t}\n\t\tif (check._zod.def.check === 'properties') {\n\t\t\t// SAFETY: the properties discriminator owns this schema shape in Zod 4.\n\t\t\tfor (const child of Object.values((check as DtoPropertiesCheck)._zod.def.shape)) {\n\t\t\t\tvisit(child)\n\t\t\t}\n\t\t}\n\t}\n\tswitch (def.type) {\n\t\tcase 'custom':\n\t\t\tif (allowNativeIds && zodToConvex(schema).kind === 'id') return\n\t\t\treject()\n\t\t\treturn\n\t\tcase 'string':\n\t\tcase 'number':\n\t\tcase 'boolean':\n\t\tcase 'bigint':\n\t\tcase 'date':\n\t\tcase 'symbol':\n\t\tcase 'null':\n\t\tcase 'undefined':\n\t\tcase 'void':\n\t\tcase 'never':\n\t\tcase 'literal':\n\t\tcase 'enum':\n\t\t\treturn\n\t\tcase 'object':\n\t\t\tif (allowNativeIds && def.catchall && def.catchall._zod.def.type !== 'never')\n\t\t\t\tthrow new Error('Variant fields cannot use object catchalls; use a record field instead')\n\t\t\tfor (const child of Object.values(def.shape)) visit(child)\n\t\t\tif (def.catchall) visit(def.catchall)\n\t\t\treturn\n\t\tcase 'array':\n\t\t\tvisit(def.element)\n\t\t\treturn\n\t\tcase 'optional':\n\t\tcase 'nullable':\n\t\tcase 'nonoptional':\n\t\t\tvisit(def.innerType)\n\t\t\treturn\n\t\tcase 'union':\n\t\t\tfor (const child of def.options) visit(child)\n\t\t\treturn\n\t\tcase 'intersection':\n\t\t\tvisit(def.left)\n\t\t\tvisit(def.right)\n\t\t\treturn\n\t\tcase 'tuple':\n\t\t\tfor (const child of def.items) visit(child)\n\t\t\tif (def.rest) visit(def.rest)\n\t\t\treturn\n\t\tcase 'record':\n\t\t\tvisit(def.keyType)\n\t\t\tvisit(def.valueType)\n\t\t\treturn\n\t\tdefault:\n\t\t\treject()\n\t}\n}\n\nfunction assertOwnedFields(fields: FieldSchemas, reserved: readonly string[]): void {\n\tfor (const key of reserved) {\n\t\tif (Object.hasOwn(fields, key))\n\t\t\tthrow new Error(`Reserved table field \"${key}\" cannot be declared`)\n\t}\n}\n\nfunction assertFieldLists(\n\tfields: FieldSchemas,\n\tserver: readonly string[],\n\tcommand: readonly string[],\n\tpublicKeys: readonly string[],\n): void {\n\tfor (const key of [...server, ...command, ...publicKeys]) {\n\t\tif (!Object.hasOwn(fields, key)) throw new Error(`Unknown table field \"${key}\"`)\n\t}\n\tconst managed = new Set<string>([...TIMESTAMP_FIELDS, ...server])\n\tfor (const key of command) {\n\t\tif (managed.has(key)) throw new Error(`Server-owned field \"${key}\" cannot be a command input`)\n\t}\n}\n"],"mappings":"4SA2BA,MAAMI,EAAkB,CACvB,UAAWC,EAAE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AG,MAAU,MAAM,wBAAwBA,EAAI,EAAE,EAEhF,IAAMgE,kBAAU,IAAI,IAAY,CAAC,GAAG1F,EAAkB,GAAGyF,CAAM,CAAC,EAChE,IAAK,IAAM/D,KAAOyC,EACjB,GAAIuB,EAAQ,IAAIhE,CAAG,EAAG,MAAU,MAAM,uBAAuBA,EAAI,4BAA4B,CAE/F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvx-kit",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
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
  "keywords": [
6
6
  "component",
@@ -18,6 +18,12 @@
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/ImRLopezAG/cvx-kit.git"
20
20
  },
21
+ "files": [
22
+ "dist",
23
+ "src/test.ts",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
21
27
  "type": "module",
22
28
  "sideEffects": false,
23
29
  "exports": {
@@ -139,6 +145,7 @@
139
145
  "test": "vp test run --exclude test/package-contents.test.mjs",
140
146
  "test:package": "bun run build && vp test run test/package-contents.test.mjs",
141
147
  "test:package:ids": "bun run build && node scripts/test-packed-ids.mjs",
148
+ "test:package:variants": "bun run build && node scripts/test-packed-variants.mjs",
142
149
  "test:package:oxlint": "bun run build && node scripts/test-packed-oxlint.mjs",
143
150
  "test:package:effect": "bun run build && node scripts/test-packed-effect.mjs",
144
151
  "test:package:auth": "bun run build && node scripts/test-packed-auth.mjs",
@@ -146,8 +153,8 @@
146
153
  "test:package:smoke:effect": "bun run build && node scripts/smoke-packed-effect.mjs",
147
154
  "test:package:smoke:bun": "bun scripts/smoke-packed-components.mjs bun",
148
155
  "test:package:smoke:npm": "node scripts/smoke-packed-components.mjs npm",
149
- "docs:sync": "rm -f docs/*.md && cp src/docs/*.md docs/",
150
- "prepublishOnly": "bun run build && bun run typecheck && bun run test && bun run docs:sync",
156
+ "skills:check": "node scripts/check-skills.mjs",
157
+ "prepublishOnly": "bun run build && bun run typecheck && bun run test && bun run skills:check",
151
158
  "build:codegen": "convex codegen --component-dir ./src/components/foundation && convex codegen --component-dir ./src/components/approvals && vp pack",
152
159
  "release": "npm version patch && git push --follow-tags",
153
160
  "release:minor": "npm version minor && git push --follow-tags"
package/AGENTS.md DELETED
@@ -1,47 +0,0 @@
1
- # Orchestration
2
-
3
- <!-- Orchestration -->
4
-
5
- Use T3 Code to orchestrate the agents the new orchestrator V2 allows you to orchestrate multiple models for working together, I have some recommendations for model selection and orchestration:
6
-
7
- ## Models
8
-
9
- | Provider | Heavy | Medium | Light |
10
- | -------- | ----------- | ----------- | ---------------------- |
11
- | OpenAI | gpt-6-astra | gpt-6.1-sol | gpt-6-luna |
12
- | Claude | flabel-5.1 | opus-5.5 | sonnet-5.5 / haiku-5.5 |
13
-
14
- Haiku and Luna are very special models for short tasks such as research file reader and those tasks that wont consume more than 100K tokens.
15
-
16
- ## Workflow
17
-
18
- I have a workflow for implementing features and working which contains some skills within the process they don't fallow an order but depending how deep we are on context we use it but those skills are:
19
-
20
- - **ce-explain**: This I used it to verify if the AI is aligned with the user's intent, then got a back and forth conversation with the user to explain the feature and the context of the feature including research on the libraries internet and packages.
21
- - **ce-plan**: This I used it to plan the feature implementation.
22
- - **ce-work**: This I used it to work on the feature implementation.
23
-
24
- These are goated skills that are used to implement features and work on them, but the real workflow for implementation itself is:
25
-
26
- | Planners | workers | low workers | researcher | reviewers |
27
- | ----------- | ----------- | ----------- | ---------------------- | ----------- |
28
- | gpt-6-astra | gpt-6.1-sol | gpt-6-luna | gpt-6-luna | gpt-6-astra |
29
- | flabel-5.1 | opus-5.5 | sonnet-5.5 | sonnet-5.5 / haiku-5.5 | N/A |
30
-
31
- Invoke **reviewers** for code review, implementation review, and security review on changed code. Use **gpt-6-astra** at **Low** effort for that role.
32
-
33
- After the planning and research session I sent the `ce-work` message to the main thread with the fallowing context:
34
- "Work on this feature into a goal with multiple agents and make sure you test every phase. And you do a code review and an implementation review as well. Make sure every phase is tested before go to the other one And commit it as well. Make a security review to make sure everything is working correctly. Use a browser or any test you need to make sure you cover the needed and everything is working correctly."
35
-
36
- with the fallowing effort:
37
-
38
- | Model | Recommended Effort |
39
- | ----------- | --------------------------------------------- |
40
- | gpt-6-astra | Medium / Low worker; Low reviewer |
41
- | flabel-5.1 | low |
42
- | opus-5.5 | low |
43
- | gpt-6-luna | low(fast) |
44
- | gpt-6.1-sol | medium(fast) |
45
- | sonnet-5.5 | medium |
46
-
47
- <!-- Orchestration -->