api and socket folder created models updated
This commit is contained in:
132
BaseFE/home.html
132
BaseFE/home.html
@@ -1,132 +0,0 @@
|
||||
<!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>
|
||||
@@ -6,7 +6,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "nodemon --exec tsx src/main.ts"
|
||||
"start:api": "nodemon --exec tsx src/api-server/main.ts"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import fs from "node:fs";
|
||||
import { Request, Response } from "express";
|
||||
import path from "path";
|
||||
import baseType from "../model/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;
|
||||
console.log('req.body: ', 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);
|
||||
}
|
||||
};
|
||||
@@ -1,101 +0,0 @@
|
||||
import fs from "node:fs";
|
||||
import { Request, Response } from "express";
|
||||
import path from "path";
|
||||
import baseType from "../model/basemodel";
|
||||
import FileMode from "../model/fileModel";
|
||||
export const fileModelCreatecontroller = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<any> => {
|
||||
console.log("req.body: ", req.body);
|
||||
const {
|
||||
pathName,
|
||||
baseId,
|
||||
modelName,
|
||||
organization,
|
||||
attributes,
|
||||
} = req.body;
|
||||
|
||||
try {
|
||||
const findBaseName=await baseType(organization).findById(baseId)
|
||||
console.log('baseId: ', baseId);
|
||||
console.log('findBaseName: ', findBaseName);
|
||||
|
||||
|
||||
const attrToSchema = (attr: any) => {
|
||||
const mapType: any = {
|
||||
string: 'String',
|
||||
number: 'Number',
|
||||
boolean: 'Boolean'
|
||||
};
|
||||
const lines = [];
|
||||
for (const [key, config] of Object.entries(attr)) {
|
||||
const line: string[] = [];
|
||||
line.push(`${key}: {`);
|
||||
line.push(` type: ${mapType[(config as any).type] || 'String'},`);
|
||||
if ((config as any).required) line.push(` required: true,`);
|
||||
if ((config as any).default !== undefined) line.push(` default: ${(config as any).default},`);
|
||||
if ((config as any).minLength) line.push(` minlength: ${(config as any).minLength},`);
|
||||
if ((config as any).maxLength) line.push(` maxlength: ${(config as any).maxLength},`);
|
||||
if ((config as any).min !== undefined) line.push(` min: ${(config as any).min},`);
|
||||
if ((config as any).max !== undefined) line.push(` max: ${(config as any).max},`);
|
||||
line.push('},');
|
||||
lines.push(line.join('\n'));
|
||||
}
|
||||
return lines.join('\n');
|
||||
};
|
||||
const schemaCode = `
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const ${modelName}Schema = new mongoose.Schema({
|
||||
${attrToSchema(attributes)}
|
||||
});
|
||||
|
||||
export const ${modelName} = mongoose.model('${modelName}', ${modelName}Schema);
|
||||
`;
|
||||
|
||||
const basePath = path.join(pathName, findBaseName.BaseName, "src","Models");
|
||||
const modelFilePath = path.join(basePath, `${modelName}SchemaModel.ts`);
|
||||
|
||||
|
||||
// Read all subfolders inside basePath
|
||||
if (!fs.existsSync(basePath)) {
|
||||
fs.mkdirSync(basePath, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(modelFilePath, schemaCode.trim());
|
||||
|
||||
return res.status(200).json({ message: 'Model file created successfully.' });
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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 FileMode(organization).create({
|
||||
// baseId,
|
||||
// modelName,
|
||||
// attributes,
|
||||
|
||||
// });
|
||||
// } else {
|
||||
// res.send(`Folder already exists: ${fullPath}`);
|
||||
// console.log(`Folder already exists: ${folder}`);
|
||||
// }
|
||||
// });
|
||||
} catch (error: unknown) {
|
||||
res.send(error);
|
||||
}
|
||||
};
|
||||
@@ -1,27 +0,0 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../connection/connection";
|
||||
export 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;
|
||||
@@ -1,20 +0,0 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../connection/connection";
|
||||
import { IBase } from "./basemodel";
|
||||
interface IFileModel extends Document {
|
||||
baseId: IBase["_id"];
|
||||
modelName: string;
|
||||
attributes: [];
|
||||
createdAt: number;
|
||||
}
|
||||
const baseFileModelSchema: Schema<IFileModel> = new Schema({
|
||||
baseId: { type: Schema.Types.ObjectId, ref: "Base" },
|
||||
modelName: { type: String, required: true },
|
||||
attributes: { type: [], required: true },
|
||||
createdAt: { type: Number, default: Date.now() }
|
||||
});
|
||||
|
||||
const FileMode = (db: any) => {
|
||||
return MainModel(db, "Model", baseFileModelSchema, "Modle");
|
||||
};
|
||||
export default FileMode;
|
||||
302
src/api-server/controller/controller.ts
Normal file
302
src/api-server/controller/controller.ts
Normal file
@@ -0,0 +1,302 @@
|
||||
import fs from "node:fs";
|
||||
import { Request, Response } from "express";
|
||||
import path from "path";
|
||||
import MVCarcModel from "../../shared/model/mvcModel";
|
||||
import ProjectType from "../../shared/model/projectmodel";
|
||||
export const createcontroller = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<any> => {
|
||||
const {
|
||||
pathName,
|
||||
organization,
|
||||
BaseName,
|
||||
useableLanguage,
|
||||
typeOfDB,
|
||||
DBName,
|
||||
userName,
|
||||
architecture,
|
||||
} = req.body;
|
||||
|
||||
try {
|
||||
const existing = await ProjectType(organization).findOne({
|
||||
createdBy: userName,
|
||||
DBName: DBName,
|
||||
typeOfDB: typeOfDB,
|
||||
useableLanguage: useableLanguage,
|
||||
architecture: architecture.toLowerCase(),
|
||||
});
|
||||
if (existing) {
|
||||
return res.status(403).json({
|
||||
message: "Project Already Exists",
|
||||
});
|
||||
} else {
|
||||
if (architecture.toLowerCase() === "mvc") {
|
||||
const basePath = path.join(pathName, BaseName, "src");
|
||||
let folders: any[];
|
||||
const existingMvcForBase = await MVCarcModel(organization).findOne({
|
||||
BaseName: BaseName,
|
||||
architecture: architecture.toLowerCase(),
|
||||
});
|
||||
if (!existingMvcForBase) {
|
||||
const getUniqueBasePath = (
|
||||
pathName: string,
|
||||
BaseName: string
|
||||
): { finalBaseName: string; fullPath: string } => {
|
||||
let counter = 0;
|
||||
let newBaseName = BaseName;
|
||||
let fullPath = path.join(pathName, newBaseName, "src");
|
||||
if (fs.existsSync(basePath)) {
|
||||
counter++;
|
||||
newBaseName = `${BaseName}-${counter}`;
|
||||
fullPath = path.join(pathName, newBaseName, "src");
|
||||
}
|
||||
|
||||
return { finalBaseName: newBaseName, fullPath };
|
||||
};
|
||||
const folderStructureFunction = async (
|
||||
folders: string[],
|
||||
rootPath: string
|
||||
): Promise<any> => {
|
||||
const folderStatus: string[] = [];
|
||||
for (const folder of folders) {
|
||||
const fullPath = path.join(rootPath, folder);
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
fs.mkdirSync(fullPath, { recursive: true });
|
||||
folderStatus.push(`Created: ${folder}`);
|
||||
console.log("folder: ", folder);
|
||||
BaseDataSave.folderNames.push(folder);
|
||||
await BaseDataSave.save();
|
||||
} else {
|
||||
folderStatus.push(`Already exists: ${folder}`);
|
||||
}
|
||||
}
|
||||
return folderStatus;
|
||||
};
|
||||
const mainFileCreate = async (rootPath: string): Promise<any> => {
|
||||
const mainfile = path.join(rootPath, "app.ts");
|
||||
try {
|
||||
await fs.promises.access(rootPath);
|
||||
} catch {
|
||||
await fs.promises.mkdir(rootPath, { recursive: true });
|
||||
}
|
||||
try {
|
||||
await fs.promises.access(mainfile);
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "Model file already exists in this name" });
|
||||
} catch {
|
||||
await fs.promises.writeFile(mainfile, "");
|
||||
}
|
||||
};
|
||||
let folderStatus: string[] = [];
|
||||
let basicName: string;
|
||||
let rootPath: string;
|
||||
if (fs.existsSync(basePath)) {
|
||||
let { finalBaseName, fullPath } = getUniqueBasePath(
|
||||
pathName,
|
||||
BaseName
|
||||
);
|
||||
basicName = finalBaseName;
|
||||
rootPath = fullPath;
|
||||
fs.mkdirSync(fullPath, { recursive: true });
|
||||
mainFileCreate(rootPath);
|
||||
} else {
|
||||
basicName = BaseName;
|
||||
rootPath = basePath;
|
||||
fs.mkdirSync(basePath, { recursive: true });
|
||||
mainFileCreate(rootPath);
|
||||
}
|
||||
let BaseDataSave = await ProjectType(organization).create({
|
||||
organization,
|
||||
BaseName: basicName,
|
||||
useableLanguage,
|
||||
typeOfDB,
|
||||
DBName,
|
||||
createdBy: userName,
|
||||
architecture: architecture.toLowerCase(),
|
||||
});
|
||||
const MVCCreation = await MVCarcModel(organization).create({
|
||||
BaseName: basicName,
|
||||
baseId: BaseDataSave._id,
|
||||
createdBy: userName,
|
||||
controllers: true,
|
||||
routes: true,
|
||||
models: true,
|
||||
services: true,
|
||||
middleware: true,
|
||||
utils: true,
|
||||
config: true,
|
||||
});
|
||||
const mvcData = MVCCreation.toObject();
|
||||
|
||||
const excludedKeys = ["baseId", "_id", "__v"];
|
||||
const folders = Object.keys(mvcData).filter(
|
||||
(key) => !excludedKeys.includes(key) && mvcData[key] === true
|
||||
);
|
||||
|
||||
folderStatus = await folderStructureFunction(folders, rootPath);
|
||||
return res.status(201).json({
|
||||
message: "Base structure created successfully",
|
||||
folderStatus,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.log("New Architecture flow");
|
||||
}
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
res.send("error");
|
||||
}
|
||||
};
|
||||
|
||||
// export const createcontroller = async (
|
||||
// req: Request,
|
||||
// res: Response
|
||||
// ): Promise<any> => {
|
||||
// const {
|
||||
// pathName,
|
||||
// organization,
|
||||
// BaseName,
|
||||
// useableLanguage,
|
||||
// typeOfDB,
|
||||
// DBName,
|
||||
// userName,
|
||||
// architecture,
|
||||
// } = req.body;
|
||||
|
||||
// try {
|
||||
// const existing = await ProjectType(organization).findOne({
|
||||
// createdBy: userName,
|
||||
// DBName: DBName,
|
||||
// typeOfDB: typeOfDB,
|
||||
// useableLanguage: useableLanguage,
|
||||
// architecture: architecture.toLowerCase(),
|
||||
// });
|
||||
// if (existing) {
|
||||
// return res.status(403).json({
|
||||
// message: "Project Already Exists",
|
||||
// });
|
||||
// } else {
|
||||
// if (architecture.toLowerCase() === "mvc") {
|
||||
// const basePath = path.join(pathName, BaseName, "src");
|
||||
// let folders: any[];
|
||||
// const existingMvcForBase = await MVCarcModel(organization).findOne({
|
||||
// BaseName: BaseName,
|
||||
// architecture: architecture.toLowerCase(),
|
||||
// });
|
||||
// if (!existingMvcForBase) {
|
||||
// const getUniqueBasePath = (
|
||||
// pathName: string,
|
||||
// BaseName: string
|
||||
// ): { finalBaseName: string; fullPath: string } => {
|
||||
// let counter = 0;
|
||||
// let newBaseName = BaseName;
|
||||
// let fullPath = path.join(pathName, newBaseName, "src");
|
||||
// if (fs.existsSync(basePath)) {
|
||||
// counter++;
|
||||
// newBaseName = `${BaseName}-${counter}`;
|
||||
// fullPath = path.join(pathName, newBaseName, "src");
|
||||
// }
|
||||
|
||||
// return { finalBaseName: newBaseName, fullPath };
|
||||
// };
|
||||
// const folderStructureFunction = async (
|
||||
// folders: string[],
|
||||
// rootPath: string
|
||||
// ): Promise<any> => {
|
||||
// const folderStatus: string[] = [];
|
||||
// for (const folder of folders) {
|
||||
// const fullPath = path.join(rootPath, folder);
|
||||
// if (!fs.existsSync(fullPath)) {
|
||||
// fs.mkdirSync(fullPath, { recursive: true });
|
||||
// folderStatus.push(`Created: ${folder}`);
|
||||
// console.log("folder: ", folder);
|
||||
// BaseDataSave.folderNames.push(folder);
|
||||
// await BaseDataSave.save();
|
||||
// } else {
|
||||
// folderStatus.push(`Already exists: ${folder}`);
|
||||
// }
|
||||
// }
|
||||
// return folderStatus;
|
||||
// };
|
||||
// const mainFileCreate = async (rootPath: string): Promise<any> => {
|
||||
// const mainfile = path.join(rootPath, "app.ts");
|
||||
// try {
|
||||
// await fs.promises.access(rootPath);
|
||||
// } catch {
|
||||
// await fs.promises.mkdir(rootPath, { recursive: true });
|
||||
// }
|
||||
// try {
|
||||
// await fs.promises.access(mainfile);
|
||||
// return res
|
||||
// .status(200)
|
||||
// .json({ message: "Model file already exists in this name" });
|
||||
// } catch {
|
||||
// await fs.promises.writeFile(mainfile, "");
|
||||
// }
|
||||
// };
|
||||
// let folderStatus: string[] = [];
|
||||
// let basicName: string;
|
||||
// let rootPath: string;
|
||||
// if (fs.existsSync(basePath)) {
|
||||
// let { finalBaseName, fullPath } = getUniqueBasePath(
|
||||
// pathName,
|
||||
// BaseName
|
||||
// );
|
||||
// basicName = finalBaseName;
|
||||
// rootPath = fullPath;
|
||||
// fs.mkdirSync(fullPath, { recursive: true });
|
||||
// mainFileCreate(rootPath);
|
||||
// } else {
|
||||
// basicName = BaseName;
|
||||
// rootPath = basePath;
|
||||
// fs.mkdirSync(basePath, { recursive: true });
|
||||
// mainFileCreate(rootPath);
|
||||
// }
|
||||
// let BaseDataSave = await ProjectType(organization).create({
|
||||
// organization,
|
||||
// BaseName: basicName,
|
||||
// useableLanguage,
|
||||
// typeOfDB,
|
||||
// DBName,
|
||||
// createdBy: userName,
|
||||
// architecture: architecture.toLowerCase(),
|
||||
// });
|
||||
// const MVCCreation = await MVCarcModel(organization).create({
|
||||
// BaseName: basicName,
|
||||
// baseId: BaseDataSave._id,
|
||||
// createdBy: userName,
|
||||
// controllers: true,
|
||||
// routes: true,
|
||||
// models: true,
|
||||
// services: true,
|
||||
// middleware: true,
|
||||
// utils: true,
|
||||
// config: true,
|
||||
// });
|
||||
// const mvcData = MVCCreation.toObject();
|
||||
|
||||
// const excludedKeys = [
|
||||
// "baseId",
|
||||
// "_id",
|
||||
// "__v",
|
||||
// ];
|
||||
// const folders = Object.keys(mvcData).filter(
|
||||
// (key) => !excludedKeys.includes(key) && mvcData[key] === true
|
||||
// );
|
||||
|
||||
// folderStatus = await folderStructureFunction(folders, rootPath);
|
||||
// return res.status(201).json({
|
||||
// message: "Base structure created successfully",
|
||||
// folderStatus,
|
||||
// });
|
||||
// }
|
||||
// } else {
|
||||
// console.log("New Architecture flow");
|
||||
// }
|
||||
// }
|
||||
// } catch (error: unknown) {
|
||||
// res.send("error");
|
||||
// }
|
||||
// };
|
||||
98
src/api-server/controller/fileModelController.ts
Normal file
98
src/api-server/controller/fileModelController.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import fs from "node:fs";
|
||||
import { Request, Response } from "express";
|
||||
import path from "path";
|
||||
import baseType from "../../shared/model/projectmodel";
|
||||
import FileMode from "../../shared/model/collectionModel";
|
||||
export const fileModelCreatecontroller = async (
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<any> => {
|
||||
console.log("req.body: ", req.body);
|
||||
const { pathName, baseId, modelName, organization, attributes } = req.body;
|
||||
|
||||
try {
|
||||
const findBaseName = await baseType(organization).findById(baseId);
|
||||
console.log("baseId: ", baseId);
|
||||
console.log("findBaseName: ", findBaseName);
|
||||
|
||||
const attrToSchema = (attr: any) => {
|
||||
const mapType: any = {
|
||||
string: "String",
|
||||
number: "Number",
|
||||
boolean: "Boolean",
|
||||
};
|
||||
const lines = [];
|
||||
for (const [key, config] of Object.entries(attr)) {
|
||||
const line: string[] = [];
|
||||
line.push(`${key}: {`);
|
||||
line.push(` type: ${mapType[(config as any).type] || "String"},`);
|
||||
if ((config as any).required) line.push(` required: true,`);
|
||||
if ((config as any).default !== undefined)
|
||||
line.push(` default: ${(config as any).default},`);
|
||||
if ((config as any).minLength)
|
||||
line.push(` minlength: ${(config as any).minLength},`);
|
||||
if ((config as any).maxLength)
|
||||
line.push(` maxlength: ${(config as any).maxLength},`);
|
||||
if ((config as any).min !== undefined)
|
||||
line.push(` min: ${(config as any).min},`);
|
||||
if ((config as any).max !== undefined)
|
||||
line.push(` max: ${(config as any).max},`);
|
||||
line.push("},");
|
||||
lines.push(line.join("\n"));
|
||||
}
|
||||
return lines.join("\n");
|
||||
};
|
||||
const schemaCode = `
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const ${modelName}Schema = new mongoose.Schema({
|
||||
${attrToSchema(attributes)}
|
||||
});
|
||||
|
||||
export const ${modelName} = mongoose.model('${modelName}', ${modelName}Schema);
|
||||
`;
|
||||
|
||||
const basePath = path.join(
|
||||
pathName,
|
||||
findBaseName.BaseName,
|
||||
"src",
|
||||
"Models"
|
||||
);
|
||||
const modelFilePath = path.join(basePath, `${modelName}SchemaModel.ts`);
|
||||
|
||||
// Read all subfolders inside basePath
|
||||
if (!fs.existsSync(basePath)) {
|
||||
fs.mkdirSync(basePath, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(modelFilePath, schemaCode.trim());
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: "Model file created successfully." });
|
||||
|
||||
// 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 FileMode(organization).create({
|
||||
// baseId,
|
||||
// modelName,
|
||||
// attributes,
|
||||
|
||||
// });
|
||||
// } else {
|
||||
// res.send(`Folder already exists: ${fullPath}`);
|
||||
// console.log(`Folder already exists: ${folder}`);
|
||||
// }
|
||||
// });
|
||||
} catch (error: unknown) {
|
||||
res.send(error);
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import express from "express";
|
||||
const app = express();
|
||||
import appRoutes from "./BaseModel/routes/routes";
|
||||
import appRoutes from "../api-server/routes/routes";
|
||||
import bodyParser from "body-parser";
|
||||
import cors from "cors";
|
||||
app.use(bodyParser.json());
|
||||
@@ -1,6 +1,6 @@
|
||||
import express from "express";
|
||||
import { createcontroller } from "../controller/controller";
|
||||
import { fileModelCreatecontroller } from "../controller/fileModelController";
|
||||
import { createcontroller } from "../../api-server/controller/controller";
|
||||
import { fileModelCreatecontroller } from "../../api-server/controller/fileModelController";
|
||||
const appRoutes = express.Router();
|
||||
|
||||
appRoutes.post("/createfolder", createcontroller);
|
||||
0
src/shared/fsafdsa
Normal file
0
src/shared/fsafdsa
Normal file
24
src/shared/model/collectionModel.ts
Normal file
24
src/shared/model/collectionModel.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../connection/connection";
|
||||
import { IProject } from "./projectmodel";
|
||||
interface IFileModel extends Document {
|
||||
projectId: IProject["_id"];
|
||||
collectionNodeName: string;
|
||||
attributes: [];
|
||||
createdAt: number;
|
||||
isArchive: boolean;
|
||||
position: [number, number, number];
|
||||
}
|
||||
const collectionSchema: Schema<IFileModel> = new Schema({
|
||||
projectId: { type: Schema.Types.ObjectId, ref: "Project" },
|
||||
collectionNodeName: { type: String, required: true },
|
||||
position: { type: [Number], required: true },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
attributes: { type: [], required: true },
|
||||
createdAt: { type: Number, default: Date.now() },
|
||||
});
|
||||
|
||||
const collectionsModel = (db: any) => {
|
||||
return MainModel(db, "collectionNode", collectionSchema, "collectionNode");
|
||||
};
|
||||
export default collectionsModel;
|
||||
41
src/shared/model/mvcModel.ts
Normal file
41
src/shared/model/mvcModel.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../connection/connection";
|
||||
import { IProject } from "./projectmodel";
|
||||
interface Ifolders extends Document {
|
||||
folderName: string;
|
||||
createdAt: number;
|
||||
}
|
||||
const folderSchema: Schema = new Schema(
|
||||
{
|
||||
folderName: { type: String, required: true },
|
||||
createdAt: { type: Date, default: Date.now },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
export interface IMVCPrject extends Document {
|
||||
projectId: IProject["_id"];
|
||||
controllers: boolean;
|
||||
routes: boolean;
|
||||
models: boolean;
|
||||
services: boolean;
|
||||
middleware: boolean;
|
||||
utils: boolean;
|
||||
config: boolean;
|
||||
folders: Ifolders[];
|
||||
}
|
||||
const mvcSchema: Schema = new Schema({
|
||||
projectId: { type: Schema.Types.ObjectId, ref: "Project" },
|
||||
controllers: { type: Boolean },
|
||||
routes: { type: Boolean },
|
||||
models: { type: Boolean },
|
||||
services: { type: Boolean },
|
||||
middleware: { type: Boolean },
|
||||
utils: { type: Boolean },
|
||||
config: { type: Boolean },
|
||||
folders: [folderSchema],
|
||||
});
|
||||
|
||||
const MVCarcModel = (db: any) => {
|
||||
return MainModel(db, "MVCArc", mvcSchema, "MVCArc");
|
||||
};
|
||||
export default MVCarcModel;
|
||||
36
src/shared/model/projectmodel.ts
Normal file
36
src/shared/model/projectmodel.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Schema, Document } from "mongoose";
|
||||
import MainModel from "../connection/connection";
|
||||
export interface IProject extends Document {
|
||||
projectName: string;
|
||||
slug: string;
|
||||
isArchive: boolean;
|
||||
createdBy: string;
|
||||
description: string;
|
||||
members: [string];
|
||||
useableLanguage: string;
|
||||
typeOfDB: string;
|
||||
DBName: string;
|
||||
architecture: string;
|
||||
}
|
||||
const projectSchema: Schema = new Schema(
|
||||
{
|
||||
projectName: { type: String },
|
||||
slug: { type: String },
|
||||
isArchive: { type: Boolean, default: false },
|
||||
createdBy: { type: String },
|
||||
description: { type: String },
|
||||
DBName: { type: String },
|
||||
typeOfDB: { type: String },
|
||||
useableLanguage: { type: String },
|
||||
architecture: { type: String },
|
||||
members: { type: [String] },
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
const ProjectType = (db: any) => {
|
||||
return MainModel(db, "Project", projectSchema, "Project");
|
||||
};
|
||||
export default ProjectType;
|
||||
Reference in New Issue
Block a user