feat(backend): Databank initialiseren
Verbinding met databank aangemaakt, eerste entities toegevoegd, centrale API aangemaakt om omgevingsvariabelen voor onze app op te vragen.
This commit is contained in:
parent
c07bb959cf
commit
62a278a6e0
8 changed files with 86 additions and 10 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -43,6 +43,9 @@ build/Release
|
|||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# package-lock.json
|
||||
backend/package-lock.json
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
web_modules/
|
||||
|
||||
|
@ -643,6 +646,7 @@ FodyWeavers.xsd
|
|||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
|
|
6
backend/src/entities/users/student.entity.ts
Normal file
6
backend/src/entities/users/student.entity.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import {User} from "./user.entity";
|
||||
import { Entity } from '@mikro-orm/core';
|
||||
|
||||
@Entity()
|
||||
export class Student extends User {
|
||||
}
|
7
backend/src/entities/users/teacher.entity.ts
Normal file
7
backend/src/entities/users/teacher.entity.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { Entity } from '@mikro-orm/core';
|
||||
import {User} from "./user.entity";
|
||||
|
||||
@Entity()
|
||||
export class Teacher extends User {
|
||||
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
|
||||
|
||||
@Entity()
|
||||
@Entity({abstract: true})
|
||||
export class User {
|
||||
@PrimaryKey({ type: 'number' })
|
||||
id!: number;
|
||||
@PrimaryKey({type: "string"})
|
||||
username!: string;
|
||||
|
||||
@Property()
|
||||
firstName: string = '';
|
|
@ -1,9 +1,14 @@
|
|||
import {Options} from '@mikro-orm/core';
|
||||
import { PostgreSqlDriver } from '@mikro-orm/postgresql';
|
||||
import {PostgreSqlDriver} from "@mikro-orm/postgresql";
|
||||
import {EnvVars, getEnvVar, getNumericEnvVar} from "./util/envvars";
|
||||
|
||||
const config: Options = {
|
||||
driver: PostgreSqlDriver,
|
||||
dbName: 'dwengo',
|
||||
host: getEnvVar(EnvVars.DbHost, {required: true}),
|
||||
port: getNumericEnvVar(EnvVars.DbPort, {defaultValue: 5432}),
|
||||
dbName: getEnvVar(EnvVars.DbName, {defaultValue: "dwengo"}),
|
||||
user: getEnvVar(EnvVars.DbUsername, {required: true}),
|
||||
password: getEnvVar(EnvVars.DbPassword, {required: true}),
|
||||
entities: ['dist/**/*.entity.js'],
|
||||
entitiesTs: ['src/**/*.entity.ts'],
|
||||
debug: true,
|
||||
|
|
|
@ -1,6 +1,19 @@
|
|||
import { MikroORM } from '@mikro-orm/core';
|
||||
import config from './mikro-orm.config.js';
|
||||
import {EnvVars, getEnvVar} from "./util/envvars";
|
||||
|
||||
export default async function initORM() {
|
||||
await MikroORM.init(config);
|
||||
const orm = await MikroORM.init(config);
|
||||
|
||||
// Update the database scheme if necessary and enabled.
|
||||
if (getEnvVar(EnvVars.DbUpdate)) {
|
||||
await orm.schema.updateSchema();
|
||||
} else {
|
||||
const diff = await orm.schema.getUpdateSchemaSQL();
|
||||
if (diff) {
|
||||
throw Error("The database structure needs to be updated in order to fit the new database structure " +
|
||||
"of the app. In order to do so automatically, set the environment variable DWENGO_DB_UPDATE to true. " +
|
||||
"The following queries will then be executed:\n" + diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
42
backend/src/util/envvars.ts
Normal file
42
backend/src/util/envvars.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
const PREFIX = "DWENGO_";
|
||||
const DB_PREFIX = PREFIX + "DB_";
|
||||
|
||||
type EnvVar = {[key: string]: {name: string, required?: boolean, defaultValue?: boolean}};
|
||||
|
||||
export const EnvVars: EnvVar = {
|
||||
DbHost: {key: DB_PREFIX + "HOST", required: true},
|
||||
DbPort: {key: DB_PREFIX + "PORT", defaultValue: 5432},
|
||||
DbName: {key: DB_PREFIX + "NAME", defaultValue: "dwengo"},
|
||||
DbUsername: {key: DB_PREFIX + "USERNAME", required: true},
|
||||
DbPassword: {key: DB_PREFIX + "PASSWORD", required: true},
|
||||
DbUpdate: {key: DB_PREFIX + "UPDATE", defaultValue: false},
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* Returns the value of the given environment variable if it is set.
|
||||
* Otherwise,
|
||||
* - throw an error if the environment variable was required,
|
||||
* - return the default value if there is one and it was not required,
|
||||
* - return an empty string if the environment variable is not required and there is also no default.
|
||||
* @param envVar The properties of the environment variable (from the EnvVar object).
|
||||
*/
|
||||
export function getEnvVar(envVar: EnvVar): string {
|
||||
const value: string | undefined = process.env[envVar.key];
|
||||
if (value) {
|
||||
return value;
|
||||
} else if (envVar.required) {
|
||||
throw new Error(`Missing environment variable: ${envVar.key}`);
|
||||
} else {
|
||||
return envVar.defaultValue || "";
|
||||
}
|
||||
}
|
||||
|
||||
export function getNumericEnvVar(envVar: EnvVar): number {
|
||||
const valueString = getEnvVar(envVar);
|
||||
const value = parseInt(valueString);
|
||||
if (isNaN(value)) {
|
||||
throw new Error(`Invalid value for environment variable ${envVar.key}: ${valueString}. Expected a number.`)
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
|
@ -6,8 +6,7 @@ services:
|
|||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: postgres
|
||||
ports:
|
||||
- "5432:5432"
|
||||
network_mode: "host"
|
||||
- "5431:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
- ./backend/config/db/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue