npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pb-params

v0.2.3

Published

Type-safe PocketBase query parameters builder with pocketbase-typegen integration

Downloads

13

Readme

pb-params

npm version npm downloads TypeScript License: MIT

Type-safe PocketBase query parameters builder with pocketbase-typegen integration and auto-expand functionality.

Overview

pb-params is a comprehensive TypeScript library that provides type-safe building of all PocketBase query parameters including filtering, field selection, relation expansion, sorting, and pagination. It's designed to work seamlessly with types generated by pocketbase-typegen.

Features

  • 🔥 Complete Parameter Coverage: Filter, fields, expand, sort, and pagination
  • 🎯 Type-Safe: Full TypeScript support with collection-specific typing
  • 🔗 Typegen Integration: Works seamlessly with pocketbase-typegen output
  • 🏗️ Fluent API: Chain methods for intuitive query building
  • Auto-Expand: Automatically generates expand from field paths - no redundancy!
  • 📦 Zero Dependencies: Lightweight with no runtime dependencies
  • 🎨 Conditional Building: Apply parameters based on runtime conditions

Installation

Install pb-params with your preferred package manager:

# npm
npm install pb-params

# pnpm
pnpm add pb-params

# yarn
yarn add pb-params

# bun
bun add pb-params

Requirements

  • TypeScript: 5.0+
  • Node.js: 18+

Getting Started

1. Generate Types (Optional but Recommended)

First, generate TypeScript types for your PocketBase collections:

npx pocketbase-typegen --db ./pb_data/data.db --out pocketbase-types.ts

2. Basic Usage

import { pbParams } from 'pb-params'
import PocketBase from 'pocketbase'
import type { UsersRecord } from './pocketbase-types' // Generated types

const pb = new PocketBase('http://127.0.0.1:8090')

// Build comprehensive query parameters with auto-expand
const params = pbParams<UsersRecord>()
  .filter(q => q
    .equal('verified', true)
    .and()
    .like('name', 'John%')
  )
  .fields(['id', 'name', 'email', 'created', 'expand.profile.avatar', 'expand.organization.name'])
  .sort(['-created', 'name'])
  .page(1, 20)
  .build()
  // Auto-generates: { expand: "profile,organization" }

// Use with PocketBase SDK
const records = await pb.collection('users').getList(1, 20, params)

3. Without Generated Types

You can also use pb-params without generated types:

interface User {
  id: string
  name: string
  email: string
  verified: boolean
  expand?: {
    profile?: { avatar: string }
  }
}

const params = pbParams<User>()
  .filter(q => q.equal('verified', true))
  .fields(['id', 'name', 'email', 'expand.profile.avatar'])
  .build()
  // Auto-generates: { expand: "profile" }

API Reference

pbParams<T>()

Creates a new parameter builder for collection type T.

const builder = pbParams<UsersRecord>()

Filtering

Build type-safe filters with full operator support:

pbParams<UsersRecord>()
  .filter(q => q
    .equal('status', 'active')
    .and()
    .greaterThan('age', 18)
    .and()
    .like('email', '%@company.com')
  )

Supported operators:

  • equal, notEqual
  • greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual
  • like, notLike
  • anyEqual, anyNotEqual, anyGreaterThan, etc. (for arrays/relations)

Helper methods:

  • search(keys, value) - Search across multiple fields
  • in(key, values) - Value in array
  • between(key, from, to) - Range queries
  • isNull(key), isNotNull(key) - Null checks

Field Selection & Auto-Expand

Select specific fields and automatically expand relations:

pbParams<UsersRecord>()
  .fields([
    'id', 
    'name', 
    'email', 
    'expand.profile.avatar',      // Auto-expands: profile
    'expand.profile.bio',
    'expand.organization.name'    // Auto-expands: organization
  ])
  // Results in: { 
  //   fields: "id,name,email,expand.profile.avatar,expand.profile.bio,expand.organization.name",
  //   expand: "profile,organization" 
  // }

Nested Relations

Handle deeply nested relations with ease:

pbParams<UsersRecord>()
  .fields([
    'id',
    'expand.profile.expand.preferences.theme',  // Auto-expands: profile,profile.preferences
    'expand.profile.expand.preferences.notifications'
  ])

Sorting

Sort results by one or more fields:

pbParams<UsersRecord>()
  .sort(['-created', 'name']) // Descending by created, then ascending by name

Pagination

Set page and page size:

pbParams<UsersRecord>()
  .page(2, 50) // Page 2, 50 records per page

Conditional Building

Apply parameters based on runtime conditions:

const includeProfile = user.isAdmin
const sortByDate = filters.sortBy === 'date'

pbParams<UsersRecord>()
  .filterIf(filters.verified !== undefined, q => q.equal('verified', filters.verified))
  .fieldsIf(includeProfile, ['id', 'name', 'expand.profile.avatar'])  // Auto-expands profile when included
  .sortIf(sortByDate, ['-created'])

Integration with pocketbase-typegen

  1. Generate types for your PocketBase collections:
npx pocketbase-typegen --db ./pb_data/data.db --out pocketbase-types.ts
  1. Use the generated types with pb-params:
import type { CollectionRecords } from './pocketbase-types'

// Type-safe parameter building
const params = pbParams<CollectionRecords['users']>()
  .filter(q => q.equal('status', 'active')) // 'status' is validated against user fields
  .fields(['id', 'name', 'email']) // Field names are type-checked
  .build()

Advanced Usage

Custom Filter Expressions

pbParams<UsersRecord>()
  .filter(q => q
    .group(sub => sub
      .equal('role', 'admin')
      .or()
      .equal('role', 'moderator')
    )
    .and()
    .isNotNull('lastLogin')
  )

Date Filtering with Macros

pbParams<UsersRecord>()
  .filter(q => q
    .greaterThan('created', '@monthStart')
    .and()
    .lessThan('updated', '@now')
  )

Type Inference

const result = pbParams<UsersRecord>()
  .fields(['id', 'name'])
  .expand(['profile'])
  .buildTyped()

// result.resultType contains the inferred type shape
// result.params contains the query parameters
// result.raw contains the raw filter query and values

Migration from v0.1.0

Version 0.1.1 introduces auto-expand functionality that eliminates API redundancy:

Before (v0.1.0):

pbParams<User>()
  .fields(['id', 'expand.profile.bio', 'expand.organization.name'])
  .expand(['profile', 'organization'])  // Redundant!
  .build()

After (v0.1.1):

pbParams<User>()
  .fields(['id', 'expand.profile.bio', 'expand.organization.name'])
  .build()  // Auto-generates expand: "profile,organization"

The expand() and expandIf() methods have been removed as expansion is now automatically inferred from field paths.

Credits & Inspiration

This project was inspired by:

Comparison with pb-query

pb-params builds upon the success of pb-query but provides:

  • Broader Scope: Complete parameter coverage vs. filtering only
  • Collection-Specific Types: Built for pocketbase-typegen integration from day one
  • Unified API: Single library for all PocketBase query needs
  • Auto-Expand: Eliminates redundancy with automatic expand generation
  • Enhanced Features: Conditional building, nested expansion, type inference

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.