Getting Started
Tutorial
A step-by-step guide to building type-safe PocketBase queries with pb-query.
Install pb-query
npm install @sergio9929/pb-query
pnpm add @sergio9929/pb-query
yarn add @sergio9929/pb-query
Create a PocketBase instance
example.ts
import PocketBase from 'pocketbase';
const pb = new PocketBase("https://example.com");
Define your types
types.ts
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.
Build a type-safe query
example.ts
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.
Use your query
example.ts
const records = await pb.collection("posts").getList(1, 20, query);
::
Install pb-query
npm install @sergio9929/pb-query
pnpm add @sergio9929/pb-query
yarn add @sergio9929/pb-query
Build a query
pb_hooks/example.pb.js
/// <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.
Use your query
pb_hooks/example.pb.js
routerAdd("GET", "/example", (e) => {
// ...
const records = $app.findRecordsByFilter(
'posts',
filter.raw,
sort,
20,
0,
filter.values,
);
return e.json(200, records);
});
:: ::
Full example
app/example.ts
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);