-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
40 lines (31 loc) · 1004 Bytes
/
db.js
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
import * as path from "path";
import esDirname from "es-dirname";
import { createPool } from "slonik";
import { SlonikMigrator } from "@slonik/migrator";
/**
* Set up the DB connection
*
* @returns {slonik.Pool} Slonik connection pool for performing queries
*/
export async function setupDB(args) {
const logger = args.logger;
const dbURL = args.dbURL;
if (!dbURL) { throw new Error("DB_URL is not configured"); }
// Build the pool
const pool = await createPool(dbURL);
const skipMigrations = !!args.skipMigrations ?? false;
if (skipMigrations) { return pool; }
// Perform DB migrations
const dirname = await esDirname();
const migrationsPath = path.resolve(path.join(dirname, "migrations"));
logger?.info(`reading migrations @ [${migrationsPath}]`);
// Build the migration object
const migrator = new SlonikMigrator({
migrationsPath,
migrationTableName: "migrations",
slonik: pool,
});
// Perform migrations
await migrator.up();
return pool;
}