tachybase_todo/packages/actions/src/middlewares/associated.ts

53 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Context, Next } from '../actions';
import { Action } from '@nocobase/resourcer';
import { HASONE, HASMANY, BELONGSTO, BELONGSTOMANY } from '@nocobase/database';
export async function associated(ctx: Context, next: Next) {
if (!(ctx.action instanceof Action)) {
return next();
}
const { associated, associatedName, associatedKey, resourceName } = ctx.action.params;
if (!associatedName || !associatedKey) {
return next();
}
if (associated) {
return next();
}
const Model = ctx.db.getModel(associatedName);
const field = ctx.db.getTable(associatedName).getField(resourceName);
let key: string;
switch (true) {
case field instanceof BELONGSTO:
// 如fields.collection对应的 API 为 /fields/119/collection此时 key 为 PK
key = Model.primaryKeyAttribute;
break;
case field instanceof HASONE:
case field instanceof HASMANY:
case field instanceof BELONGSTOMANY:
key = field.options.sourceKey;
break;
}
if (key) {
const model = await Model.findOne({
where: {
[key]: associatedKey,
}
});
if (model) {
ctx.action.setParam('associated', model);
ctx.action.setParam('resourceField', field);
}
}
await next();
}
export default associated;