Имеется приложение на Nest Js с Mongo DB и mongoose
Как сделать пагинацию?
например для получения 10 записей для каждой страницы
CreateSnippetDto:
export class CreateSnippetDto {
readonly title: string
readonly steps: Array<Object>
readonly tags: Array<String>
}
snippet.schema.ts:
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
export type SnippetDocument = Snippet & Document
@Schema()
export class Snippet {
@Prop()
title: string
@Prop()
steps: Array<Object>
@Prop()
tags: Array<String>
}
export const SnippetSchema = SchemaFactory.createForClass(Snippet)
Service:
@Injectable()
export class SnippetsService {
constructor(@InjectModel(Snippet.name) private snippetModel: Model<SnippetDocument>) {
}
//get all snippets from database
async getAll(): Promise<Snippet[]> {
return await this.snippetModel.find().exec();
}
}
Controller:
@Controller('snippets')
export class SnippetsController {
constructor(private readonly snippetsService: SnippetsService) {
}
@Get()
@HttpCode(HttpStatus.OK)
async getAll(): Promise<Snippet[]> {
const snippets = await this.snippetsService.getAll();
return snippets;
}
}