# DBAL Documentation Index Complete analysis of the Database Abstraction Layer, architecture, and extension points. --- ## Documents in This Analysis ### 1. **DBAL_ANALYSIS_SUMMARY.md** ⭐ START HERE - **Purpose**: Executive summary and quick overview - **Length**: ~600 lines - **Best for**: Understanding DBAL at high level - **Contains**: - What is DBAL? - Architecture overview - Core findings (5 key insights) - Recommended implementation approach - Key insight tables - File structure summary - FAQ section **Start here if**: You have 10 minutes and want to understand the system --- ### 2. **DBAL_QUICK_REFERENCE.md** ⭐ FOR DEVELOPERS - **Purpose**: Fast lookup reference guide - **Length**: ~400 lines - **Best for**: Looking up specific code locations and patterns - **Contains**: - File map with line counts - Key interface definitions - Entity operations pattern - Multi-tenant filtering code - Adapter composition chain - Error code reference - Validation pattern - Testing helpers - Common queries **Start here if**: You're implementing features and need to find code --- ### 3. **DBAL_ARCHITECTURE_ANALYSIS.md** ⭐ FOR DEEP UNDERSTANDING - **Purpose**: Comprehensive architectural analysis - **Length**: ~1,200 lines - **Best for**: Understanding every detail of the system - **Contains**: - Complete DBAL client structure - Detailed adapter pattern explanation - All 4 adapter implementations - Adapter composition via factory - Entity operations pattern with example - Validation layer architecture - Multi-tenant filtering mechanism - Tenant context and permission checks - All 19 entity types documented - Workflow integration deep dive - Error handling architecture - Performance characteristics table - Extension points identified - Recommended next steps **Start here if**: You need to understand architecture for design decisions --- ### 4. **DBAL_INTEGRATION_GUIDE.md** ⭐ FOR IMPLEMENTATION - **Purpose**: Step-by-step guide to implement validation registry - **Length**: ~600 lines - **Best for**: Actually building the ValidationAdapter - **Contains**: - Architecture diagram (ASCII) - Workflow validation process diagram - Step-by-step implementation: 1. Create ValidationAdapter class (complete code) 2. Create ValidationRegistry (complete code) 3. Update adapter factory 4. Update config (optional) 5. Create tests - Usage examples - Integration checklist - Performance considerations - Testing strategy **Start here if**: You're ready to implement the validation system --- ## Quick Navigation by Use Case ### "I need to understand the DBAL in 10 minutes" → Read: **DBAL_ANALYSIS_SUMMARY.md** - Start with "Core Findings" section - Skim "Architecture at a Glance" - Check "Key Insights" ### "I'm implementing a new feature using DBAL" → Read: **DBAL_QUICK_REFERENCE.md** - Use "Core Files Map" to find code - Check "Entity Operations Pattern" for template - See "Multi-Tenant Filtering" for copy-paste code - Look up "Error Code Reference" when needed ### "I need to implement the ValidationAdapter" → Read: **DBAL_INTEGRATION_GUIDE.md** - Follow "Implementation Steps" section - Use complete code snippets provided - Check "Integration Checklist" - Copy test examples ### "I'm designing a new system that uses DBAL" → Read: **DBAL_ARCHITECTURE_ANALYSIS.md** - Read "Architecture Overview" for high-level view - Study "Adapter Pattern Deep Dive" for extension strategy - Review "Extension Points for Workflow Validation Registry" - Check "Performance Characteristics" for load estimates ### "I need to debug a multi-tenant issue" → Read: **DBAL_QUICK_REFERENCE.md** → "Multi-Tenant Filtering" - Then: **DBAL_ARCHITECTURE_ANALYSIS.md** → "Multi-Tenant Filtering Mechanism" ### "I'm reviewing DBAL code in a PR" → Read: **DBAL_QUICK_REFERENCE.md** → "File Structure Summary" - Then: **DBAL_QUICK_REFERENCE.md** → "Key Interfaces at a Glance" --- ## File Structure Map ``` DBAL System Root ├── /dbal/development/src/ │ ├── index.ts # Public API │ │ │ ├── core/ │ │ ├── client/ │ │ │ ├── client.ts # DBALClient main class ⭐ │ │ │ ├── adapter-factory.ts # Adapter composition ⭐ │ │ │ ├── builders.ts # Entity operations factory │ │ │ ├── factory.ts # Singleton factory │ │ │ └── mappers.ts # Config normalization │ │ │ │ │ ├── entities/ │ │ │ ├── index.ts # Barrel exports │ │ │ ├── operations/ │ │ │ │ ├── core/ │ │ │ │ │ ├── user-operations.ts │ │ │ │ │ ├── workflow-operations.ts ⭐ (6.7KB) │ │ │ │ │ └── session-operations.ts │ │ │ │ └── system/ │ │ │ │ ├── page-operations.ts │ │ │ │ ├── component-operations.ts │ │ │ │ └── package-operations.ts │ │ │ └── workflow/ │ │ │ ├── crud/ │ │ │ │ ├── create-workflow.ts │ │ │ │ ├── read-workflow.ts │ │ │ │ ├── update-workflow.ts │ │ │ │ └── list-workflows.ts │ │ │ ├── validation/ │ │ │ └── store/ │ │ │ │ │ ├── foundation/ │ │ │ ├── types/ │ │ │ │ ├── types.generated.ts # All 19 entity interfaces ⭐ │ │ │ │ ├── entities.ts │ │ │ │ ├── operations.ts │ │ │ │ ├── events.ts │ │ │ │ └── [category folders] │ │ │ │ │ │ │ ├── validation/ │ │ │ │ ├── index.ts # Validation exports │ │ │ │ ├── entities/ │ │ │ │ │ ├── user/ │ │ │ │ │ ├── workflow/ │ │ │ │ │ └── [other entities] │ │ │ │ └── predicates/ # Type helpers │ │ │ │ │ │ │ ├── errors.ts # DBALError definitions ⭐ │ │ │ ├── tenant-context.ts │ │ │ └── tenant/ │ │ │ ├── tenant-types.ts # TenantContext, TenantIdentity │ │ │ ├── permission-checks.ts # RBAC functions │ │ │ └── quota-checks.ts │ │ │ ├── adapters/ │ │ ├── adapter.ts # Base interface ⭐ │ │ │ │ │ ├── prisma/ │ │ │ ├── index.ts # PrismaAdapter class │ │ │ ├── context.ts # Prisma client config │ │ │ ├── types.ts │ │ │ └── operations/ │ │ │ ├── crud.ts │ │ │ ├── bulk.ts │ │ │ ├── query.ts │ │ │ └── capabilities.ts │ │ │ │ │ ├── memory/ │ │ │ └── index.ts # MemoryAdapter implementation │ │ │ │ │ ├── acl-adapter/ │ │ │ ├── acl-adapter.ts # ACLAdapter class │ │ │ ├── context.ts │ │ │ ├── read-strategy.ts │ │ │ ├── write-strategy.ts │ │ │ ├── guards.ts │ │ │ └── types.ts │ │ │ │ │ ├── acl/ │ │ │ └── default-rules.ts │ │ │ │ │ └── validation-adapter/ (TO BE CREATED) │ │ ├── validation-adapter.ts # NEW ⭐ │ │ └── __tests__/ │ │ │ ├── workflow/ │ │ ├── types.ts # Workflow types ⭐ │ │ ├── dag-executor.ts # DAG execution engine ⭐ │ │ ├── node-executor-registry.ts # Plugin registry system ⭐ │ │ ├── priority-queue.ts # Task scheduling │ │ ├── validation-registry.ts (TO BE CREATED) # NEW ⭐ │ │ └── executors/ │ │ ├── ts/ │ │ └── [other languages] │ │ │ ├── runtime/ │ │ ├── config.ts # DBALConfig interface ⭐ │ │ └── prisma-client.ts │ │ │ ├── blob/ │ │ └── [storage implementation] │ │ │ ├── bridges/ │ │ └── websocket-bridge.ts # Remote connection │ │ │ └── seeds/ │ └── [database seeding] │ └── Related Directories ├── /workflow/executor/ # Workflow execution runtime ├── /workflow/plugins/ # Node plugins └── /codegen/ # CodeForge IDE (uses DBAL) ``` --- ## Key Code Locations Cheat Sheet | What | Location | Lines | |------|----------|-------| | **Main client class** | `core/client/client.ts` | 88 | | **Adapter factory** | `core/client/adapter-factory.ts` | 66 | | **Base adapter interface** | `adapters/adapter.ts` | 35 | | **Workflow operations** | `core/entities/operations/core/workflow-operations.ts` | 175 | | **Entity types** | `core/foundation/types/types.generated.ts` | 299 | | **Error definitions** | `core/foundation/errors.ts` | 91 | | **Workflow types** | `workflow/types.ts` | 341 | | **DAG executor** | `workflow/dag-executor.ts` | 300+ | | **Node registry** | `workflow/node-executor-registry.ts` | 130 | | **Prisma adapter** | `adapters/prisma/index.ts` | 108 | | **Memory adapter** | `adapters/memory/index.ts` | 260 | | **ACL adapter** | `adapters/acl-adapter/acl-adapter.ts` | 87 | | **Config interface** | `runtime/config.ts` | 35 | --- ## Architecture Decision Records (ADRs) ### ADR-1: Adapter Pattern for Storage **Status**: ✅ Implemented **Decision**: Use DBALAdapter interface with composable implementations **Rationale**: Supports multiple backends, easy to add new adapters **Location**: `core/client/adapter-factory.ts` ### ADR-2: Multi-Tenant First **Status**: ✅ Implemented **Decision**: All queries must filter by tenantId; error if missing **Rationale**: Prevents cross-tenant data leaks **Location**: `core/entities/operations/*/operations.ts` ### ADR-3: Entity Operations Pattern **Status**: ✅ Implemented **Decision**: Each entity has dedicated operations factory **Rationale**: Single responsibility, consistent patterns, easy to extend **Location**: `core/entities/operations/` ### ADR-4: Validation Registry (Proposed) **Status**: 🚀 Recommended Implementation **Decision**: ValidationAdapter wraps base adapter, calls registry for Workflow entity **Rationale**: Non-invasive, composable with other adapters, testable in isolation **Location**: To be implemented in `adapters/validation-adapter/` ### ADR-5: Error Codes **Status**: ✅ Implemented **Decision**: Standardized DBALError codes mapped to HTTP status codes **Rationale**: Clear error semantics, easy client handling, audit logging **Location**: `core/foundation/errors.ts` --- ## Common Patterns ### Pattern: Validate & Persist ```typescript // Step 1: Validate input (in entity operations) const errors = validateWorkflowCreate(data) if (errors.length > 0) { throw DBALError.validationError('Invalid workflow', errors.map(e => ({ field: 'workflow', error: e }))) } // Step 2: Validate business rules (in ValidationAdapter) const result = await registry.validate(data) if (!result.valid) { throw DBALError.validationError('Validation failed', ...) } // Step 3: Persist (in base adapter) return baseAdapter.create('Workflow', data) ``` ### Pattern: Multi-Tenant Filtering ```typescript // Always resolve tenant ID const resolvedTenantId = resolveTenantId(configuredTenantId, data?.tenantId) if (!resolvedTenantId) { throw DBALError.validationError('Tenant ID is required', ...) } // Always include in filters const filter = { ...options.filter, tenantId: resolvedTenantId } return adapter.list('Entity', { ...options, filter }) ``` ### Pattern: Adapter Composition ```typescript let adapter = createBaseAdapter(config) // Prisma, Memory, etc. if (config.security?.enableWorkflowValidation) { adapter = new ValidationAdapter(adapter) // Wrap with validation } if (config.auth?.user) { adapter = new ACLAdapter(adapter, config.auth.user) // Wrap with ACL } return adapter ``` --- ## Testing Patterns ### Use MemoryAdapter for Unit Tests ```typescript const adapter = new MemoryAdapter() const client = new DBALClient({ mode: 'development', adapter: 'memory', tenantId: 'test' }) // Use like normal DBAL client ``` ### Test Validation Rules ```typescript const registry = new WorkflowValidationRegistry() registerDefaultRules(registry) const result = await registry.validate(testWorkflow) expect(result.valid).toBe(false) expect(result.errors).toContain('expected error message') ``` ### Test Multi-Tenant Isolation ```typescript const client1 = new DBALClient({ ..., tenantId: 'tenant-1' }) const client2 = new DBALClient({ ..., tenantId: 'tenant-2' }) const wf1 = await client1.workflows.create(...) const list2 = await client2.workflows.list() expect(list2.items).not.toContain(wf1) // Should not see cross-tenant data ``` --- ## Performance Guidelines - **Add validation sparingly**: ~1-5ms per operation - **Use indexes on tenantId**: Essential for multi-tenant performance - **Batch operations**: Use createMany/updateMany for bulk work - **Connection pooling**: Configured in PrismaAdapter - **Query timeouts**: Configurable in DBALConfig --- ## Frequently Referenced Sections ### When working on... **New Entity Type** → See: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Entity Operations Pattern" → Also: **DBAL_QUICK_REFERENCE.md** > "Entity Operations Pattern" **Validation Rules** → See: **DBAL_INTEGRATION_GUIDE.md** > "Step 2: Create Validation Registry" → Also: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Extension Point: Adding Workflow Validation Adapter" **Multi-Tenant Workflows** → See: **DBAL_QUICK_REFERENCE.md** > "Multi-Tenant Filtering" → Also: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Multi-Tenant Filtering Mechanism" **New Adapter** → See: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Adapter Pattern Deep Dive" → Also: **DBAL_QUICK_REFERENCE.md** > "Adapter Composition Chain" **Error Handling** → See: **DBAL_QUICK_REFERENCE.md** > "Error Code Reference" → Also: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Error Handling Architecture" **Workflow Execution** → See: **DBAL_ARCHITECTURE_ANALYSIS.md** > "Workflow Integration Points" → Also: **DBAL_QUICK_REFERENCE.md** > "Workflow Command" --- ## Related Code Repositories - **Main DBAL**: `/dbal/development/` ← Focus of this analysis - **Shared Schemas**: `/dbal/shared/api/schema/` (YAML entity definitions) - **Workflow Executor**: `/workflow/executor/` (Execution runtime) - **Workflow Plugins**: `/workflow/plugins/` (Node implementations) - **CodeForge IDE**: `/codegen/` (Uses DBAL for persistence) - **Database**: `/prisma/` (Prisma schema definition) --- ## Feedback & Updates This analysis is current as of 2026-01-22. The DBAL is: - ✅ Phase 2 Complete (TypeScript development) - 🚀 Ready for ValidationAdapter implementation - 📋 Documented and analyzable from source code For updates: 1. Check if changes were made to `/dbal/development/src/` 2. Review new files in `/dbal/development/src/core/entities/operations/` 3. Check adapter implementations for new capabilities 4. Review `/dbal/development/package.json` for dependency updates --- ## Summary This documentation suite provides: - **4 complementary documents** covering different depths - **~3,000 total lines** of analysis and code - **Complete file map** with locations - **Step-by-step implementation guide** for ValidationAdapter - **Quick reference** for developers - **Architectural insights** for designers **Start with**: DBAL_ANALYSIS_SUMMARY.md (10 min read) **Then read**: DBAL_QUICK_REFERENCE.md (as needed) **For implementation**: DBAL_INTEGRATION_GUIDE.md **For deep understanding**: DBAL_ARCHITECTURE_ANALYSIS.md