sequelize-typescript-paginate
Sequelize Base Model with pagination for Sequelize-Typescript
Inspired on https://github.com/eclass/sequelize-paginate
Installation
npm i sequelize-typescript-paginate
Usege
import { PaginatedModel } from 'sequelize-typescript-pagination';
import { Sequelize, Column, DataType, ForeignKey, Table, HasMany } from 'sequelize-typescript';
@Table({ tableName: 'book' })
export class Book extends PaginatedModel {
@Column({ type: DataType.STRING })
name: string;
@ForeignKey(() => Author)
@Column({ type: DataType.INTEGER })
authorId: string;
}
@Table({ tableName: 'author' })
export class Author extends PaginatedModel {
@Column({ type: DataType.STRING })
name: string;
@HasMany(() => Book)
books: Book[];
}
const { docs, pages, total } = await Author.paginate();
const options = {
attributes: ['id', 'name'],
page: 1,
pageSize: 25,
order: [['name', 'DESC']],
where: { name: { [Op.like]: `%elliot%` } },
};
const { docs, pages, total } = await Author.paginate(options);
NOTE: If options includes limit or offset they will be ignored.
License
MIT