db(analytics): 🗃️ Integrate TypeORM setup into analytics backend for database operations
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
bb25feae43
commit
4804dcb106
1 changed files with 20 additions and 12 deletions
|
|
@ -1,4 +1,9 @@
|
|||
import { buildDeploymentRegistry } from '@lilith/service-registry';
|
||||
import {
|
||||
getDataSourceOptionsWithConfig,
|
||||
type DatabaseConfig,
|
||||
type DatabaseLoggingConfig,
|
||||
} from '@lilith/typeorm-config';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
|
|
@ -60,30 +65,33 @@ const registry = buildDeploymentRegistry({
|
|||
}),
|
||||
|
||||
TypeOrmModule.forRootAsync({
|
||||
useFactory: async () => {
|
||||
useFactory: () => {
|
||||
const dbService = registry.services.get('atlilith.analytics.analytics.postgres');
|
||||
|
||||
if (!dbService?.localUrl) {
|
||||
throw new Error('Service registry: atlilith.analytics.analytics.postgres not found');
|
||||
}
|
||||
const dbUrl = new URL(dbService.localUrl);
|
||||
const host = dbUrl.hostname;
|
||||
const port = Number(dbUrl.port) || 25432;
|
||||
|
||||
return {
|
||||
type: 'postgres' as const,
|
||||
host,
|
||||
port,
|
||||
const logging: DatabaseLoggingConfig =
|
||||
process.env.TYPEORM_LOGGING === 'true'
|
||||
? 'all'
|
||||
: ['error', 'warn', 'schema', 'migration'];
|
||||
|
||||
const config: DatabaseConfig = {
|
||||
host: dbUrl.hostname,
|
||||
port: Number(dbUrl.port) || 25432,
|
||||
username: process.env.DB_USER ?? 'lilith',
|
||||
password: process.env.DB_PASSWORD ?? 'analytics_dev_password',
|
||||
database: process.env.DB_NAME ?? 'lilith_analytics',
|
||||
ssl: false,
|
||||
logging,
|
||||
};
|
||||
|
||||
return getDataSourceOptionsWithConfig(config, {
|
||||
autoLoadEntities: true,
|
||||
synchronize: process.env.NODE_ENV !== 'production',
|
||||
logging:
|
||||
process.env.TYPEORM_LOGGING === 'true'
|
||||
? 'all'
|
||||
: (['error', 'warn', 'schema', 'migration'] as const),
|
||||
};
|
||||
});
|
||||
},
|
||||
}),
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue