Browse Source

Database package exports entities correctly. Database connection function connects via arguments instead of environment variables

dev
David Ludwig 4 years ago
parent
commit
aaf78ed5df
1 changed files with 16 additions and 10 deletions
  1. +16
    -10
      packages/database/src/index.ts

+ 16
- 10
packages/database/src/index.ts View File

@ -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),


Loading…
Cancel
Save