Hooks

Hooks support the insertion of specific contextual operations at specific times when a query is executed. This article describes the hook functions supported by Leoric and how to use them

Table of Contents

  1. Declaring
  2. Available Hooks:
    1. create
    2. bulkCreate
    3. update
    4. save
    5. upsert
    6. remove
  3. Table of hooks

Declaring

You can declare Hook in the following way :

// class syntax
class User extends Bone {
  static beforeCreate() {}
  static afterUpdate() {}
});

// define
Realm.define('User', attrs, {
  hooks: {
    beforeCreate() {},
    afterUpdate() {},
  }
});

Available Hooks:

Model.method means hook call by Model class, instance.method means hook call by Model’s instance, the arguments of hooks are slightly different for different calling methods

create

create supports:

// create hooks, args are create function's arguments
Model.beforeCreate(args) // function's context is the instance to be created
Model.afterCreate(instance, createResult) // createResult is create function's returns
instance.beforeCreate(args)
instance.afterCreate(instance, createResult) // function's context is the instance to be created

Please be noted that the function context of the hooks of create are the instance to be created.

bulkCreate

bulkCreate supports two types of hooks:

// bulkCreate hooks
Model.beforeBulkCreate(records, queryOptions) // function's context is the Model
Model.afterBulkCreate(instances, Model) // the argument 'instances' are instances to be created

update

update supports:

// update hooks
Model.beforeUpdate(args) // function's context is the `Model`
Model.afterUpdate(updateResult, Model)
instance.beforeUpdate(args) // function's context is the instance to be created
instance.afterUpdate(instance, updateResult) // function's context is the instance to be created, 'updateResult' is update function's returns.

save

save supports:

instance.beforeSave(options)
instance.afterSave(instance, options)

Note that calling save may trigger the other functions’ hooks (such as create, update or upsert).

upsert

upsert supports:

instance.beforeUpsert(opts) // function's context is the instance
instance.afterUpsert(instance, upsertResult)

remove

remove supports:

Model.beforeRemove(args)
Model.afterRemove(removeResult, Model)
instance.beforeRemove(args)
instance.afterRemove(instance, removeResult)

Table of hooks

// create hooks
Model.beforeCreate(args)
Model.afterCreate(instance, createResult)
instance.beforeCreate(args)
instance.afterCreate(instance, createResult)

// bulkCreate hooks
Model.beforeBulkCreate(records, queryOptions)
Model.afterBulkCreate(instances, Model)

// update hooks
Model.beforeUpdate(args)
Model.afterUpdate(updateResult, Model)
instance.beforeUpdate(args)
instance.afterUpdate(instance, updateResult)

// save hooks
instance.beforeSave(options)
instance.afterSave(instance, options)

// upsert hooks
instance.beforeUpsert(opts)
instance.afterUpsert(instance, upsertResult)

// remove hooks
Model.beforeRemove(args)
Model.afterRemove(removeResult, Model)
instance.beforeRemove(args)
instance.afterRemove(instance, removeResult)