(configExports: unknown, contextName = 'default', path = '')
| 19 | * The caller is responsible for further adjustments or dependency imports. |
| 20 | */ |
| 21 | export async function searchConfiguration< |
| 22 | Driver extends IDatabaseDriver = IDatabaseDriver, |
| 23 | EM extends EntityManager<Driver> & Driver[typeof EntityManagerType] = EntityManager<Driver> & |
| 24 | Driver[typeof EntityManagerType], |
| 25 | Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = ( |
| 26 | | string |
| 27 | | EntityClass<AnyEntity> |
| 28 | | EntitySchema |
| 29 | )[], |
| 30 | >(configExports: unknown, contextName = 'default', path = ''): Promise<Options<Driver, EM, Entities>> { |
| 31 | let tmp = configExports as Options<Driver, EM, Entities>; |
| 32 | |
| 33 | // oxfmt-ignore |
| 34 | const configFinder = (cfg: unknown) => { |
| 35 | return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : contextName === 'default'); |
| 36 | }; |
| 37 | |
| 38 | const isValidConfigFactoryResult = (cfg: unknown) => { |
| 39 | return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName); |
| 40 | }; |
| 41 | |
| 42 | if (Array.isArray(tmp)) { |
| 43 | const tmpFirstIndex = tmp.findIndex(configFinder); |
| 44 | if (tmpFirstIndex === -1) { |
| 45 | // Static config not found. Try factory functions |
| 46 | let configCandidate: Options<Driver, EM, Entities>; |
| 47 | for (let i = 0, l = tmp.length; i < l; ++i) { |
| 48 | const f = tmp[i]; |
| 49 | if (typeof f !== 'function') { |
| 50 | continue; |
| 51 | } |
| 52 | configCandidate = await f(contextName); |
| 53 | if (!isValidConfigFactoryResult(configCandidate)) { |
| 54 | continue; |
| 55 | } |
| 56 | tmp = configCandidate; |
| 57 | break; |
| 58 | } |
| 59 | if (Array.isArray(tmp)) { |
| 60 | throw new Error( |
| 61 | `MikroORM config '${contextName}' was not found within the config file '${path}'. Either add a config with this name to the array, or add a function that when given this name will return a configuration object without a name, or with name set to this name.`, |
| 62 | ); |
| 63 | } |
| 64 | } else { |
| 65 | const tmpLastIndex = tmp.findLastIndex(configFinder); |
| 66 | if (tmpLastIndex !== tmpFirstIndex) { |
| 67 | throw new Error( |
| 68 | `MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`, |
| 69 | ); |
| 70 | } |
| 71 | tmp = tmp[tmpFirstIndex]; |
| 72 | } |
| 73 | } else { |
| 74 | if (tmp instanceof Function) { |
| 75 | tmp = await tmp(contextName); |
| 76 | |
| 77 | if (!isValidConfigFactoryResult(tmp)) { |
| 78 | throw new Error( |
no test coverage detected