Project Structure
Understanding the Base project layout and data directory.
Data Directory
When Base starts, it creates a data directory (default hz_data/) with the following structure:
data.db
logs.db
main.pb.js
1709000000_initial.js
| Path | Description |
|---|---|
hz_data/data.db | Main SQLite database (dev mode) |
hz_data/logs.db | Request and audit logs |
hz_data/storage/ | Uploaded files (local filesystem) |
hz_data/backups/ | Automatic backups |
hz_hooks/ | JavaScript hooks and custom routes |
hz_migrations/ | Schema migration files |
Custom Hooks
Place JavaScript files in hz_hooks/ to extend Base with custom logic:
// hz_hooks/main.pb.js
// Before creating a task, auto-generate a slug
onRecordCreate((e) => {
if (e.collection.name === 'tasks') {
const title = e.record.get('title')
e.record.set('slug', title.toLowerCase().replace(/\s+/g, '-'))
}
return e.next()
}, 'tasks')
// Custom API endpoint
routerAdd('GET', '/api/stats', (e) => {
const tasks = $app.findRecordsByFilter('tasks', 'status = "done"')
return e.json(200, { completed: tasks.length })
})Migrations
Migration files in hz_migrations/ run automatically on startup with --automigrate:
// hz_migrations/1709000000_create_tasks.js
migrate((app) => {
const collection = new Collection({
name: 'tasks',
type: 'base',
fields: [
{ name: 'title', type: 'text', required: true },
{ name: 'status', type: 'select', values: ['active', 'done'] },
],
})
return app.save(collection)
})Last updated on