File tree Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Expand file tree Collapse file tree 2 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,25 @@ import userRoute from "@/routes/users.routes";
77import { middlewares } from "@/middlewares" ;
88
99const app : Express = express ( ) ;
10+ // connect to database
11+ /*
12+ * Example conncting to MongoDB using mongoose
13+ *
14+ async function connectToMongoDB() {
15+ try {
16+ await mongoose.connect(env.MONGODB_URI, {
17+ connectTimeoutMS: 1000000000,
18+ });
19+ console.log("✅ Connected to MongoDB");
20+ } catch (error) {
21+ console.error("❌ MongoDB connection error:", error);
22+ process.exit(1); // Exit if DB connection fails
23+ }
24+ }
1025
26+ connectToMongoDB().catch((err) => console.error(err));
27+
28+ */
1129// Middlewares
1230app . use ( cors ( ) ) ;
1331app . use ( express . json ( ) ) ;
Original file line number Diff line number Diff line change @@ -14,21 +14,22 @@ try {
1414 process . exit ( 1 ) ;
1515}
1616
17- // ✅ Define Zod schema for validation
17+ // Define Zod schema for validation and transformation
1818const envSchema = z . object ( {
1919 PORT : z
2020 . string ( )
2121 . default ( "3000" )
22- . transform ( Number )
23- . refine ( ( val ) => ! isNaN ( val ) , {
24- message : "PORT must be a valid number" ,
22+ . transform ( ( val ) => {
23+ const parsed = Number ( val ) ;
24+ if ( Number . isNaN ( parsed ) ) throw new Error ( "PORT must be a valid number" ) ;
25+ return parsed ;
2526 } ) ,
2627 HOST : z . string ( ) . default ( "localhost" ) ,
2728 NODE_ENV : z . enum ( [ "development" , "production" , "test" ] ) . default ( "development" ) ,
2829 DB_URI : z . string ( ) . url ( "DB_URI must be a valid URL" ) ,
2930} ) ;
3031
31- // ✅ Validate process.env
32+ // Validate process.env and infer typed env object
3233const parsedEnv = envSchema . safeParse ( process . env ) ;
3334
3435if ( ! parsedEnv . success ) {
@@ -37,6 +38,6 @@ if (!parsedEnv.success) {
3738 process . exit ( 1 ) ;
3839}
3940
40- // ✅ Export validated, typed, and transformed env values
41- export const env = parsedEnv . data ;
41+ export type Env = z . infer < typeof envSchema > ;
4242
43+ export const env : Env = parsedEnv . data ;
You can’t perform that action at this time.
0 commit comments