@emresandikci/pocketbase-query

中文文档
0.5.1发布于 2026/11/01MITTypeScriptESM + CJS零依赖构建来源已验证

A pocketbase query builder

原始使用文档
文档目录16 节
  1. Overview
  2. Installation
  3. Usage
  4. Optional filters
  5. Fields and values
  6. Fields
  7. Values
  8. API
  9. Comparison methods
  10. in(field, values)
  11. customFilter(filter)
  12. Grouping and logic
  13. Output
  14. Development
  15. Releases
  16. License

Pocketbase Query

semantic-release

Overview

@emresandikci/pocketbase-query is a small, zero-dependency TypeScript query builder for PocketBase filters. It builds filter strings through a fluent, chainable API, escapes values safely, and drops conditions whose value is empty, which keeps optional filters (search forms, table filters) simple.

It follows the PocketBase v0.40 filter syntax.


Installation

npm i @emresandikci/pocketbase-query
// ESM / TypeScript
import PocketbaseQuery from '@emresandikci/pocketbase-query';

// CommonJS
const PocketbaseQuery = require('@emresandikci/pocketbase-query').default;

Usage

getInstance<T>() returns a new query builder. T is your record type and gives you autocomplete and type checking on field names. Every call returns its own instance, so queries can be built side by side or nested.

import PocketBase from 'pocketbase';
import PocketbaseQuery from '@emresandikci/pocketbase-query';

interface Post {
  title: string;
  status: string;
  comments: number;
  tags: string[];
  created: string;
}

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

const filter = PocketbaseQuery.getInstance<Post>()
  .equal('status', 'active')
  .and()
  .greaterThan('comments', 50)
  .build();
// status="active" && comments>50

const posts = await pb.collection('posts').getFullList({
  filter,
  expand: 'comments_via_post',
});

Optional filters

Conditions with an empty value (null, undefined, '', NaN) are skipped, together with the && / || and empty brackets left around them. You can chain every possible filter and let the builder drop the unused ones:

function buildFilter(form: { search?: string; status?: string; tag?: string }) {
  return PocketbaseQuery.getInstance<Post>()
    .openBracket()
    .like('title', form.search)
    .or()
    .anyLike('tags', form.tag)
    .closeBracket()
    .and()
    .equal('status', form.status)
    .build();
}

buildFilter({ search: 'pocketbase' }); // (title~"pocketbase")
buildFilter({ status: 'active' }); // status="active"
buildFilter({}); // ""

Fields and values

Fields

field is a key of T, optionally with:

  • a modifier: title:lower (case-insensitive), tags:length (item count), tags:each (every item)
  • a relation path: author.name
query.like('title:lower', 'news').and().greaterThan('tags:length', 2).and().equal('author.name', 'John');
// title:lower~"news" && tags:length>2 && author.name="John"

Values

value can be string | number | boolean | Date | null | undefined.

ValueOutput
'active'"active", escaped like pb.filter() in the official SDK, so quotes and backslashes in user input cannot break out of the string
50, true50, true (no quotes)
new Date('2026-01-02T03:04:05Z')"2026-01-02 03:04:05.000Z" (PocketBase datetime format)
'@now', '@todayStart', ...@now, @todayStart (datetime macros, no quotes)
null, undefined, '', NaNcondition is skipped

Supported macros: @now, @second, @minute, @hour, @weekday, @day, @month, @year, @yesterday, @tomorrow, @todayStart, @todayEnd, @monthStart, @monthEnd, @yearStart, @yearEnd.

Editors suggest these macros as you type a value, and field names with their modifiers as you type a field. Any other string still works as a normal value.

query.greaterThanOrEqual('created', '@todayStart').and().lessThan('created', new Date('2026-12-31'));
// created>=@todayStart && created<"2026-12-31 00:00:00.000Z"

API

Comparison methods

All methods take (field, value) and return the builder for chaining.

MethodOperatorMethod (any item of an array field)Operator
equal=anyEqual?=
notEqual!=anyNotEqual?!=
greaterThan>anyGreaterThan?>
greaterThanOrEqual>=anyGreaterThanOrEqual?>=
lessThan<anyLessThan?<
lessThanOrEqual<=anyLessThanOrEqual?<=
like~anyLike?~
notLike!~anyNotLike?!~

like / notLike match partially: PocketBase wraps the value in % unless you add a % wildcard yourself ('Lorem%').

query.anyEqual('tags', 'sale');
// tags?="sale"

in(field, values)

Joins one like condition per value with ||. Empty values are skipped.

query.in('status', ['draft', 'review']);
// status~"draft" || status~"review"

Because it uses ~, 'act' also matches active and inactive. Use equal with or() for exact matches.

customFilter(filter)

Appends a raw filter string for syntax the builder does not cover, such as strftime(), geoDistance(), @request.* or @collection.*. It is not escaped, so never pass untrusted input.

query
  .customFilter("strftime('%Y-%m', created) = '2026-01'")
  .and()
  .customFilter('geoDistance(address.lon, address.lat, 23.32, 42.69) < 25');

Grouping and logic

MethodAdds
and()&&
or()||
openBracket() / closeBracket()( / )
query
  .openBracket()
  .equal('status', 'active')
  .or()
  .equal('status', 'pending')
  .closeBracket()
  .and()
  .greaterThan('comments', 50)
  .build();
// (status="active" || status="pending") && comments>50

Output

MethodReturns
build()The final filter string. Removes leftover operators and empty brackets, then resets the builder.
getQuery()The raw string built so far, without cleanup or reset. Useful for debugging.
getLastQueryValue()The last value added to the query.

Development

npm ci
npm test           # jest
npm run typecheck  # tsc
npm run validate   # prettier --check + eslint
npm run build      # tsup -> dist/ (ESM + CJS + types)

Releases

Versions and npm releases are automated with semantic-release. Every push to main runs the tests, and when the commits since the last tag contain a fix:, feat: or breaking change, the next version is published to npm with provenance, tagged, and added to CHANGELOG.md. Pull requests get a comment with the version they would release.

Commit with npm run commit or write Conventional Commits by hand.


License

MIT License

版本与发布记录

@emresandikci/pocketbase-query 共发布 7 个版本,首次发布于 2025/01/31;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/11/012 天前
0.5.02026/11/012 天前
0.4.12025/05/261.4 年前
0.4.02025/02/261.7 年前
0.3.02025/02/231.7 年前
0.2.02025/02/041.7 年前

关于 @emresandikci/pocketbase-query 的常见问题

@emresandikci/pocketbase-query 是什么?

一个pocketbase查询生成器

如何安装 @emresandikci/pocketbase-query?

在项目目录运行 npm install @emresandikci/pocketbase-query,或使用 pnpm add / yarn add / bun add。

@emresandikci/pocketbase-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@emresandikci/pocketbase-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@emresandikci/pocketbase-query 可以商用吗?

@emresandikci/pocketbase-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@emresandikci/pocketbase-query 还在维护吗?

最新版本 0.5.1 发布于 2026/11/01(2 天前);至今共发布 7 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/01/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。