Skip to content

Hooks ​

trigger ​

typemethodsmultidetails
before, after, aroundcreate, patch, update, removeyessource

Options ​

The options of the trigger hook are of type: Subcription or Subcription[] or (context: HookContext) => Promisable<Subscription | Subscription[]>. But what is a Subcription?

A subscription is an object with the following properties:

PropertyDescription
serviceThe service to subscribe to.
To be used if you share trigger options between several services/methods.

Type: string | string[]
optional - Default: undefined
methodThe method to subscribe to.
To be used if you share trigger options between several services/methods.

Type: string | string[]
optional - Default: undefined
dataCheck the context.data for something. Uses sift.js under the hood.
On multi create, every item of context.data is checked on its own, just like on single create: the action only runs for the created items whose data matched. An item is mapped to the result by its id in context.data, otherwise by its position in context.result.
Type:
Record<string, any> | boolean | (item: T, context: HookContext) => Promisable<boolean | Record<string, any>>
optional - Default: true
resultCheck the context.result for something. Uses sift.js under the hood.
Type:
Record<string, any> | boolean | (change: { item: T; before: T | undefined }, context: HookContext) => Promisable<boolean | Record<string, any>>
optional - Default: true
paramsCheck the context.params for something. Uses sift.js under the hood.
Type:
Record<string, any> | boolean | (item: T, context: HookContext) => Promisable<boolean | Record<string, any>>
optional - Default: true
fetchBeforeThe trigger hook lets you compare the result against the item before. This is disabled by default for performance reasons. If you use before, it will fetch the items in the before hook automatically. If you don't need before but your result looks something like: ({ before }) => ({ publishedAt: { $gt: before.publishedAt } }) you need to set fetchBefore:true explicitely

As a function, it's called once per service call in the before hook, so context.result isn't available yet.

Type: boolean | (context: HookContext) => Promisable<boolean>
optional - Default: false
beforeCheck the before object from trigger before hook, if available. Uses sift.js under the hood.
Type:
Record<string, any> | boolean | (item: T, context: HookContext) => Promisable<boolean | Record<string, any>>
optional - Default: true
manipulateParamsYou can extend the params for the subscription, e.g. for population.
The items after the call are refetched by their id, so params.query doesn't have the filters of the call there. Query params your adapter needs to shape the items, like $eager, have to be added to params.query again. The query of the call is still in context.params.query.

Type: (params: Params, context: HookContext) => (Promisable<Params>)
optional - Default: undefined
actionThe action, that will be run, if all checks pass for the subscription.

Type: ({ before, item }, { context, items, subscription }) => Promisable<any>
Type (batchMode=true): (changes: [change: { before, item }, options: { context, items, subscription }][], context) => Promisable<any>
optional - Default: undefined
batchModeEnables batch mode. In batch mode: If multiple items are passed to create, it will only run action once with all items matching the conditions.

Type: boolean
optional - Default: false
isBlockingWhether the trigger hook should wait for the async action before continuing.

As a function, it's called once per service call in the before hook, so context.result isn't available yet.

Type: boolean | (context: HookContext) => Promisable<boolean>
optional - Default: true
debugLogs why the subscription runs or gets skipped, prefixed with [FEATHERS_TRIGGER DEBUG]. Helpful if an action doesn't run as expected.

As a function, it's called once per service call in the before hook, so context.result isn't available yet.

Type: boolean | (context: HookContext) => Promisable<boolean>
optional - Default: false

Action ​

The action from subscription is the function that runs, after all conditions are fulfilled. The function looks like the following:

js
const action = async ({ before, item }, { context, items, subscription }) => {
  // do your own implementation
};

// In batch mode

const action = async (changes) => {
  for (const [{ before, item }, { context, items, subscription }] of changes) {
    // do your own implementation
  }
};

changesById ​

typemethodsmultidetails
before, after, aroundcreate, patch, update, removeyessource

Options ​

PropertyDescription
paramsWith the params options, you can manipulate params for changesById if you need to, e.g. for population.
The items after the call are refetched by their id, so params.query doesn't have the filters of the call there. Query params your adapter needs to shape the items, like $eager, have to be added to params.query again. The query of the call is still in context.params.query.

Type: (params: Params, context: HookContext) => (Params | Promise<Params>)
optional - Default: undefined
skipHooksUse find or _find for refetching

Type: boolean
optional - Default: false

Released under the MIT License.