Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
provide relation info in nested params
  • Loading branch information
Tenrys committed Mar 24, 2023
commit 8b10b0c232ced45cf80b846d87100b3c0f0f8127
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type NestedAction =

export type NestedParams = Omit<Prisma.MiddlewareParams, "action"> & {
action: NestedAction;
relation?: Prisma.DMMF.Field;
scope?: NestedParams;
};

Expand All @@ -44,8 +45,8 @@ type PromiseCallbackRef = {
reject: (reason?: any) => void;
};

const readOperations: NestedReadAction[] = ["include", "select"];
const writeOperations: NestedAction[] = [
export const readOperations: NestedReadAction[] = ["include", "select"];
export const writeOperations: NestedAction[] = [
"create",
"update",
"upsert",
Expand Down Expand Up @@ -125,6 +126,7 @@ function extractNestedWriteOperations(
params: {
...params,
model,
relation,
action: operation,
args: operation === "create" ? { data: item } : item,
scope: params,
Expand All @@ -139,6 +141,7 @@ function extractNestedWriteOperations(
params: {
...params,
model,
relation,
action: operation,
args:
operation === "create"
Expand Down Expand Up @@ -169,6 +172,7 @@ function extractNestedReadOperations(
params: {
...params,
model,
relation,
action: operation,
args: arg,
scope: params,
Expand Down
24 changes: 20 additions & 4 deletions test/calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Prisma } from "@prisma/client";
import faker from "faker";
import { get } from "lodash";

import { createNestedMiddleware, NestedParams } from "../src";
import { createNestedMiddleware, NestedParams, readOperations, writeOperations } from "../src";
import { createParams } from "./utils/createParams";

type MiddlewareCall<Model extends Prisma.ModelName> = {
model: Model;
relation?: Prisma.DMMF.Field;
action:
| "create"
| "update"
Expand All @@ -25,6 +26,13 @@ type MiddlewareCall<Model extends Prisma.ModelName> = {
scope?: MiddlewareCall<any>;
};

const operations = [...readOperations, ...writeOperations].reduce((acc, x) => {
acc[x] = true;
return acc;
}, {} as Record<string, boolean>);
const getLastFieldNameFromArgsPath = (argsPath: string) => argsPath.split(".").reverse().find(x =>
!operations[x] && !/\d+/.test(x)
)
function nestedParamsFromCall<Model extends Prisma.ModelName>(
rootParams: Prisma.MiddlewareParams,
call: MiddlewareCall<Model>
Expand All @@ -35,11 +43,19 @@ function nestedParamsFromCall<Model extends Prisma.ModelName>(
call.action,
call.action === "create" ? { data: args } : args
);

const scope = call.scope
? nestedParamsFromCall(rootParams, call.scope)
: rootParams as NestedParams;

return {
...params,
scope: call.scope
? nestedParamsFromCall(rootParams, call.scope)
: rootParams,
relation: Prisma.dmmf.datamodel
.models.find(x => x.name == scope.model)
?.fields.find(x =>
x.name == getLastFieldNameFromArgsPath(call.argsPath)
),
scope
};
}

Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
"skipLibCheck": true,
"noEmit": true
},
"exclude": ["test"]
}