Database Integration Demo
This page demonstrates how to connect to PostgreSQL and query data using the $db object.
✓ Database Connected!
| Database: | ddata |
| Host: | pgsql-prod01.postgres.database.azure.com:5432 |
| Query Time: | 99ms |
| Total Records: | 400713 |
Query Results
Showing 1 - 10 of 400713 records
| RID | Operation | Table Name | User ID | Description | Created |
|---|---|---|---|---|---|
| 400718 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:33:33 am | |
| 400717 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:32:42 am | |
| 400716 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:12:05 am | |
| 400715 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:08:22 am | |
| 400714 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:08:03 am | |
| 400713 | INSERT | chat_askai_queries | pgsql_access_rw | 19/12/2025, 7:04:08 am | |
| 400712 | INSERT | chat_askai_queries | pgsql_access_rw | 15/12/2025, 12:16:57 pm | |
| 400711 | INSERT | chat_askai_queries | pgsql_access_rw | 15/12/2025, 11:09:25 am | |
| 400710 | INSERT | chat_askai_queries | pgsql_access_rw | 03/12/2025, 10:49:40 am | |
| 400709 | INSERT | chat_askai_queries | pgsql_access_rw | 03/12/2025, 10:48:23 am |
Database API Reference
The $db object is available in all components and provides two main methods for database operations.
1. $db.query() - Standard Queries
Use for regular database operations without row-level security.
Single Query
Multiple Queries (Array Syntax)
Execute multiple queries on the same connection. Returns array of results.
Transactions
Wrap operations in BEGIN/COMMIT. All succeed or all fail together.
2. $db.secureQuery() - Row-Level Security
Use for tables with PostgreSQL RLS enabled. Automatically manages session variables in a transaction.
Single Session Variable
Multiple Session Variables
Multiple Queries with RLS
How secureQuery() Works
Automatic Session Variable Management:
- Gets a dedicated connection from the pool
- Starts a transaction (BEGIN)
- Sets session variable(s) with SET LOCAL
- Executes your query/queries
- Commits the transaction (automatically clears SET LOCAL variables)
- Releases the connection back to the pool
Why this matters: Session variables are scoped to the transaction, preventing variable leakage between requests.
Setting up Row-Level Security in PostgreSQL
⚠️ Important: Regular $db.query() doesn't set session variables. Always use $db.secureQuery() for RLS-enabled tables, or RLS policies will block all rows.
Configuration
Database configuration is loaded from your .env file:
Return Value Format
Important: StoneJS $db.query() returns rows directly, not wrapped in a .rows property like the native pg library.
| Library | Return Format |
|---|---|
pg (native) |
result.rows |
$db.query() |
result (array directly) |
Best Practices
- Always use parameterized queries - Use
$1, $2placeholders to prevent SQL injection - Use secureQuery() for RLS tables - Automatically manages session variables
- Use array syntax for related queries - Ensures they run on the same connection
- Use transactions for atomic operations - Wrap in BEGIN/COMMIT
- Handle errors gracefully - Use try/catch blocks
- Combine with caching - Use
$cache.remember()for expensive queries
Example: Caching Database Results
Quick Reference Table
| Feature | $db.query() | $db.secureQuery() |
|---|---|---|
| Single query | ✓ Yes | ✓ Yes |
| Multiple queries | ✓ Array syntax | ✓ Array syntax |
| Transactions | ✓ Manual BEGIN/COMMIT | ✓ Automatic wrapper |
| Session variables | ✗ No | ✓ Automatic SET LOCAL |
| Use case | Standard queries | RLS-enabled tables |
| Connection handling | Automatic pool management | Dedicated connection per call |
Session Stats: You've viewed this page 1 time(s).