Skip to content

Commit

Permalink
Merge pull request #1331 from jgnieuwhof/refresh-prisma-delegate
Browse files Browse the repository at this point in the history
Keep Prisma delegate in-sync with client cache
  • Loading branch information
hayes authored Nov 1, 2024
2 parents 7a4fa4c + 52a70e9 commit 2529fc2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/nice-fishes-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pothos/plugin-drizzle": patch
"@pothos/plugin-prisma": patch
---

Load client when db request is initialized rather than caching on model loader
6 changes: 2 additions & 4 deletions packages/plugin-drizzle/src/model-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class ModelLoader {
table: TableRelationalConfig;
columns: Column[];
selectSQL;
client: DrizzleClient;

constructor(
context: object,
Expand All @@ -54,8 +53,6 @@ export class ModelLoader {
this.columns.length > 1
? sql`(${sql.join(this.columns, sql`, `)})`
: this.columns[0].getSQL();

this.client = getClient(builder, context);
}

static forModel<Types extends SchemaTypes>(
Expand Down Expand Up @@ -166,11 +163,12 @@ export class ModelLoader {
this.staged.add(entry);

const nextTick = createResolvablePromise<void>();
const client = getClient(this.builder, this.context);

nextTick.promise
.then(() => {
const api = (
this.client.query as Record<
client.query as Record<
string,
{ findMany: (...args: unknown[]) => Promise<Record<string, unknown>[]> }
>
Expand Down
17 changes: 8 additions & 9 deletions packages/plugin-prisma/src/model-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export class ModelLoader {
models: Map<object, ResolvablePromise<Record<string, unknown> | null>>;
}>();

delegate: PrismaDelegate;

tick = Promise.resolve();

constructor(
Expand All @@ -53,10 +51,6 @@ export class ModelLoader {
this.builder = builder;
this.findUnique = findUnique;
this.modelName = modelName;
this.delegate = getDelegateFromModel(
getClient(this.builder, this.context as never),
this.modelName,
);
}

static forRef<Types extends SchemaTypes>(
Expand Down Expand Up @@ -285,6 +279,11 @@ export class ModelLoader {
}

async initLoad(state: SelectionState, initialModel: {}) {
const delegate = getDelegateFromModel(
getClient(this.builder, this.context as never),
this.modelName,
);

const models = new Map<object, ResolvablePromise<Record<string, unknown> | null>>();

const promise = createResolvablePromise<Record<string, unknown> | null>();
Expand All @@ -302,15 +301,15 @@ export class ModelLoader {
this.staged.delete(entry);

for (const [model, { resolve, reject }] of entry.models) {
if (this.delegate.findUniqueOrThrow) {
this.delegate
if (delegate.findUniqueOrThrow) {
delegate
.findUniqueOrThrow({
...selectionToQuery(state),
where: { ...(this.findUnique(model as Record<string, unknown>, this.context) as {}) },
} as never)
.then(resolve as () => {}, reject);
} else {
this.delegate
delegate
.findUnique({
rejectOnNotFound: true,
...selectionToQuery(state),
Expand Down

0 comments on commit 2529fc2

Please sign in to comment.