Documentation Index
Fetch the complete documentation index at: https://mintlify.com/backtest-kit/backtest-kit-redis-mongo-docker/llms.txt
Use this file to discover all available pages before exploring further.
MongoService (exported as MongooseService) is the infrastructure service responsible for establishing and supervising the Mongoose connection to MongoDB. It is registered in the IoC container as a singleton and exposes a single public method — waitForInit() — that all database adapters call before performing any read or write operation. Because the method is wrapped with singleshot from functools-kit, the connection is initiated at most once no matter how many adapters call it concurrently; every subsequent caller receives the same resolved promise.
Implementation
Constructor
MongooseService takes no constructor arguments. It is instantiated by the IoC container (di-factory) and receives its loggerService dependency via property injection:
new MongooseService() directly; access it through ioc.mongoService.
Methods
waitForInit(): Promise<Mongoose>
Establishes the Mongoose connection to MongoDB and resolves to the connected Mongoose instance. Safe to call multiple times — subsequent calls resolve immediately against the already-settled promise thanks to the singleshot wrapper.
The method races the connection event against a 15-second timeout. If the timeout fires before the connected event, waitForInit.clear() resets the singleshot gate so the next call will attempt a fresh connection, and the method throws Error("Mongoose connection timeout").
Usage from src/main/backtest.ts:
Connection String Configuration
The connection URI is read from theCC_MONGO_CONNECTION_STRING environment variable (defined in src/config/params.ts):
| Variable | Default |
|---|---|
CC_MONGO_CONNECTION_STRING | mongodb://localhost:27017/backtest-pro?wtimeoutMS=15000 |
The default connection string includes
wtimeoutMS=15000, which sets a 15-second write concern timeout. This means any write operation that cannot be acknowledged within 15 seconds will fail rather than hang indefinitely. Adjust this value in production environments where network latency to the MongoDB replica set may be higher.The waitForInfra() Pattern
src/config/setup.ts defines a shared waitForInfra() singleshot that gates every persist adapter on both mongoService and redisService being ready:
waitForInit(initial: boolean) calls waitForInfra() only on the first invocation (initial === true):
Connection Lifecycle Events
AfterwaitForInit() resolves, MongooseService registers listeners on the Mongoose connection for operational visibility:
| Event | Behaviour |
|---|---|
connected | Logs a confirmation message |
error | Logs the error payload and rethrows as a typed error with originalError |
disconnected | Logs a warning |
reconnected | Logs a recovery message |
SIGINT | Gracefully closes the connection before the process exits |