savannabits / typeorm-pagination

A Pagination plugin for Node Express with TypeORM
MIT License
48 stars 8 forks source link

How to paginate when using Active Record pattern? #2

Closed avidianity closed 3 years ago

avidianity commented 3 years ago

I have a Statement entity.

import { Column, Entity, BaseEntity } from 'typeorm';

@Entity()
export class Statement extends BaseEntity {
    @Column()
    school: string;

    @Column()
    schoolAddress: string;

    @Column()
    referenceNumber: string;

    @Column()
    date: string;

    @Column()
    to: string;
}

How do I paginate this? Something like Statement.paginate() would be great.

coolsam726 commented 3 years ago

@avidianity , unfortunately I haven't extended the package to support the Active Record pattern, for now it only supports the Repository pattern. Any ideas or PRs are welcome. Thanks.

coolsam726 commented 3 years ago

@avidianity and anyone still interested in how to use Pagination with the Active Record Pattern, I revisited this. The package supports AR pagination out of the box. Since pagination is built as an extension of the SelectQuerybuilder interface, here is how to achieve this:

Your Entity

@Entity()
export class UserType extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;
    @Column({
        unique: true,
        type: String
    })
    slug: string;

    @Column({
        unique: true,
        type: String
    })
    name: string;
    @OneToMany(type => User, user => user.type)
    users: User[]
}

Paginate using AR

return await UserType.createQueryBuilder().paginate();