This document provides a comprehensive overview of the Vault repository structure, explaining the purpose and organization of each component.
Vault is a TypeScript/Express.js implementation of the Outbox Pattern using PostgreSQL WAL (Write-Ahead Log) for reliable event streaming. The repository contains:
- TypeScript implementation - Core library and consumer service
- Express.js integration - HTTP server and middleware
- Infrastructure - Docker, scripts, and configuration files
vault/
├── src/ # Source code
│ ├── server/ # Express.js server
│ ├── lib/ # Core library packages
│ ├── consumers/ # Event consumers
│ └── types/ # TypeScript type definitions
├── scripts/ # Utility scripts
├── docker-compose.yaml # Docker Compose configuration
├── Dockerfile # Dockerfile for Vault service
├── package.json # Node.js dependencies
├── tsconfig.json # TypeScript configuration
├── justfile # Task runner configuration
├── README.md # Main project documentation
└── structure.md # This file
HTTP server and API endpoints.
src/server/
├── index.ts # Main Express.js server entry point
├── routes/ # API routes
│ ├── health/ # Health check endpoints
│ │ └── index.ts # Health check route handlers
│ └── metrics/ # Metrics endpoints
│ └── index.ts # Prometheus metrics route handlers
├── middleware/ # Express middleware
│ ├── errorHandler.ts # Error handling middleware
│ └── logger.ts # Request logging middleware
└── config/ # Server configuration
├── config.ts # Configuration management
└── logger.ts # Logger initialization
Purpose:
src/server/- Express.js HTTP server with health checks and metricssrc/server/routes/health/- Health check endpoints for monitoringsrc/server/routes/metrics/- Prometheus metrics endpointsrc/server/middleware/- Express middleware for error handling and logging
Reusable TypeScript packages that form the core of Vault.
src/lib/
├── common/ # Shared types and utilities
│ ├── fact.ts # Fact interface and validation
│ └── types.ts # Common interfaces and type aliases
│
├── logger/ # Logging abstraction
│ ├── logger.ts # Logger interface
│ └── default.ts # Default logger implementation
│
├── metrics/ # Prometheus metrics
│ └── metrics.ts # Metric definitions and registration
│
├── outbox/ # Outbox pattern implementation
│ ├── consumer/ # Event consumer
│ │ ├── consumer.ts # OutboxConsumer - main consumer logic
│ │ ├── consumer.test.ts # Consumer tests
│ │ └── kafka.ts # Kafka adapter for publishing events
│ └── producer/ # Event producer
│ └── producer.ts # PostgresAdapter - emits events to WAL
│
├── postgres/ # PostgreSQL integration
│ ├── wal.ts # WALSubscriber - logical replication consumer
│ ├── wal.integration.test.ts # Integration tests for WAL
│ ├── health.ts # Health check utilities
│ ├── pool.ts # Connection pool management
│ └── types.ts # PostgreSQL-specific types and interfaces
│
└── proto/ # Protocol Buffer definitions
├── outbox.proto # Protobuf schema for OutboxEvent
└── outbox.ts # Generated TypeScript types from proto
Key Components:
- fact.ts: Defines the
Factinterface representing an event - types.ts: Common interfaces (
OutboxClient,OutboxWorker,EventHandler)
- producer.ts:
PostgresAdapter- Emits events usingpg_logical_emit_message()- Works within database transactions
- Serializes events as Protocol Buffers
- Supports prefix-based routing
- consumer.ts:
OutboxConsumer- Orchestrates event consumption- Subscribes to WAL events
- Routes events to registered handlers
- Manages acknowledgment flow
- kafka.ts:
KafkaAdapter- Kafka producer implementation- Publishes events to Kafka topics
- Handles acknowledgments
- Supports SSL/SASL authentication
- wal.ts:
WALSubscriber- PostgreSQL logical replication subscriber- Manages replication slots and publications
- Consumes logical replication stream
- Filters events by prefix
- Tracks LSN (Log Sequence Number) for resumability
- pool.ts: Connection pool management using
pgorpgxlibraries - health.ts: Health check utilities for monitoring
- outbox.proto: Protocol Buffer schema defining
OutboxEventmessage - outbox.ts: Generated TypeScript types (do not edit manually)
Consumer service implementations.
src/consumers/
├── vault-consumer/ # Main Vault consumer service
│ ├── index.ts # Consumer service entry point
│ └── handlers/ # Event handlers
│ └── default.ts # Default event handler
│
└── e2e/ # End-to-end testing consumer
└── index.ts # E2E test runner demonstrating full pipeline
Purpose:
src/consumers/vault-consumer/- Production-ready consumer service that bridges PostgreSQL WAL to Kafkasrc/consumers/e2e/- Demonstrates complete workflow: producer → WAL → consumer → Kafka
Helper scripts for setup and development.
scripts/
├── init-postgres.sh # PostgreSQL initialization script
│ # Creates publication and replication slot
│
└── console-config.yml # Kafka console configuration
Usage:
init-postgres.sh: Run once to set up PostgreSQL for logical replicationconsole-config.yml: Configuration for Kafka console tools
-
docker-compose.yaml: Docker Compose setup for development- PostgreSQL with logical replication enabled
- Kafka broker
- Zookeeper (for Kafka)
- Vault service
-
Dockerfile: Docker image for Vault consumer service -
justfile: Task runner configuration (alternative to Make)- Common tasks: build, test, lint, run
-
package.json: Node.js dependencies and scripts -
tsconfig.json: TypeScript compiler configuration
-
README.md: Main project documentation- Problem statement
- Architecture overview
- Setup instructions
- Usage examples
- Reliability guarantees
-
structure.md: This file - repository structure documentation
- Events are emitted atomically within database transactions
- Separate consumer process reads and publishes events
- Ensures consistency between database and message broker
PostgresAdapterabstracts event emissionKafkaAdapterabstracts Kafka publishing- Supports multiple database drivers (pg, pgx)
SQLExecutorinterface for database operationsOutboxProducerinterface for event emissionEventHandlerinterface for event processing
- Events serialized as Protocol Buffers
- Language-agnostic event format
- Supports schema evolution
src/lib/
├── outbox/
│ └── consumer/
│ └── consumer.test.ts # Unit tests for consumer
│
└── postgres/
└── wal.integration.test.ts # Integration tests for WAL
Test Types:
- Unit Tests: Test individual components in isolation
- Integration Tests: Test WAL subscription with real PostgreSQL
- E2E Tests: Full pipeline test in
src/consumers/e2e/
Prometheus metrics exposed at /metrics:
vault_events_emitted_total- Total events emittedvault_events_consumed_total- Total events consumedvault_event_processing_seconds- Event processing latencyvault_kafka_produce_success_total- Successful Kafka publishesvault_kafka_produce_failures_total- Failed Kafka publishesvault_wal_replication_errors_total- WAL replication errorsvault_event_handler_success_total- Successful handler executionsvault_event_handler_failures_total- Failed handler executions
HTTP endpoint: GET /health
- Returns
200 OKif WAL consumer is healthy - Returns
503 Service Unavailableif unhealthy - Monitors replication slot status and connection health
- Setup PostgreSQL: Run
scripts/init-postgres.sh - Start Infrastructure:
docker-compose up -d postgres kafka - Install Dependencies:
npm installoryarn install - Build:
npm run buildorjust build - Run Consumer:
npm startordocker-compose up vault - Emit Events: Use producer from your application
- Monitor: Check
/healthand/metricsendpoints
| Language | Status | Location | Purpose |
|---|---|---|---|
| TypeScript | ✅ Production | src/ |
Core library and consumer service |
| Express.js | ✅ Production | src/server/ |
HTTP server and API |
pgorpgx- PostgreSQL driverpg-logical-replication- Logical replication librarykafkajsornode-rdkafka- Kafka clientprotobufjs- Protocol Buffer supportprom-client- Prometheus metricsexpress- HTTP server frameworktypescript- TypeScript compiler
When adding new features:
- Core Logic: Add to appropriate
src/lib/package - Server Routes: Add to
src/server/routes/directory - Tests: Add alongside code with
.test.tssuffix - Documentation: Update
README.mdandstructure.md - Examples: Add to
src/consumers/e2e/for demonstration
- TypeScript files:
camelCase.tsorkebab-case.ts - Test files:
*.test.tsor*.spec.ts - Proto files:
*.proto - Config files:
*.yaml,*.yml,*.json - Scripts:
*.sh
- Never edit generated files:
outbox.ts(from proto) is generated - Transaction safety: Always emit facts within database transactions
- LSN tracking: Consumer tracks LSN positions for resumability
- Prefix filtering: Events are filtered by prefix in WAL subscriber
- Health monitoring: Consumer exposes health checks for monitoring
- TypeScript strict mode: Project uses strict TypeScript configuration
- Async/await: Prefer async/await over callbacks for better error handling
Last updated: Generated from current repository structure