UNPKG

feathers-sequelize

Version:
55 lines (46 loc) 1.67 kB
import type { HookContext } from '@feathersjs/feathers'; import type { ModelStatic, Model, Includeable } from 'sequelize'; import type { HydrateOptions } from '../declarations'; const factory = (Model: ModelStatic<Model>, include?: Includeable | Includeable[]) => { return (item: any) => { // (Darren): We have to check that the Model.Instance static property exists // first since it's been deprecated in Sequelize 4.x. // See: http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html const shouldBuild = !(item instanceof Model); if (shouldBuild) { return Model.build(item, { isNewRecord: false, include }); } return item; }; }; export const hydrate = <H extends HookContext = HookContext>(options?: HydrateOptions) => { options = options || {}; return (context: H) => { if (context.type !== 'after') { throw new Error('feathers-sequelize hydrate() - should only be used as an "after" hook'); } const makeInstance = factory(context.service.Model, options.include); switch (context.method) { case 'find': if (context.result.data) { context.result.data = context.result.data.map(makeInstance); } else { context.result = context.result.map(makeInstance); } break; case 'get': case 'update': context.result = makeInstance(context.result); break; case 'create': case 'patch': if (Array.isArray(context.result)) { context.result = context.result.map(makeInstance); } else { context.result = makeInstance(context.result); } break; } return Promise.resolve(context); }; };