What You’ll Build
A full-featured CRUD application with:- List view with edit and delete actions
- Create and update forms with validation
- Delete confirmation dialogs
- Success and error messages
- URL-based state management
- Reusable page components
Database Schema
For this example, we’ll create a currency management system.sqlpage/migrations/0007_currencies.sql
List View with Actions
The list view displays all records with edit and delete buttons.currencies_list.sql
Key Patterns
Alert Messages via URL Parameters
Alert Messages via URL Parameters
Success and error messages are passed through URL parameters:After an operation, redirect with the message:
Action Icons in Tables
Action Icons in Tables
The Use Markdown image links for icons:
markdown property on the table component allows rendering Markdown in specific columns:URL-Based Actions
URL-Based Actions
Pass the action type and record ID through URL parameters:
- Create:
?action=INSERT - Edit:
?id=5 - Delete:
?id=5&action=DELETE
Create/Edit Form
A single form handles both creating new records and editing existing ones.currencies_item_form.sql
Form Pattern Highlights
Determine Form Mode
The form behaves differently based on URL parameters:
- No
$id: Create mode (INSERT) $idpresent: Edit mode (UPDATE)$id+$action=DELETE: Delete confirmation mode
Load Existing Data
When editing (
$id is set), query the database to populate form fields with current values.Preserve Values on Error
If validation fails, the handler redirects back with
$values containing the submitted data as JSON.Data Manipulation Handler
The DML (Data Manipulation Language) handler processes INSERT, UPDATE, and DELETE operations.currencies_item_dml.sql
CRUD Implementation Patterns
UPSERT Pattern
UPSERT Pattern
Use
INSERT ... ON CONFLICT ... UPDATE to handle both create and update in one query:- If
$idis NULL: New record is inserted (autoincrement generates ID) - If
$idexists: Existing record is updated
Validation Before Mutation
Validation Before Mutation
Check constraints before modifying data:
RETURNING for Immediate Feedback
RETURNING for Immediate Feedback
Use RETURNING to redirect after successful operations:If the DELETE affects 0 rows, no redirect occurs and you can show an error.
URL Encoding Messages
URL Encoding Messages
Always encode user messages in URLs to handle special characters:
Handling Edge Cases
- Empty String vs NULL
- Invalid ID Parameters
- Concurrent Modifications
- Foreign Key Violations
Form fields submit empty strings, not NULL:
User Feedback Patterns
Success Messages
Error Messages
Confirmation Dialogs
Application Structure
Complete CRUD Flow Diagram
Advanced CRUD Patterns
Master-Detail Forms
Handle parent records with multiple child records:
Soft Deletes
Mark records as deleted instead of removing them:
Audit Logging
Track all changes to records:
Bulk Operations
Allow selecting and operating on multiple records:
Production Checklist
Input Validation
Validate all inputs server-side, even if client-side validation exists:
- Required fields
- Data types and ranges
- String lengths
- Format patterns (email, phone, etc.)
Error Handling
Handle all error cases gracefully:
- Database constraints
- Missing records
- Permission denied
- Concurrent modifications
Authorization
Verify user permissions for each operation:
- Can user view this record?
- Can user edit this record?
- Can user delete this record?
Full Example
The complete CRUD example with authentication is available in the SQLPage repository.Related Examples
Todo Application
Simpler CRUD example without authentication
User Authentication
Learn about session management and protected pages