Understanding the Global API
The Global API provides utility functions that are available both in EJS templates and in any JavaScript code via require('pocketpages')
. These functions are context-free, meaning they don't depend on the current request or response context.
How to Access the Global API
There are two ways to access these functions:
- In EJS Templates: The functions are automatically available (along with the Context API)
- In JavaScript Files: Import via require
Example: Using the Global API in JavaScript
const { dbg, stringify, url } = require('pocketpages')
function processData(data) {
dbg('Processing data:', stringify(data))
const parsed = url('https://example.com/path?q=search')
return parsed.searchParams.get('q')
}
Example: Using the Global API in EJS
<%
// Logging
dbg("Debug message");
info("Info message");
warn("Warning message");
error("Error message");
// JSON handling
const obj = { hello: "world" };
const str = stringify(obj);
// URL parsing
const parsed = url('https://example.com/path?q=search');
%>
<p>Search param: <%= parsed.searchParams.get('q') %></p>
<%
// Database helpers
const record = findRecordByFilter('users', 'username = "admin"');
const records = findRecordsByFilter('posts', 'published = true');
// Micro-dash utilities
forEach([1, 2, 3], n => {
dbg(n);
});
%>
Available Global Functions
The Global API provides several utility functions:
PocketBase JS SDKClient
pb()
- Get configured PocketBase SDK client
Logging
dbg()
- Debug level logginginfo()
- Info level loggingwarn()
- Warning level loggingerror()
- Error level logging
Data Handling
stringify()
- Safe JSON stringificationurl()
- URL parsing and manipulation
Database Helpers
findRecordByFilter()
- Find single recordfindRecordsByFilter()
- Find multiple records
User Management
createUser()
- Create a regular usercreateAnonymousUser()
- Create an anonymous user
Micro-dash Utilities
forEach()
- Array iterationkeys()
- Object key extractionvalues()
- Object value extractionmerge()
- Object merging
Using in Libraries
The Global API is particularly useful when writing reusable functions that don't need request context:
// myLibrary.js
const { dbg, stringify } = require('pocketpages')
function processUserData(user) {
dbg('Processing user:', stringify(user))
// ... processing logic
}
module.exports = { processUserData }
Additional Notes
- Global API functions are always available, whether in templates or JavaScript files
- They're designed to be context-free and work consistently everywhere
- The same functions are merged into the Context API for convenience in templates
- Use
require('pocketpages')
when you need these functions in separate files or libraries
For detailed information about specific API methods, please refer to the individual API documentation pages in the sidebar.