UNPKG

typeorm

Version:

Data-Mapper ORM for TypeScript and ES2023+. Supports MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, MongoDB databases.

233 lines • 8.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlatformTools = exports.Writable = exports.Readable = exports.ReadStream = exports.EventEmitter = void 0; const tslib_1 = require("tslib"); const formatter_1 = require("@sqltools/formatter"); const ansis_1 = tslib_1.__importDefault(require("ansis")); const crypto_1 = tslib_1.__importDefault(require("crypto")); const fs_1 = tslib_1.__importDefault(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const sql_highlight_1 = require("sql-highlight"); var events_1 = require("events"); Object.defineProperty(exports, "EventEmitter", { enumerable: true, get: function () { return events_1.EventEmitter; } }); var fs_2 = require("fs"); Object.defineProperty(exports, "ReadStream", { enumerable: true, get: function () { return fs_2.ReadStream; } }); var stream_1 = require("stream"); Object.defineProperty(exports, "Readable", { enumerable: true, get: function () { return stream_1.Readable; } }); Object.defineProperty(exports, "Writable", { enumerable: true, get: function () { return stream_1.Writable; } }); /** * Platform-specific tools. */ class PlatformTools { /** * Type of the currently running platform. */ static { this.type = "node"; } /** * @returns the platform-specific global variable */ static getGlobalVariable() { if (typeof globalThis !== "undefined") { return globalThis; } return global; } /** * Loads ("require"-s) given file or package. * This operation is only supported on the NodeJS platform * * @param name name of the module to be imported * @returns the module */ static load(name) { /* eslint-disable @typescript-eslint/no-require-imports */ try { // Use literal requires on purpose so bundlers can trace the client libraries. // Wrapped in try-catch so bundlers can identify the libraries as optional dependencies. switch (name) { // AWS Aurora Data API (PostgreSQL/MySQL) case "typeorm-aurora-data-api-driver": return require("typeorm-aurora-data-api-driver"); // better-sqlite3 case "better-sqlite3": return require("better-sqlite3"); // Expo case "expo-sqlite": return require("expo-sqlite"); // Google Cloud Spanner case "@google-cloud/spanner": return require("@google-cloud/spanner"); // Microsoft SQL Server case "mssql": return require("mssql"); // MongoDB case "mongodb": return require("mongodb"); // MySQL / MariaDB case "mysql2": return require("mysql2"); // Oracle case "oracledb": return require("oracledb"); // PostgreSQL case "pg": return require("pg"); case "pg-native": return require("pg-native"); case "pg-query-stream": return require("pg-query-stream"); // React Native case "react-native-sqlite-storage": return require("react-native-sqlite-storage"); // SAP HANA case "@sap/hana-client": return require("@sap/hana-client"); case "@sap/hana-client/extension/Stream": return require("@sap/hana-client/extension/Stream"); // sql.js case "sql.js": return require("sql.js"); // redis case "redis": return require("redis"); case "ioredis": return require("ioredis"); } } catch (error) { if (error?.code !== "MODULE_NOT_FOUND") { throw error; } return require(path_1.default.join(process.cwd(), "node_modules", name)); } /* eslint-enable @typescript-eslint/no-require-imports */ throw new TypeError(`Invalid Package for PlatformTools.load: ${name}`); } /** * Returns a SHA-1 hex digest for internal IDs/aliases (not for cryptographic security) * * @param input string to encode * @returns the SHA-1 digest of the input string */ static sha1(input) { const hashFunction = crypto_1.default.createHash("sha1"); hashFunction.update(input, "utf8"); return hashFunction.digest("hex"); } /** * Normalizes given path. Does "path.normalize" and replaces backslashes with forward slashes on Windows. * * @param pathStr */ static pathNormalize(pathStr) { let normalizedPath = path_1.default.normalize(pathStr); if (process.platform === "win32") normalizedPath = normalizedPath.replaceAll("\\", "/"); return normalizedPath; } /** * Returns the extension of the given path. Does "path.extname". * * @param pathStr */ static pathExtname(pathStr) { return path_1.default.extname(pathStr); } /** * Resolves the given paths. Does "path.resolve". * * @param paths */ static pathResolve(...paths) { return path_1.default.resolve(...paths); } /** * Synchronously checks if file exists. Does "fs.existsSync". * * @param pathStr */ static fileExist(pathStr) { return fs_1.default.existsSync(pathStr); } static readFileSync(filename) { return fs_1.default.readFileSync(filename); } static appendFileSync(filename, data) { fs_1.default.appendFileSync(filename, data); } static async writeFile(path, data) { return fs_1.default.promises.writeFile(path, data); } /** * Highlights sql string to be printed in the console. * * @param sql */ static highlightSql(sql) { return (0, sql_highlight_1.highlight)(sql, { colors: { keyword: ansis_1.default.blueBright.open, function: ansis_1.default.magentaBright.open, number: ansis_1.default.green.open, string: ansis_1.default.white.open, identifier: ansis_1.default.white.open, special: ansis_1.default.white.open, bracket: ansis_1.default.white.open, comment: ansis_1.default.gray.open, clear: ansis_1.default.reset.open, }, }); } /** * Pretty-print sql string to be print in the console. * * @param sql * @param dataSourceType */ static formatSql(sql, dataSourceType) { const databaseLanguageMap = { oracle: "pl/sql", }; const databaseLanguage = dataSourceType ? (databaseLanguageMap[dataSourceType] ?? "sql") : "sql"; return (0, formatter_1.format)(sql, { language: databaseLanguage, indent: " ", }); } /** * Logging functions needed by AdvancedConsoleLogger * * @param prefix * @param info */ static logInfo(prefix, info) { console.log(ansis_1.default.gray.underline(prefix), info); } static logError(prefix, error) { console.log(ansis_1.default.underline.red(prefix), error); } static logWarn(prefix, warning) { console.log(ansis_1.default.underline.yellow(prefix), warning); } static log(message) { console.log(ansis_1.default.underline(message)); } static info(info) { return ansis_1.default.gray(info); } static error(error) { return ansis_1.default.red(error); } static warn(message) { return ansis_1.default.yellow(message); } static logCmdErr(prefix, err) { console.log(ansis_1.default.black.bgRed(prefix)); if (err) console.error(err); } } exports.PlatformTools = PlatformTools; //# sourceMappingURL=PlatformTools.js.map