Browse Source

Fix database connection error handling

pull/1125/head
Thomas 3 years ago
parent
commit
3bd7bf49e8
  1. 16
      apps/api/src/app/app.controller.ts
  2. 2
      apps/api/src/app/order/order.service.ts
  3. 4
      apps/api/src/services/exchange-rate-data.service.ts
  4. 18
      apps/api/src/services/prisma.service.ts

16
apps/api/src/app/app.controller.ts

@ -1,6 +1,20 @@
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { Controller } from '@nestjs/common'; import { Controller } from '@nestjs/common';
@Controller() @Controller()
export class AppController { export class AppController {
public constructor() {} public constructor(
private readonly exchangeRateDataService: ExchangeRateDataService,
private readonly prismaService: PrismaService
) {
this.initialize();
}
private async initialize() {
try {
await this.prismaService.$connect();
await this.exchangeRateDataService.initialize();
} catch {}
}
} }

2
apps/api/src/app/order/order.service.ts

@ -21,7 +21,7 @@ import {
} from '@prisma/client'; } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { endOfToday, isAfter } from 'date-fns'; import { endOfToday, isAfter } from 'date-fns';
import { groupBy, isString } from 'lodash'; import { groupBy } from 'lodash';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { Activity } from './interfaces/activities.interface'; import { Activity } from './interfaces/activities.interface';

4
apps/api/src/services/exchange-rate-data.service.ts

@ -22,9 +22,7 @@ export class ExchangeRateDataService {
private readonly dataProviderService: DataProviderService, private readonly dataProviderService: DataProviderService,
private readonly prismaService: PrismaService, private readonly prismaService: PrismaService,
private readonly propertyService: PropertyService private readonly propertyService: PropertyService
) { ) {}
this.initialize();
}
public getCurrencies() { public getCurrencies() {
return this.currencies?.length > 0 ? this.currencies : [this.baseCurrency]; return this.currencies?.length > 0 ? this.currencies : [this.baseCurrency];

18
apps/api/src/services/prisma.service.ts

@ -1,15 +1,25 @@
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit
} from '@nestjs/common';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
@Injectable() @Injectable()
export class PrismaService export class PrismaService
extends PrismaClient extends PrismaClient
implements OnModuleInit, OnModuleDestroy { implements OnModuleInit, OnModuleDestroy
async onModuleInit() { {
public async onModuleInit() {
try {
await this.$connect(); await this.$connect();
} catch (error) {
Logger.error(error, 'PrismaService');
}
} }
async onModuleDestroy() { public async onModuleDestroy() {
await this.$disconnect(); await this.$disconnect();
} }
} }

Loading…
Cancel
Save