Skip to content

Instantly share code, notes, and snippets.

@smith
Created April 17, 2025 15:32
Show Gist options
  • Save smith/d83ca300c82debfcf6e201230a5c5751 to your computer and use it in GitHub Desktop.
Save smith/d83ca300c82debfcf6e201230a5c5751 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
function extractEntitiesAndRelationships(baseDir) {
const entities = [];
function processFile(filePath) {
const fileContent = fs.readFileSync(filePath, "utf8");
const data = yaml.load(fileContent);
if (data.groups) {
data.groups.forEach((group) => {
if (group.type === "entity") {
entities.push(group);
}
});
}
}
function walkDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
walkDirectory(fullPath);
} else if (file === "resources.yaml") {
processFile(fullPath);
}
});
}
walkDirectory(baseDir);
return entities;
}
function generateMermaidDiagram(entities) {
console.log("classDiagram");
for (const entity of entities) {
for (const attr of entity.attributes) {
console.log(` ${entity.name} : attribute ${attr.ref}`);
}
for (const attr of entity.descriptive_attributes ?? []) {
console.log(` ${entity.name} : descriptive_attribute ${attr.ref}`);
}
for (const relationship of entity.relationships ?? []) {
for ([relationshipType, relatedEntityType] of Object.entries(
relationship,
)) {
console.log(
` ${entity.name} --> ${relatedEntityType}: ${relationshipType}`,
);
}
}
}
}
generateMermaidDiagram(
extractEntitiesAndRelationships(path.join(__dirname, "model")),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment