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

Function delete

packages/orm/src/client/crud/operations/base.ts:2353–2427  ·  view source on GitHub ↗
(
        kysely: AnyKysely,
        model: string,
        where: any,
        limit?: number,
        filterModel?: string,
        fieldsToReturn?: readonly string[],
    )

Source from the content-addressed store, hash-verified

2351 // #endregion
2352
2353 protected async delete(
2354 kysely: AnyKysely,
2355 model: string,
2356 where: any,
2357 limit?: number,
2358 filterModel?: string,
2359 fieldsToReturn?: readonly string[],
2360 ): Promise<QueryResult<unknown>> {
2361 filterModel ??= model;
2362
2363 const modelDef = this.requireModel(model);
2364
2365 if (modelDef.baseModel) {
2366 if (limit !== undefined) {
2367 throw createNotSupportedError('Deleting with a limit is not supported for polymorphic models');
2368 }
2369 // just delete base and it'll cascade back to this model
2370 return this.processBaseModelDelete(kysely, modelDef.baseModel, where, limit, filterModel);
2371 }
2372
2373 fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
2374
2375 let needIdFilter = false;
2376
2377 if (limit !== undefined && !this.dialect.supportsDeleteWithLimit) {
2378 // if the dialect doesn't support delete with limit natively, we'll
2379 // simulate it by filtering by id with a limit
2380 needIdFilter = true;
2381 }
2382
2383 if (modelDef.isDelegate || modelDef.baseModel) {
2384 // if the model is in a delegate hierarchy, we'll need to filter by
2385 // id because the filter may involve fields in different models in
2386 // the hierarchy
2387 needIdFilter = true;
2388 }
2389
2390 const deleteFilter = needIdFilter
2391 ? (eb: ExpressionBuilder<any, any>) =>
2392 eb(
2393 eb.refTuple(
2394 // @ts-expect-error
2395 ...this.buildIdFieldRefs(kysely, model),
2396 ),
2397 'in',
2398 // the outer "select *" is needed to isolate the sub query (as needed for dialects like mysql)
2399 eb
2400 .selectFrom(
2401 this.dialect
2402 .buildSelectModel(filterModel, filterModel)
2403 .where(() => this.dialect.buildFilter(filterModel, filterModel, where))
2404 .select(this.buildIdFieldRefs(kysely, filterModel))
2405 .$if(limit !== undefined, (qb) => qb.limit(limit!))
2406 .as('$sub'),
2407 )
2408 .selectAll(),
2409 )
2410 : () => this.dialect.buildFilter(model, model, where);

Callers

nothing calls this directly

Calls 5

createNotSupportedErrorFunction · 0.90
requireIdFieldsFunction · 0.90
requireModelMethod · 0.80
buildFilterMethod · 0.80
executeQueryMethod · 0.45

Tested by

no test coverage detected