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

Function applyExtResultToRow

packages/orm/src/client/client-impl.ts:1101–1168  ·  view source on GitHub ↗
(
    row: unknown,
    model: string,
    originalArgs: unknown,
    schema: SchemaDef,
    plugins: AnyPlugin[],
    extResultDefs: Map<string, ExtResultFieldDef>,
)

Source from the content-addressed store, hash-verified

1099}
1100
1101function applyExtResultToRow(
1102 row: unknown,
1103 model: string,
1104 originalArgs: unknown,
1105 schema: SchemaDef,
1106 plugins: AnyPlugin[],
1107 extResultDefs: Map<string, ExtResultFieldDef>,
1108): unknown {
1109 if (!row || typeof row !== 'object') {
1110 return row;
1111 }
1112
1113 const data = row as Record<string, unknown>;
1114 const typedArgs = (originalArgs && typeof originalArgs === 'object' ? originalArgs : {}) as Record<string, unknown>;
1115 const select = typedArgs['select'] as Record<string, unknown> | undefined;
1116 const omit = typedArgs['omit'] as Record<string, unknown> | undefined;
1117 const include = typedArgs['include'] as Record<string, unknown> | undefined;
1118
1119 // Compute ext result fields for the current model
1120 for (const [fieldName, fieldDef] of extResultDefs) {
1121 if (select && !select[fieldName]) {
1122 continue;
1123 }
1124 if (omit?.[fieldName]) {
1125 continue;
1126 }
1127 const needsSatisfied = Object.keys(fieldDef.needs).every((needField) => needField in data);
1128 if (needsSatisfied) {
1129 data[fieldName] = fieldDef.compute(data);
1130 }
1131 }
1132
1133 // Strip fields that shouldn't be in the result: when `select` was used,
1134 // drop any field not in the original select and not a computed ext result field;
1135 // when `omit` was used, re-delete any field the user originally omitted.
1136 if (select) {
1137 for (const key of Object.keys(data)) {
1138 if (!select[key] && !extResultDefs.has(key)) {
1139 delete data[key];
1140 }
1141 }
1142 } else if (omit) {
1143 for (const key of Object.keys(omit)) {
1144 if (omit[key] && !extResultDefs.has(key)) {
1145 delete data[key];
1146 }
1147 }
1148 }
1149
1150 // Recurse into nested relation data
1151 const relationSource = include ?? select;
1152 if (relationSource) {
1153 for (const [field, value] of Object.entries(relationSource)) {
1154 if (data[field] == null) {
1155 continue;
1156 }
1157 const fieldDef = getField(schema, model, field);
1158 if (!fieldDef?.relation) {

Callers 1

applyExtResultFunction · 0.85

Calls 2

getFieldFunction · 0.90
applyExtResultFunction · 0.85

Tested by

no test coverage detected