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

Function createModelCrudHandler

packages/orm/src/client/client-impl.ts:602–909  ·  view source on GitHub ↗
(
    client: ClientContract<any>,
    model: string,
    inputValidator: InputValidator<any>,
    resultProcessor: ResultProcessor<any>,
)

Source from the content-addressed store, hash-verified

600}
601
602function createModelCrudHandler(
603 client: ClientContract<any>,
604 model: string,
605 inputValidator: InputValidator<any>,
606 resultProcessor: ResultProcessor<any>,
607): ModelOperations<any, any> {
608 // check if any plugin defines ext result fields
609 const plugins = client.$options.plugins ?? [];
610 const schema = client.$schema;
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,

Callers 1

createClientProxyFunction · 0.85

Calls 7

createNotSupportedErrorFunction · 0.90
lowerCaseFirstFunction · 0.90
requireModelFunction · 0.90
isUnsupportedFieldFunction · 0.90
fieldHasDefaultValueFunction · 0.90
hasExtResultFieldDefsFunction · 0.85
createPromiseFunction · 0.85

Tested by

no test coverage detected