-
-
Save JayBee007/291ce1536d34af64db60b14608e95e14 to your computer and use it in GitHub Desktop.
Example use of feathers-sequelize object hydration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const hydrate = require('feathers-sequelize/hooks/hydrate'); | |
| function includePoster() { | |
| return function (hook) { | |
| const model = hook.app.service('users').Model; | |
| const association = { include: [{ model: model, as: 'poster', attributes: ['userId', 'displayName', 'avatar'] }] }; | |
| switch (hook.type) { | |
| case 'before': | |
| hook.params.sequelize = Object.assign(association, { raw: false }); | |
| return Promise.resolve(hook); | |
| break; | |
| case 'after': | |
| hydrate( association ).call(this, hook); | |
| break; | |
| } | |
| } | |
| } | |
| module.exports = { | |
| before: { | |
| all: [ includePoster() ], | |
| ... | |
| }, | |
| after: { | |
| all: [ includePoster() ], | |
| ... | |
| }, | |
| error { | |
| ... | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment