feat: improve the params of the ctx.action

This commit is contained in:
chenos 2022-01-23 12:42:35 +08:00
parent 2b078a28be
commit f0eb2eee20
7 changed files with 24 additions and 19 deletions

View File

@ -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,
});

View File

@ -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,

View File

@ -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,

View File

@ -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') {

View File

@ -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 {

View File

@ -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) {

View File

@ -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 }),