Database Connection Update #4

Merged
humenius merged 49 commits from feature/database-connection into master 2021-02-08 03:16:51 +01:00
4 changed files with 126 additions and 88 deletions
Showing only changes of commit c514337fea - Show all commits

View File

@@ -1,17 +1,19 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { SinusBotService } from "./services/sinusbot.service"; import { SinusBotService } from './services/sinusbot.service';
import { DatabaseService } from './database/database.service'; import { DatabaseService } from './database/database.service';
import { LoggerMiddleware } from './logger.middleware'; import { LoggerMiddleware } from './logger.middleware';
import { PrismaService } from './prisma/prisma.service';
import { TimeTrackerPredicate } from './database/timetracking.predicate';
@Module({ @Module({
imports: [], imports: [],
controllers: [AppController], controllers: [AppController],
providers: [AppService, SinusBotService, DatabaseService], providers: [AppService, SinusBotService, DatabaseService, PrismaService, TimeTrackerPredicate],
}) })
export class AppModule implements NestModule { export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer): any { configure(consumer: MiddlewareConsumer): any {
consumer.apply(LoggerMiddleware).forRoutes('*') consumer.apply(LoggerMiddleware).forRoutes('*');
} }
} }

View File

@@ -1,8 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { PrismaClient, seasons, timetracker, user, ranks } from '@prisma/client';
import logger from 'src/logger/Logger';
import { TimeTrackerPredicate } from './timetracking.predicate'; import { TimeTrackerPredicate } from './timetracking.predicate';
import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases'; import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases';
import { PrismaService } from '../prisma/prisma.service';
@Injectable() @Injectable()
export class DatabaseService { export class DatabaseService {
@@ -15,28 +14,30 @@ export class DatabaseService {
// private database = process.env.MYSQL_DATABASE // private database = process.env.MYSQL_DATABASE
constructor( constructor(
private readonly prismaClient: PrismaClient, private readonly prismaClient: PrismaService,
private readonly timetrackerPredicate: TimeTrackerPredicate private readonly timetrackerPredicate: TimeTrackerPredicate,
) {} ) {
}
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => { public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
let seasons let seasons;
return this.prismaClient.seasons.findOne({ return this.prismaClient.seasons
.findOne({
where: { where: {
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
season_id: Number(seasonId) season_id: Number(seasonId),
} },
}) })
.then(result => seasons = result) .then(result => (seasons = result))
.then(this.fetchMaxSeasonId) .then(this.fetchMaxSeasonId)
.then(result => { .then(result => {
seasons.maxSeasonId = result seasons.maxSeasonId = result;
return seasons return seasons;
}) });
}; };
public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => { public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => {
let response let response;
return this.fetchSeasonInfos(seasonId) return this.fetchSeasonInfos(seasonId)
.then(result => { .then(result => {
response = { response = {
@@ -44,17 +45,17 @@ export class DatabaseService {
maxSeasonid: result.maxSeasonId, maxSeasonid: result.maxSeasonId,
dates: { dates: {
start: result.start_date, start: result.start_date,
end: result.end_date end: result.end_date,
} },
} };
return result.season_id.toString() return result.season_id.toString();
}) })
.then(this.fetchTimeTrackerStats) .then(this.fetchTimeTrackerStats)
.then(this.timetrackerPredicate.process) .then(this.timetrackerPredicate.process)
.then(result => { .then(result => {
response.stats = result response.stats = result;
return response return response;
}) });
}; };
// public fetchStats = async (seasonId: string): Promise<TableEntry[]> => this.prismaClient.timetracker.findMany({ // public fetchStats = async (seasonId: string): Promise<TableEntry[]> => this.prismaClient.timetracker.findMany({
@@ -69,28 +70,31 @@ export class DatabaseService {
// }) // })
// .then(this.timetrackerPredicate.process); // .then(this.timetrackerPredicate.process);
fetchTimeTrackerStats: (string) => Promise<TimeTrackerStats[]> = (seasonId: string) => this.prismaClient.timetracker.findMany({ fetchTimeTrackerStats: (string) => Promise<TimeTrackerStats[]> = (
seasonId: string,
) =>
this.prismaClient.timetracker.findMany({
include: { include: {
user: true, user: true,
ranks: true ranks: true,
}, },
where: { where: {
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
season_id: Number(seasonId) season_id: Number(seasonId),
} },
}) });
fetchMaxSeasonId: () => Promise<number> = () => new Promise( fetchMaxSeasonId: () => Promise<number> = () =>
(resolve, reject) => { new Promise((resolve, reject) => {
this.prismaClient.seasons.aggregate({ this.prismaClient.seasons
.aggregate({
max: { max: {
// eslint-disable-next-line @typescript-eslint/camelcase // eslint-disable-next-line @typescript-eslint/camelcase
season_id: true season_id: true,
} },
}) })
.then(result => result.max.season_id) .then(result => result.max.season_id)
.then(resolve) .then(resolve)
.catch(reject) .catch(reject);
} });
)
} }

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from './prisma.service';
describe('PrismaService', () => {
let service: PrismaService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PrismaService],
}).compile();
service = module.get<PrismaService>(PrismaService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

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