dbFindOpts Quick Reference
Description
The dbFindOpts parameter provides options for post-processing data after it has been retrieved from the database but before it is returned to the client. These options control:
- Ordering - Sort results by a field
- Pagination - Skip and limit the number of returned entries
- Direction - Reverse the order of results
- Aggregation - Group, count, min, max, and average over fields
This allows for efficient data manipulation without requiring additional processing on the client side.
Options
reverse
Reverses the order of the results.
// Query
{
dbFindOpts: {
reverse: true
}
}
// If original results are: [1, 2, 3, 4, 5]
// Final results will be: [5, 4, 3, 2, 1]
Note: When sortBy is not specified, reverse is applied immediately during file iteration. When sortBy is used, the final sort order is determined by sortAsc.
offset
Skips a specified number of entries from the beginning of the results.
// Query
{
dbFindOpts: {
offset: 10
}
}
// Skips the first 10 entries and returns the rest
Default: 0
limit
Limits the number of returned entries.
// Query
{
dbFindOpts: {
limit: 20
}
}
// Returns only the first 20 entries (after offset is applied)
Default: -1 (no limit)
sortBy
Sorts results by a specified field name or randomly.
// Sort by a field
{
dbFindOpts: {
sortBy: "age"
}
}
// Random sort
{
dbFindOpts: {
sortBy: "random()"
}
}
Special value: "random()" - Shuffles results randomly using Math.random().
Default: undefined (no sorting, uses data order)
sortAsc
Determines the sort direction when sortBy is specified.
// Ascending order (default)
{
dbFindOpts: {
sortBy: "age",
sortAsc: true
}
}
// Descending order
{
dbFindOpts: {
sortBy: "age",
sortAsc: false
}
}
Default: true
groupBy
Groups results by one or more fields. When specified alongside aggregation operators (min, max, avg, count), computations are performed per group.
// Group by a single field
{
dbFindOpts: {
groupBy: "category",
count: { total: "id" }
}
}
// Group by multiple fields
{
dbFindOpts: {
groupBy: ["category", "status"],
count: { total: "id" }
}
}
Type: string | string[]
Default: undefined (no grouping, single aggregate over all data)
count
Counts non-null, non-undefined values per field. Maps output keys to source fields.
// Count total entries
{
dbFindOpts: {
count: { total: "id" }
}
}
// Count with grouping
{
dbFindOpts: {
groupBy: "category",
count: { total: "id", activeUsers: "status" }
}
}
Type: Record<string, string> - { outputKey: sourceField }
Default: undefined
min
Computes the minimum numeric value of a field. Maps output keys to source fields.
// Youngest age across all entries
{
dbFindOpts: {
min: { youngest: "age" }
}
}
// Min with grouping
{
dbFindOpts: {
groupBy: "category",
min: { cheapest: "price" }
}
}
Type: Record<string, string> - { outputKey: sourceField }
Default: undefined
max
Computes the maximum numeric value of a field. Maps output keys to source fields.
// Oldest age across all entries
{
dbFindOpts: {
max: { oldest: "age" }
}
}
// Max with grouping
{
dbFindOpts: {
groupBy: "category",
max: { mostExpensive: "price" }
}
}
Type: Record<string, string> - { outputKey: sourceField }
Default: undefined
avg
Computes the average (mean) numeric value of a field. Maps output keys to source fields.
// Average age across all entries
{
dbFindOpts: {
avg: { averageAge: "age" }
}
}
// Average with grouping
{
dbFindOpts: {
groupBy: "category",
avg: { averagePrice: "price" }
}
}
Type: Record<string, string> - { outputKey: sourceField }
Default: undefined
Execution Flow
Without Aggregation (min/max/avg/groupBy/count)
- Iterate through files
- Apply
reverseduring iteration (if specified) - Apply
offset(skip entries) - Apply
limit(truncate results)
With Aggregation (min/max/avg/groupBy/count)
- Collect all entries from all files
- If
sortByis specified, sort entries: - If
sortBy === "random()": shuffle randomly - Otherwise: sort by field value using
compareSafe() - Group entries by
groupByfields (if specified) - Per group, compute
min,max,avg,count(if specified) - Apply
offsetandlimitto aggregated results
With sortBy (no aggregation)
- Collect all entries from all files
- Sort entries:
- If
sortBy === "random()": shuffle randomly - Otherwise: sort by field value using
compareSafe() - Apply
offsetandlimitto sorted results
Examples
Basic Pagination
// Get page 2 with 10 items per page
{
dbFindOpts: {
offset: 10,
limit: 10
}
}
Sort by Field Descending
// Get users sorted by age (oldest first)
{
dbFindOpts: {
sortBy: "age",
sortAsc: false,
limit: 50
}
}
Random Sample
// Get 10 random entries
{
dbFindOpts: {
sortBy: "random()",
limit: 10
}
}
Reverse Order
// Get results in reverse file order
{
dbFindOpts: {
reverse: true,
offset: 0,
limit: 100
}
}
Combined Options
// Complex query with sorting and pagination
{
dbFindOpts: {
sortBy: "createdAt",
sortAsc: false, // Newest first
offset: 0,
limit: 20
}
}
Aggregate per Group
// Get count, min, max, avg price per category
{
dbFindOpts: {
groupBy: "category",
count: { itemCount: "id" },
min: { minPrice: "price" },
max: { maxPrice: "price" },
avg: { avgPrice: "price" }
}
}
Global Aggregation (no grouping)
// Stats across all entries
{
dbFindOpts: {
count: { total: "id" },
min: { youngest: "age" },
max: { oldest: "age" },
avg: { averageAge: "age" }
}
}