- Notifications
You must be signed in to change notification settings - Fork2
Add triggers & actions to your feathers app.
License
fratzinger/feathers-trigger
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NOTE: This is the version for Feathers v5. For Feathers v4 usefeathers-trigger v0
For more information, please have a look at the docs:https://feathers-trigger.netlify.app/
npm i feathers-trigger
Imagine you want to notify users when apost
gets published.
How can this be done? In this example apost
has apublishedAt
property which shows when apost
was published. Apost
withpublishedAt === null
means that thepost
is not published yet. Apost
can be created as a draft which is not published.
But how do you know when apost
gets published? Sounds silly, but there are three possibilities:
- A
post
gets created withpublishedAt: { $ne: null }
. - A
post
gets updated bydatapublishedAt: { $ne: null }
, it hadpublishedAt: null
before and theresult really haspublishedAt: { $ne: null }
. - A
post
gets patched bydatapublishedAt: { $ne: null }
, it hadpublishedAt: null
before and theresult really haspublishedAt: { $ne: null }
.
How can this be accomplished?
- Check
context.data
forpublishedAt: { $ne: null }
and if that's not true, the subscription is not true. - Check if the post in the database has
publishedAt === null
and therefore is not published yet. You need to check that in abefore
hook. If that's not true, the subscription is not true. - Check if the
context.result
really haspublishedAt: { $ne: null }
(maybe it's handled by another permission hook, or something). If that's not true, the subscription is not true. - If all three checks are true, run the
notify
function.
It's up to you how you define thenotify
action. For the example above the solution withfeathers-trigger
looks like the following:
// posts.hooks.jsimport{trigger}from"feathers-trigger";constnotifyPublished=trigger({data:{publishedAt:{$ne:null}},before:{publishedAt:null},result:{publishedAt:{$ne:null}},action:({ item},context)=>{returncontext.app.service("/notify").create(item);},});exportdefault{before:{create:[notifyPublished],update:[notifyPublished],patch:[notifyPublished],},after:{create:[notifyPublished],update:[notifyPublished],patch:[notifyPublished],},};
For more advanced examples, please have a look at thedocs
Simply runnpm test
and all your tests in thetest/
directory will be run. It has full support forVisual Studio Code. You can use the debugger to set breakpoints.
For more information on all the things you can do, visitFeathersJS.
Licensed under theMIT license.
About
Add triggers & actions to your feathers app.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.