quilon
Version:
Generate ERDs from your entity files automagically
80 lines (79 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MermaidBuilder = void 0;
class MermaidBuilder {
constructor() {
this.fileExtension = "mmd";
// TODO: Type keys with Relations
this.mappedRelationTypes = {
OneToMany: "||--o{",
ManyToOne: "o{--||",
OneToOne: "||--||",
ManyToMany: "o{--o{",
};
this.diagram = "";
this.processedRelations = new Set();
this.diagram = "erDiagram\n";
}
/**
* Returns the current state of the generated diagram.
*
* @returns {string} The Mermaid formatted diagram.
*/
getDiagram() {
return this.diagram;
}
/**
* Appends an entity, including its table name, columns, and relations, to the diagram.
*
* @param {IEntityData} entity - The entity data to add to the diagram.
*/
appendEntity(entity) {
this.appendTableName(entity);
this.appendColumns(entity);
this.appendRelations(entity);
this.diagram += "\n";
}
/**
* Appends the table name for the given entity to the diagram.
*
* @private
* @param {IEntityData} entity - The entity whose table name is to be added.
*/
appendTableName(entity) {
this.diagram += ` ${entity.name}`;
}
/**
* Appends columns for the given entity to the diagram.
*
* @private
* @param {IEntityData} entity - The entity whose columns are to be added.
*/
appendColumns(entity) {
this.diagram += " {\n";
entity.columns.forEach((column) => {
this.diagram += ` ${column.type} ${column.name}\n`;
});
this.diagram += " }\n";
}
/**
* Appends relations for the given entity to the diagram, avoiding duplicate relations.
*
* @private
* @param {IEntityData} entity - The entity whose relations are to be added.
*/
appendRelations(entity) {
entity.relations.forEach((relation) => {
const relatedEntityName = relation.type;
const relationType = this.mappedRelationTypes[relation.relation];
const relationKey = `${entity.name}-${relatedEntityName}`;
const reverseRelationKey = `${relatedEntityName}-${entity.name}`;
// Check if the relation or its reverse has already been processed (For mermaid, one is enough)
if (!this.processedRelations.has(relationKey) && !this.processedRelations.has(reverseRelationKey)) {
this.diagram += ` ${entity.name} ${relationType} ${relatedEntityName}: "${relation.relation}"\n`;
this.processedRelations.add(relationKey);
}
});
}
}
exports.MermaidBuilder = MermaidBuilder;