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

refactor(policy): optimize post-create read to only use id fields to find the created entity #1201

Merged
merged 1 commit into from
Mar 31, 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
9 changes: 2 additions & 7 deletions packages/runtime/src/enhancements/policy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
);
}

private addFluentSelect(args: any, field: string, fluentArgs: any) {
// overwrite include/select with the fluent field
delete args.include;
args.select = { [field]: fluentArgs ?? true };
}

private async doFind(args: any, actionName: FindOperations, handleRejection: () => any) {
const origArgs = args;
const _args = this.policyUtils.clone(args);
Expand Down Expand Up @@ -364,7 +358,8 @@ export class PolicyProxyHandler<DbClient extends DbClientContract> implements Pr
const key = getEntityKey(model, scalarData);
// only check if entity is created, not connected
if (!connectedEntities.has(key) && !postCreateChecks.has(key)) {
postCreateChecks.set(key, { model, operation: 'create', uniqueFilter: scalarData });
const idFields = this.policyUtils.getIdFieldValues(model, scalarData);
postCreateChecks.set(key, { model, operation: 'create', uniqueFilter: idFields });
}
});

Expand Down
11 changes: 11 additions & 0 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1235,5 +1235,16 @@ export class PolicyUtil extends QueryUtils {
return guard;
}

/**
* Given an entity data, returns an object only containing id fields.
*/
getIdFieldValues(model: string, data: any) {
if (!data) {
return undefined;
}
const idFields = this.getIdFields(model);
return Object.fromEntries(idFields.map((f) => [f.name, data[f.name]]));
}

//#endregion
}
Loading