98 lines
3.2 KiB
TypeScript
98 lines
3.2 KiB
TypeScript
import { asFunction, asValue, type AwilixContainer } from 'awilix';
|
|
import { defined } from '@stock-bot/invariants';
|
|
import { MongoDBClient } from '@stock-bot/mongodb';
|
|
import { PostgreSQLClient } from '@stock-bot/postgres';
|
|
import { QuestDBClient } from '@stock-bot/questdb';
|
|
import type { AppConfig } from '../config/schemas/index';
|
|
import type { ServiceDefinitions } from '../container/types';
|
|
|
|
export function registerDatabaseServices(
|
|
container: AwilixContainer<ServiceDefinitions>,
|
|
config: AppConfig
|
|
): void {
|
|
// MongoDB
|
|
if (config.mongodb?.enabled) {
|
|
container.register({
|
|
mongoClient: asFunction(({ logger }) => {
|
|
// The driver parses URI credentials, SRV and query options. Do not
|
|
// reconstruct them with an incomplete URI regular expression.
|
|
const mongoConfig = {
|
|
host: config.mongodb.host ?? 'localhost',
|
|
port: config.mongodb.port ?? 27017,
|
|
database: config.mongodb.database,
|
|
username: config.mongodb.user,
|
|
password: config.mongodb.password,
|
|
authSource: config.mongodb.authSource,
|
|
uri: config.mongodb.uri,
|
|
poolSettings: {
|
|
maxPoolSize: config.mongodb.poolSize,
|
|
minPoolSize: 1,
|
|
maxIdleTime: 10000,
|
|
},
|
|
};
|
|
return new MongoDBClient(mongoConfig, logger);
|
|
}).singleton(),
|
|
});
|
|
} else {
|
|
container.register({
|
|
mongoClient: asValue(null),
|
|
});
|
|
}
|
|
|
|
// PostgreSQL
|
|
if (config.postgres?.enabled) {
|
|
container.register({
|
|
postgresClient: asFunction(({ logger }) => {
|
|
const pgConfig = {
|
|
host: config.postgres.host,
|
|
port: config.postgres.port,
|
|
database: config.postgres.database,
|
|
username: config.postgres.user,
|
|
password: config.postgres.password,
|
|
ssl: { enabled: config.postgres.ssl, rejectUnauthorized: true },
|
|
poolSettings: {
|
|
min: 0,
|
|
max: config.postgres.poolSize,
|
|
idleTimeoutMillis: config.postgres.idleTimeout,
|
|
},
|
|
// Omitted query/session limits retain the server's configured defaults.
|
|
timeouts: { connection: config.postgres.connectionTimeout },
|
|
};
|
|
|
|
logger.debug('PostgreSQL config:', {
|
|
...pgConfig,
|
|
password: pgConfig.password ? '***' : 'NO_PASSWORD',
|
|
});
|
|
return new PostgreSQLClient(pgConfig, logger);
|
|
}).singleton(),
|
|
});
|
|
} else {
|
|
container.register({
|
|
postgresClient: asValue(null),
|
|
});
|
|
}
|
|
|
|
// QuestDB
|
|
if (config.questdb?.enabled) {
|
|
container.register({
|
|
questdbClient: asFunction(({ logger }) => {
|
|
return new QuestDBClient(
|
|
{
|
|
host: defined(config.questdb).host,
|
|
httpPort: defined(config.questdb).httpPort,
|
|
pgPort: defined(config.questdb).pgPort,
|
|
influxPort: defined(config.questdb).influxPort,
|
|
database: defined(config.questdb).database,
|
|
user: defined(config.questdb).user,
|
|
password: defined(config.questdb).password,
|
|
},
|
|
logger
|
|
);
|
|
}).singleton(),
|
|
});
|
|
} else {
|
|
container.register({
|
|
questdbClient: asValue(null),
|
|
});
|
|
}
|
|
}
|