UNPKG

quilon

Version:

Generate ERDs from your entity files automagically

69 lines (68 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Driver = void 0; const config_1 = require("../global/config"); const types_1 = require("../global/types"); const filesystem_1 = require("../utils/filesystem"); const Typeorm_1 = require("./typeorm/Typeorm"); class Driver { constructor() { const configFile = filesystem_1.FileSystemUtils.readAndParseJSONFile(config_1.GlobalConfig.CONFIG_PATH); const { orm } = configFile; this.orm = orm; } /** * Sets the file path for the entity to be parsed. * * @param {string} filePath - The path of the file to parse. */ setFilePath(filePath) { this.filePath = filePath; } /** * Parses an entity from the specified file path using the appropriate ORM driver. * * @returns {IEntityData} The parsed entity data. * @throws {Error} If the file path is not set or if no suitable driver is found. */ parseEntity() { if (!this.filePath) { throw new Error("filePath is not defined."); } const driver = this.getDriver(); return driver.parseEntity(this.filePath); } /** * Returns the filename pattern to match entity files for the configured ORM. * * @returns {string} The file name pattern. * @throws {Error} If no pattern is implemented for the configured ORM. */ getFileNamePattern() { switch (this.orm) { case types_1.ORMs.TypeORM: return "*.entity.ts"; default: throw new Error(`No Driver for ORM ${this.orm} implemented.`); } } /** * Returns the appropriate driver instance based on the configured ORM. * * @private * @returns {IDriver} The driver instance for the specified ORM. * @throws {Error} If the file path is not set or if no suitable driver is found. */ getDriver() { if (!this.filePath) { throw new Error("filePath is not defined."); } switch (this.orm) { case types_1.ORMs.TypeORM: return new Typeorm_1.TypeORMDriver(this.filePath); default: throw new Error(`No Driver for ORM ${this.orm} implemented.`); } } } exports.Driver = Driver;