MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / createPromise

Function createPromise

packages/orm/src/client/client-impl.ts:613–673  ·  view source on GitHub ↗
(
        operation: CoreCrudOperations,
        nominalOperation: AllCrudOperations,
        args: unknown,
        handler: BaseOperationHandler<any>,
        postProcess = false,
        throwIfNoResult = false,
    )

Source from the content-addressed store, hash-verified

611 const hasAnyExtResult = hasExtResultFieldDefs(plugins);
612
613 const createPromise = (
614 operation: CoreCrudOperations,
615 nominalOperation: AllCrudOperations,
616 args: unknown,
617 handler: BaseOperationHandler<any>,
618 postProcess = false,
619 throwIfNoResult = false,
620 ) => {
621 return createZenStackPromise(async (txClient?: ClientContract<any>) => {
622 let proceed = async (_args: unknown) => {
623 // prepare args for ext result: strip ext result field names from select/omit,
624 // inject needs fields into select (recursively handles nested relations)
625 const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has(operation);
626 const processedArgs = shouldApplyExtResult
627 ? prepareArgsForExtResult(_args, model, schema, plugins)
628 : _args;
629
630 const _handler = txClient ? handler.withClient(txClient) : handler;
631 const r = await _handler.handle(operation, processedArgs);
632 if (!r && throwIfNoResult) {
633 throw createNotFoundError(model);
634 }
635 let result: unknown;
636 if (r && postProcess) {
637 result = resultProcessor.processResult(r, model, processedArgs);
638 } else {
639 result = r ?? null;
640 }
641
642 // compute ext result fields (recursively handles nested relations)
643 if (result && shouldApplyExtResult) {
644 result = applyExtResult(result, model, _args, schema, plugins);
645 }
646
647 return result;
648 };
649
650 // apply plugins
651 const plugins = [...(client.$options.plugins ?? [])];
652 for (const plugin of plugins) {
653 const onQuery = plugin.onQuery;
654 if (onQuery) {
655 const _proceed = proceed;
656 proceed = (_args: unknown) => {
657 const ctx: any = {
658 client,
659 model,
660 operation: nominalOperation,
661 // reflect the latest override if provided
662 args: _args,
663 // ensure inner overrides are propagated to the previous proceed
664 proceed: (nextArgs: unknown) => _proceed(nextArgs),
665 };
666 return (onQuery as (ctx: any) => Promise<unknown>)(ctx);
667 };
668 }
669 }
670

Callers 1

createModelCrudHandlerFunction · 0.85

Calls 2

createZenStackPromiseFunction · 0.90
proceedFunction · 0.85

Tested by

no test coverage detected