Skip to content
Benoit Chesneau edited this page Feb 26, 2015 · 5 revisions

rcouch version 1.2.x introduces a new hook mechanism in preview. Each module can subscribe to events and a hook in the module code is called when the event occurs.

Note: in the version 1.2.x the mechanism is dedicated to database hooks but more will be added soon.

Example:

couch_users_db.erl is an example how the hook mechanisme can be used.

API

Functions to register the hooks:

couch_hooks:add(Hook, DbName, Module, Function, Priority)
couch_hooks:delete(Hook, DbName, Module, Function, Priority)
  • Hook = atom() : name of the event (see below).
  • DbName = string() | binary() |'*' : name of the database or the atom '*' for all.
  • Module = atom() : module to call. can be undefined
  • Function = atom() : function to call
  • Priority = integer() : determine in which order the hooks are run (when several hooks are defined for the same event). Useful if you want to chain some hooks.

Database Hooks

  • after_doc_read(Doc, Db) -> Doc2 : sent after a doc has been read from the filesystem
  • before_doc_update(Doc, Db) -> Doc2 : sent before updating the document. Useful to modify the document before updating it to the filesystem.
  • db_updated(DbName) -> ok: sent when a database is updated
  • ddoc_updated(DbName, DDocId) -> ok: when a design document is updated
  • db_created(DbName) -> ok: Sent when a database is created
  • db_deleted(DbName) -> ok: Sent when a database is deleted
  • db_compacted(DbName) -> ok: Sent when a database is compacted