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Ac;QACxB,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU;YACvC,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,CAAA;QAElC,OAAO,WAAW,CAAA;IACtB,CAAC;CACJ;AArQD,0DAqQC","file":"ConnectionOptionsReader.js","sourcesContent":["import appRootPath from \"app-root-path\"\nimport path from \"path\"\nimport { DataSourceOptions } from \"../data-source/DataSourceOptions\"\nimport { PlatformTools } from \"../platform/PlatformTools\"\nimport { ConnectionOptionsEnvReader } from \"./options-reader/ConnectionOptionsEnvReader\"\nimport { TypeORMError } from \"../error\"\nimport { isAbsolute } from \"../util/PathUtils\"\nimport { importOrRequireFile } from \"../util/ImportUtils\"\n\n/**\n * Reads connection options from the ormconfig.\n */\nexport class ConnectionOptionsReader {\n // -------------------------------------------------------------------------\n // Constructor\n // -------------------------------------------------------------------------\n\n constructor(\n protected options?: {\n /**\n * Directory where ormconfig should be read from.\n * By default its your application root (where your app package.json is located).\n */\n root?: string\n\n /**\n * Filename of the ormconfig configuration. By default its equal to \"ormconfig\".\n */\n configName?: string\n },\n ) {}\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Returns all connection options read from the ormconfig.\n */\n async all(): Promise {\n const options = await this.load()\n if (!options)\n throw new TypeORMError(\n `No connection options were found in any orm configuration files.`,\n )\n\n return options\n }\n\n /**\n * Gets a connection with a given name read from ormconfig.\n * If connection with such name would not be found then it throw error.\n */\n async get(name: string): Promise {\n const allOptions = await this.all()\n const targetOptions = allOptions.find(\n (options) =>\n options.name === name || (name === \"default\" && !options.name),\n )\n if (!targetOptions)\n throw new TypeORMError(\n `Cannot find connection ${name} because its not defined in any orm configuration files.`,\n )\n\n return targetOptions\n }\n\n /**\n * Checks if there is a TypeORM configuration file.\n */\n async has(name: string): Promise {\n const allOptions = await this.load()\n if (!allOptions) return false\n\n const targetOptions = allOptions.find(\n (options) =>\n options.name === name || (name === \"default\" && !options.name),\n )\n return !!targetOptions\n }\n\n // -------------------------------------------------------------------------\n // Protected Methods\n // -------------------------------------------------------------------------\n\n /**\n * Loads all connection options from a configuration file.\n *\n * todo: get in count NODE_ENV somehow\n */\n protected async load(): Promise {\n let connectionOptions:\n | DataSourceOptions\n | DataSourceOptions[]\n | undefined = undefined\n\n const fileFormats = [\n \"env\",\n \"js\",\n \"mjs\",\n \"cjs\",\n \"ts\",\n \"mts\",\n \"cts\",\n \"json\",\n ]\n\n // Detect if baseFilePath contains file extension\n const possibleExtension = this.baseFilePath.substr(\n this.baseFilePath.lastIndexOf(\".\"),\n )\n const fileExtension = fileFormats.find(\n (extension) => `.${extension}` === possibleExtension,\n )\n\n // try to find any of following configuration formats\n const foundFileFormat =\n fileExtension ||\n fileFormats.find((format) => {\n return PlatformTools.fileExist(this.baseFilePath + \".\" + format)\n })\n\n // Determine config file name\n const configFile = fileExtension\n ? this.baseFilePath\n : this.baseFilePath + \".\" + foundFileFormat\n\n // if .env file found then load all its variables into process.env using dotenv package\n if (foundFileFormat === \"env\") {\n PlatformTools.dotenv(configFile)\n } else if (PlatformTools.fileExist(this.baseDirectory + \"/.env\")) {\n PlatformTools.dotenv(this.baseDirectory + \"/.env\")\n }\n\n // try to find connection options from any of available sources of configuration\n if (\n PlatformTools.getEnvVariable(\"TYPEORM_CONNECTION\") ||\n PlatformTools.getEnvVariable(\"TYPEORM_URL\")\n ) {\n connectionOptions = await new ConnectionOptionsEnvReader().read()\n } else if (\n foundFileFormat === \"js\" ||\n foundFileFormat === \"mjs\" ||\n foundFileFormat === \"cjs\" ||\n foundFileFormat === \"ts\" ||\n foundFileFormat === \"mts\" ||\n foundFileFormat === \"cts\"\n ) {\n const [importOrRequireResult, moduleSystem] =\n await importOrRequireFile(configFile)\n const configModule = await importOrRequireResult\n\n if (\n moduleSystem === \"esm\" ||\n (configModule &&\n \"__esModule\" in configModule &&\n \"default\" in configModule)\n ) {\n connectionOptions = configModule.default\n } else {\n connectionOptions = configModule\n }\n } else if (foundFileFormat === \"json\") {\n connectionOptions = require(configFile)\n }\n\n // normalize and return connection options\n if (connectionOptions) {\n return this.normalizeConnectionOptions(connectionOptions)\n }\n\n return undefined\n }\n\n /**\n * Normalize connection options.\n */\n protected normalizeConnectionOptions(\n connectionOptions: DataSourceOptions | DataSourceOptions[],\n ): DataSourceOptions[] {\n if (!Array.isArray(connectionOptions))\n connectionOptions = [connectionOptions]\n\n connectionOptions.forEach((options) => {\n options.baseDirectory = this.baseDirectory\n if (options.entities) {\n const entities = (options.entities as any[]).map((entity) => {\n if (\n typeof entity === \"string\" &&\n entity.substr(0, 1) !== \"/\"\n )\n return this.baseDirectory + \"/\" + entity\n\n return entity\n })\n Object.assign(connectionOptions, { entities: entities })\n }\n if (options.subscribers) {\n const subscribers = (options.subscribers as any[]).map(\n (subscriber) => {\n if (\n typeof subscriber === \"string\" &&\n subscriber.substr(0, 1) !== \"/\"\n )\n return this.baseDirectory + \"/\" + subscriber\n\n return subscriber\n },\n )\n Object.assign(connectionOptions, { subscribers: subscribers })\n }\n if (options.migrations) {\n const migrations = (options.migrations as any[]).map(\n (migration) => {\n if (\n typeof migration === \"string\" &&\n migration.substr(0, 1) !== \"/\"\n )\n return this.baseDirectory + \"/\" + migration\n\n return migration\n },\n )\n Object.assign(connectionOptions, { migrations: migrations })\n }\n\n // make database path file in sqlite relative to package.json\n if (\n options.type === \"sqlite\" ||\n options.type === \"better-sqlite3\"\n ) {\n if (\n typeof options.database === \"string\" &&\n !isAbsolute(options.database) &&\n options.database.substr(0, 1) !== \"/\" && // unix absolute\n options.database.substr(1, 2) !== \":\\\\\" && // windows absolute\n options.database !== \":memory:\"\n ) {\n Object.assign(options, {\n database: this.baseDirectory + \"/\" + options.database,\n })\n }\n }\n })\n\n return connectionOptions\n }\n\n /**\n * Gets directory where configuration file should be located and configuration file name.\n */\n protected get baseFilePath(): string {\n return path.resolve(this.baseDirectory, this.baseConfigName)\n }\n\n /**\n * Gets directory where configuration file should be located.\n */\n protected get baseDirectory(): string {\n if (this.options && this.options.root) return this.options.root\n\n return appRootPath.path\n }\n\n /**\n * Gets configuration file name.\n */\n protected get baseConfigName(): string {\n if (this.options && this.options.configName)\n return this.options.configName\n\n return \"ormconfig\"\n }\n}\n"],"sourceRoot":".."}