mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix(api): enhance projection parameters typing
This commit is contained in:
parent
0e3187c844
commit
34636de9c5
@ -219,12 +219,10 @@ describe('NlpSampleRepository', () => {
|
||||
skip: 0,
|
||||
sort: ['text', 'asc'],
|
||||
} as PageQueryDto<NlpSample>;
|
||||
const projection = { text: 1 };
|
||||
|
||||
const result = await nlpSampleRepository.findByEntitiesAndPopulate(
|
||||
{ filters, values },
|
||||
page,
|
||||
projection,
|
||||
{ text: 1 },
|
||||
);
|
||||
|
||||
expect(Array.isArray(result)).toBe(true);
|
||||
|
@ -14,14 +14,13 @@ import {
|
||||
Document,
|
||||
Model,
|
||||
PipelineStage,
|
||||
ProjectionType,
|
||||
Query,
|
||||
Types,
|
||||
} from 'mongoose';
|
||||
|
||||
import { BaseRepository, DeleteResult } from '@/utils/generics/base-repository';
|
||||
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||
import { TFilterQuery, TProjectionType } from '@/utils/types/filter.types';
|
||||
|
||||
import { TNlpSampleDto } from '../dto/nlp-sample.dto';
|
||||
import { NlpSampleEntity } from '../schemas/nlp-sample-entity.schema';
|
||||
@ -183,7 +182,7 @@ export class NlpSampleRepository extends BaseRepository<
|
||||
values: NlpValue[];
|
||||
},
|
||||
page?: PageQueryDto<NlpSample>,
|
||||
projection?: ProjectionType<NlpSample>,
|
||||
projection?: TProjectionType<NlpSample>,
|
||||
): Aggregate<NlpSampleDocument[]> {
|
||||
return this.model.aggregate<NlpSampleDocument>([
|
||||
...this.buildFindByEntitiesStages(criterias),
|
||||
@ -211,7 +210,7 @@ export class NlpSampleRepository extends BaseRepository<
|
||||
values: NlpValue[];
|
||||
},
|
||||
page?: PageQueryDto<NlpSample>,
|
||||
projection?: ProjectionType<NlpSample>,
|
||||
projection?: TProjectionType<NlpSample>,
|
||||
): Promise<NlpSample[]> {
|
||||
const aggregation = this.findByEntitiesAggregation(
|
||||
criterias,
|
||||
@ -239,7 +238,7 @@ export class NlpSampleRepository extends BaseRepository<
|
||||
values: NlpValue[];
|
||||
},
|
||||
page?: PageQueryDto<NlpSample>,
|
||||
projection?: ProjectionType<NlpSample>,
|
||||
projection?: TProjectionType<NlpSample>,
|
||||
): Promise<NlpSampleFull[]> {
|
||||
const aggregation = this.findByEntitiesAggregation(
|
||||
criterias,
|
||||
|
@ -12,7 +12,7 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Document, ProjectionType, Query } from 'mongoose';
|
||||
import { Document, Query } from 'mongoose';
|
||||
import Papa from 'papaparse';
|
||||
|
||||
import { Message } from '@/chat/schemas/message.schema';
|
||||
@ -22,7 +22,11 @@ import { LanguageService } from '@/i18n/services/language.service';
|
||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||
import { BaseService } from '@/utils/generics/base-service';
|
||||
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
|
||||
import { TFilterQuery, THydratedDocument } from '@/utils/types/filter.types';
|
||||
import {
|
||||
TFilterQuery,
|
||||
THydratedDocument,
|
||||
TProjectionType,
|
||||
} from '@/utils/types/filter.types';
|
||||
|
||||
import { NlpSampleEntityCreateDto } from '../dto/nlp-sample-entity.dto';
|
||||
import { NlpSampleCreateDto, TNlpSampleDto } from '../dto/nlp-sample.dto';
|
||||
@ -78,7 +82,7 @@ export class NlpSampleService extends BaseService<
|
||||
patterns: NlpValueMatchPattern[];
|
||||
},
|
||||
page?: PageQueryDto<NlpSample>,
|
||||
projection?: ProjectionType<NlpSample>,
|
||||
projection?: TProjectionType<NlpSample>,
|
||||
): Promise<NlpSample[]> {
|
||||
if (!patterns.length) {
|
||||
return await this.repository.find(filters, page, projection);
|
||||
@ -118,7 +122,7 @@ export class NlpSampleService extends BaseService<
|
||||
patterns: NlpValueMatchPattern[];
|
||||
},
|
||||
page?: PageQueryDto<NlpSample>,
|
||||
projection?: ProjectionType<NlpSample>,
|
||||
projection?: TProjectionType<NlpSample>,
|
||||
): Promise<NlpSampleFull[]> {
|
||||
if (!patterns.length) {
|
||||
return await this.repository.findAndPopulate(filters, page, projection);
|
||||
|
@ -20,7 +20,6 @@ import {
|
||||
HydratedDocument,
|
||||
Model,
|
||||
PipelineStage,
|
||||
ProjectionType,
|
||||
Query,
|
||||
SortOrder,
|
||||
UpdateQuery,
|
||||
@ -33,6 +32,7 @@ import {
|
||||
TFilterQuery,
|
||||
TFlattenOption,
|
||||
THydratedDocument,
|
||||
TProjectionType,
|
||||
TQueryOptions,
|
||||
} from '@/utils/types/filter.types';
|
||||
|
||||
@ -351,7 +351,7 @@ export abstract class BaseRepository<
|
||||
*/
|
||||
protected findOneQuery(
|
||||
criteria: string | TFilterQuery<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Query<T | null, T, object, T, 'findOne', object> {
|
||||
if (!criteria) {
|
||||
// An empty criteria would return the first document that it finds
|
||||
@ -378,7 +378,7 @@ export abstract class BaseRepository<
|
||||
async findOne(
|
||||
criteria: string | TFilterQuery<T>,
|
||||
options?: ClassTransformOptions,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T | null> {
|
||||
if (!criteria) {
|
||||
// @TODO : Issue a warning ?
|
||||
@ -401,7 +401,7 @@ export abstract class BaseRepository<
|
||||
*/
|
||||
async findOneAndPopulate(
|
||||
criteria: string | TFilterQuery<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull | null> {
|
||||
this.ensureCanPopulate();
|
||||
const query = this.findOneQuery(criteria, projection).populate(
|
||||
@ -413,7 +413,7 @@ export abstract class BaseRepository<
|
||||
protected findQuery(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Query<T[], T, object, T, 'find', object>;
|
||||
|
||||
/**
|
||||
@ -422,7 +422,7 @@ export abstract class BaseRepository<
|
||||
protected findQuery(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Query<T[], T, object, T, 'find', object>;
|
||||
|
||||
/**
|
||||
@ -439,7 +439,7 @@ export abstract class BaseRepository<
|
||||
protected findQuery(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Query<T[], T, object, T, 'find', object> {
|
||||
if (Array.isArray(pageQuery)) {
|
||||
const query = this.model.find<T>(filter, projection);
|
||||
@ -461,7 +461,7 @@ export abstract class BaseRepository<
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]>;
|
||||
|
||||
/**
|
||||
@ -470,7 +470,7 @@ export abstract class BaseRepository<
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]>;
|
||||
|
||||
/**
|
||||
@ -490,7 +490,7 @@ export abstract class BaseRepository<
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]> {
|
||||
if (Array.isArray(pageQuery)) {
|
||||
const query = this.findQuery(filter, pageQuery, projection);
|
||||
@ -518,7 +518,7 @@ export abstract class BaseRepository<
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]>;
|
||||
|
||||
/**
|
||||
@ -527,7 +527,7 @@ export abstract class BaseRepository<
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]>;
|
||||
|
||||
/**
|
||||
@ -547,7 +547,7 @@ export abstract class BaseRepository<
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]> {
|
||||
this.ensureCanPopulate();
|
||||
if (Array.isArray(pageQuery)) {
|
||||
|
@ -9,12 +9,12 @@
|
||||
import { ConflictException, Inject } from '@nestjs/common';
|
||||
import { ClassTransformOptions } from 'class-transformer';
|
||||
import { MongoError } from 'mongodb';
|
||||
import { ProjectionType } from 'mongoose';
|
||||
|
||||
import { LoggerService } from '@/logger/logger.service';
|
||||
import {
|
||||
TFilterQuery,
|
||||
TFlattenOption,
|
||||
TProjectionType,
|
||||
TQueryOptions,
|
||||
} from '@/utils/types/filter.types';
|
||||
|
||||
@ -51,14 +51,14 @@ export abstract class BaseService<
|
||||
async findOne(
|
||||
criteria: string | TFilterQuery<T>,
|
||||
options?: ClassTransformOptions,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T | null> {
|
||||
return await this.repository.findOne(criteria, options, projection);
|
||||
}
|
||||
|
||||
async findOneAndPopulate(
|
||||
criteria: string | TFilterQuery<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
) {
|
||||
return await this.repository.findOneAndPopulate(criteria, projection);
|
||||
}
|
||||
@ -66,7 +66,7 @@ export abstract class BaseService<
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]>;
|
||||
|
||||
/**
|
||||
@ -75,13 +75,13 @@ export abstract class BaseService<
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]>;
|
||||
|
||||
async find(
|
||||
filter: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<T[]> {
|
||||
if (Array.isArray(pageQuery))
|
||||
return await this.repository.find(filter, pageQuery, projection);
|
||||
@ -92,7 +92,7 @@ export abstract class BaseService<
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]>;
|
||||
|
||||
/**
|
||||
@ -101,13 +101,13 @@ export abstract class BaseService<
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]>;
|
||||
|
||||
async findAndPopulate(
|
||||
filters: TFilterQuery<T>,
|
||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||
projection?: ProjectionType<T>,
|
||||
projection?: TProjectionType<T>,
|
||||
): Promise<TFull[]> {
|
||||
if (Array.isArray(pageQuery))
|
||||
return await this.repository.findAndPopulate(
|
||||
|
@ -146,3 +146,9 @@ export type THydratedDocument<T> = TOmitId<HydratedDocument<T>>;
|
||||
export type TFlattenOption = { shouldFlatten?: boolean };
|
||||
|
||||
export type TQueryOptions<D> = (QueryOptions<D> & TFlattenOption) | null;
|
||||
|
||||
export type TProjectField = 0 | 1;
|
||||
|
||||
export type TProjectionType<T> = {
|
||||
[K in keyof T]?: TProjectField;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user