Skip to content

Commit

Permalink
fix compatability with prisma 4.13.*
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed May 10, 2023
1 parent f36eba7 commit dbdb6f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-scissors-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-prisma': minor
---

Fix compatability with prisma@4.13.*
5 changes: 4 additions & 1 deletion packages/plugin-prisma/src/util/datamodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export function getModel<Types extends SchemaTypes>(
builder: PothosSchemaTypes.SchemaBuilder<Types>,
) {
const dmmf = getDMMF(builder);
const modelData = dmmf.datamodel.models.find((model) => model.name === name);

const modelData = Array.isArray(dmmf.models)
? dmmf.models.find((model) => model.name === name)
: dmmf.models[name];

if (!modelData) {
throw new PothosSchemaError(`Model '${name}' not found in DMMF`);
Expand Down
21 changes: 17 additions & 4 deletions packages/plugin-prisma/src/util/get-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface DMMFField {
isUpdatedAt?: boolean;
}

interface DMMF {
export interface DMMF {
datamodel: {
models: {
name: string;
Expand All @@ -29,6 +29,18 @@ interface DMMF {
};
}

export interface RuntimeDataModel {
models: Record<
string,
{
fields: DMMFField[];
primaryKey: { name: string | null; fields: string[] } | null;
uniqueIndexes: { name: string | null; fields: string[] }[];
documentation?: string;
}
>;
}

const prismaClientCache = createContextCache(
<Types extends SchemaTypes>(builder: PothosSchemaTypes.SchemaBuilder<Types>) =>
createContextCache((context: object) =>
Expand All @@ -51,15 +63,16 @@ export function getClient<Types extends SchemaTypes>(

export function getDMMF<Types extends SchemaTypes>(
builder: PothosSchemaTypes.SchemaBuilder<Types>,
): DMMF {
): DMMF['datamodel'] | RuntimeDataModel {
if ('dmmf' in builder.options.prisma && builder.options.prisma.dmmf) {
return builder.options.prisma.dmmf as DMMF;
return (builder.options.prisma.dmmf as DMMF).datamodel;
}

const client = builder.options.prisma.client as unknown as {
_baseDmmf?: DMMF;
_dmmf: DMMF;
_runtimeDataModel: RuntimeDataModel;
};

return client._baseDmmf ?? client._dmmf;
return client._runtimeDataModel ?? client._baseDmmf?.datamodel ?? client._dmmf.datamodel;
}
44 changes: 30 additions & 14 deletions packages/plugin-prisma/src/util/relation-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContextCache } from '@pothos/core';
import { DMMF } from '@prisma/generator-helper';
import { DMMF, RuntimeDataModel } from './get-client';

export interface FieldMap {
model: string;
Expand All @@ -8,26 +8,42 @@ export interface FieldMap {

export type RelationMap = Map<string, FieldMap>;

export const getRelationMap = createContextCache((dmmf: { datamodel: unknown }) =>
createRelationMap(dmmf.datamodel as DMMF.Datamodel),
export const getRelationMap = createContextCache(
(datamodel: DMMF['datamodel'] | RuntimeDataModel) => createRelationMap(datamodel),
);

export function createRelationMap({ models }: DMMF.Datamodel) {
export function createRelationMap({ models }: DMMF['datamodel'] | RuntimeDataModel) {
const relationMap: RelationMap = new Map();

models.forEach((model) => {
relationMap.set(model.name, { model: model.name, relations: new Map() });
});
if (Array.isArray(models)) {
models.forEach((model) => {
relationMap.set(model.name, { model: model.name, relations: new Map() });
});

models.forEach((model) => {
const map = relationMap.get(model.name)!.relations;

model.fields.forEach((field) => {
if (field.kind === 'object' && relationMap.has(field.type)) {
map.set(field.name, relationMap.get(field.type)!);
}
});
});
} else {
Object.keys(models).forEach((name) => {
relationMap.set(name, { model: name, relations: new Map() });
});

models.forEach((model) => {
const map = relationMap.get(model.name)!.relations;
Object.entries(models).forEach(([name, model]) => {
const map = relationMap.get(name)!.relations;

model.fields.forEach((field) => {
if (field.kind === 'object' && relationMap.has(field.type)) {
map.set(field.name, relationMap.get(field.type)!);
}
model.fields.forEach((field) => {
if (field.kind === 'object' && relationMap.has(field.type)) {
map.set(field.name, relationMap.get(field.type)!);
}
});
});
});
}

return relationMap;
}

1 comment on commit dbdb6f0

@vercel
Copy link

@vercel vercel bot commented on dbdb6f0 May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.