UNPKG

dbcube

Version:
116 lines (113 loc) 3.82 kB
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x + '" is not supported'); }); // src/lib/Dbcube.ts import { Config } from "@dbcube/core"; import { Database } from "@dbcube/query-builder"; // src/lib/FileUtils.ts import * as fs from "fs"; import * as path from "path"; var FileUtils = class { /** * Verifica si un archivo existe (asincrónico). * @param filePath - Ruta del archivo. * @returns Promise que resuelve true si el archivo existe, false si no. */ static async fileExists(filePath) { return new Promise((resolve2) => { fs.access(path.resolve(filePath), fs.constants.F_OK, (err) => { resolve2(!err); }); }); } /** * Verifica si un archivo existe (sincrónico). * @param filePath - Ruta del archivo. * @returns True si el archivo existe, false si no. */ static fileExistsSync(filePath) { try { fs.accessSync(path.resolve(filePath), fs.constants.F_OK); return true; } catch { return false; } } /** * Extrae el nombre de la base de datos de un string con formato @database(). * @param input - String de entrada que contiene la referencia a la base de datos. * @returns El nombre de la base de datos o null si no se encuentra. */ static extractDatabaseName(input) { const match = input.match(/@database\(["']?([\w-]+)["']?\)/); return match ? match[1] : null; } /** * Lee recursivamente archivos que terminan en un sufijo dado y los ordena numéricamente. * @param dir - Directorio base (relativo o absoluto). * @param suffix - Sufijo de archivo (como 'table.cube'). * @returns Rutas absolutas de los archivos encontrados y ordenados. */ static getCubeFilesRecursively(dir, suffix) { const baseDir = path.resolve(dir); const cubeFiles = []; function recurse(currentDir) { const entries = fs.readdirSync(currentDir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(currentDir, entry.name); if (entry.isDirectory()) { recurse(fullPath); } else if (entry.isFile() && entry.name.endsWith(suffix)) { cubeFiles.push(fullPath); } } } recurse(baseDir); cubeFiles.sort((a, b) => { const aNum = parseInt(path.basename(a)); const bNum = parseInt(path.basename(b)); return (isNaN(aNum) ? 0 : aNum) - (isNaN(bNum) ? 0 : bNum); }); return cubeFiles; } }; var FileUtils_default = FileUtils; // src/lib/Dbcube.ts import path2 from "path"; var Dbcube = class { configPath; config; databases; constructor() { this.configPath = path2.join(process.cwd(), "dbcube.config.js"); this.config = new Config(); this.databases = {}; } async loadConfig() { const exists = await FileUtils_default.fileExists(this.configPath); exists ?? console.log("\u274C Dont exists config file, please create a dbcube.config.js file"); } async init(configCreate = {}) { await this.loadConfig(); const config = __require(this.configPath); config(this.config); const databases = Object.keys(this.config.getAllDatabases()); for (const database of databases) { console.log(database); this.databases[database] = new Database(database); } if (configCreate.databaseName) { this.databases[configCreate.databaseName] = new Database(configCreate.databaseName); } } database(databaseName) { return this.databases[databaseName]; } }; export { Dbcube }; //# sourceMappingURL=index.js.map