Skip to content

Commit

Permalink
Merge pull request #70 from haard18/typeorm_Init
Browse files Browse the repository at this point in the history
chore: Added TypeOrm Support
  • Loading branch information
Bhanu-code committed Jun 2, 2024
2 parents adbd560 + 462dca8 commit 8190f85
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 31 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
.idea/
.vscode/
node_modules/
build/
tmp/
temp/
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
Expand All @@ -8,7 +14,7 @@ prisma
/models
# testing
/coverage

/src
# next.js
/.next/
/out/
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function startProcess() {
childProcess = spawn('node', [path.join(__dirname, '/servers/prisma.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
} else if (process.argv[2] === 't') {
childProcess = spawn('node', [path.join(__dirname, '/servers/torm.js')], {
stdio: [process.stdin, process.stdout, process.stderr]
});
}

// Setup event handlers for child process
Expand Down
13 changes: 13 additions & 0 deletions ormconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
"synchronize": true,
"logging": false,
"entities": ["src/entity/**/*.ts"],
"migrations": ["src/migration/**/*.ts"],
"subscribers": ["src/subscriber/**/*.ts"]
}
71 changes: 41 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
{
"name": "db-drawer",
"version": "2.0.1",
"description": "schema visualization and validation tool for node.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Bhanu-code/db-drawer.git"
},
"bugs": {
"url": "https://github.com/Bhanu-code/db-drawer/issues"
},
"homepage": "https://github.com/Bhanu-code/db-drawer#readme",
"bin": {
"db-draw": "index.js"
},
"author": "Bhanu Chowhan",
"license": "ISC",
"dependencies": {
"arrow-line": "^0.7.5",
"chokidar": "^3.6.0",
"dotenv": "^16.4.5",
"ejs": "^3.1.9",
"express": "^4.19.2",
"net": "^1.0.2",
"read-directory": "^3.0.2"
}
}
"name": "db-drawer",
"version": "2.0.1",
"description": "schema visualization and validation tool for node.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "ts-node src/index.ts",
"typeorm": "typeorm-ts-node-commonjs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Bhanu-code/db-drawer.git"
},
"bugs": {
"url": "https://github.com/Bhanu-code/db-drawer/issues"
},
"homepage": "https://github.com/Bhanu-code/db-drawer#readme",
"bin": {
"db-draw": "index.js"
},
"author": "Bhanu Chowhan",
"license": "ISC",
"dependencies": {
"arrow-line": "^0.7.5",
"chokidar": "^3.6.0",
"dotenv": "^16.4.5",
"ejs": "^3.1.10",
"express": "^4.19.2",
"mysql": "^2.18.1",
"net": "^1.0.2",
"pg": "^8.4.0",
"read-directory": "^3.0.2",
"reflect-metadata": "^0.1.13",
"typeorm": "0.3.20"
},
"devDependencies": {
"@types/node": "^16.11.10",
"ts-node": "10.9.1",
"typescript": "4.5.2"
}
}
107 changes: 107 additions & 0 deletions servers/torm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// CREATING BASIC EXPRESS APP
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
const net = require('net');
const dotenv = require("dotenv");
const e = require("express");
dotenv.config();
// SUPPORT FOR JSON & PUBLIC FOLDER
app.use(express.static(path.join(__dirname, '../public')));
app.use(express.json());

// SETTING VIEW ENGINE AS EJS
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/../views"));

function parseTypeOrmEntity() {
const entityFolderPath = path.join(process.cwd(), "/src/entity"||__dirname+"/src/entity");

// Check if entity folder exists
if (!fs.existsSync(entityFolderPath)) {
throw new Error("Entity folder not found");
}

// Read all entity files
const entityFiles = fs.readdirSync(entityFolderPath).filter(file => file.endsWith('.ts') || file.endsWith('.js'));

const entities = {};

entityFiles.forEach(file => {
const entityContent = fs.readFileSync(path.join(entityFolderPath, file), "utf-8");

let currentEntity = null;

// Splitting the entity file by lines
const lines = entityContent.split("\n");

lines.forEach((line, index) => {
// Assuming entities start with "@Entity" decorator
if (line.trim().startsWith("@Entity")) {
let entityName = file.replace('.ts', '').replace('.js', ''); // Extracting entity name from file name
const decoratorContent = line.trim().match(/\((.*?)\)/);
if (decoratorContent) {
const customName = decoratorContent[1].replace(/'/g, '');
if (customName) {
entityName = customName;
}
}
entities[entityName] = {};
currentEntity = entityName;
} else if (currentEntity && (line.trim().startsWith("@PrimaryGeneratedColumn") || line.trim().startsWith("@Column"))) {
let nextLineIndex = index + 1;
while (!lines[nextLineIndex].trim().includes(':')) {
nextLineIndex++;
}
const nextLine = lines[nextLineIndex].trim();
const columnParts = nextLine.split(':'); // Splitting by colon
const columnName = columnParts[0].trim(); // Extracting column name
const columnType = columnParts[1].split(';')[0].trim(); // Extracting column type
entities[currentEntity][columnName] = columnType; // Storing column name and type
}
});
});

return entities;
}
const entityData = parseTypeOrmEntity();
// console.log(entityData);
const tables = Object.keys(entityData);
const attrs =Object.values(entityData);
// console.log(tables);
// console.log(attrs);
const PORT = process.env.PORT || 3001;

// SETTING ROUTE TO home.ejs
app.get("/", (req, res) =>{
res.render(__dirname + "/../views/torm.ejs", { modelsArr: tables, fieldsArr: attrs});
});
app.get("/schema", (req, res) =>{
res.json({modelsArr: tables, fieldsArr: attrs});
});
function startServer(port) {
const server = net.createServer();

server.once('error', (err) => {
if (err.code === 'EADDRINUSE') {
// port is currently in use, try the next one
startServer(++port);
} else {
// some other error, throw it
throw err;
}
});

server.once('listening', () => {
// close the server and start the express app on this port
server.close();
app.listen(port, () => {
console.log(`Visualization server up at http://localhost:${port}`);
});
});

server.listen(port);
}

startServer(PORT);
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"lib": [
"es5",
"es6"
],
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
}
}
43 changes: 43 additions & 0 deletions views/torm.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TypeORM Visualizer</title>

