Skip to content
Paul Stovell edited this page Apr 17, 2020 · 7 revisions

Hooks provide a way for you to be notified when Nevermore is about to insert, update, or delete a document, or commit a transaction.

Hooks are useful when you need to handle some cross-cutting concerns:

  • To build an audit trail
  • To keep something else in sync when a document changes

You create a hook by implementing IHook. There are default implementations for each, so you only need to implement the method you want (see default interface methods).

For example, maybe certain documents should be "read-only" and cannot be edited.

class ReadOnlyHook : IHook 
{
    public void BeforeUpdate(object document, DocumentMap map, IWriteTransaction transaction)
    {
        if (document is IReadOnly)
            throw new Exception("This document is read-only!");
    }
}

You then register your hook with the store:

Configuration.HookRegistry.Register(new ReadOnlyHook());

Here are the methods you can implement:

public interface IHook
{
    void BeforeInsert(object document, DocumentMap map, IWriteTransaction transaction);
    void AfterInsert(object document, DocumentMap map, IWriteTransaction transaction);
    void BeforeUpdate(object document, DocumentMap map, IWriteTransaction transaction);
    void AfterUpdate(object document, DocumentMap map, IWriteTransaction transaction);
    void BeforeDelete(string id, DocumentMap map, IWriteTransaction transaction);
    void AfterDelete(string id, DocumentMap map, IWriteTransaction transaction);
    void BeforeCommit(IWriteTransaction transaction);
    void AfterCommit(IWriteTransaction transaction);

    Task BeforeInsertAsync(object document, DocumentMap map, IWriteTransaction transaction);
    Task AfterInsertAsync(object document, DocumentMap map, IWriteTransaction transaction);
    Task BeforeUpdateAsync(object document, DocumentMap map, IWriteTransaction transaction);
    Task AfterUpdateAsync(object document, DocumentMap map, IWriteTransaction transaction);
    Task BeforeDeleteAsync(string id, DocumentMap map, IWriteTransaction transaction);
    Task AfterDeleteAsync(string id, DocumentMap map, IWriteTransaction transaction);
    Task BeforeCommitAsync(IWriteTransaction transaction);
    Task AfterCommitAsync(IWriteTransaction transaction);
}
Clone this wiki locally