schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
1
{"version":3,"sources":["../../src/connection/ConnectionOptionsReader.ts"],"names":[],"mappings":";;;AACA,2DAAwD;AACxD,0FAAuF;AACvF,0FAAuF;AACvF,0FAAuF;AAEvF;;;GAGG;AACH;IAEI,4EAA4E;IAC5E,cAAc;IACd,4EAA4E;IAE5E,iCAAsB,OAWrB;QAXqB,YAAO,GAAP,OAAO,CAW5B;IACD,CAAC;IAED,4EAA4E;IAC5E,iBAAiB;IACjB,4EAA4E;IAE5E;;OAEG;IACG,qCAAG,GAAT;;;;;4BACoB,qBAAM,IAAI,CAAC,IAAI,EAAE,EAAA;;wBAA3B,OAAO,GAAG,SAAiB;wBACjC,IAAI,CAAC,OAAO;4BACR,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;wBAEvF,sBAAO,OAAO,EAAC;;;;KAClB;IAED;;;OAGG;IACG,qCAAG,GAAT,UAAU,IAAY;;;;;4BACC,qBAAM,IAAI,CAAC,GAAG,EAAE,EAAA;;wBAA7B,UAAU,GAAG,SAAgB;wBAC7B,aAAa,GAAG,UAAU,CAAC,IAAI,CAAC,UAAA,OAAO,IAAI,OAAA,OAAO,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAA9D,CAA8D,CAAC,CAAC;wBACjH,IAAI,CAAC,aAAa;4BACd,MAAM,IAAI,KAAK,CAAC,4BAA0B,IAAI,6DAA0D,CAAC,CAAC;wBAE9G,sBAAO,aAAa,EAAC;;;;KACxB;IAED;;OAEG;IACG,qCAAG,GAAT,UAAU,IAAY;;;;;4BACC,qBAAM,IAAI,CAAC,IAAI,EAAE,EAAA;;wBAA9B,UAAU,GAAG,SAAiB;wBACpC,IAAI,CAAC,UAAU;4BACX,sBAAO,KAAK,EAAC;wBAEX,aAAa,GAAG,UAAU,CAAC,IAAI,CAAC,UAAA,OAAO,I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pD,CAAC;;;OAAA;IAKD,sBAAc,mDAAc;QAH5B;;WAEG;aACH;YACI,IAAI,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU;gBACvC,OAAO,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC;YAEnC,OAAO,WAAW,CAAC;QACvB,CAAC;;;OAAA;IAEL,8BAAC;AAAD,CA7MA,AA6MC,IAAA;AA7MY,0DAAuB","file":"ConnectionOptionsReader.js","sourcesContent":["import {ConnectionOptions} from \"./ConnectionOptions\";\nimport {PlatformTools} from \"../platform/PlatformTools\";\nimport {ConnectionOptionsEnvReader} from \"./options-reader/ConnectionOptionsEnvReader\";\nimport {ConnectionOptionsYmlReader} from \"./options-reader/ConnectionOptionsYmlReader\";\nimport {ConnectionOptionsXmlReader} from \"./options-reader/ConnectionOptionsXmlReader\";\n\n/**\n * Reads connection options from the ormconfig.\n * Can read from multiple file extensions including env, json, js, xml and yml.\n */\nexport class ConnectionOptionsReader {\n\n    // -------------------------------------------------------------------------\n    // Constructor\n    // -------------------------------------------------------------------------\n\n    constructor(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<ConnectionOptions[]> {\n        const options = await this.load();\n        if (!options)\n            throw new Error(`No connection options were found in any of configurations file.`);\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<ConnectionOptions> {\n        const allOptions = await this.all();\n        const targetOptions = allOptions.find(options => options.name === name || (name === \"default\" && !options.name));\n        if (!targetOptions)\n            throw new Error(`Cannot find connection ${name} because its not defined in any orm configuration files.`);\n\n        return targetOptions;\n    }\n\n    /**\n     * Checks if there is a TypeORM configuration file.\n     */\n    async has(name: string): Promise<boolean> {\n        const allOptions = await this.load();\n        if (!allOptions)\n            return false;\n\n        const targetOptions = allOptions.find(options => options.name === name || (name === \"default\" && !options.name));\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<ConnectionOptions[]|undefined> {\n        let connectionOptions: ConnectionOptions|ConnectionOptions[]|undefined = undefined;\n\n        const fileFormats = [\"env\", \"js\", \"ts\", \"json\", \"yml\", \"yaml\", \"xml\"];\n\n        // Detect if baseFilePath contains file extension\n        const possibleExtension = this.baseFilePath.substr(this.baseFilePath.lastIndexOf(\".\"));\n        const fileExtension = fileFormats.find(extension => `.${extension}` === possibleExtension);\n\n        // try to find any of following configuration formats\n        const foundFileFormat = fileExtension || fileFormats.find(format => {\n            return PlatformTools.fileExist(this.baseFilePath + \".\" + format);\n        });\n\n        // if .env file found then load all its variables into process.env using dotenv package\n        if (foundFileFormat === \"env\") {\n            const dotenv = PlatformTools.load(\"dotenv\");\n            dotenv.config({ path: this.baseFilePath });\n        } else if (PlatformTools.fileExist(\".env\")) {\n            const dotenv = PlatformTools.load(\"dotenv\");\n            dotenv.config({ path: \".env\" });\n        }\n\n        // Determine config file name\n        const configFile = fileExtension ? this.baseFilePath : this.baseFilePath + \".\" + foundFileFormat;\n\n        // try to find connection options from any of available sources of configuration\n        if (PlatformTools.getEnvVariable(\"TYPEORM_CONNECTION\") || PlatformTools.getEnvVariable(\"TYPEORM_URL\")) {\n            connectionOptions = new ConnectionOptionsEnvReader().read();\n\n        } else if (foundFileFormat === \"js\") {\n            connectionOptions = PlatformTools.load(configFile);\n\n        } else if (foundFileFormat === \"ts\") {\n            connectionOptions = PlatformTools.load(configFile);\n\n        } else if (foundFileFormat === \"json\") {\n            connectionOptions = PlatformTools.load(configFile);\n\n        } else if (foundFileFormat === \"yml\") {\n            connectionOptions = new ConnectionOptionsYmlReader().read(configFile);\n\n        } else if (foundFileFormat === \"yaml\") {\n            connectionOptions = new ConnectionOptionsYmlReader().read(configFile);\n\n        } else if (foundFileFormat === \"xml\") {\n            connectionOptions = await new ConnectionOptionsXmlReader().read(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(connectionOptions: ConnectionOptions|ConnectionOptions[]): ConnectionOptions[] {\n        if (!(connectionOptions instanceof Array))\n            connectionOptions = [connectionOptions];\n\n        connectionOptions.forEach(options => {\n            if (options.entities) {\n                const entities = (options.entities as any[]).map(entity => {\n                    if (typeof entity === \"string\" && entity.substr(0, 1) !== \"/\")\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(subscriber => {\n                    if (typeof subscriber === \"string\" && subscriber.substr(0, 1) !== \"/\")\n                        return this.baseDirectory + \"/\" + subscriber;\n\n                    return subscriber;\n                });\n                Object.assign(connectionOptions, { subscribers: subscribers });\n            }\n            if (options.migrations) {\n                const migrations = (options.migrations as any[]).map(migration => {\n                    if (typeof migration === \"string\" && migration.substr(0, 1) !== \"/\")\n                        return this.baseDirectory + \"/\" + migration;\n\n                    return migration;\n                });\n                Object.assign(connectionOptions, { migrations: migrations });\n            }\n\n            // make database path file in sqlite relative to package.json\n            if (options.type === \"sqlite\") {\n                if (typeof options.database === \"string\" &&\n                    options.database.substr(0, 1) !== \"/\" &&  // unix absolute\n                    options.database.substr(1, 2) !== \":\\\\\" && // windows absolute\n                    options.database !== \":memory:\") {\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 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)\n            return this.options.root;\n\n        return PlatformTools.load(\"app-root-path\").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}\n"],"sourceRoot":".."}