The query is returned (not reset) using .build().
// ❌ Wrong
const query = pbQuery<Post>()
.like('content', 'Top Secret%');
console.log(query); // object with functions
// ✅ Right
const query = pbQuery<Post>()
.like('content', 'Top Secret%')
.build();
console.log(query);
// {
// fields: '',
// expand: '',
// sort: '',
// filter: {
// raw: 'content~{:content1}',
// values: { content1: 'Top Secret%' }
// }
// }
You can use this principle to create dynamic queries:
const dynamicQuery = pbQuery<Post>().like('content', 'Top Secret%');
if (user) {
dynamicQuery.and().equal('author', user.id);
}
const query = dynamicQuery.build();
By default, we don't filter your query. Using .build() returns the unfiltered query and values separately.
// ❌ Unfiltered query
const { filter } = pbQuery<Post>()
.like('content', 'Top Secret%')
.build();
console.log(filter);
// {
// raw: 'content~{:content1}',
// values: { content1: 'Top Secret%' }
// }
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");
// ✅ Filtered query
const { filter } = pbQuery<Post>()
.like('content', 'Top Secret%')
.build(pb.filter); // use PocketBase's filter function
console.log(filter); // "content~'Top Secret%'"