Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/common/interfaces/common.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export interface IProposalAcceptedMessage {
}

export interface ILimitsFilter {
limit: number;
skip: number;
order: string;
limit?: number;
skip?: number;
order?: string;
}

export interface IFilters<T, Y = null> {
Expand Down Expand Up @@ -69,14 +69,17 @@ export interface IDatafileFilter {
type?: string;
}

export type IFiltersV4<T, Y = null, Z = null> = IFilters<T, Y> & {
export type IFiltersV4<T, Y = null, Z = null> = Pick<
IFilters<T, Y>,
"where"
> & {
include?: Z;
limits?: ILimitsV4<T>;
fields?: (keyof Y)[];
};

export interface ILimitsV4<T = null> {
limit: number;
skip: number;
sort: Record<keyof T, "asc" | "desc">;
limit?: number;
skip?: number;
sort?: Record<keyof T, "asc" | "desc">;
}
46 changes: 8 additions & 38 deletions src/datasets/datasets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,49 +332,19 @@ export class DatasetsService {
}

async findOneComplete(
filter: FilterQuery<DatasetDocument>,
filter: IDatasetFiltersV4<DatasetDocument, IDatasetFields>,
): Promise<OutputDatasetDto | null> {
const whereFilter: FilterQuery<DatasetDocument> = filter.where ?? {};
let fieldsProjection: string[] = filter.fields ?? {};

const limits: QueryOptions<DatasetDocument> = filter.limits ?? {
filter.limits = filter.limits ?? {
skip: 0,
sort: { createdAt: "desc" },
sort: { createdAt: "desc" } as Record<
keyof DatasetDocument,
"asc" | "desc"
>,
};

const pipeline: PipelineStage[] = [{ $match: whereFilter }];
const addedRelations = this.addLookupFields(pipeline, filter.include);

if (Array.isArray(fieldsProjection)) {
fieldsProjection = Array.from(
new Set([...fieldsProjection, ...addedRelations]),
);
}

if (!isEmpty(fieldsProjection)) {
const projection = parsePipelineProjection(fieldsProjection);
pipeline.push({ $project: projection });
}
const [data] = await this.findAllComplete(filter);

if (!isEmpty(limits.sort)) {
const sort = parsePipelineSort(limits.sort);
pipeline.push({ $sort: sort });
}

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

try {
const [data] = await this.datasetModel
.aggregate<OutputDatasetDto | undefined>(pipeline)
.exec();

return data || null;
} catch (error) {
if (error instanceof Error) {
throw new BadRequestException(error.message);
}
throw new BadRequestException("An unknown error occurred");
}
return (data as OutputDatasetDto) || null;
}

async count(
Expand Down
8 changes: 5 additions & 3 deletions src/datasets/interfaces/dataset-filters.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface IDatasetRelation {
scope: IDatasetScopes;
}

export type IDatasetFiltersV4<T, Y = null> = IFiltersV4<T, Y> & {
include?: DatasetLookupKeysEnum[] | IDatasetRelation[];
};
export type IDatasetFiltersV4<T, Y = null> = IFiltersV4<
T,
Y,
DatasetLookupKeysEnum[] | IDatasetRelation[]
>;
1 change: 1 addition & 0 deletions test/DatasetV4.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ describe("2500: Datasets v4 tests", () => {
skip: 0,
sort: {
datasetName: "asc",
createdAt: "asc",
},
},
};
Expand Down