Overview
Overview
Relevant source files
The following files were used as context for generating this wiki page:
The @primebrick/sdk package provides shared microservice infrastructure for the Primebrick v3 ecosystem. Its primary goal is to standardize common cross-cutting concerns—such as configuration loading, database migrations, service registration, and lifecycle management—while remaining strictly decoupled from specific database drivers or business logic.
The SDK is built on the Ports and Adapters (Hexagonal) architecture pattern. By defining abstract interfaces (Ports) for external dependencies, the SDK allows consuming microservices to provide their own implementations (Adapters) using whatever Data Access Layer (DAL) or database technology they prefer.
System Purpose & Core Philosophy
The SDK serves as the foundational backbone for microservices, ensuring that every service follows a consistent operational pattern:
- Zero Direct Dependencies: The SDK has no mandatory runtime dependencies on database drivers or specific frameworks package.json:33.
- Dependency Inversion: Consumers implement port interfaces to bridge the SDK with their chosen persistence layer src/index.ts:4-5.
- Graceful Resilience: Built-in support for signal handling, parallel resource cleanup, and idempotent migrations README.md:10-13.
- Type Safety: First-class TypeScript support with exported interfaces for all configuration and registry entities src/index.ts:18-58.
High-Level Architecture
The following diagram illustrates how the SDK sits between the Microservice logic and external infrastructure through the use of Port interfaces.
SDK Integration Pattern
Code
Sources: src/index.ts:1-16, README.md:5-17
Module Map
The SDK is organized into several functional modules, each addressing a specific microservice requirement:
| Module | Primary Responsibility | Key Entities |
|---|---|---|
| Ports | Abstract contracts for external systems. | ConfigRepositoryPort, DatabasePort, ServiceRegistryPort, HealthCheckPort |
| Config | Database-backed configuration with in-memory caching. | ConfigLoader, IConfigEntity |
| Migrations | SHA256-based idempotent database patch runner. | applyPatches, PATCH_REGISTRY_DDL |
| Service | Service discovery and heartbeat management. | ServiceRegistrar, IServiceRegistry |
| Lifecycle | Signal handling and graceful shutdown orchestration. | GracefulShutdown |
| HTTP | Lightweight health check server. | createHttpServer, HealthCheck |
| Env | Centralized environment variable validation. | validateEnv, requireEnv |
| NATS | Singleton management for NATS messaging. | NatsClient |
Sources: src/index.ts:18-58, README.md:5-17
Integration Flow
When a microservice starts, it typically follows this integration sequence using the SDK:
- Environment Validation: Use
requireEnvto ensure all necessaryprocess.envvariables are present src/index.ts:57. - Port Implementation: The service creates classes or objects that satisfy the Port interfaces (e.g., a
DatabasePortimplementation using Drizzle or Prisma). - Bootstrap:
- Initialize
ConfigLoaderto fetch runtime settings src/index.ts:26. - Execute
applyPatchesto ensure the database schema is up to date src/index.ts:40. - Register the service instance via
ServiceRegistrarto start heartbeats src/index.ts:44.
- Initialize
- Lifecycle Setup: Install
GracefulShutdownhandlers to manageSIGTERMandSIGINTsignals src/index.ts:47.
Entity Mapping: Code to Infrastructure
Code
Sources: src/index.ts:25-44, src/migrations/patch-registry.ts:30-31
Next Steps
To dive deeper into specific areas of the SDK, refer to the following sections:
- Getting Started: A step-by-step guide on installing the package, handling the
natspeer dependency, and wiring the SDK into a new service. - Architecture: Ports & Adapters: A detailed look at the hexagonal design, the specific port contracts, and how to write custom adapters.
- Core Modules: Detailed documentation for each individual module (Config, Migrations, Lifecycle, etc.).