* Gets a named configuration * * @param contextName Load a config with the given `contextName` value. Used when config file exports array or factory function. Setting it to "default" matches also config objects without `contextName` set. * @param paths Array of possible paths for a configur
(contextName?: string, paths?: string[], options: Partial<Options<D>> = {})
| 29 | * @param options Additional options to augment the final configuration with. |
| 30 | */ |
| 31 | static async getConfiguration< |
| 32 | D extends IDatabaseDriver = IDatabaseDriver, |
| 33 | EM extends D[typeof EntityManagerType] & EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, |
| 34 | >(contextName?: string, paths?: string[], options: Partial<Options<D>> = {}): Promise<Configuration<D, EM>> { |
| 35 | this.commonJSCompat(options); |
| 36 | paths ??= await this.getConfigPaths(); |
| 37 | const deps = fs.getORMPackages(); |
| 38 | |
| 39 | if (!deps.has('@mikro-orm/cli') && !process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI && !process.versions.deno) { |
| 40 | throw new Error('@mikro-orm/cli needs to be installed as a local dependency!'); |
| 41 | } |
| 42 | |
| 43 | contextName ??= process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default'; |
| 44 | const env = await this.loadEnvironmentVars(); |
| 45 | |
| 46 | const result = await this.getConfigFile(paths); |
| 47 | if (!result[0]) { |
| 48 | if (Utils.hasObjectKeys(env)) { |
| 49 | const merged = Utils.mergeConfig( |
| 50 | { contextName }, |
| 51 | options.preferEnvVars ? options : env, |
| 52 | options.preferEnvVars ? env : options, |
| 53 | ); |
| 54 | await loadOptionalDependencies(merged); |
| 55 | return new Configuration(merged); |
| 56 | } |
| 57 | throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`); |
| 58 | } |
| 59 | |
| 60 | const [path, configExports] = result; |
| 61 | const tmp = await searchConfiguration(configExports, contextName, path); |
| 62 | |
| 63 | const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {}; |
| 64 | await loadOptionalDependencies(tmp); |
| 65 | |
| 66 | const preferEnvVars = options.preferEnvVars ?? tmp.preferEnvVars; |
| 67 | return new Configuration( |
| 68 | Utils.mergeConfig({}, esmConfigOptions, tmp, preferEnvVars ? options : env, preferEnvVars ? env : options), |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | static commonJSCompat(options: Partial<Options>): void { |
| 73 | if (this.isESM()) { |
no test coverage detected