Skip to content

Commit bfd9e09

Browse files
committed
chore: refactor findOneComplete to avoid dup
1 parent 6ced57f commit bfd9e09

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

src/datasets/datasets.service.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export class DatasetsService {
194194

195195
pipeline.push({ $skip: limits.skip || 0 });
196196

197-
pipeline.push({ $limit: limits.limit || 10 });
197+
if (limits?.limit)
198+
pipeline.push({ $limit: limits.limit });
198199

199200
const data = await this.datasetModel
200201
.aggregate<PartialOutputDatasetDto>(pipeline)
@@ -300,33 +301,14 @@ export class DatasetsService {
300301
async findOneComplete(
301302
filter: FilterQuery<DatasetDocument>,
302303
): Promise<OutputDatasetDto | null> {
303-
const whereFilter: FilterQuery<DatasetDocument> = filter.where ?? {};
304-
const fieldsProjection: string[] = filter.fields ?? {};
305-
const limits: QueryOptions<DatasetDocument> = filter.limits ?? {
304+
filter.limits = filter.limits ?? {
306305
skip: 0,
307306
sort: { createdAt: "desc" },
308307
};
309308

310-
const pipeline: PipelineStage[] = [{ $match: whereFilter }];
311-
if (!isEmpty(fieldsProjection)) {
312-
const projection = parsePipelineProjection(fieldsProjection);
313-
pipeline.push({ $project: projection });
314-
}
315-
316-
if (!isEmpty(limits.sort)) {
317-
const sort = parsePipelineSort(limits.sort);
318-
pipeline.push({ $sort: sort });
319-
}
320-
321-
pipeline.push({ $skip: limits.skip || 0 });
322-
323-
this.addLookupFields(pipeline, filter.include);
324-
325-
const [data] = await this.datasetModel
326-
.aggregate<OutputDatasetDto | undefined>(pipeline)
327-
.exec();
309+
const [data] = await this.findAllComplete(filter);
328310

329-
return data || null;
311+
return (data as OutputDatasetDto) || null;
330312
}
331313

332314
async count(

0 commit comments

Comments
 (0)