मुख्य सामग्री पर जाएँ
Skillpedia
API और सर्वर (backend)33k · #8873

pb-hooks

यह क्या करती है

यह स्किल JavaScript में लिखे कस्टम सर्वर कोड से PocketBase ऐप को बढ़ाने में मदद करती है। यह उन डेवलपर्स के लिए है जिन्हें बिल्ट-इन सुविधाओं से आगे का लॉजिक चाहिए।

कब इस्तेमाल करें

  • ✓जब आपको PocketBase में कस्टम API route चाहिए।
  • ✓जब आप चाहते हैं कि कोई रिकॉर्ड बनने या बदलने पर कोड चले।
  • ✓जब आपको PocketBase से शेड्यूल्ड जॉब या बाहर जाने वाला HTTP अनुरोध चाहिए।

Skill का लिंक

https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/pocketbase/pb-hooks/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/pocketbase/pb-hooks/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“एक PocketBase hook लिखिए जो हर नया order रिकॉर्ड बनने पर मुझे ईमेल भेजे।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

एजेंट pb_hooks फ़ोल्डर में .pb.js फ़ाइलें लिखता है, PocketBase के JavaScript engine का इस्तेमाल करते हुए, जिसमें routes, events, डेटाबेस क्वेरी और ईमेल के फ़ंक्शन होते हैं।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

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

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")

// Headers
var token = e.request.header.get("Authorization")

// Uploaded files
var files = e.findUploadedFiles("document")  // returns array of *filesystem.File

// Auth state
var user = e.auth          // current auth record or null
var isSuper = e.hasSuperuserAuth()

Middleware

Built-in middleware

routerAdd("GET", "/api/protected", handler,
    $apis.requireAuth(),                // any authenticated user
    // OR
    $apis.requireAuth("users"),         // only "users" collection
    // OR
    $apis.requireSuperuserAuth(),       // superusers only
    // OR
    $apis.requireGuestOnly(),           // unauthenticated only
    // OR
    $apis.bodyLimit(5 * 1024 * 1024),   // 5MB body limit
    // OR
    $apis.gzip()                        // gzip compression
)

Global middleware

routerUse(function(e) {
    // runs before every request
    console.log(e.request.method, e.request.url.path)
    return e.next()  // MUST call e.next() to continue
})

Custom route middleware

function myMiddleware(e) {
    // pre-processing
    var result = e.next()  // call next handler
    // post-processing
    return result
}

routerAdd("GET", "/api/test", handler, myMiddleware)

Priority: middleware runs in order — first registered, first executed.

Event Hooks

Record lifecycle

Each record event has 3 variants:

  • onRecord*Execute — wraps the default action. Call e.next() to proceed.
  • onRecord*AfterSuccess — runs after successful execution
  • onRecord*AfterError — runs after execution error
// Before/during create
onRecordCreateExecute(function(e) {
    // e.record — the record being created
    e.record.set("status", "pending")
    return e.next()  // proceed with creation
}, "posts")  // optional collection filter

// After successful create
onRecordAfterCreateSuccess(function(e) {
    // e.record — the created record (has ID now)
    console.log("Created:", e.record.id)
}, "posts")

// After failed create
onRecordAfterCreateError(function(e) {
    // e.error — the error
    console.log("Failed:", e.error)
}, "posts")

All record hooks

Hook Event object fields
onRecordCreateExecute e.record
onRecordUpdateExecute e.record
onRecordDeleteExecute e.record
onRecordAfterCreateSuccess e.record — after successful create
onRecordAfterUpdateSuccess e.record — after successful update
onRecordAfterDeleteSuccess e.record — after successful delete
onRecordAfterCreateError e.record, e.error — after failed create
onRecordAfterUpdateError e.record, e.error — after failed update
onRecordAfterDeleteError e.record, e.error — after failed delete
onRecordValidate e.record — add custom validation errors
onRecordEnrich e.record — modify API respons
अक्सर पूछे जाने वाले सवाल

क्या मैं आधुनिक JavaScript फ़ीचर इस्तेमाल कर सकता हूँ?

सभी नहीं। engine ES5 और कुछ ES6 सपोर्ट करता है, और modules व async/await उपलब्ध नहीं हैं।

क्या फ़ाइल बदलने पर यह दोबारा लोड होता है?

हाँ। Hook फ़ाइलें ऐप शुरू होने पर लोड होती हैं और बदलने पर दोबारा लोड होती हैं।

क्या hooks के बीच डेटा साझा होता है?

नहीं। हर handler अपने संदर्भ में चलता है, इसलिए अनुरोधों के बीच कोई साझा स्थिति नहीं होती।

जानकारी

लेखक का मूल विवरण

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.

repo: davila7/claude-code-templates

लोकप्रियता

GitHub स्टार
33k
रैंकिंग में स्थान
#8873
लाइसेंस
MIT
अपडेट की गई
2/11/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

owasp-security

33k

आपके एजेंट को वेब ऐप, API, मोबाइल, कंटेनर और AI एजेंट्स के लिए OWASP सुरक्षा मार्गदर्शन सिखाती है, ताकि वह आम कमज़ोरियाँ पहचान सके और सुधार सुझा सके।

कोडिंगसिक्योरिटी

agent-management

33k

आपके एजेंट को AI Maestro कमांड-लाइन से AI एजेंट बनाने, सूची देखने, hibernate और wake करने, नाम बदलने और हटाने देती है, हर एजेंट के जीवन-चक्र को संभालते हुए।

AI और एजेंटएजेंट

agent-messaging

33k

Agent Messaging Protocol का उपयोग करके AI एजेंट्स के बीच हस्ताक्षरित संदेश भेजने, प्राप्त करने और उनका जवाब देने देती है, ज़्यादातर आपकी अपनी मशीन पर।

AI और एजेंटएजेंट

docs-search

33k

आपके एजेंट को फ़ंक्शन सिग्नेचर, क्लास परिभाषा और कमेंट के लिए ऑटो-जनरेटेड कोड डॉक्यूमेंटेशन खोजने देती है, ताकि कोड बदलने से पहले वह असली API जाँच सके।

कोडिंगसामान्य कोडिंग

मिलती-जुलती skills