Skip to content

delimaa/mikro-orm-cursor-pagination

Repository files navigation

MikroORM Cursor Pagination

Cursor pagination support for MikroORM following Relay GraphQL Cursor Connections Specification.

Note

This library is not relevant for MikroORM v6 as the native em.findByCursor() is the recommended way for cursor pagination. However, it is still useful for software using pre v6 versions.

Installation

npm install @delimaa/mikro-orm-cursor-pagination

Usage

import { cursorPaginationFind } from '@delimaa/mikro-orm-cursor-pagination';

@Entity()
class User {
  @PrimaryKey()
  id!: number;

  @Property()
  name!: string;

  @Property({ nullable: true })
  age: number | null;

  @ManyToMany()
  friends = new Collection<User>(this);
}

// Initial cursor pagination when no cursor is provided
let page = await cursorPaginationFind(
  em,
  User,
  // 👇 Initial pagination when no cursor is provided, pass `first` & `orderBy`
  {
    first: 10,
    orderBy: {
      name: QueryOrder.DESC,
      // 👇 Supports NULL ordering by specifying NULLS_FIRST or NULLS_LAST order on nullable column
      age: QueryOrder.ASC_NULLS_FIRST,
      id: QueryOrder.ASC,
    },
  },
  // 👇 Pass any native MikroORM where condition
  {
    age: { $gt: 20 },
  },
  // 👇 Pass any native MikroORM options except `limit`, `offset` and `orderBy` properties which are not allowed
  {
    populate: ['friends'],
  },
);

page.totalCount; // Total count of records
page.pageInfo.hasNextPage; // Whether next page is available
page.pageInfo.hasPreviousPage; // Whether previous page is available
page.pageInfo.startCursor; // Start cursor
page.pageInfo.endCursor; // End cursor
page.edges; // Array of edges containing cursor and node { cursor: string, node: Loaded<User, 'friends'> }

// Forward cursor pagination
page = await cursorPaginationFind(
  em,
  User,
  // 👇 For forward cursor pagination, pass `first` & `after`
  {
    first: 10,
    after: page.pageInfo.endCursor,
  },
  {
    age: { $gt: 20 },
  },
  {
    populate: ['friends'],
  },
);

// Backward cursor pagination
page = await cursorPaginationFind(
  em,
  User,
  // 👇 For backward cursor pagination, pass `last` & `before`
  {
    last: 10,
    before: page.pageInfo.startCursor,
  },
  {
    age: { $gt: 20 },
  },
  {
    populate: ['friends'],
  },
);

About

Cursor pagination for MikroORM following Relay GraphQL Cursor Connections Specification

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published