Skip to content

CollectionHooksInput

Defined in: packages/core/src/collections/hooks.ts:91

Lifecycle hooks for a collection. beforeChange/beforeDelete run inline in the write path and may reject a write by throwing. afterChange/ afterDelete run via convex-helpers triggers, after the write commits, through the mutation builder returned by createVexMutations — they never run for a write made through the raw _generated/server builder, the Convex dashboard, or npx convex import.

Each method is generic over TDataModel (not fixed to the interface’s own type params) so a dispatch call site — collection.hooks.beforeChange({ ctx: realCtx, ... }) — infers TDataModel fresh from the real ctx it passes in, with no cast: GenericMutationCtx<ConcreteDataModel> is otherwise not assignable to GenericMutationCtx<GenericDataModel> at all (Convex’s index-query builder is invariant across DataModel instantiations), the same reason validateFields is generic over TDataModel rather than fixed. Author a hook inline (contextual typing — hooks: { beforeChange: async ({ doc, ctx }) => ... }) or via beforeChangeHook/beforeDeleteHook/afterChangeHook/afterDeleteHook; an extracted, explicitly-BeforeChangeProps<...>-annotated function is the one authoring style this does NOT accept (its own ctx type is pinned to GenericDataModel, not generic per assignment).

TCollectionSlug extends CollectionSlug = CollectionSlug

optional afterChange<TDataModel>(props): void | Promise<void>

Defined in: packages/core/src/collections/hooks.ts:98

TDataModel extends GenericDataModel = GenericDataModel

AfterChangeProps<TCollectionSlug, TDataModel>

void | Promise<void>


optional afterDelete<TDataModel>(props): void | Promise<void>

Defined in: packages/core/src/collections/hooks.ts:101

TDataModel extends GenericDataModel = GenericDataModel

AfterDeleteProps<TCollectionSlug, TDataModel>

void | Promise<void>


optional beforeChange<TDataModel>(props): TDocument | Promise<TDocument>

Defined in: packages/core/src/collections/hooks.ts:92

TDataModel extends GenericDataModel = GenericDataModel

BeforeChangeProps<TCollectionSlug, TDataModel>

TDocument | Promise<TDocument>


optional beforeDelete<TDataModel>(props): void | Promise<void>

Defined in: packages/core/src/collections/hooks.ts:95

TDataModel extends GenericDataModel = GenericDataModel

BeforeDeleteProps<TCollectionSlug, TDataModel>

void | Promise<void>