Pagination total count behavior across different service adapters
0 reputation · 08 Jun 2024, 16:22 UTC
0 reputation · 08 Jun 2024, 16:22 UTC
FeathersJS provides a standardized pagination interface for the find method using page and perPage parameters. However, the mechanism for calculating the total metadata field varies depending on the underlying adapter used by the service.
While the memory adapter handles the total count inherently, other database adapters may require a separate count query to populate this value. In low-traffic environments where resource optimization is a priority, executing an additional count query for every paginated request can introduce unnecessary overhead.
What is the documented strategy for ensuring consistent total field behavior when switching between adapters? Is there a configuration to disable the automatic count query to reduce database load?
32275 reputation · 09 Jun 2024, 02:00 UTC
Feathers v5+ documents a single, service-level option to control the total field: paginate.disableCount. When set to true, the framework skips the separate count query that SQL and MongoDB adapters normally execute. The memory adapter continues to return a total because it can compute it in-memory without extra I/O. If you turn pagination off entirely (paginate: false), the response becomes a plain array with no pagination metadata at all.
count query (Knex, Sequelize, MongoDB, etc.) omit it; the total property is absent from the pagination envelope.find triggers a count query; total is always present.total because the dataset is already in memory.total, just [item, item, …].src/services//.class.ts or the generated service file).paginate option with disableCount: true:
app.use('messages', new MessageService({
paginate: {
default: 25,
max: 100,
disableCount: true
}
}));
GET /messages?page=1&perPage=10
total field inside pagination.disableCount. Check the adapter’s README or source for a conditional if (!params.paginate?.disableCount) await this.Model.count… pattern.response.pagination.total will break—plan a migration or provide a fallback.npm ls @feathersjs/feathers).SELECT COUNT(*) runs.total still appears there.Which specific database adapter (e.g., @feathersjs/knex, feathers-mongoose, feathers-mongodb) are you using? A few community adapters still require a custom patch to honor disableCount.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.