-
Notifications
You must be signed in to change notification settings - Fork 4
/
postgresql.environment.ts
45 lines (36 loc) · 1.71 KB
/
postgresql.environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {join} from 'path';
import {TestcontainersEnvironment} from '@trendyol/jest-testcontainers';
import migration from 'node-pg-migrate';
import {PostgreSQLAdapter} from '../../src/postgresql/postgresql.adapter';
import {PostgreSQLConfig} from '../../src/postgresql/postgresql.config';
class PostgreSQLEnvironment extends TestcontainersEnvironment {
private static readonly MIGRATION_DIR = join(__dirname, '../../migrations');
private static readonly MIGRATION_TABLE = 'pgmirations';
private static readonly POSTGRESQL_DB = 'postgres';
private static readonly POSTGRESQL_AUTH = 'postgres:integration-pass';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private postgreSQLAdapter: PostgreSQLAdapter = undefined as any;
async setup() {
await super.setup();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const globals: any = (this as any).global;
const uri = `postgresql://${PostgreSQLEnvironment.POSTGRESQL_AUTH}@${globals.__TESTCONTAINERS_POSTGRE_IP__}:${globals.__TESTCONTAINERS_POSTGRE_PORT_5432__}/${PostgreSQLEnvironment.POSTGRESQL_DB}`;
const postgreSQLConfig: PostgreSQLConfig = {uri};
const postgreSQLAdapter = new PostgreSQLAdapter(postgreSQLConfig);
await postgreSQLAdapter.connect();
await migration({
databaseUrl: uri,
dir: PostgreSQLEnvironment.MIGRATION_DIR,
migrationsTable: PostgreSQLEnvironment.MIGRATION_TABLE,
direction: 'up',
count: 999,
});
globals.postgreSQLAdapter = postgreSQLAdapter;
this.postgreSQLAdapter = postgreSQLAdapter;
}
async teardown() {
await super.teardown();
this.postgreSQLAdapter && (await this.postgreSQLAdapter.close());
}
}
module.exports = PostgreSQLEnvironment;