diff --git a/packages/actions/src/actions/destroy.ts b/packages/actions/src/actions/destroy.ts index f5cf1e3485..09a9ac7f15 100644 --- a/packages/actions/src/actions/destroy.ts +++ b/packages/actions/src/actions/destroy.ts @@ -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, }); diff --git a/packages/actions/src/actions/get.ts b/packages/actions/src/actions/get.ts index 1f5f82c082..5fa8074101 100644 --- a/packages/actions/src/actions/get.ts +++ b/packages/actions/src/actions/get.ts @@ -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, diff --git a/packages/actions/src/actions/update.ts b/packages/actions/src/actions/update.ts index 687c1063b9..52e0116350 100644 --- a/packages/actions/src/actions/update.ts +++ b/packages/actions/src/actions/update.ts @@ -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, diff --git a/packages/actions/src/actions/utils.ts b/packages/actions/src/actions/utils.ts index c9536066c6..4f91547ba8 100644 --- a/packages/actions/src/actions/utils.ts +++ b/packages/actions/src/actions/utils.ts @@ -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 = ( - ctx.db.getCollection(associatedName).repository.relation(resourceName).of(associatedIndex) - ); - } else { - repository = ctx.db.getCollection(resourceName).repository; + if (resourceOf) { + return ctx.db.getRepository(resourceName, resourceOf); } - return repository; + return ctx.db.getRepository(resourceName); } export function RelationRepositoryActionBuilder(method: 'remove' | 'set') { diff --git a/packages/database/src/database.ts b/packages/database/src/database.ts index 43035e480a..9efee278ec 100644 --- a/packages/database/src/database.ts +++ b/packages/database/src/database.ts @@ -128,7 +128,7 @@ export class Database extends EventEmitter implements AsyncEmitter { } } - getRepository(name: string): Repository; + getRepository(name: string): R; getRepository(name: string, relationId: string | number): R; getRepository(name: string, relationId?: string | number): Repository | R { diff --git a/packages/resourcer/src/action.ts b/packages/resourcer/src/action.ts index be9bb66dca..701807667b 100644 --- a/packages/resourcer/src/action.ts +++ b/packages/resourcer/src/action.ts @@ -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 = []; constructor(options: ActionOptions) { diff --git a/packages/resourcer/src/resourcer.ts b/packages/resourcer/src/resourcer.ts index f8931988a9..d22c93888f 100644 --- a/packages/resourcer/src/resourcer.ts +++ b/packages/resourcer/src/resourcer.ts @@ -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 }),