Skip to content

Commit

Permalink
API: Use config for ORM config (#365)
Browse files Browse the repository at this point in the history
Use config instead of directly accessing env vars for additional type
safety and validation.

Feedback from
#324 (comment)
  • Loading branch information
dkarnutsch authored Sep 23, 2024
1 parent 5236c3e commit 8a5c19c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 3 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"lint:eslint": "eslint --ext .ts,.tsx,.js,.jsx,.json,.md --max-warnings 0 src/ package.json",
"lint:generated-files-not-modified": "npm run api-generator && git diff --exit-code HEAD --",
"lint:tsc": "tsc --project ./tsconfig.lint.json",
"mikro-orm": "mikro-orm",
"mikro-orm:drop": "mikro-orm schema:drop -r",
"mikro-orm:migration:generate": "mikro-orm migration:create",
"mikro-orm": "dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- mikro-orm",
"mikro-orm:drop": "dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- mikro-orm schema:drop -r",
"mikro-orm:migration:generate": "dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- mikro-orm migration:create",
"start": "npm run prebuild && dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- nest start",
"start:debug": "npm run prebuild && dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput",
"start:dev": "npm run prebuild && npm run db:migrate && npm run console createBlockIndexViews && dotenv -e .env -e .env.local -e .env.secrets -e .env.site-configs -- nest start --watch --preserveWatchOutput",
Expand Down
9 changes: 9 additions & 0 deletions api/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export function createConfig(processEnv: NodeJS.ProcessEnv) {
apiUrl: envVars.API_URL,
apiPort: envVars.API_PORT,
corsAllowedOrigin: new RegExp(envVars.CORS_ALLOWED_ORIGIN),
db: {
host: envVars.POSTGRESQL_HOST,
port: envVars.POSTGRESQL_PORT,
user: envVars.POSTGRESQL_USER,
password: envVars.POSTGRESQL_PASSWORD,
database: envVars.POSTGRESQL_DB,
ssl: envVars.POSTGRESQL_USE_SSL,
caCert: envVars.POSTGRESQL_CA_CERT,
},
auth: {
useAuthProxy: envVars.USE_AUTHPROXY,
systemUserPassword: envVars.BASIC_AUTH_SYSTEM_USER_PASSWORD,
Expand Down
15 changes: 9 additions & 6 deletions api/src/db/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { createMigrationsList, createOrmConfig } from "@comet/cms-api";
import { EntityCaseNamingStrategy } from "@mikro-orm/core";
import { createConfig } from "@src/config/config";
import path from "path";

const config = createConfig(process.env);

export const ormConfig = createOrmConfig({
type: "postgresql",
host: process.env.POSTGRESQL_HOST,
port: Number(process.env.POSTGRESQL_PORT),
user: process.env.POSTGRESQL_USER,
password: process.env.POSTGRESQL_PASSWORD,
dbName: process.env.POSTGRESQL_DB,
host: config.db.host,
port: config.db.port,
user: config.db.user,
password: config.db.password,
dbName: config.db.database,
driverOptions: {
connection: { ssl: process.env.POSTGRESQL_USE_SSL === "true" ? { rejectUnauthorized: true, ca: process.env.POSTGRESQL_CA_CERT } : false },
connection: { ssl: config.db.ssl ? { rejectUnauthorized: true, ca: config.db.caCert } : false },
},
namingStrategy: EntityCaseNamingStrategy,
debug: false,
Expand Down

0 comments on commit 8a5c19c

Please sign in to comment.