narayan_system_engine 1.0.2
narayan_system_engine: ^1.0.2 copied to clipboard
The backend execution engine for the Narayan system, handling Outbox processing and Projections.
Narayan System Engine #
The backend execution engine for the Narayan system. It acts as the "Muscle" to the Core's "Brain".
Responsibilities #
- Command Handling: Entry point for apps invokes Use Cases in Core.
- Event Persistence (Outbox): Captures events emitted by Use Cases and saves them atomically to Firestore.
- Reliable Processing: Processes the Outbox and executes Projectors.
- Read Model Projection: Updates localized views (
customer_orders_view,loyalty_view) for fast app querying.
Firestore Schema #
outbox Collection #
Stores all domain and system events for reliable processing.
{
"eventId": "uuid",
"eventType": "OrderCompleted",
"aggregateId": "order-123",
"payload": { ... },
"occurredAt": Timestamp,
"status": "PENDING | PROCESSED | FAILED",
"retryCount": 0,
"processedAt": Timestamp,
"lastError": null
}
Read Models #
customer_orders_view
Optimized for customer order history.
// Collection: customer_orders_view
// DocId: orderId
{
"orderId": "...",
"customerId": "...",
"status": "COMPLETED",
"placedAt": "ISO8601",
"lastUpdatedAt": Timestamp
}
loyalty_view
Optimized for showing loyalty status.
// Collection: loyalty_view
// DocId: accountId (or customerId)
{
"accountId": "...",
"lastRewardAmount": 10.0,
"lastRewardType": "CASHBACK",
"lastUpdatedAt": Timestamp,
"rewards": [ ... ]
}
Architecture #
- Engine (
SystemEngine): Composition root. - Bus (
FirebaseOutboxEventBus): AdaptsEventBusPortto write to Firestore Outbox. - Processor (
OutboxProcessor): Polls/Listens to Outbox and delegates to Projectors. - Projector (
OrderPlacedProjector): Updates specific Read Models purely from Events.
Usage #
final engine = SystemEngine(clock: RealClock());
engine.start();
// Execute a command
await engine.placeOrder(input, ...);
// Stop
await engine.stop();
Testing Philosophy #
The System Engine is the most critical component for data integrity. We employ a rigorous testing strategy:
- FakeFirestore: All tests run against
fake_cloud_firestoreto ensure fast, deterministic execution without real network or emulator dependencies. - Idempotency Verification: Projectors are tested by re-playing events multiple times to ensure data is never corrupted or duplicated.
- Chaos Testing: We simulate correct handling of duplicate events and partial failures.
- Strict Boundaries: The engine is tested to ensure it never depends on internal Core logic, only the public API.
Running Tests #
Use flutter test (due to cloud_firestore dependency):
flutter test
License #
This project is licensed under the MIT License - see the LICENSE file for details.