Returning Values

Filter

Filters results using operators.
There is no .filter() function, you construct it using operators.

By default, we don't filter your query. Using .build() returns the unfiltered query and values separately. This is useful if you want to use pb-query inside PocketBase Hooks, you can see how in the tutorial.

const { filter } = pbQuery<Post>()
  .like('content', 'Top Secret%')
  .build();

console.log(filter);
// {
//   raw: 'content~{:content1}',
//   values: { content1: 'Top Secret%' }
// }

If you want to use pb-query in your app (this is the main usecase of pb-query), you need to filter it. We expose a filter function, but we recommend using the native pb.filter() function instead.

import PocketBase from 'pocketbase';

// PocketBase instance
const pb = new PocketBase("https://example.com");

const { filter } = pbQuery<Post>()
  .like('content', 'Top Secret%')
  .build(pb.filter); // use PocketBase's filter function

console.log(filter);
// "content~'Top Secret%'"
You can see a full example in the tutorial.

Dynamic Queries

Since v0.3.2

pb-query cleans the final query by removing unnecessary spaces and any leading or trailing logical operators, even inside the .custom() function, so you can safely build dynamic queries like these:

const query = pbQuery<User>().equal("name", "Sergio").and();

query.equal("city", "New York").and(); // trailing logical operator

console.log(query.build(pb.filter).filter);
// Output: "name='Sergio' && city='New York'"
This project is maintained by @sergio9929 with ❤️