Skip to content

Commit 34322a4

Browse files
committed
fix: import missing health module
Signed-off-by: William Phetsinorath <william.phetsinorath-open@interieur.gouv.fr>
1 parent 48f18b6 commit 34322a4

13 files changed

Lines changed: 237 additions & 23 deletions

File tree

apps/server-nestjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@nestjs/config": "^4.0.3",
4444
"@nestjs/core": "^11.1.16",
4545
"@nestjs/platform-express": "^11.1.16",
46+
"@nestjs/terminus": "^11.1.1",
4647
"@opentelemetry/api": "^1.9.0",
4748
"@opentelemetry/auto-instrumentations-node": "^0.70.1",
4849
"@opentelemetry/exporter-metrics-otlp-proto": "^0.213.0",

apps/server-nestjs/src/cpin-module/application-initialization/application-initialization-service/application-initialization.service.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('applicationInitializationServiceService', () => {
1919
PluginManagementService,
2020
DatabaseInitializationService,
2121
DatabaseService,
22+
PrismaService,
2223
],
2324
}).compile()
2425

apps/server-nestjs/src/cpin-module/application-initialization/database-initialization/database-initialization.service.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import type { TestingModule } from '@nestjs/testing'
22
import { Test } from '@nestjs/testing'
33
import { beforeEach, describe, expect, it } from 'vitest'
44

5+
import { PrismaService } from '../../infrastructure/database/prisma.service'
56
import { DatabaseInitializationService } from './database-initialization.service'
67

