Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
feat: include relation in NestedParams
For certain middleware it is necessary to reference the relation for
that nested action. For example instead of deleting a toOne relation a
soft delete middleware may decide instead to unlink the relation and
update it to have a deleted mark which would require both the isList and
the foreign key information stored on the relation.
  • Loading branch information
olivierwilkinson committed Mar 27, 2023
commit f926d3d31e8776cf86631ef046c55e95fd860537
2 changes: 1 addition & 1 deletion src/lib/createNestedMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (!Prisma.dmmf) {
);
}

const relationsByModel: Record<string, Prisma.DMMF.Field[]> = {};
export const relationsByModel: Record<string, Prisma.DMMF.Field[]> = {};
Prisma.dmmf.datamodel.models.forEach((model: Prisma.DMMF.Model) => {
relationsByModel[model.name] = model.fields.filter(
(field) => field.kind === "object" && field.relationName
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type MiddlewareCall = {
export type NestedParams = Omit<Prisma.MiddlewareParams, "action"> & {
action: NestedAction;
scope?: NestedParams;
relation?: Prisma.DMMF.Field;
};

export type NestedMiddleware<T = any> = (
Expand Down
6 changes: 5 additions & 1 deletion src/lib/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Target,
} from "../types";

import { normaliseRootArgs } from './args';
import { normaliseRootArgs } from "./args";

export const readActions: NestedReadAction[] = ["include", "select"];
export const writeActions: NestedWriteAction[] = [
Expand Down Expand Up @@ -89,6 +89,7 @@ export function extractNestedWriteActions(
runInTransaction,
dataPath: [],
scope: params,
relation,
},
}))
);
Expand All @@ -108,6 +109,7 @@ export function extractNestedWriteActions(
runInTransaction,
dataPath: [],
scope: params,
relation,
},
});
});
Expand Down Expand Up @@ -138,6 +140,7 @@ export function extractNestedReadActions(
runInTransaction,
dataPath: [],
scope: params,
relation,
},
});

Expand All @@ -152,6 +155,7 @@ export function extractNestedReadActions(
runInTransaction,
dataPath: [],
scope: params,
relation,
},
});
}
Expand Down
Loading