3.4.7 • Published 27 days ago

@sigmaott/paginate v3.4.7

Weekly downloads
-
License
MIT
Repository
github
Last release
27 days ago

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

3.4.7

27 days ago

3.4.6

5 months ago

3.4.5

5 months ago

3.4.4

1 year ago

3.4.3

1 year ago

3.4.2

1 year ago

3.4.0

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.4.1

2 years ago

3.3.2

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.1.2

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.11

3 years ago

2.0.7

3 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago