UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

87 lines 2.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Config = exports.loadOptionsEnv = void 0; const Package_1 = require("../core/Package"); const dotenv_1 = __importDefault(require("dotenv")); const resolveEnvPath = (customEnv) => { const NODE_ENV = customEnv ?? process.env?.NODE_ENV; const env = Package_1.Package.path.join(Package_1.Package.path.resolve(), ".env"); if (NODE_ENV == null) { return env; } const envWithNodeEnv = Package_1.Package.path.join(Package_1.Package.path.resolve(), `.env.${NODE_ENV}`); if (Package_1.Package.fs.existsSync(envWithNodeEnv)) { return envWithNodeEnv; } return env; }; dotenv_1.default.config({ path: resolveEnvPath() }); const ENV = process.env; const rawEnv = { // general HOST: ENV.DB_HOST ?? "localhost", PORT: ENV.DB_PORT ?? 3306, USERNAME: ENV.DB_USERNAME ?? ENV.DB_USER, PASSWORD: ENV.DB_PASSWORD ?? "", DATABASE: ENV.DB_DATABASE, CONNECTION_LIMIT: ENV.DB_CONNECTION_LIMIT ?? 10, // for mysql2 only DATE_STRINGS: ENV.DB_DATE_STRINGS ?? false, // options CLUSTER: ENV.DB_CLUSTER ?? false, DRIVER: ENV.DB_DRIVER ?? "mysql2", CACHE: ENV.DB_CACHE, CONNECTION_ERROR: ENV.DB_CONNECTION_ERROR ?? false, CONNECTION_SUCCESS: ENV.DB_CONNECTION_SUCCESS ?? false, }; const parseEnv = (env) => { const parsed = {}; for (const [key, value] of Object.entries(env)) { if (value == null) continue; if (value === "") { parsed[key] = value; continue; } if (/^-?\d+$/.test(value)) { if (Number.isSafeInteger(Number(value))) { parsed[key] = Number(value); continue; } parsed[key] = value; continue; } if (typeof value === "string" && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) { parsed[key] = value.toLowerCase() === "true"; continue; } parsed[key] = value; } return parsed; }; const env = parseEnv(rawEnv); const loadOptionsEnv = (customEnv) => { const pathEnv = resolveEnvPath(customEnv); dotenv_1.default.config({ path: pathEnv, override: true }); const ENV = process.env; const rawEnv = { cluster: ENV.DB_CLUSTER ?? false, driver: ENV.DB_DRIVER ?? "mysql2", host: ENV.DB_HOST, port: ENV.DB_PORT || 3306, username: ENV.DB_USERNAME, password: ENV.DB_PASSWORD || "", database: ENV.DB_DATABASE, }; const env = parseEnv(rawEnv); return Object.freeze(env); }; exports.loadOptionsEnv = loadOptionsEnv; const Config = Object.freeze(env); exports.Config = Config; exports.default = Config; //# sourceMappingURL=index.js.map