PocketBase Server-Side JavaScript (pb_hooks)

Hook

by davila7 · Added 5mo ago

Claude

Install

npx claude-code-templates@latest --hook=cli-tool/components/skills/pocketbase/pb-hooks --yes

Add to Claude

claude mcp add pocketbase-server-side-javascript-pbhooks

About


name: "PocketBase Hooks" description: "Server-side JavaScript hooks for PocketBase (pb_hooks). Use when writing custom routes, event hooks, cron jobs, sending emails, making HTTP requests, querying the database, or extending PocketBase with server-side logic. Covers the goja ES5 runtime, routing, middleware, all event hooks, DB queries, record operations, and global APIs."

PocketBase Server-Side JavaScript (pb_hooks)

Runtime Basics

  • Files go in pb_hooks/*.pb.js (must end with .pb.js)
  • Engine: goja — ES5.1 + some ES6. No ES6 modules (import/export), no async/await, no arrow functions in older versions. Use function(){} and CommonJS require().
  • Each file is loaded on app start and on hot-reload
  • __hooks — absolute path to the pb_hooks directory
  • TypeScript declarations: pb_data/types.d.ts (auto-generated, useful for IDE support)
  • --hooksPool=25 flag controls concurrent JS goroutines (default: 25)
  • Each handler runs in an isolated context — no shared mutable state between requests

Routing

Adding routes

routerAdd("GET", "/api/hello/{name}", function(e) {
    var name = e.request.pathValue("name")
    return e.json(200, { "message": "Hello " + name })
}, /* optional middleware */)

Path patterns

  • {name} — named path parameter
  • {path...} — wildcard (matches rest of path)
  • {$} — exact match (no trailing slash)

Response methods

MethodUsage
e.json(status, data)JSON response
e.string(status, text)Plain text
e.html(status, html)HTML response
e.redirect(status, url)Redirect (301/302)
e.blob(status, contentType, bytes)Binary data
e.stream(status, contentType, reader)Streaming response
e.noContent(status)No body (204)

Reading request data

// Body (JSON)
var body = new DynamicModel({ name: "", age: 0 })
e.bindBody(body)

// Query params
var page = e.request.url.query().get("page")

// He

Tags

PreCommitaitmplclaude-code-templates

From Our Store

View all →
Claude Code

Claude Code Power User Kit

$39+

Advanced Claude Code skills and configurations

OpenClaw

OpenClaw Business Starter Kit

$59+

Set Up Your AI Assistant in 60 Minutes