Hooks
trigger
before | after | methods | multi | details |
---|---|---|---|---|
yes | yes | create , patch , update , remove | yes | source |
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:
Property | Description |
---|---|
service | The service to subscribe to. To be used if you share trigger options between several services/methods. Type: string | string[] optional - Default: undefined |
method | The method to subscribe to. To be used if you share trigger options between several services/methods. Type: string | string[] optional - Default: undefined |
conditionsData | Check the context.data for something. Uses sift.js under the hood.Replaces all {{ placeholders }} with the sub.view object. For more information, look at the View section down below.Type: Record<string, any> | boolean optional - Default: true |
conditionsResult | Check the context.result for something. Uses sift.js under the hood.Replaces all {{ placeholders }} with the sub.view object. For more information, look at the View section down below.Type: Record<string, any> | boolean optional - Default: true |
conditionsParams | Check the context.params for something. Uses sift.js under the hood.Replaces all {{ placeholders }} with the sub.view object. For more information, look at the View section down below.Type: Record<string, any> | boolean optional - Default: true |
fetchBefore | The trigger hook lets you compare the result against the item before. This is disabled by default for performance reasons. If you use conditionsBefore , it will fetch the items in the before hook automatically. If you don't need conditionsBefore but your conditionsResult looks something like: { publishedAt: { $gt: '{{ before.publishedAt }}' } } you need to set fetchBefore:true explicitelyType: boolean optional - Default: false |
conditionsBefore | Check the before object from trigger before hook, if available. Uses sift.js under the hood.Replaces all {{ placeholders }} with the sub.view object. For more information, look at the View section down below.Type: Record<string, any> | boolean optional - Default: true |
view | The trigger function provides some view properties by default. See the View chapter down below. You can extend the view, if you need to.Type 1: Record<string, any> Type 2: (view, item, items, subscriptions, context) => Promisable<Record<string, any>> optional - Default: false |
params | You can extend the params for the subscription, e.g. for population.Type: (params: Params, context: HookContext) => (Promisable<Params>) optional - Default: undefined |
action | The action, that will be run, if all checks pass for the subscription. Type: ({ before, item }, { context, items, subscription, view }) => Promisable<any> Type (batchMode=true): (changes: [change: { before, item }, options: { context, items, subscription, view }][], context) => Promisable<any> optional - Default: undefined |
batchMode | Enables 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 |
isBlocking | Wether the trigger hook should wait for the async action before continuing.Type: boolean optional - Default: true |
anything else | You can add whatever property you need for a subscription. |
View
The concept of ''
is heavily inspired by mustache.js where the render
function takes a view
object. That's why it's called view
in feathers-trigger
. Views can be used to transform conditionsData
, conditionsBefore
, conditionsResult
and replace placeholder curly brackets strings with values. The trigger
hook is capable of more complex conditions where you need to compare the result
against data that is dynamic in the context
.
Properties of views for mustache-like transformations of conditions have the following by default:
item
: the result itembefore
: the item from thetrigger
before-hookdata
:context.data
(see feathers.js docs)id
:context.id
(see feathers.js docs)method
:context.method
(see feathers.js docs)now
:new Date()
(see feathers.js docs)params
:context.params
(see feathers.js docs)path
:context.path
(see feathers.js docs)service
:context.service
(see feathers.js docs)type
:context.type
(see feathers.js docs)user
:context.params.user
(see feathers.js docs)
You can extend the views by the views
option per subscription described above.
Action
The action
from subscription
is the function that runs, after all conditions
are fulfilled. The function looks like the following:
const action = async (
{ before, item },
{ context, items, subscription, view }
) => {
// do your own implementation
}
// In batch mode
const action = async (changes) => {
for (const [{ before, item }, { context, items, subscription, view }] of changes) {
// do your own implementation
}
}
changesById
before | after | methods | multi | details |
---|---|---|---|---|
yes | yes | create , patch , update , remove | yes | source |
Options
Property | Description |
---|---|
params | With the params options, you can manipulate params for changesById if you need to.Type: (params: Params, context: HookContext) => (Params | Promise<Params>) optional - Default: undefined |
skipHooks | Use find or _find for refetchingType: boolean optional - Default: false |