sequelize-typescript-paginate
v1.1.0
Published
Sequelize Model with pagination for Sequelize-Typescript
Downloads
9
Readme
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[];
}
//...
//...
//...
// Default page = 1 and pageSize = 25
const { docs, pages, total } = await Author.paginate();
// Or with extra options
const options = {
attributes: ['id', 'name'],
page: 1, // Default 1
pageSize: 25, // Default 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.