Case StudiesProductsBlogOpen SourceBook a call

pocketbase-schema

Type-safe PocketBase development — automatically.

pocketbase-schema is a utility that generates TypeScript types directly from your PocketBase collections. It’s built for developers who want stronger guarantees, better autocomplete, and fewer runtime surprises when building on top of PocketBase.

Links:


What it does

Installation

npm install @sparkstone/pocketbase-schema --save-dev

or

pnpm add @sparkstone/pocketbase-schema --save-dev

Usage

Generate types from an exported PocketBase schema:

pocketbase-schema generate \
  --input=pb_schema.json \
  --output=src/lib/pb-types.ts

Or use it programmatically:

import { generate } from "@sparkstone/pocketbase-schema";

generate({
  input: "./pb_schema.json",
  output: "./src/lib/pb-types.ts",
});

Configuration

Configuration is handled via cosmiconfig. Supported formats include .json, .yaml, .js, or .ts.

Example .pocketbase-schema.config.ts:

export default {
  email: "admin@example.com",
  password: "yourpassword",
  url: "http://127.0.0.1:8090",
  schema: {
    outputPath: "src/lib/pb.schema.json",
  },
  types: {
    outputPath: "src/lib/pb.types.ts",
  },
};

Be sure to add your config file to .gitignore to avoid leaking credentials.

Using the generated types

import PocketBase from "pocketbase";
import { Collections, Posts } from "./pb.types";

const pb = new PocketBase("http://127.0.0.1:8090");

const posts = await pb.collection(Collections.Posts).getFullList<Posts>();

Expanding relations

The recommended pattern is to extend generated types:

import { Posts, Comments, Reactions } from "./pb.types";

interface Post extends Posts {
  expand: {
    comments: Comments[];
    reactions: Reactions[];
  };
}

pb.collection(Collections.Posts).getFullList<Post>({
  expand: "comments,reactions",
});

Why it exists

PocketBase makes it easy to move fast, but keeping frontend and backend schemas in sync can quietly become a source of bugs. pocketbase-schema turns your PocketBase schema into a single source of truth for types, eliminating copy-paste definitions and fragile string-based queries.

“We built pocketbase-schema because we love PocketBase — and we wanted full type safety without manual copy-pasting.” — The Sparkstone Team

This remind you of a problem you've been dealing with?

I'd be happy to discuss it with you and see if it's something we could turn into a solution.

Book a call

Newsletter

Subscribe to my newsletter to be the first to hear about what I do next