-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pdt24 25 | Create a wrapper for environment variables #9
Changes from 7 commits
ed04c97
8d28b90
c5c0a83
a0e9c52
351952d
03d8a32
58b3343
f1a5e74
5233053
81a71f0
b3e172f
1976ee8
6872405
02a5139
4d17f87
5a59750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||
import { z } from 'zod'; | ||||||
export const envVariables = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
POSTGRES_DB: process.env.POSTGRES_DB, | ||||||
POSTGRES_USER: process.env.POSTGRES_USER, | ||||||
POSTGRES_PASSWORD: process.env.POSTGRES_PASSWORD, | ||||||
POSTGRES_PORT: +(process.env.POSTGRES_PORT || 5432), | ||||||
DB_HOST: process.env.DB_HOST, | ||||||
APP_PORT: +(process.env.APP_PORT || 3000), | ||||||
noxiousghost marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
NODE_ENV: process.env.NODE_ENV, | ||||||
EMAIL_HOST: process.env.EMAIL_HOST, | ||||||
EMAIL_PASS: process.env.EMAIL_PASS, | ||||||
EMAIL_USER: process.env.EMAIL_USER, | ||||||
EMAIL_PORT: +(process.env.EMAIL_PORT || 587), | ||||||
} as const; | ||||||
|
||||||
enum APP_ENVIRONVENT { | ||||||
DEVELOPMENT = 'development', | ||||||
PRODUCTION = 'production', | ||||||
} | ||||||
|
||||||
enum SMTP_PORTS { | ||||||
STANDARD = 25, | ||||||
DEFAULT = 587, | ||||||
TLS = 465, | ||||||
} | ||||||
const validationSchema = z | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
.object({ | ||||||
POSTGRES_DB: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
POSTGRES_USER: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
POSTGRES_PASSWORD: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
POSTGRES_PORT: z.number().gt(0, { message: 'Port cannot be empty' }), | ||||||
DB_HOST: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
APP_PORT: z.number().gt(0, { message: 'Port cannot be empty' }), | ||||||
NODE_ENV: z.nativeEnum(APP_ENVIRONVENT), | ||||||
EMAIL_HOST: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
EMAIL_PORT: z.nativeEnum(SMTP_PORTS), | ||||||
EMAIL_USER: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
EMAIL_PASS: z.string().min(2, { message: 'Must be atleast 2 characters long' }), | ||||||
}) | ||||||
.required(); | ||||||
|
||||||
export const validate = (): object => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use return type |
||||||
const value = validationSchema.safeParse(envVariables); | ||||||
if (!value.success) { | ||||||
throw new Error(`---${value.error.issues[0].path} :: ${value.error.issues[0].message.toUpperCase()}---`); | ||||||
noxiousghost marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
return value.data; | ||||||
}; |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,14 @@ | ||||||||||
import { writeFile } from 'fs/promises'; | ||||||||||
import { join } from 'path'; | ||||||||||
import { DataSource, DataSourceOptions } from 'typeorm'; | ||||||||||
import { envVariables } from '@/config/env.config'; | ||||||||||
|
||||||||||
const dataBaseConfigurations = { | ||||||||||
type: 'postgres', | ||||||||||
port: +(process.env.POSTGRES_PORT ?? '5432'), | ||||||||||
username: process.env.POSTGRES_USER, | ||||||||||
password: process.env.POSTGRES_PASSWORD, | ||||||||||
database: process.env.POSTGRES_DB, | ||||||||||
port: +(envVariables.POSTGRES_PORT ?? '5432'), | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After we return |
||||||||||
username: envVariables.POSTGRES_USER, | ||||||||||
password: envVariables.POSTGRES_PASSWORD, | ||||||||||
database: envVariables.POSTGRES_DB, | ||||||||||
synchronize: false, // Should be false in production to use migrations | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||||||||||
logging: true, | ||||||||||
entities: [join(__dirname, '/../entities', '*.entity.{ts,js}')], | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please prefer import aliases
"@/*"
rather than relative imports.It will help a lot for code organization.