Pagination
Use take and skip to implement pagination for large datasets.
Quick Navigation
Basic Pagination
Implement page-based pagination:
import { DataStore } from "@routier/datastore";
import { s } from "@routier/core/schema";
import { MemoryPlugin } from "@routier/memory-plugin";
const productSchema = s.define("products", {
_id: s.string().key().identity(),
name: s.string(),
price: s.number(),
category: s.string(),
inStock: s.boolean(),
tags: s.string("computer", "accessory").array(),
createdDate: s.date().default(() => new Date())
}).compile();
class AppDataStore extends DataStore {
products = this.collection(productSchema).create();
constructor() {
super(new MemoryPlugin("app"));
}
}
const dataStore = new AppDataStore();
// Pagination - get second page (skip first 10, take next 10)
const pageSize = 10;
const pageNumber = 2; // 0-based
const secondPage = await dataStore.products
.skip(pageSize * pageNumber)
.take(pageSize)
.toArrayAsync();Simple Take and Skip
// Get first 10 items
const firstPage = await dataStore.products.take(10).toArrayAsync();
// Skip first 10, get next 10
const secondPage = await dataStore.products.skip(10).take(10).toArrayAsync();
Pagination with Filtering
Paginate filtered results:
const expensiveProductsPage = await dataStore.products
.where((p) => p.price > 100)
.sort((p) => p.price)
.skip(20)
.take(10)
.toArrayAsync();
Pagination with Sorting
Paginate sorted results:
const sortedProductsPage = await dataStore.products
.sortDescending((p) => p.price)
.skip(0)
.take(5)
.toArrayAsync();