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

Function disconnectRelation

packages/orm/src/client/crud/operations/base.ts:2030–2139  ·  view source on GitHub ↗
(kysely: AnyKysely, model: string, data: any, fromRelation: FromRelationContext)

Source from the content-addressed store, hash-verified

2028 }
2029
2030 protected async disconnectRelation(kysely: AnyKysely, model: string, data: any, fromRelation: FromRelationContext) {
2031 let disconnectConditions: any[] = [];
2032 if (typeof data === 'boolean') {
2033 if (data === false) {
2034 return;
2035 } else {
2036 disconnectConditions = [true];
2037 }
2038 } else if (isEmptyObject(data)) {
2039 // empty object means true
2040 disconnectConditions = [true];
2041 } else {
2042 disconnectConditions = this.normalizeRelationManipulationInput(model, data);
2043
2044 if (disconnectConditions.length === 0) {
2045 return;
2046 }
2047 }
2048
2049 if (disconnectConditions.length === 0) {
2050 return;
2051 }
2052
2053 const m2m = getManyToManyRelation(this.schema, fromRelation.model, fromRelation.field);
2054 if (m2m) {
2055 // handle many-to-many relation: batch fetch all entity IDs in one query
2056 const allIds = await this.getEntitiesIds(kysely, model, disconnectConditions);
2057 if (allIds.length > 0) {
2058 await this.handleManyToManyRelation(
2059 kysely,
2060 'disconnect',
2061 fromRelation.model,
2062 fromRelation.field,
2063 fromRelation.ids,
2064 m2m.otherModel,
2065 m2m.otherField,
2066 allIds,
2067 m2m.joinTable,
2068 );
2069 }
2070 } else {
2071 const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(
2072 this.schema,
2073 fromRelation.model,
2074 fromRelation.field,
2075 );
2076
2077 const eb = expressionBuilder<any, any>();
2078 if (ownedByModel) {
2079 // record parent fk update
2080 invariant(disconnectConditions.length === 1, 'only one entity can be disconnected');
2081 const condition = disconnectConditions[0];
2082
2083 if (condition === true) {
2084 // just disconnect, record parent fk update
2085 for (const { fk } of keyPairs) {
2086 fromRelation.parentUpdates[fk] = null;
2087 }

Callers

nothing calls this directly

Calls 7

isEmptyObjectFunction · 0.90
getManyToManyRelationFunction · 0.90
invariantFunction · 0.90
fieldsToSelectObjectFunction · 0.90
makeIdSelectMethod · 0.80
executeQueryMethod · 0.45

Tested by

no test coverage detected