-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add relationData enhancer to serialize relation's value
- Loading branch information
1 parent
bb3859f
commit 9721856
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
packages/core/src/actions/context/enhancers/crud/relationData.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import context from '@foscia/core/actions/context/enhancers/context'; | ||
import serializeWith from '@foscia/core/actions/context/utils/serializeWith'; | ||
import makeEnhancersExtension from '@foscia/core/actions/extensions/makeEnhancersExtension'; | ||
import { Action, ActionParsedExtension, ConsumeSerializer } from '@foscia/core/actions/types'; | ||
import { ModelInstance, ModelRelationKey } from '@foscia/core/model/types'; | ||
|
||
/** | ||
* Serialize the given instance's relation as the context's data. | ||
* | ||
* @param instance | ||
* @param key | ||
* | ||
* @category Enhancers | ||
*/ | ||
export default function relationData<C extends {}, I extends ModelInstance, Record, Related, Data>( | ||
instance: I, | ||
key: ModelRelationKey<I>, | ||
) { | ||
return async ( | ||
action: Action<C & ConsumeSerializer<Record, Related, Data>>, | ||
) => action.use(context({ | ||
data: await serializeWith(action, async (serializer, ctx) => serializer.serialize( | ||
await serializer.serializeRelation(instance, instance.$model.$schema[key], ctx), | ||
ctx, | ||
)), | ||
})); | ||
} | ||
|
||
type EnhancerExtension = ActionParsedExtension<{ | ||
relationData<C extends {}, E extends {}, I extends ModelInstance, Record, Related, Data>( | ||
this: Action<C & ConsumeSerializer<Record, Related, Data>, E>, | ||
instance: I, | ||
key: ModelRelationKey<I>, | ||
): Action<C, E>; | ||
}>; | ||
|
||
relationData.extension = makeEnhancersExtension({ relationData }) as EnhancerExtension; |
This file contains 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
This file contains 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