Skip to content

Commit a7c3504

Browse files
committed
extract common helper for processNestedSingleRead and processSingleRead
1 parent da2570a commit a7c3504

1 file changed

Lines changed: 33 additions & 37 deletions

File tree

packages/server/src/api/rest/index.ts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -809,18 +809,16 @@ export class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
809809
return resp;
810810
}
811811

812-
private async processSingleRead(
813-
client: ClientContract<Schema>,
812+
/**
813+
* Builds the ORM `args` object (include, select) shared by single-read operations.
814+
* Returns the args to pass to findUnique/findFirst and the resolved `include` list for serialization,
815+
* or an error response if query params are invalid.
816+
*/
817+
private buildSingleReadArgs(
814818
type: string,
815-
resourceId: string,
816819
query: Record<string, string | string[]> | undefined,
817-
): Promise<Response> {
818-
const typeInfo = this.getModelInfo(type);
819-
if (!typeInfo) {
820-
return this.makeUnsupportedModelError(type);
821-
}
822-
823-
const args: any = { where: this.makeIdFilter(typeInfo.idFields, resourceId) };
820+
): { args: any; include: string[] | undefined; error?: Response } {
821+
const args: any = {};
824822

825823
// include IDs of relation fields so that they can be serialized
826824
this.includeRelationshipIds(type, args, 'include');
@@ -830,7 +828,7 @@ export class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
830828
if (query?.['include']) {
831829
const { select, error, allIncludes } = this.buildRelationSelect(type, query['include'], query);
832830
if (error) {
833-
return error;
831+
return { args, include, error };
834832
}
835833
if (select) {
836834
args.include = { ...args.include, ...select };
@@ -840,18 +838,34 @@ export class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
840838

841839
// handle partial results for requested type
842840
const { select, error } = this.buildPartialSelect(type, query);
843-
if (error) return error;
841+
if (error) return { args, include, error };
844842
if (select) {
845843
args.select = { ...select, ...args.select };
846844
if (args.include) {
847-
args.select = {
848-
...args.select,
849-
...args.include,
850-
};
845+
args.select = { ...args.select, ...args.include };
851846
args.include = undefined;
852847
}
853848
}
854849

850+
return { args, include };
851+
}
852+
853+
private async processSingleRead(
854+
client: ClientContract<Schema>,
855+
type: string,
856+
resourceId: string,
857+
query: Record<string, string | string[]> | undefined,
858+
): Promise<Response> {
859+
const typeInfo = this.getModelInfo(type);
860+
if (!typeInfo) {
861+
return this.makeUnsupportedModelError(type);
862+
}
863+
864+
const { args, include, error } = this.buildSingleReadArgs(type, query);
865+
if (error) return error;
866+
867+
args.where = this.makeIdFilter(typeInfo.idFields, resourceId);
868+
855869
const entity = await (client as any)[type].findUnique(args);
856870

857871
if (entity) {
@@ -1161,28 +1175,10 @@ export class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
11611175
const childType = resolved.childType;
11621176
const typeInfo = this.getModelInfo(childType)!;
11631177

1164-
const args: any = {
1165-
where: this.mergeFilters(this.makeIdFilter(typeInfo.idFields, childId), nestedFilter),
1166-
};
1167-
this.includeRelationshipIds(childType, args, 'include');
1168-
1169-
let include: string[] | undefined;
1170-
if (query?.['include']) {
1171-
const { select, error, allIncludes } = this.buildRelationSelect(childType, query['include'], query);
1172-
if (error) return error;
1173-
if (select) args.include = { ...args.include, ...select };
1174-
include = allIncludes;
1175-
}
1176-
1177-
const { select, error } = this.buildPartialSelect(childType, query);
1178+
const { args, include, error } = this.buildSingleReadArgs(childType, query);
11781179
if (error) return error;
1179-
if (select) {
1180-
args.select = { ...select, ...args.select };
1181-
if (args.include) {
1182-
args.select = { ...args.select, ...args.include };
1183-
args.include = undefined;
1184-
}
1185-
}
1180+
1181+
args.where = this.mergeFilters(this.makeIdFilter(typeInfo.idFields, childId), nestedFilter);
11861182

11871183
const entity = await (client as any)[childType].findFirst(args);
11881184
if (!entity) return this.makeError('notFound');

0 commit comments

Comments
 (0)