Skip to content

Helpers

Helpers are syntactic sugar to get records from your PocketBase instance. In addition to simplifying the process of fetching records, they also provide type safety, autocompletion and caching.

To access helpers, you have to, at least, provide an instance of PocketBase SDK to the helpersFrom function.

import { helpersFrom } from "zod-pocketbase";
import Pocketbase from "pocketbase";
const { getRecord, getRecords } = helpersFrom({
pocketbase: new Pocketbase(import.meta.env.ZOD_POCKETBASE_URL),
cache: "1d",
});

getRecord

getRecord is a helper to get a single record from your PocketBase instance. It takes a RecordRef as its first argument and an object with a schema as its second argument.

const myFirstPost = await getRecord({ collection: "posts", slug: "my-first-post" }, { schema: PostRecord });
const mySecondPost = await getRecord({ collection: "posts", id: "3vwc4g9d23orc1r" }, { schema: PostRecord });

getRecords

getRecords is a helper to get multiple records from your PocketBase instance. It takes a collection name as its first argument and an object with some options (at least a record schema) as its second argument.

const myPosts = await getRecords("posts", { schema: PostRecord });
const someSpecificPosts = await getRecords("posts", { schema: PostRecord, sort: "-updated", page: 2, perPage: 10 });