Skip to content

Commit

Permalink
refactor(policy): optimize post-create read to only use id fields to …
Browse files Browse the repository at this point in the history
…find the created entity (#1201)
  • Loading branch information
ymc9 authored Mar 31, 2024
1 parent ee3c716 commit ee0166b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
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
}

0 comments on commit ee0166b

Please sign in to comment.