pb()
- PocketBase Client
The pb()
function returns a configured PocketBase client instance that is ready to communicate with your PocketBase server directly in the JSVM.
Basic Usage
const pb = globalApi.pb()
// Use any PocketBase SDK method
const users = pb.collection('users').getFullList()
const posts = pb.collection('posts').getList(1, 20)
Configuration
The client is automatically configured using your +config.js
settings:
// +config.js
export default {
host: 'http://127.0.0.1:8090', // Default PocketBase URL
}
SDK References
The client uses a special JSVM-compatible version of the PocketBase JS SDK that provides synchronous methods instead of async/await:
Benefits
While using the raw JSVM API might be slightly more efficient, the PocketBase REST API (via the SDK) offers several advantages:
- Well-documented and widely used API surface
- Consistent API between frontend and backend code
- Access to high-level features not easily available in raw JSVM
- Familiar JavaScript patterns and nomenclature
- Rich ecosystem of examples and community support
Example Usage
const pb = globalApi.pb()
// Create a record
const post = pb.collection('posts').create({
title: 'Hello World',
content: 'This is my first post',
})
// Query with filters
const published = pb.collection('posts').getFullList({
filter: 'status = "published"',
sort: '-created',
})
// Update a record
pb.collection('posts').update(post.id, {
views: post.views + 1,
})
Template Usage
<%
const pb = globalApi.pb()
// Get recent posts
const posts = pb.collection('posts').getFullList({
sort: '-created',
filter: 'published = true',
limit: 5
})
%>
<% posts.forEach(post => { %>
<article>
<h2><%= post.title %></h2>
<p><%= post.content %></p>
</article>
<% }) %>
Important Notes
- All SDK methods are synchronous (no async/await needed)
- Real-time features are not available in the JSVM version
- AsyncAuthStore is not supported
- The client is cached after first initialization