tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
79 lines • 2.75 kB
JavaScript
;
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 tools_1 = require("../tools");
const dotenv_1 = __importDefault(require("dotenv"));
const resolveEnvPath = (customEnv) => {
const NODE_ENV = customEnv ?? process.env?.NODE_ENV;
const env = tools_1.Tool.path.join(tools_1.Tool.path.resolve(), '.env');
if (NODE_ENV == null) {
return env;
}
const envWithNodeEnv = tools_1.Tool.path.join(tools_1.Tool.path.resolve(), `.env.${NODE_ENV}`);
if (tools_1.Tool.fs.existsSync(envWithNodeEnv)) {
return envWithNodeEnv;
}
return env;
};
dotenv_1.default.config({ path: resolveEnvPath() });
const ENV = process.env;
const rawEnv = {
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 ?? 20,
DATE_STRINGS: ENV.DB_DATE_STRINGS ?? false,
CLUSTER: ENV.DB_CLUSTER ?? false,
DRIVER: ENV.DB_DRIVER ?? 'mysql2',
CACHE: ENV.DB_CACHE ?? 'memory',
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 (!isNaN(Number(value))) {
(parsed)[key] = Number(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