@sigmaott/paginate
v3.4.7
Published
A pagination, filtering and sorting lib for nestjs using mongoose orm
Downloads
186
Readme
nestjs-mongoose-paginate
A pagination, filtering and sorting lib for nestjs using mongoose orm
Usage
Exposing properties
Create a class to hold information about filterable and sortable properties
You need to @ExposeQuery
properties of this class. By default none of it is filterable nor sortable.
You should set this parameters explicitly.
In case you want to expose some of the properties with a different name, you need to specify a name
option in this decorator.
import {
CollectionProperties,
ExposeQuery
} from '@sigmaott/paginate';
export class MyCollectionEntity {
@ExposeQuery({ name: 'createdAt', sortable: true })
readonly created_at: 'desc' | 'asc';
@ExposeQuery({ sortable: true, default: true, filterable: true })
readonly userName: 'desc' | 'asc';
readonly unsortable: string;
}
Validation Pipe
import {
CollectionDto,
CollectionValidationPipe,
CollectionResponse
} from '@sigmaott/paginate';
@Controller()
export class AppController {
@Get('list')
async filter(
@Query(new CollectionValidationPipe(MyCollectionEntity))
collectionDto: CollectionDto,
): Promise<CollectionResponse<MyDocument>> {
return await this.service.list(collectionDto);
}
}
Document collector usage
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import {
CollectionDto,
DocumentCollector,
CollectionResponse
} from '@sigmaott/paginate';
@Injectable()
export class AppService {
private moduleCollection
constructor(
@InjectModel('MyModel') private readonly model: Model<MyDocument>,
)
onModuleInit() {
this.modelCollection = new DocumentCollector<MyDocument>(this.model);
}
async list(
collectionDto: CollectionDto,
): Promise<CollectionResponse<MyDocument>> {
return this.modelCollection.find(collectionDto);
}
}
Queries
You may now send a request, like in example below:
http://localhost:3000/list?username=test&sort=createdAt|asc&page=0&perPage=10
query parameter
you can make complex query mongo via field q
q={"$and":[{"userInput.selectedOption":"0"}]}
Selection
You can limit field return in response by fields
fields=id,name,age,!sex
field start with character ! meaning you will receive response without this field
Sort
You can specify more than one field for sorting by providing a list of them separated by a semicolon: createdAt|asc,userName|desc
.
Filter
filter format example
field=$gte:3&name=$cont:name
A subset of mongoose query language is supported. Currently only these operators are supported:
- $eq (=, equal)
- $ne (!=, not equal)
- $gt (>, greater than)
- $lt (<, lower that)
- $gte (>=, greater than or equal)
- $lte (<=, lower than or equal)
- $in (IN, in range, accepts multiple values)
- $nin (NOT IN, not in range, accepts multiple values)
- $regex (REGEX, regex)
- $not (NOT, negation)
- $like (LIKE, %like%)
populate
You can populate nested documents by providing a list of them separated by a semicolon: populate=user,author