Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide the prisma data model as a config parameter #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@prisma/client": "*"
},
"devDependencies": {
"@prisma/client": "^5.0.0",
"@prisma/client": "^5.19.0",
"@types/faker": "^5.5.9",
"@types/jest": "^29.2.5",
"@types/lodash": "^4.14.185",
Expand All @@ -50,7 +50,7 @@
"jest": "^29.3.1",
"kcd-scripts": "^5.0.0",
"npm-run-all": "^4.1.5",
"prisma": "^5.0.0",
"prisma": "^5.19.0",
"semantic-release": "^17.0.2",
"ts-jest": "^29.0.3",
"ts-node": "^9.1.1",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/nestedOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
stripIdSymbolsFromResult,
updateResultRelation,
} from "./utils/results";
import { setDmmf } from "./utils/dmmf";

type NonNullable<T> = Exclude<T, null | undefined>;

Expand All @@ -32,6 +33,7 @@ export function withNestedOperations<
>({
$rootOperation,
$allNestedOperations,
dmmf
}: {
$rootOperation: NonNullable<
Types.Extensions.DynamicQueryExtensionArgs<
Expand All @@ -40,7 +42,12 @@ export function withNestedOperations<
>["$allModels"]["$allOperations"]
>;
$allNestedOperations: (params: NestedParams<ExtArgs>) => Promise<any>;
dmmf?: typeof Prisma.dmmf,
}): typeof $rootOperation {
if(!!dmmf) {
setDmmf(dmmf);
}

return async (rootParams) => {
let calls: OperationCall<ExtArgs>[] = [];

Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export type NestedParams<ExtArgs extends Types.Extensions.InternalArgs> = {
args: any;
operation: NestedOperation;
scope?: Scope<ExtArgs>;
dmmf?: typeof Prisma.dmmf;
};

export type ExecuteFunction<
Expand Down
17 changes: 17 additions & 0 deletions src/lib/utils/dmmf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Prisma } from "@prisma/client";

let dmmf = Prisma.dmmf;

export function setDmmf(_dmmf: typeof Prisma.dmmf) {
dmmf = _dmmf;
}

export function getDmmf() {
if (!dmmf) {
throw new Error(
"Prisma DMMF not found, please generate Prisma client using `npx prisma generate`"
);
}

return dmmf;
}
10 changes: 5 additions & 5 deletions src/lib/utils/extractNestedOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
modifiers,
readOperations,
} from "./operations";
import { findOppositeRelation, relationsByModel } from "./relations";
import { findOppositeRelation, getRelationsByModel } from "./relations";

type NestedOperationInfo<
ExtArgs extends Types.Extensions.InternalArgs = Types.Extensions.DefaultArgs
Expand Down Expand Up @@ -51,7 +51,7 @@ export function extractRelationLogicalWhereOperations<
parentTarget?: Target,
parentOperations: { logicalOperator: LogicalOperator; index?: number }[] = []
): NestedOperationInfo[] {
const relations = relationsByModel[params.model || ""] || [];
const relations = getRelationsByModel()[params.model || ""] || [];
const nestedWhereOperations: NestedOperationInfo[] = [];

const operationsPath: string[] = [];
Expand Down Expand Up @@ -225,7 +225,7 @@ export function extractRelationLogicalWhereOperations<
export function extractRelationWhereOperations<
ExtArgs extends Types.Extensions.InternalArgs = Types.Extensions.DefaultArgs
>(params: NestedParams<ExtArgs>, parentTarget?: Target): NestedOperationInfo[] {
const relations = relationsByModel[params.model || ""] || [];
const relations = getRelationsByModel()[params.model || ""] || [];

const nestedWhereOperations = extractRelationLogicalWhereOperations(
params,
Expand Down Expand Up @@ -299,7 +299,7 @@ export function extractRelationWhereOperations<
export function extractRelationWriteOperations<
ExtArgs extends Types.Extensions.InternalArgs = Types.Extensions.DefaultArgs
>(params: NestedParams<ExtArgs>, parentTarget?: Target): NestedOperationInfo[] {
const relations = relationsByModel[params.model || ""] || [];
const relations = getRelationsByModel()[params.model || ""] || [];

if (!isWriteOperation(params.operation)) return [];

Expand Down Expand Up @@ -389,7 +389,7 @@ export function extractRelationWriteOperations<
export function extractRelationReadOperations<
ExtArgs extends Types.Extensions.InternalArgs = Types.Extensions.DefaultArgs
>(params: NestedParams<ExtArgs>, parentTarget?: Target): NestedOperationInfo[] {
const relations = relationsByModel[params.model || ""] || [];
const relations = getRelationsByModel()[params.model || ""] || [];
const nestedOperations: NestedOperationInfo[] = [];

relations.forEach((relation) => {
Expand Down
28 changes: 16 additions & 12 deletions src/lib/utils/relations.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Prisma } from "@prisma/client";
import { getDmmf } from './dmmf';

if (!Prisma.dmmf) {
throw new Error(
"Prisma DMMF not found, please generate Prisma client using `npx prisma generate`"
);
export function getRelationsByModel(): Record<string, Prisma.DMMF.Field[]> {
const dmmf = getDmmf();
const relationsByModel: Record<string, Prisma.DMMF.Field[]> = {};

dmmf.datamodel.models.forEach((model: Prisma.DMMF.Model) => {
relationsByModel[model.name] = model.fields.filter(
(field) => field.kind === "object" && field.relationName
);
});

return relationsByModel;

}

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

export function findOppositeRelation(relation: Prisma.DMMF.Field) {

export function findOppositeRelation(relation: Prisma.DMMF.Field): Prisma.DMMF.Field {
const parentRelations =
relationsByModel[relation.type as Prisma.ModelName] || [];
getRelationsByModel()[relation.type as Prisma.ModelName] || [];

const oppositeRelation = parentRelations.find(
(parentRelation) =>
Expand Down
4 changes: 2 additions & 2 deletions test/unit/calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import faker from "faker";
import { get } from "lodash";

import { withNestedOperations, NestedParams } from "../../src";
import { relationsByModel } from "../../src/lib/utils/relations";
import { getRelationsByModel } from "../../src/lib/utils/relations";
import { LogicalOperator, Modifier } from "../../src/lib/types";
import { createParams } from "./helpers/createParams";

Expand Down Expand Up @@ -65,7 +65,7 @@ function getModelRelation<Model extends Prisma.ModelName>(
model: Model,
relationName: string
): Prisma.DMMF.Field {
const modelRelation = relationsByModel[model].find(
const modelRelation = getRelationsByModel()[model].find(
(relation) => relation.name === relationName
);
if (!modelRelation) {
Expand Down
Loading