From ee0166be04f37a1dc739fe9571278c17d56990cd Mon Sep 17 00:00:00 2001 From: Yiming Date: Sun, 31 Mar 2024 13:59:14 -0700 Subject: [PATCH] refactor(policy): optimize post-create read to only use id fields to find the created entity (#1201) --- packages/runtime/src/enhancements/policy/handler.ts | 9 ++------- .../runtime/src/enhancements/policy/policy-utils.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/runtime/src/enhancements/policy/handler.ts b/packages/runtime/src/enhancements/policy/handler.ts index f429066f9..96e71641c 100644 --- a/packages/runtime/src/enhancements/policy/handler.ts +++ b/packages/runtime/src/enhancements/policy/handler.ts @@ -126,12 +126,6 @@ export class PolicyProxyHandler 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); @@ -364,7 +358,8 @@ export class PolicyProxyHandler 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 }); } }); diff --git a/packages/runtime/src/enhancements/policy/policy-utils.ts b/packages/runtime/src/enhancements/policy/policy-utils.ts index bc313f7c3..b33f8ded1 100644 --- a/packages/runtime/src/enhancements/policy/policy-utils.ts +++ b/packages/runtime/src/enhancements/policy/policy-utils.ts @@ -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 }