巢-多糖-贴花
使用 mongoose orm 的 paging, 过滤和排序 lib 用于 nockjs v8( 对于 v7 请使用 < v1.1)
使用量
曝光属性
创建类以保存关于可过滤和可排序属性的信息
你必须要 @Expose 属性。 默认情况下,它们中没有任何一个是可过滤的或可分类的.
您应该明确设置此参数 .
如果您想要披露一些名称不同的属性, 您需要指定一个 name 选项.
import {
CollectionProperties,
Expose
} from '@forlagshuset/nestjs-mongoose-paginate';
export class MyCollectionProperties extends CollectionProperties {
@Expose({ name: 'createdAt', sortable: true })
readonly created_at: 'desc' | 'asc';
@Expose({ sortable: true, default: true, filterable: true })
readonly userName: 'desc' | 'asc';
readonly unsortable: string;
}
验证管道
import {
CollectionDto,
ValidationPipe,
CollectionResponse
} from '@forlagshuset/nestjs-mongoose-paginate';
@Controller()
export class AppController {
@Get('list')
async filter(
@Query(new ValidationPipe(MyCollectionProperties))
collectionDto: CollectionDto,
): Promise<CollectionResponse<MyDocument>> {
return await this.service.list(collectionDto);
}
}
文档收集器的使用
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import {
CollectionDto,
DocumentCollector,
CollectionResponse
} from '@forlagshuset/nestjs-mongoose-paginate';
@Injectable()
export class AppService {
constructor(
@InjectModel('MyModel') private readonly model: Model<MyDocument>,
)
async list(
collectionDto: CollectionDto,
): Promise<CollectionResponse<MyDocument>> {
const collector = new DocumentCollector<MyDocument>(this.model);
return collector.find(collectionDto);
}
}
查询
您现在可以发出请求, 例如:
http://localhost:3000/list?filter={"userName": {"$regex": "^test"}}&sort=-createdAt&page=0&limit=100
您需要运行参数通过 urlencode 来正确解析查询。 所以过滤器部分看起来更像
%7B%22userName%22%3A%20%7B%22%24regex%22%3A%20%22%5Etest%22%7D%7D
排序
您可以通过提供用分号分隔的列表来指定一个以上的字段进行排序: createdAt;userName。 。 。 .
要使用 DESC 排序顺序 : -createdAt。 。 。 .
过滤器
梦雁查询语言的子集被支持. 目前仅支持这些运算符:
'$eq', '$gt', '$gte', '$in', '$lt', '$lte', '$ne', '$nin', '$and', '$not', '$nor', '$or', '$regex'