📚 StoneJS Developer Guide

Complete reference for building modern web applications with the StoneJS Framework

📚 API Reference

$db (Database)

Method Description Example
query(sql, params) Execute single query await $db.query('SELECT * FROM users WHERE id = $1', [123])
query([queries]) Execute multiple queries await $db.query([{sql: 'SELECT...', params: []}])
secureQuery(sql, params, vars, values) Query with session variables (RLS) await $db.secureQuery('SELECT...', [], 'session.accountcode', code)
getConnection() Get connection from pool const client = await $db.getConnection()

$cache (Caching)

Method Description Example
get(key) Get cached value const value = $cache.get('mykey')
set(key, value, ttl) Set cached value $cache.set('mykey', data, 300)
remember(key, ttl, fn) Cache function result await $cache.remember('users', 300, async () => {...})
delete(key) Remove from cache $cache.delete('mykey')
has(key) Check if exists if ($cache.has('mykey')) {...}
clear() Clear all cache $cache.clear()
size() Get cache size const size = $cache.size()
getStats() Get cache statistics const stats = $cache.getStats()

$context (Context)

Method Description Example
get(key) Get context value const title = $context.get('pageTitle')
set(key, value) Set context value $context.set('title', 'My Page')
has(key) Check if exists if ($context.has('user')) {...}

$req (Request)

Property Description Example
method HTTP method $req.method // 'GET', 'POST'
path URL path $req.path // '/users/profile'
query Query parameters $req.query.id // '123'
body POST body data $req.body.name
headers HTTP headers $req.headers['user-agent']
ip Client IP address $req.ip

$res (Response)

Method Description Example
json(data) Send JSON response $res.json({ success: true })
send(text) Send text response $res.send('Hello')
redirect(url) Redirect to URL $res.redirect('/login')
status(code) Set status code $res.status(404).send('Not found')
setHeader(name, val) Set HTTP header $res.setHeader('X-Custom', 'value')

Template Functions

Function Description Example
include(path) Include component <%- await include('components/header')
next() Call next autohandler/page <%- await next()

Section Tags

Tag When It Runs Use Case
<%%doc> Never (removed) Documentation comments
<%%once> Once on server start Expensive initialization
<%%init> Start of each request Request setup
<%%js> During rendering Inline JavaScript
<%%cleanup> After response sent Logging, cleanup

Back to Overview View All Demos

Need more help?

View All Demos Getting Started GitHub Repository