Skip to content

Commit 24947ec

Browse files
feat: add MongoDB setup and installation options in orchestrateSetup
1 parent 71c51eb commit 24947ec

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

src/orchestrator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { addMulter } from "./tasks/addMulter.js";
88
import { configureScripts } from "./tasks/configureScripts.js";
99
import { configurePackageJson } from "./tasks/configurePackageJson.js";
1010
import { setupESLintConfig } from "./tasks/setupESLint.js";
11+
import { setupDb } from "./tasks/setupDb.js";
1112

1213
export async function orchestrateSetup() {
1314
console.log("🚀 Welcome to create-node-backend!");
@@ -16,15 +17,17 @@ export async function orchestrateSetup() {
1617
const useAuth = await askQuestion("Include auth? (y/n): ");
1718
const useMulter = await askQuestion("Include Multer (file uploads)? (y/n): ");
1819
const useLint = await askQuestion("Use ESLint? (y/n): ");
20+
const useDb = await askQuestion("Use MongoDB? (y/n): ");
1921

2022
await setupNpm(projectName);
21-
await installDependencies(useAuth, useMulter, useLint);
23+
await installDependencies(useAuth, useMulter, useLint, useDb);
2224
await setupFolderStructure(projectName);
2325
await setupEnv(projectName);
2426

2527
if (useAuth.toLowerCase() === "y") await addAuth(projectName);
2628
if (useMulter.toLowerCase() === "y") await addMulter(projectName);
2729

30+
if (useDb.toLowerCase() === "y") await setupDb(projectName);
2831
await configureScripts(projectName);
2932
await configurePackageJson(projectName);
3033
if (useLint.toLowerCase() === "y") await setupESLintConfig(projectName);

src/tasks/configureScripts.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ export async function configureScripts(projectName) {
55
const indexContent = `
66
import express from 'express';
77
import dotenv from 'dotenv';
8+
import connectDB from './config/db.config.js';
89
import routes from './routes/userRoutes.js';
910
11+
1012
dotenv.config();
1113
1214
const app = express();

src/tasks/installDependencies.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { execSync } from "child_process";
22

3-
export async function installDependencies(useAuth, useMulter, useLint) {
3+
export async function installDependencies(
4+
useAuth,
5+
useMulter,
6+
useLint = "n",
7+
useDb
8+
) {
49
console.log("📥 Installing core dependencies...");
510
execSync("npm install express dotenv", { stdio: "inherit" });
611

@@ -14,14 +19,22 @@ export async function installDependencies(useAuth, useMulter, useLint) {
1419
execSync("npm install multer", { stdio: "inherit" });
1520
}
1621

22+
if (useDb.toLowerCase() === "y") {
23+
console.log("🗄️ Installing MongoDB dependencies...");
24+
execSync("npm install mongoose", { stdio: "inherit" });
25+
}
26+
1727
console.log("📦 Installing dev dependencies...");
1828

1929
execSync("npm install --save-dev nodemon", { stdio: "inherit" });
2030
if (useLint.toLowerCase() === "y") {
2131
console.log("🔍 Installing ESLint...");
22-
execSync("npm install --save-dev eslint eslint-plugin-n eslint-plugin-promise", {
23-
stdio: "inherit",
24-
});
32+
execSync(
33+
"npm install --save-dev eslint eslint-plugin-n eslint-plugin-promise",
34+
{
35+
stdio: "inherit",
36+
}
37+
);
2538
}
2639

2740
console.log("⚙️ Installing dev dependencies...");

src/tasks/setupDb.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
export async function setupDb(projectName) {
5+
const authMiddleware = `
6+
import mongoose from "mongoose";
7+
import dotenv from "dotenv";
8+
9+
const connectDB = async () => {
10+
try {
11+
await mongoose.connect(process.env.MONGO_URI);
12+
console.log("MongoDB connected successfully");
13+
} catch (err) {
14+
console.error("Error connecting to MongoDB:", err);
15+
process.exit(1);
16+
}
17+
};
18+
19+
export default connectDB;
20+
`;
21+
22+
fs.writeFileSync(
23+
path.join(process.cwd(), "src", "config", "db.config.js"),
24+
authMiddleware.trim()
25+
);
26+
}

src/tasks/setupEnv.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export async function setupEnv(projectName) {
55
const envContent = `
66
PORT=3000
77
JWT_SECRET=your_jwt_secret
8+
MONGO_URI=mongodb://localhost:27017/${projectName}
9+
810
`;
911
fs.writeFileSync(path.join(process.cwd(), ".env"), envContent.trim());
1012
}

0 commit comments

Comments
 (0)