mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 09:47:10 +00:00
feat: improve the params of the ctx.action
This commit is contained in:
parent
2b078a28be
commit
f0eb2eee20
@ -4,11 +4,11 @@ import { getRepositoryFromParams } from './utils';
|
||||
export async function destroy(ctx: Context, next) {
|
||||
const repository = getRepositoryFromParams(ctx);
|
||||
|
||||
const { resourceIndex, filter } = ctx.action.params;
|
||||
const { filterByTk, filter } = ctx.action.params;
|
||||
|
||||
const instance = await repository.destroy({
|
||||
filter,
|
||||
filterByTk: resourceIndex,
|
||||
filterByTk,
|
||||
context: ctx,
|
||||
});
|
||||
|
||||
|
@ -4,10 +4,10 @@ import { getRepositoryFromParams } from './utils';
|
||||
export async function get(ctx: Context, next) {
|
||||
const repository = getRepositoryFromParams(ctx);
|
||||
|
||||
const { resourceIndex, fields, appends, except, filter } = ctx.action.params;
|
||||
const { filterByTk, fields, appends, except, filter } = ctx.action.params;
|
||||
|
||||
const instance = await repository.findOne({
|
||||
filterByTk: resourceIndex,
|
||||
filterByTk,
|
||||
fields,
|
||||
appends,
|
||||
except,
|
||||
|
@ -3,10 +3,10 @@ import { getRepositoryFromParams } from './utils';
|
||||
|
||||
export async function update(ctx: Context, next) {
|
||||
const repository = getRepositoryFromParams(ctx);
|
||||
const { resourceIndex, values, whitelist, blacklist, filter, updateAssociationValues } = ctx.action.params;
|
||||
const { filterByTk, values, whitelist, blacklist, filter, updateAssociationValues } = ctx.action.params;
|
||||
|
||||
const instance = await repository.update({
|
||||
filterByTk: resourceIndex,
|
||||
filterByTk,
|
||||
values,
|
||||
whitelist,
|
||||
blacklist,
|
||||
|
@ -2,19 +2,13 @@ import { MultipleRelationRepository, Repository } from '@nocobase/database';
|
||||
import { Context } from '..';
|
||||
|
||||
export function getRepositoryFromParams(ctx: Context) {
|
||||
const { resourceName, associatedName, associatedIndex } = ctx.action.params;
|
||||
const { resourceName, resourceOf } = ctx.action;
|
||||
|
||||
let repository: MultipleRelationRepository | Repository;
|
||||
|
||||
if (associatedName) {
|
||||
repository = <MultipleRelationRepository>(
|
||||
ctx.db.getCollection(associatedName).repository.relation(resourceName).of(associatedIndex)
|
||||
);
|
||||
} else {
|
||||
repository = <Repository>ctx.db.getCollection(resourceName).repository;
|
||||
if (resourceOf) {
|
||||
return ctx.db.getRepository<MultipleRelationRepository>(resourceName, resourceOf);
|
||||
}
|
||||
|
||||
return repository;
|
||||
return ctx.db.getRepository<Repository>(resourceName);
|
||||
}
|
||||
|
||||
export function RelationRepositoryActionBuilder(method: 'remove' | 'set') {
|
||||
|
@ -128,7 +128,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
getRepository(name: string): Repository;
|
||||
getRepository<R extends Repository>(name: string): R;
|
||||
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
||||
|
||||
getRepository<R extends RelationRepository>(name: string, relationId?: string | number): Repository | R {
|
||||
|
@ -122,6 +122,7 @@ export interface ActionOptions {
|
||||
* - params 是开发配置参数 + 客户端参数的结合体
|
||||
*/
|
||||
export interface ActionParams {
|
||||
filterByTk?: any;
|
||||
/**
|
||||
* 输出哪些字段
|
||||
*
|
||||
@ -202,6 +203,10 @@ export class Action {
|
||||
|
||||
public params: ActionParams = {};
|
||||
|
||||
public actionName: string;
|
||||
public resourceName: string;
|
||||
public resourceOf: any;
|
||||
|
||||
public readonly middlewares: Array<Middleware> = [];
|
||||
|
||||
constructor(options: ActionOptions) {
|
||||
|
@ -305,15 +305,21 @@ export class Resourcer {
|
||||
// action 需要 clone 之后再赋给 ctx
|
||||
ctx.action = this.getAction(getNameByParams(params), params.actionName).clone();
|
||||
ctx.action.setContext(ctx);
|
||||
ctx.action.actionName = params.actionName;
|
||||
ctx.action.resourceOf = params.associatedIndex;
|
||||
ctx.action.resourceName = params.associatedName
|
||||
? `${params.associatedName}.${params.resourceName}`
|
||||
: params.resourceName;
|
||||
ctx.action.params.filterByTk = params.resourceIndex;
|
||||
const query = parseQuery(ctx.request.querystring);
|
||||
if (pathToRegexp('/resourcer/{:associatedName.}?:resourceName{\\::actionName}').test(ctx.request.path)) {
|
||||
await ctx.action.mergeParams({
|
||||
ctx.action.mergeParams({
|
||||
...query,
|
||||
...params,
|
||||
...ctx.request.body,
|
||||
});
|
||||
} else {
|
||||
await ctx.action.mergeParams({
|
||||
ctx.action.mergeParams({
|
||||
...query,
|
||||
...params,
|
||||
...(_.isEmpty(ctx.request.body) ? {} : { values: ctx.request.body }),
|
||||
|
Loading…
Reference in New Issue
Block a user