PocketBase Hooks

Write server-side JavaScript hooks, routes, and cron jobs for PocketBase pb_hooks.

30.5k|3.5k|Updated Jul 4, 2025
One-click install
npx skills add https://github.com/davila7/claude-code-templates --skill pocketbase-hooks
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: PocketBase Hooks
Source: https://github.com/davila7/claude-code-templates/tree/main/cli-tool/components/skills/pocketbase/pb-hooks
Command: npx skills add https://github.com/davila7/claude-code-templates --skill pocketbase-hooks

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Extending PocketBase with custom server-side logic requires knowing the goja ES5 runtime constraints, the event hook system, and the global APIs, which are easy to get wrong without a reference.

Core Features & Use Cases

  • Custom Routes & Middleware: Add HTTP endpoints with routerAdd, path parameters, auth middleware, and request/response helpers.
  • Event Hooks & Record Operations: Hook into record create/update/delete lifecycles, auth flows, realtime subscriptions, and run database queries with the $dbx expression builder.
  • Cron, Email & HTTP: Schedule cron jobs, send and customize system emails, and make outbound HTTP requests from hooks.
  • Use Case: When a new post is created, automatically assign the authenticated author, validate the title length, and fire a webhook to an external service, all inside pb_hooks.

Quick Start

Write a PocketBase pb_hooks file that adds a custom GET route and an onRecordCreateExecute hook that auto-assigns the author field.

Frequently Asked Questions about PocketBase Hooks

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add a custom API route in PocketBase?

Use routerAdd in a pb_hooks/*.pb.js file with a method, path pattern, and handler function. Path parameters use {name} syntax, and you respond with helpers like e.json(200, data). Optional middleware such as $apis.requireAuth() can protect the route.

How do I run code when a PocketBase record is created?

Register onRecordCreateExecute with a callback and optional collection filter. Modify the record via e.record.set() before calling e.next(), or use onRecordAfterCreateSuccess to react after the record is saved.

Does PocketBase pb_hooks support ES6 or async/await?

No. pb_hooks runs on the goja engine, which supports ES5.1 plus limited ES6. Use function expressions and CommonJS require(); import/export, async/await, and arrow functions in older versions are not supported.

How do I schedule cron jobs in PocketBase?

Call cronAdd with a job name, a standard cron expression like "0 3 * * *", and a function. Jobs run inside the PocketBase process and can query or delete records using $app; remove them with cronRemove.

Why does my PocketBase hook fail with SQL errors?

Raw queries must use named parameters like {:status} with .bind() instead of string concatenation. Also verify collection and field names, and use $dbx expression helpers such as $dbx.hashExp for safe where clauses.