<link rel="stylesheet" href="./styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Reddit+Mono:wght@200..900&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/arrow-line/dist/arrow-line.min.js">
</script>
<link rel="stylesheet" type="text/css" href="/styles/prisma.css">
<link rel="stylesheet" type="text/css" href="/styles/style.css">
</head>
<body>
<% for(let i = 0; i < modelsArr.length; i++) { %>
<table class="modelTable">
<thead>
<tr>
<th class="modelName" colspan="2"><%= modelsArr[i] %></th>
</tr>
</thead>
<tbody>
<% for(var key in fieldsArr[i]) { %>
<tr>
<td><%= key %></td>
<td>
<button class="flag" style="background-color:red"><%= fieldsArr[i][key] %></button>
</td>
</tr>
<% } %>
</tbody>
</table>
<% } %>
<a href="https://github.com/Bhanu-code/db-drawer" target="_blank" class="github-link">
<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="50" height="50" viewBox="0 0 50 50">
<path d="M17.791,46.836C18.502,46.53,19,45.823,19,45v-5.4c0-0.197,0.016-0.402,0.041-0.61C19.027,38.994,19.014,38.997,19,39 C0,39,3,39,3.6,39c1.5,0,2.8-0.6,3.4-1.8c0.7-1.3,1-3.5,2.8-4.7C8.9,32.3,9.1,32,9.7,32c0.6,0.1,1.9,0.9,2.7,2 c0.9,1.1,1.8,2,3.4,2c2.487,0,3.82-0.125,4.622-0.555C21.356,34.056,22.649,33,24,33v-0.025c-5.668-0.182-9.289-2.066-10.975-4.975 c-3.665,0.042-6.856,0.405-8.677,0.707c-0.058-0.327-0.108-0.656-0.151-0.987c1.797-0.296,4.843-0.647,8.345-0.714 c-0.112-0.276-0.209-0.559-0.291-0.849c-3.511-0.178-6.541-0.039-8.187,0.097c-0.02-0.332-0.047-0.663-0.051-0.999 c1.649-0.135,4.597-0.27,8.018-0.111c-0.079-0.5-0.13-1.011-0.13-1.543c0-1.7,0.6-3.5,1.7-5c-0.5-1.7-1.2-5.3,0.2-6.6 c2.7,0,4.6,1.3,5.5,2.1C21,13.4,22.9,13,25,13s4,0.4,5.6,1.1c0.9-0.8,2.8-2.1,5.5-2.1c1.5,1.4,0.7,5,0.2,6.6 c1.1,1.5,1.7,3.2,1.6,5c0,0.484-0.045,0.951-0.11,1.409c3.499-0.172,6.527-0.034,8.204,0.102c-0.002,0.337-0.033,0.666-0.051,0.999 c-1.671-0.138-4.775-0.28-8.359-0.089c-0.089,0.336-0.197,0.663-0.325,0.98c3.546,0.046,6.665,0.389,8.548,0.689 c-0.043,0.332-0.093,0.661-0.151,0.987c-1.912-0.306-5.171-0.664-8.879-0.682C35.112,30.873,31.557,32.75,26,32.969V33 c2.6,0,5,3.9,5,6.6V45c0,0.823,0.498,1.53,1.209,1.836C41.37,43.804,48,35.164,48,25,C48,12.318,37.683,2,25,2S2,12.318,2,25 C2,35.164,8.63,43.804,17.791,46.836z"></path>
</svg>
</a>
</body>
</html>

0 comments on commit 8190f85

Please sign in to comment.