PocketBase Server-Side JavaScript (pb_hooks)
Hookby davila7 · Added 5mo ago
Claude
Install
npx claude-code-templates@latest --hook=cli-tool/components/skills/pocketbase/pb-hooks --yesAdd to Claude
claude mcp add pocketbase-server-side-javascript-pbhooksAbout
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. Usefunction(){}and CommonJSrequire(). - 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=25flag 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
| Method | Usage |
|---|---|
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