12 KiB
DBAL Integration Guide
This document explains how the C++ and TypeScript DBAL (Database Abstraction Layer) components are integrated into the MetaBuilder project.
Overview
The DBAL system provides a unified interface for data storage and retrieval across the MetaBuilder application. It consists of:
- C++ DBAL Daemon - Production-ready HTTP server with database abstraction
- TypeScript DBAL Client - Browser-compatible client for development
- Integration Layer - Bridges the DBAL with existing MetaBuilder components
Architecture
┌─────────────────────────────────────────────────────────┐
│ MetaBuilder App │
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ React Hooks │ │ Integration │ │ Database │ │
│ │ (useDBAL) │→ │ Layer │→ │ Class │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
│ ↓ ↓ │
│ ┌─────────────────────────────────┐ │
│ │ TypeScript DBAL Client │ │
│ │ - KV Store (In-Memory) │ │
│ │ - Blob Storage (In-Memory) │ │
│ │ - Tenant Management │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────────────────────┘
│
│ HTTP API
↓
┌─────────────────────────────────────────────────────────┐
│ C++ DBAL Daemon (Production) │
│ - HTTP/1.1 Server (Nginx-compatible) │
│ - PostgreSQL Adapter │
│ - Blob Storage (S3/Filesystem) │
│ - Multi-tenant System │
│ - Access Control & Quotas │
└─────────────────────────────────────────────────────────┘
Directory Structure
metabuilder/
├── dbal/
│ ├── cpp/ # C++ DBAL Daemon
│ │ ├── src/
│ │ │ ├── daemon/ # HTTP server & main
│ │ │ ├── core/ # Core DBAL logic
│ │ │ ├── adapters/ # Database adapters
│ │ │ └── blob/ # Blob storage
│ │ ├── include/
│ │ ├── tests/
│ │ ├── Dockerfile
│ │ └── CMakeLists.txt
│ └── ts/ # TypeScript DBAL Client
│ └── src/
│ ├── core/ # Client, types, validation
│ ├── adapters/ # Memory adapter
│ ├── blob/ # Blob storage implementations
│ └── runtime/ # Runtime utilities
├── src/
│ ├── lib/
│ │ └── dbal-integration.ts # Integration layer
│ ├── hooks/
│ │ └── useDBAL.ts # React hooks
│ └── components/
│ └── DBALDemo.tsx # Demo component
└── deployment/ # Docker deployment configs
Integration Components
1. DBAL Integration Layer (src/lib/dbal-integration.ts)
The integration layer provides a singleton interface to the DBAL system:
import { dbal } from '@/lib/dbal-integration'
// Initialize DBAL (happens automatically)
await dbal.initialize()
// Key-Value operations
await dbal.kvSet('user-preferences', { theme: 'dark' })
const prefs = await dbal.kvGet('user-preferences')
// Blob storage operations
await dbal.blobUpload('avatar.png', imageData)
const data = await dbal.blobDownload('avatar.png')
// List operations
await dbal.kvListAdd('recent-files', ['file1.txt', 'file2.txt'])
const files = await dbal.kvListGet('recent-files')
2. React Hooks (src/hooks/useDBAL.ts)
React hooks for easy DBAL access in components:
import { useKVStore, useBlobStorage, useCachedData } from '@/hooks/useDBAL'
// In your component
function MyComponent() {
// KV Store hook
const kv = useKVStore()
await kv.set('key', 'value')
const value = await kv.get('key')
// Blob Storage hook
const blob = useBlobStorage()
await blob.upload('file.txt', data)
const fileData = await blob.download('file.txt')
// Cached Data hook (automatic serialization)
const { data, save, clear } = useCachedData('user-settings')
await save({ theme: 'light' })
}
3. Demo Component (src/components/DBALDemo.tsx)
A comprehensive demonstration component showing all DBAL features:
- Key-Value store operations
- List management
- Blob storage upload/download
- Cached data with React hooks
Access it by importing:
import { DBALDemo } from '@/components/DBALDemo'
<DBALDemo />
Usage Examples
Storing User Preferences
import { useKVStore } from '@/hooks/useDBAL'
function UserSettings() {
const kv = useKVStore()
const savePreferences = async () => {
await kv.set('user-preferences', {
theme: 'dark',
language: 'en',
notifications: true
}, 3600) // Cache for 1 hour
}
const loadPreferences = async () => {
const prefs = await kv.get('user-preferences')
return prefs
}
}
Uploading Files
import { useBlobStorage } from '@/hooks/useDBAL'
function FileUploader() {
const blob = useBlobStorage()
const handleUpload = async (file: File) => {
const arrayBuffer = await file.arrayBuffer()
const data = new Uint8Array(arrayBuffer)
await blob.upload(`uploads/${file.name}`, data, {
'content-type': file.type,
'uploaded-at': new Date().toISOString()
})
}
}
Managing Lists
import { useKVStore } from '@/hooks/useDBAL'
function RecentFiles() {
const kv = useKVStore()
const addRecentFile = async (filename: string) => {
await kv.listAdd('recent-files', [filename])
}
const getRecentFiles = async () => {
const files = await kv.listGet('recent-files', 0, 10) // Get first 10
return files
}
}
Automatic Caching
import { useCachedData } from '@/hooks/useDBAL'
function UserProfile() {
const { data, loading, save } = useCachedData('user-profile')
// data is automatically loaded from cache on mount
// save() automatically updates both cache and local state
const updateProfile = async (newData) => {
await save(newData, 7200) // Cache for 2 hours
}
}
Multi-Tenant Support
The DBAL system includes full multi-tenant support with isolation, access control, and quotas:
import { dbal } from '@/lib/dbal-integration'
// Create a new tenant
const tenantManager = dbal.getTenantManager()
await tenantManager.createTenant('company-123', {
maxBlobStorageBytes: 1024 * 1024 * 1024, // 1GB
maxBlobCount: 10000,
maxRecords: 100000
})
// Use tenant-specific storage
const kv = useKVStore('company-123', 'user-456')
await kv.set('company-data', { ... })
Production Deployment
In production, the TypeScript client can connect to the C++ DBAL daemon via HTTP:
// Configure DBAL to use remote server
await dbal.initialize({
adapter: 'http',
endpoint: 'http://dbal-daemon:8080'
})
The C++ daemon provides:
- High-performance HTTP/1.1 server
- PostgreSQL adapter for persistent storage
- S3/Filesystem blob storage
- Nginx-compatible reverse proxy headers
- Health checks and monitoring
- Multi-tenant isolation
- Access control and quotas
Testing
Run the DBAL demo component to test all features:
# Start the development server
npm run dev
# Navigate to the DBAL Demo page
# Or import <DBALDemo /> in any component
Configuration
Environment Variables
# For TypeScript client
DBAL_ADAPTER=memory # or 'http' for production
DBAL_ENDPOINT=http://dbal-daemon:8080 # if using HTTP adapter
# TODO: deployment docs live under docs/deployments/; update this reference.
# For C++ daemon (see deployment/README.md)
DBAL_BIND_ADDRESS=0.0.0.0
DBAL_PORT=8080
DBAL_LOG_LEVEL=info
DBAL_MODE=production
TypeScript Configuration
The DBAL module is aliased in tsconfig.json and vite.config.ts:
{
"paths": {
"@/*": ["./src/*"],
"@/dbal/*": ["./dbal/*"]
}
}
API Reference
DBALIntegration
Main singleton class for DBAL access.
Methods:
initialize(config?)- Initialize DBAL systemgetClient()- Get DBAL client instancegetTenantManager()- Get tenant managergetKVStore()- Get KV store instancegetBlobStorage()- Get blob storage instancekvSet(key, value, ttl?, tenantId?, userId?)- Store key-value datakvGet(key, tenantId?, userId?)- Retrieve key-value datakvDelete(key, tenantId?, userId?)- Delete key-value datakvListAdd(key, items, tenantId?, userId?)- Add items to listkvListGet(key, tenantId?, userId?, start?, end?)- Get list itemsblobUpload(key, data, metadata?)- Upload blob datablobDownload(key)- Download blob datablobDelete(key)- Delete blob datablobList(prefix?)- List all blobsblobGetMetadata(key)- Get blob metadata
React Hooks
useDBAL()
- Returns:
{ isReady, error } - Ensures DBAL is initialized
useKVStore(tenantId?, userId?)
- Returns:
{ isReady, set, get, delete, listAdd, listGet } - KV store operations with automatic error handling
useBlobStorage()
- Returns:
{ isReady, upload, download, delete, list, getMetadata } - Blob storage operations with automatic error handling
useCachedData(key, tenantId?, userId?)
- Returns:
{ data, loading, error, save, clear, isReady } - Automatic caching with React state management
Troubleshooting
DBAL not initializing
Check browser console for errors. Common issues:
- Path alias not configured correctly in vite.config.ts
- TypeScript path mapping missing in tsconfig.json
Can't import DBAL modules
Ensure path aliases are configured:
// Should work
import { dbal } from '@/lib/dbal-integration'
import { DBALClient } from '@/dbal/ts/src'
Type errors
Run npm run build to check for TypeScript errors:
npm run build
Memory adapter limitations
The in-memory adapter stores data in browser memory:
- Data is lost on page reload
- Limited by browser memory constraints
- For production, use HTTP adapter with C++ daemon
Future Enhancements
- HTTP adapter for TypeScript client
- Prisma integration for SQL queries
- Real-time sync between clients
- Offline support with sync
- Encryption at rest
- Advanced query builder
- GraphQL API layer
- WebSocket support for real-time updates
Related Documentation
TODO: Fix related doc links (deployments path and local implementation docs).
- C++ DBAL Documentation
- TypeScript DBAL Documentation
- Docker Deployment
- Multi-Tenant System
- Blob Storage
Support
For issues or questions:
- Check this documentation
- Review the demo component (
src/components/DBALDemo.tsx) - Check the integration tests
- Create a GitHub issue
License
TODO: No LICENSE file exists at repo root; update to correct location (e.g., docs/LICENSE) or add one. See LICENSE file in project root.