{
  "slug": "mongodb-mongoose",
  "runtimes": {
    "node": {
      "frameworks": {
        "express": {
          "dependencies": {
            "runtime": [
              "mongoose"
            ],
            "dev": []
          },
          "env": [
            "DATABASE_URL"
          ],
          "architectures": {
            "mvc": {
              "files": [
                {
                  "type": "file",
                  "path": "src/configs/env.ts",
                  "content": "import \"dotenv-flow/config\";\r\nimport { z } from \"zod\";\r\n\r\nexport const envSchema = z.object({\r\n  DATABASE_URL: z.url()\r\n});\r\n\r\nexport type Env = z.infer<typeof envSchema>;\r\n\r\nconst result = envSchema.safeParse(process.env);\r\n\r\nif (!result.success) {\r\n  console.error(\"❌ Invalid environment configuration\");\r\n  console.error(z.prettifyError(result.error));\r\n  process.exit(1);\r\n}\r\n\r\nexport const env: Readonly<Env> = Object.freeze(result.data);\r\n\r\nexport default env;\r\n"
                },
                {
                  "type": "file",
                  "path": "src/configs/db.ts",
                  "content": "import mongoose from \"mongoose\";\r\nimport env from \"./env\";\r\n\r\nif (!env.DATABASE_URL) {\r\n  throw new Error(\"Please provide DATABASE_URL in the environment variables\");\r\n}\r\n\r\nexport const connectDB = async (): Promise<void> => {\r\n  try {\r\n    const conn = await mongoose.connect(env.DATABASE_URL as string);\r\n    console.log(`MongoDB Connected: ${conn.connection.host}`);\r\n  } catch (error) {\r\n    console.error(\"MongoDB Connection Failed:\", error);\r\n    process.exit(1);\r\n  }\r\n};\r\n\r\n/**\r\n * ? Usage:\r\n * server.ts\r\n * import { connectDB } from \"../configs/db\";\r\n * connectDB()\r\n  .then(() => {\r\n    const server = app.listen(env.PORT, () => {\r\n      logger.info(`[server]: Server is running at http://localhost:${env.PORT}`);\r\n      logger.info(`[server]: Environment: ${env.NODE_ENV}`);\r\n      logger.info(\r\n        `[server]: Swagger docs are available at http://localhost:${env.PORT}/api/docs`\r\n      );\r\n    });\r\n    configureGracefulShutdown(server);\r\n  })\r\n  .catch(error => {\r\n    logger.error(error, \"MongoDB Connection Failed:\");\r\n    process.exit(1);\r\n  });\r\n */\r\n"
                }
              ]
            },
            "feature": {
              "files": [
                {
                  "type": "file",
                  "path": "src/shared/configs/env.ts",
                  "content": "import \"dotenv-flow/config\";\r\nimport { z } from \"zod\";\r\n\r\nexport const envSchema = z.object({\r\n  DATABASE_URL: z.url()\r\n});\r\n\r\nexport type Env = z.infer<typeof envSchema>;\r\n\r\nconst result = envSchema.safeParse(process.env);\r\n\r\nif (!result.success) {\r\n  console.error(\"❌ Invalid environment configuration\");\r\n  console.error(z.prettifyError(result.error));\r\n  process.exit(1);\r\n}\r\n\r\nexport const env: Readonly<Env> = Object.freeze(result.data);\r\n\r\nexport default env;\r\n"
                },
                {
                  "type": "file",
                  "path": "src/shared/configs/db.ts",
                  "content": "import mongoose from \"mongoose\";\r\nimport { env } from \"./env\";\r\n\r\nif (!env.DATABASE_URL) {\r\n  throw new Error(\"Please provide DATABASE_URL in the environment variables\");\r\n}\r\n\r\nexport const connectDB = async (): Promise<void> => {\r\n  try {\r\n    const conn = await mongoose.connect(env.DATABASE_URL as string);\r\n    console.log(`MongoDB Connected: ${conn.connection.host}`);\r\n  } catch (error) {\r\n    console.error(\"MongoDB Connection Failed:\", error);\r\n    process.exit(1);\r\n  }\r\n};\r\n"
                }
              ]
            }
          }
        }
      }
    }
  }
}
