first commit
This commit is contained in:
4
.env
Normal file
4
.env
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
MONGO_URI=mongodb://192.168.0.110/
|
||||||
|
MONGO_USER=mydata
|
||||||
|
MONGO_PASSWORD=mongodb@hexr2002
|
||||||
|
MONGO_AUTH_DB=admin
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
node_modules
|
||||||
132
BaseFE/home.html
Normal file
132
BaseFE/home.html
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Database Configuration Form</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type="text"] {
|
||||||
|
width: 100%;
|
||||||
|
padding: 8px;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
border-radius: 4px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
background-color: #4caf50;
|
||||||
|
color: white;
|
||||||
|
padding: 10px 15px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background-color: #45a049;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Database Configuration</h1>
|
||||||
|
<form id="dbConfigForm">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="Bname">Base Name:</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="Bname"
|
||||||
|
name="Bname"
|
||||||
|
required
|
||||||
|
aria-required="true"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="pname">Path Name:</label>
|
||||||
|
<input type="text" id="pname" name="pname" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="org">Organization:</label>
|
||||||
|
<input type="text" id="org" name="org" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="Lang">Usable Language:</label>
|
||||||
|
<input type="text" id="Lang" name="Lang" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="type">Type of Database:</label>
|
||||||
|
<input type="text" id="type" name="type" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="DB">Database Name:</label>
|
||||||
|
<input type="text" id="DB" name="DB" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="name">Username:</label>
|
||||||
|
<input type="text" id="name" name="name" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="frame">Architecture:</label>
|
||||||
|
<input type="text" id="frame" name="frame" required />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit">Submit Configuration</button>
|
||||||
|
</form>
|
||||||
|
<script>
|
||||||
|
document
|
||||||
|
.getElementById("dbConfigForm")
|
||||||
|
.addEventListener("submit", function (e) {
|
||||||
|
e.preventDefault(); // Prevent default form submit
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
BaseName: document.getElementById("Bname").value,
|
||||||
|
pathName: document.getElementById("pname").value,
|
||||||
|
organization: document.getElementById("org").value,
|
||||||
|
useableLanguage: document.getElementById("Lang").value,
|
||||||
|
typeOfDB: document.getElementById("type").value,
|
||||||
|
DBName: document.getElementById("DB").value,
|
||||||
|
userName: document.getElementById("name").value,
|
||||||
|
architecture: document.getElementById("frame").value,
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch("http://localhost:9696/createfolder", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
})
|
||||||
|
.then((response) => {
|
||||||
|
console.log("response: ", response);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Network response was not ok");
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
console.log("data: ", data);
|
||||||
|
alert("Folder created: " + data.path);
|
||||||
|
console.log(data);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log("error: ", error);
|
||||||
|
console.error("Fetch error:", error);
|
||||||
|
alert("Failed to create folder.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
1134
package-lock.json
generated
Normal file
1134
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
package.json
Normal file
27
package.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"name": "new-folder",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "nodemon --exec tsx src/main.ts"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^2.2.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^17.2.0",
|
||||||
|
"express": "^5.1.0",
|
||||||
|
"fs": "^0.0.1-security",
|
||||||
|
"mongoose": "^8.16.3",
|
||||||
|
"path": "^0.12.7"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.19",
|
||||||
|
"@types/express": "^5.0.3",
|
||||||
|
"@types/node": "^24.0.14"
|
||||||
|
}
|
||||||
|
}
|
||||||
50
src/BaseModel/connection/connection.ts
Normal file
50
src/BaseModel/connection/connection.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import mongoose, { Schema, Connection, Model } from "mongoose";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
interface ConnectionCache {
|
||||||
|
[key: string]: Connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connections: ConnectionCache = {};
|
||||||
|
dotenv.config({ quiet: true });
|
||||||
|
const MainModel = <T>(
|
||||||
|
db: string,
|
||||||
|
modelName: string,
|
||||||
|
schema: Schema<T>,
|
||||||
|
collectionName: string
|
||||||
|
): Model<T> => {
|
||||||
|
const db1_url = `${process.env.MONGO_URI}${db}`;
|
||||||
|
const authOptions = {
|
||||||
|
user: process.env.MONGO_USER,
|
||||||
|
pass: process.env.MONGO_PASSWORD,
|
||||||
|
authSource: process.env.MONGO_AUTH_DB || "admin",
|
||||||
|
maxPoolSize: 50,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (connections[db]) {
|
||||||
|
return connections[db].model<T>(modelName, schema, collectionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const db1 = mongoose.createConnection(db1_url, authOptions);
|
||||||
|
|
||||||
|
connections[db] = db1;
|
||||||
|
|
||||||
|
db1.on("connected", () => {
|
||||||
|
console.log(`Connected to MongoDB database: ${db}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
db1.on("error", (err) => {
|
||||||
|
console.error(
|
||||||
|
`MongoDB connection error for database ${db}:`,
|
||||||
|
err.message
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return db1.model<T>(modelName, schema, collectionName);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Database connection error:", (error as Error).message);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default MainModel;
|
||||||
49
src/BaseModel/controller/controller.ts
Normal file
49
src/BaseModel/controller/controller.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
|
import { Request, Response } from "express";
|
||||||
|
import path from "path";
|
||||||
|
import baseType from "../project/basemodel";
|
||||||
|
export const createcontroller = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response
|
||||||
|
): Promise<any> => {
|
||||||
|
console.log("req.bod: ", req.body);
|
||||||
|
const {
|
||||||
|
pathName,
|
||||||
|
organization,
|
||||||
|
BaseName,
|
||||||
|
useableLanguage,
|
||||||
|
typeOfDB,
|
||||||
|
DBName,
|
||||||
|
userName,
|
||||||
|
architecture,
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const basePath = path.join(pathName, BaseName, "src");
|
||||||
|
const folders = ["Controller", "Routes", "Models", "Services"];
|
||||||
|
if (!fs.existsSync(basePath)) {
|
||||||
|
fs.mkdirSync(basePath, { recursive: true });
|
||||||
|
console.log(`Created root path: ${basePath}`);
|
||||||
|
}
|
||||||
|
folders.forEach(async (folder) => {
|
||||||
|
const fullPath = path.join(basePath, folder);
|
||||||
|
if (!fs.existsSync(fullPath)) {
|
||||||
|
fs.mkdirSync(fullPath, { recursive: true });
|
||||||
|
const BaseDataSave = await baseType(organization).create({
|
||||||
|
organization,
|
||||||
|
BaseName,
|
||||||
|
useableLanguage,
|
||||||
|
typeOfDB,
|
||||||
|
DBName,
|
||||||
|
createdBy: userName,
|
||||||
|
architecture,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.send(`Folder already exists: ${fullPath}`);
|
||||||
|
console.log(`Folder already exists: ${folder}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error: unknown) {
|
||||||
|
res.send(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
27
src/BaseModel/project/basemodel.ts
Normal file
27
src/BaseModel/project/basemodel.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { Schema, Document } from "mongoose";
|
||||||
|
import MainModel from "../connection/connection";
|
||||||
|
interface IBase extends Document {
|
||||||
|
BaseName: string;
|
||||||
|
createdBy: string;
|
||||||
|
description: string;
|
||||||
|
members: [string];
|
||||||
|
useableLanguage: string;
|
||||||
|
typeOfDB: string;
|
||||||
|
DBName: string;
|
||||||
|
architecture: string;
|
||||||
|
}
|
||||||
|
const baseSchema: Schema = new Schema({
|
||||||
|
BaseName: { type: String },
|
||||||
|
createdBy: { type: String },
|
||||||
|
description: { type: String },
|
||||||
|
DBName: { type: String },
|
||||||
|
typeOfDB: { type: String },
|
||||||
|
useableLanguage: { type: String },
|
||||||
|
architecture: { type: String },
|
||||||
|
members: { type: [String] },
|
||||||
|
});
|
||||||
|
|
||||||
|
const baseType = (db: any) => {
|
||||||
|
return MainModel(db, "Base", baseSchema, "Base");
|
||||||
|
};
|
||||||
|
export default baseType;
|
||||||
6
src/BaseModel/routes/routes.ts
Normal file
6
src/BaseModel/routes/routes.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import express from "express";
|
||||||
|
import { createcontroller } from "../controller/controller";
|
||||||
|
const appRoutes = express.Router();
|
||||||
|
|
||||||
|
appRoutes.post("/createfolder", createcontroller);
|
||||||
|
export default appRoutes
|
||||||
12
src/main.ts
Normal file
12
src/main.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import express from "express";
|
||||||
|
const app = express();
|
||||||
|
import appRoutes from "./BaseModel/routes/routes";
|
||||||
|
import bodyParser from "body-parser";
|
||||||
|
import cors from "cors";
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
app.use(cors());
|
||||||
|
app.use(appRoutes);
|
||||||
|
const port = 9696;
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log(`Port is running on the ${port}`);
|
||||||
|
});
|
||||||
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ESNext",
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"noEmit": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"typeRoots": ["node_modules/@types"],
|
||||||
|
"types": ["node"],
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user