@@ -8,45 +8,45 @@ import { exec } from "child_process";
8
8
import { promisify } from "util" ;
9
9
10
10
export class DbHelper {
11
- private client : PrismaDbClient | null = null ;
12
- private container : StartedPostgreSqlContainer | null = null ;
13
- private readonly execAsync = promisify ( exec ) ;
14
- private readonly cwd = new URL ( ".." , import . meta. url ) ;
11
+ # client: PrismaDbClient | null = null ;
12
+ # container: StartedPostgreSqlContainer | null = null ;
13
+ readonly # execAsync = promisify ( exec ) ;
14
+ readonly # cwd = new URL ( ".." , import . meta. url ) ;
15
15
16
16
static async create ( ) : Promise < DbHelper > {
17
17
const dbHelper = new DbHelper ( ) ;
18
- dbHelper . container = await new PostgreSqlContainer ( ) . start ( ) ;
19
- dbHelper . client = await dbHelper . setupDatabase ( ) ;
18
+ dbHelper . # container = await new PostgreSqlContainer ( ) . start ( ) ;
19
+ dbHelper . # client = await dbHelper . setupDatabase ( ) ;
20
20
return dbHelper ;
21
21
}
22
22
23
23
getClient ( ) : PrismaDbClient {
24
- if ( ! this . client ) {
24
+ if ( ! this . # client) {
25
25
throw new Error ( "PrismaClient has not been initialized" ) ;
26
26
}
27
- return this . client ;
27
+ return this . # client;
28
28
}
29
29
30
30
getContainer ( ) : StartedPostgreSqlContainer {
31
- if ( ! this . container ) {
31
+ if ( ! this . # container) {
32
32
throw new Error ( "Container has not been initialized" ) ;
33
33
}
34
- return this . container ;
34
+ return this . # container;
35
35
}
36
36
37
37
async setupDatabase ( ) : Promise < PrismaDbClient > {
38
- await this . execAsync ( "npm run db:push" , {
38
+ await this . # execAsync( "npm run db:push" , {
39
39
env : {
40
40
...process . env ,
41
- DATABASE_URL : this . container ! . getConnectionUri ( ) ,
41
+ DATABASE_URL : this . # container! . getConnectionUri ( ) ,
42
42
} ,
43
- cwd : this . cwd ,
43
+ cwd : this . # cwd,
44
44
encoding : "utf-8" ,
45
45
} ) ;
46
46
return new PrismaDbClient ( {
47
47
datasources : {
48
48
db : {
49
- url : this . container ! . getConnectionUri ( ) ,
49
+ url : this . # container! . getConnectionUri ( ) ,
50
50
} ,
51
51
} ,
52
52
log : [
@@ -65,4 +65,9 @@ export class DbHelper {
65
65
await this . getClient ( ) . survey . deleteMany ( ) ;
66
66
await this . getClient ( ) . role . deleteMany ( ) ;
67
67
}
68
+
69
+ async stop ( ) {
70
+ await this . getClient ( ) . $disconnect ( ) ;
71
+ await this . getContainer ( ) . stop ( ) ;
72
+ }
68
73
}
0 commit comments