78
describe('databaseInitializationService', () => {
89
let service: DatabaseInitializationService
910

1011
beforeEach(async () => {
1112
const module: TestingModule = await Test.createTestingModule({
12-
providers: [DatabaseInitializationService],
13+
providers: [
14+
DatabaseInitializationService,
15+
PrismaService,
16+
],
1317
}).compile()
1418

1519
service = module.get<DatabaseInitializationService>(

apps/server-nestjs/src/cpin-module/application-initialization/database-initialization/database-initialization.service.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import { Injectable, Logger } from '@nestjs/common'
2-
import prisma from '../../../prisma'
1+
import { Inject, Injectable, Logger } from '@nestjs/common'
2+
import { PrismaService } from '../../infrastructure/database/prisma.service'
33

44
import { modelKeys } from './utils'
55

6-
type ExtractKeysWithFields<T> = {
7-
[K in keyof T]: T[K] extends { fields: any } ? K : never;
8-
}[keyof T]
9-
10-
type Models = ExtractKeysWithFields<typeof prisma>
11-
12-
type Imports = Partial<Record<Models, object[]>> & {
13-
associations: [Models, any[]]
6+
type ModelKey = (typeof modelKeys)[number]
7+
type Imports = Partial<Record<ModelKey, object[]>> & {
8+
associations: [ModelKey, any[]][]
149
}
1510

1611
@Injectable()
@@ -19,6 +14,8 @@ export class DatabaseInitializationService {
1914
DatabaseInitializationService.name,
2015
)
2116

17+
constructor(@Inject(PrismaService) private readonly prisma: PrismaService) {}
18+
2219
async initDb(data: Imports) {
2320
const dataStringified = JSON.stringify(data)
2421
const dataParsed = JSON.parse(dataStringified, (key, value) => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Inject, Injectable } from '@nestjs/common'
2+
import { HealthIndicatorService } from '@nestjs/terminus'
3+
import { PrismaService } from './prisma.service'
4+
5+
@Injectable()
6+
export class DatabaseHealthService {
7+
constructor(
8+
@Inject(PrismaService) private readonly prisma: PrismaService,
9+
@Inject(HealthIndicatorService) private readonly healthIndicatorService: HealthIndicatorService,
10+
) {}
11+
12+
async check(key: string) {
13+
const indicator = this.healthIndicatorService.check(key)
14+
try {
15+
await this.prisma.$queryRaw`SELECT 1`
16+
return indicator.up()
17+
} catch (error) {
18+
return indicator.down({
19+
message: error instanceof Error ? error.message : String(error),
20+
})
21+
}
22+
}
23+
}

apps/server-nestjs/src/cpin-module/infrastructure/database/database.service.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import type { TestingModule } from '@nestjs/testing'
22
import { Test } from '@nestjs/testing'
3-
import { beforeEach, describe, expect, it } from 'vitest'
3+
import { beforeEach, describe, expect, it, vi } from 'vitest'
44

55
import { ConfigurationModule } from '../configuration/configuration.module'
66
import { DatabaseService } from './database.service'
7+
import { PrismaService } from './prisma.service'
78

89
describe('databaseService', () => {
910
let service: DatabaseService
1011

1112
beforeEach(async () => {
1213
const module: TestingModule = await Test.createTestingModule({
1314
imports: [ConfigurationModule],
14-
providers: [DatabaseService],
15+
providers: [
16+
DatabaseService,
17+
{
18+
provide: PrismaService,
19+
useValue: {
20+
$connect: vi.fn().mockResolvedValue(undefined),
21+
$disconnect: vi.fn().mockResolvedValue(undefined),
22+
},
23+
},
24+
],
1525
}).compile()
1626

1727
service = module.get<DatabaseService>(DatabaseService)

apps/server-nestjs/src/cpin-module/infrastructure/database/database.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { setTimeout } from 'node:timers/promises'
22
import { Inject, Injectable, Logger } from '@nestjs/common'
3-
import prisma from '../../../prisma'
43
import { ConfigurationService } from '../configuration/configuration.service'
4+
import { PrismaService } from './prisma.service'
55

66
@Injectable()
77
export class DatabaseService {
88
private readonly loggerService = new Logger(DatabaseService.name)
99

10-
constructor(@Inject(ConfigurationService) private readonly configurationService: ConfigurationService) {
10+
constructor(
11+
@Inject(PrismaService) private readonly prisma: PrismaService,
12+
@Inject(ConfigurationService) private readonly configurationService: ConfigurationService,
13+
) {
1114
this.DELAY_BEFORE_RETRY
1215
= this.configurationService.isTest || this.configurationService.isCI
1316
? 1000
@@ -33,7 +36,7 @@ export class DatabaseService {
3336
`Trying to connect to Postgres with: ${this.configurationService.dbUrl}`,
3437
)
3538
}
36-
await prisma.$connect()
39+
await this.prisma.$connect()
3740

3841
this.loggerService.log('Connected to Postgres!')
3942
} catch (error) {
@@ -59,7 +62,7 @@ export class DatabaseService {
5962
async closeConnections() {
6063
this.closingConnections = true
6164
try {
62-
await prisma.$disconnect()
65+
await this.prisma.$disconnect()
6366
} catch (error) {
6467
this.loggerService.error(error)
6568
} finally {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { OnModuleDestroy, OnModuleInit } from '@nestjs/common'
2+
import { Injectable } from '@nestjs/common'
3+
import { PrismaClient } from '@prisma/client'
4+
5+
@Injectable()
6+
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
7+
async onModuleInit() {
8+
await this.$connect()
9+
}
10+
11+
async onModuleDestroy() {
12+
await this.$disconnect()
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Controller, Get, Inject } from '@nestjs/common'
2+
import { HealthCheck, HealthCheckService } from '@nestjs/terminus'
3+
import { DatabaseHealthService } from '../database/database-health.service'
4+
5+
@Controller('health')
6+
export class HealthController {
7+
constructor(
8+
@Inject(HealthCheckService) private readonly health: HealthCheckService,
9+
@Inject(DatabaseHealthService) private readonly database: DatabaseHealthService,
10+
) {}
11+
12+
@Get()
13+
@HealthCheck()
14+
check() {
15+
return this.health.check([
16+
() => this.database.check('database'),
17+
])
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Module } from '@nestjs/common'
2+
import { TerminusModule } from '@nestjs/terminus'
3+
import { DatabaseHealthService } from '../database/database-health.service'
4+
import { HealthController } from './health.controller'
5+
6+
@Module({
7+
imports: [
8+
TerminusModule,
9+
DatabaseHealthService,
10+
],
11+
controllers: [HealthController],
12+
})
13+
export class HealthModule {}

0 commit comments

Comments
 (0)