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

Method handle

packages/plugins/policy/src/policy-handler.ts:82–165  ·  view source on GitHub ↗
(node: RootOperationNode, proceed: ProceedKyselyQueryFunction)

Source from the content-addressed store, hash-verified

80 // #region main entry point
81
82 async handle(node: RootOperationNode, proceed: ProceedKyselyQueryFunction) {
83 if (!this.isCrudQueryNode(node)) {
84 if (this.options.dangerouslyAllowRawSql && RawNode.is(node as never)) {
85 return proceed(node);
86 }
87 // non-CRUD queries are not allowed
88 throw createRejectedByPolicyError(
89 undefined,
90 RejectedByPolicyReason.OTHER,
91 'non-CRUD queries are not allowed',
92 );
93 }
94
95 if (!this.isMutationQueryNode(node)) {
96 // transform and proceed with read directly
97 return proceed(this.transformNode(node));
98 }
99
100 const { mutationModel } = this.getMutationModel(node);
101
102 // reject non-existing model
103 this.tryRejectNonexistentModel(mutationModel);
104
105 // #region Pre mutation work
106
107 // create
108 if (InsertQueryNode.is(node)) {
109 await this.preCreateCheck(mutationModel, node, proceed);
110 }
111
112 // update
113 if (UpdateQueryNode.is(node)) {
114 await this.preUpdateCheck(mutationModel, node, proceed);
115 }
116
117 // post-update: load before-update entities if needed
118 const needsPostUpdateCheck = UpdateQueryNode.is(node) && this.hasPostUpdatePolicies(mutationModel);
119 let beforeUpdateInfo: Awaited<ReturnType<typeof this.loadBeforeUpdateEntities>> | undefined;
120 if (needsPostUpdateCheck) {
121 beforeUpdateInfo = await this.loadBeforeUpdateEntities(
122 mutationModel,
123 node.where,
124 proceed,
125 // force load pre-update entities if dialect doesn't support returning,
126 // so we can rely on pre-update ids to read back updated entities
127 !this.dialect.supportsReturning,
128 );
129 }
130
131 // #endregion
132
133 // #region mutation execution
134
135 const result = await proceed(this.transformNode(node));
136
137 // #endregion
138
139 // #region Post mutation work

Callers 1

onKyselyQueryMethod · 0.95

Calls 14

isCrudQueryNodeMethod · 0.95
isMutationQueryNodeMethod · 0.95
getMutationModelMethod · 0.95
preCreateCheckMethod · 0.95
preUpdateCheckMethod · 0.95
hasPostUpdatePoliciesMethod · 0.95
postUpdateCheckMethod · 0.95
onlyReturningIdMethod · 0.95
processReadBackMethod · 0.95

Tested by

no test coverage detected