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

fix(hooks): include delegate base models when calculating models affected by a mutation #1121

Merged
merged 1 commit into from
Mar 11, 2024
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
2 changes: 1 addition & 1 deletion packages/runtime/src/cross/model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export type ModelInfo = {
name: string;

/**
* Base types
* Base types (not including abstract base models).
*/
baseTypes?: string[];

Expand Down
16 changes: 16 additions & 0 deletions packages/runtime/src/cross/query-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ModelMeta } from './model-meta';
import { NestedReadVisitor } from './nested-read-visitor';
import { NestedWriteVisitor } from './nested-write-visitor';
import type { PrismaWriteActionType } from './types';
import { getModelInfo } from './utils';

/**
* Gets models read (including nested ones) given a query args.
Expand Down Expand Up @@ -71,6 +72,11 @@ export async function getMutatedModels(
await visitor.visit(model, operation, mutationArgs);
}

// include delegate base models recursively
result.forEach((m) => {
getBaseRecursively(m, modelMeta, result);
});

return [...result];
}

Expand All @@ -92,3 +98,13 @@ function collectDeleteCascades(model: string, modelMeta: ModelMeta, result: Set<
collectDeleteCascades(m, modelMeta, result, visited);
});
}

function getBaseRecursively(model: string, modelMeta: ModelMeta, result: Set<string>) {
const bases = getModelInfo(modelMeta, model)?.baseTypes;
if (bases) {
bases.forEach((base) => {
result.add(base);
getBaseRecursively(base, modelMeta, result);
});
}
}
4 changes: 0 additions & 4 deletions packages/runtime/src/enhancements/create-enhancement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { isDelegateModel, type ModelMeta } from '../cross';
import type { AuthUser } from '../types';
import { withDefaultAuth } from './default-auth';
import { withDelegate } from './delegate';
import { Logger } from './logger';
import { withOmit } from './omit';
import { withPassword } from './password';
import { withPolicy } from './policy';
Expand Down Expand Up @@ -130,9 +129,6 @@ export function createEnhancement<DbClient extends object>(
);
}

const logger = new Logger(prisma);
logger.info(`Enabled ZenStack enhancements: ${options.kinds?.join(', ')}`);

let result = prisma;

if (
Expand Down
Loading