pb-querynpm install @sergio9929/pb-query
pnpm add @sergio9929/pb-query
yarn add @sergio9929/pb-query
import PocketBase from 'pocketbase';
const pb = new PocketBase("https://example.com");
export interface User {
id: string;
name: string;
email: string;
created: Date;
updated: Date;
}
export interface Post {
id: string;
title: string;
content: string;
author: User;
status: 'draft' | 'published';
created: Date;
updated: Date;
}
You can read more about how to set up you types in Setting up Types.
import { pbQuery } from '@sergio9929/pb-query';
import type { Post } from './types';
const query = pbQuery<Post>() // posts collection
.fields([
'title',
'content:excerpt(100,true)',
'status',
'author',
'expand.author.name', // Automatically expanded
])
.search(['title', 'content', 'author.name'], 'footba')
.and()
.group((q) =>
q.equal('status', 'published')
.or()
.equal('author.name', 'Sergio')
)
.sort(['-created'])
.build(pb.filter);
Now your query object contains the properly formatted filter, expand, fields, and sort strings.
You can read more about the output returned by pbQuery() in Returning Values.
const records = await pb.collection("posts").getList(1, 20, query);
pb-querynpm install @sergio9929/pb-query
pnpm add @sergio9929/pb-query
yarn add @sergio9929/pb-query
/// <reference path="../pb_data/types.d.ts" />
routerAdd("GET", "/example", (e) => {
const { pbQuery } = require('@sergio9929/pb-query');
const { filter, sort } = pbQuery()
.fields([
'title',
'content:excerpt(100,true)',
'status',
'author',
'expand.author.name', // Automatically expanded
])
.search(['title', 'content', 'author.name'], 'footba')
.and()
.group((q) =>
q.equal('status', 'published')
.or()
.equal('author.name', 'Sergio')
)
.sort(['-created'])
.build(pb.filter);
// ...
});
Now your query object contains the properly formatted filter, expand, fields, and sort strings.
You can read more about the output returned by pbQuery() in Returning Values.
routerAdd("GET", "/example", (e) => {
// ...
const records = $app.findRecordsByFilter(
'posts',
filter.raw,
sort,
20,
0,
filter.values,
);
return e.json(200, records);
});
import PocketBase from 'pocketbase';
import { pbQuery } from '@sergio9929/pb-query';
import type { Post } from './types';
const pb = new PocketBase("https://example.com");
const query = pbQuery<Post>() // posts collection
.fields([
'title',
'content:excerpt(100,true)',
'status',
'author',
'expand.author.name', // Automatically expanded
])
.search(['title', 'content', 'author.name'], 'footba')
.and()
.group((q) =>
q.equal('status', 'published')
.or()
.equal('author.name', 'Sergio')
)
.sort(['-created'])
.build(pb.filter);
const records = await pb.collection("posts").getList(1, 20, query);