|
|
@ -1,19 +1,25 @@ |
|
|
|
import * as entities from "./entities"; |
|
|
|
import { readFile } from "fs/promises"; |
|
|
|
import { createConnection } from "typeorm"; |
|
|
|
|
|
|
|
export async function connectToDatabase() { |
|
|
|
// Fetch the database password from the secret file
|
|
|
|
let password = (await readFile(<string>process.env["DB_PASSWORD_FILE"])).toString().trim(); |
|
|
|
/** |
|
|
|
* Export the entities |
|
|
|
*/ |
|
|
|
export * from "./entities"; |
|
|
|
|
|
|
|
/** |
|
|
|
* A convenience function to connect to the database |
|
|
|
*/ |
|
|
|
export default async function connectToDatabase(host: string, port: number, username: string, |
|
|
|
password: string, database: string) |
|
|
|
{ |
|
|
|
// Create the database connection
|
|
|
|
await createConnection({ |
|
|
|
type : <"mysql" | "mariadb">process.env["DB_TYPE"], |
|
|
|
host : process.env["DB_HOST"], |
|
|
|
port : parseInt(<string>process.env["DB_PORT"]), |
|
|
|
username : process.env["DB_USER"], |
|
|
|
return await createConnection({ |
|
|
|
type : "mysql", |
|
|
|
host : host, |
|
|
|
port : port, |
|
|
|
username : username, |
|
|
|
password : password, |
|
|
|
database : process.env["DB_DATABASE"], |
|
|
|
database : database, |
|
|
|
// synchronize: process.env["NODE_ENV"] != "production",
|
|
|
|
synchronize: true, // Seems stable enough for my liking
|
|
|
|
entities : Object.values(entities), |
|
|
|