@mikro-orm/core 6.3.14-dev.72 → 6.3.14-dev.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.3.14-dev.72",
3
+ "version": "6.3.14-dev.74",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.2.0",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.3.14-dev.72",
67
+ "mikro-orm": "6.3.14-dev.74",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
@@ -7,32 +7,29 @@ import { Configuration, type Options } from './Configuration';
7
7
  */
8
8
  export declare class ConfigurationLoader {
9
9
  /**
10
- * Gets a named configuration from a set of paths
10
+ * Gets a named configuration
11
11
  *
12
- * @param contextName Name of context to load from the config. Used when config file exports array or factory function.
13
- * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used.
12
+ * @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.
13
+ * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link ConfigurationLoader.getConfigPaths}.
14
14
  * @param options Additional options to augment the final configuration with.
15
15
  */
16
- static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(contextName: string, paths: string[], options?: Partial<Options>): Promise<Configuration<D, EM>>;
16
+ static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(contextName: string, paths?: string[], options?: Partial<Options>): Promise<Configuration<D, EM>>;
17
17
  /**
18
- * Gets a named configuration from a set of paths
18
+ * Gets the default config from the default paths
19
19
  *
20
- * @param contextName Name of context to load from the config. Used when config file exports array or factory function. Defaults to "default".
21
- * @param paths Array of possible paths for a configuration file. Files will be checked in order, and the first existing one will be used. Defaults to the output of {@link ConfigurationLoader.getConfigPaths}
22
- * @param options Additional options to augment the final configuration with.
23
- *
24
- * @deprecated Prefer to explicitly set the parameter values. The defaults are available for backwards compatibility.
20
+ * @deprecated Prefer to explicitly set the `contextName` at the first argument. This signature is available for backwards compatibility, and may be removed in v7.
25
21
  */
26
- static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(contextName?: string, paths?: string[], options?: Partial<Options>): Promise<Configuration<D, EM>>;
22
+ static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(): Promise<Configuration<D, EM>>;
27
23
  /**
28
- * Gets default configuration
24
+ * Gets default configuration out of the default paths, and possibly from `process.argv`
29
25
  *
30
- * @param validate It was used to control Configuration constructor's validate parameter. Currently is no-op. Set to `true` to enable cross-version calls that also check for --config in process.argv.
31
- * @param options Additional options to augment the final configuration with.
26
+ * @param validate Whether to validate the final configuration.
27
+ * @param options Additional options to augment the final configuration with (just before validation).
32
28
  *
33
- * @deprecated Use the other overload of this method. This signature will be removed in v7.
29
+ * @deprecated Use the other overloads of this method. This signature will be removed in v7.
34
30
  */
35
- static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(validate: true, options?: Partial<Options>): Promise<Configuration<D, EM>>;
31
+ static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager = EntityManager>(validate: boolean, options?: Partial<Options>): Promise<Configuration<D, EM>>;
32
+ static getConfigFile(paths: string[]): Promise<[string, unknown] | []>;
36
33
  static getPackageConfig(basePath?: string): Dictionary;
37
34
  static getSettings(): Settings;
38
35
  static configPathsFromArg(): string[] | undefined;
@@ -17,9 +17,25 @@ const Utils_1 = require("./Utils");
17
17
  */
18
18
  class ConfigurationLoader {
19
19
  static async getConfiguration(contextName = 'default', paths = ConfigurationLoader.getConfigPaths(), options = {}) {
20
- if (contextName === true || !Array.isArray(paths)) {
20
+ // Backwards compatibility layer
21
+ if (typeof contextName === 'boolean' || !Array.isArray(paths)) {
22
+ this.commonJSCompat(options);
23
+ this.registerDotenv(options);
21
24
  const configPathFromArg = ConfigurationLoader.configPathsFromArg();
22
- const config = (await ConfigurationLoader.getConfiguration(process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default', configPathFromArg ?? ConfigurationLoader.getConfigPaths(), Array.isArray(paths) ? undefined : paths));
25
+ const configPaths = configPathFromArg ?? (Array.isArray(paths) ? paths : ConfigurationLoader.getConfigPaths());
26
+ const config = contextName
27
+ ? (await ConfigurationLoader.getConfiguration(process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default', configPaths, Array.isArray(paths) ? {} : paths))
28
+ : await (async () => {
29
+ const env = this.loadEnvironmentVars();
30
+ const [path, tmp] = await this.getConfigFile(configPaths);
31
+ if (!path) {
32
+ if (Utils_1.Utils.hasObjectKeys(env)) {
33
+ return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig({}, options, env), false);
34
+ }
35
+ throw new Error(`MikroORM config file not found in ['${configPaths.join(`', '`)}']`);
36
+ }
37
+ return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig(tmp, options, env), false);
38
+ })();
23
39
  if (configPathFromArg) {
24
40
  config.getLogger().warn('deprecated', 'Path for config file was inferred from the command line arguments. Instead, you should set the MIKRO_ORM_CLI_CONFIG environment variable to specify the path, or if you really must use the command line arguments, import the config manually based on them, and pass it to init.', { label: 'D0001' });
25
41
  }
@@ -32,63 +48,71 @@ class ConfigurationLoader {
32
48
  const isValidConfigFactoryResult = (cfg) => {
33
49
  return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName);
34
50
  };
51
+ const result = await this.getConfigFile(paths);
52
+ if (!result[0]) {
53
+ if (Utils_1.Utils.hasObjectKeys(env)) {
54
+ return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig({ contextName }, options, env));
55
+ }
56
+ throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
57
+ }
58
+ const path = result[0];
59
+ let tmp = result[1];
60
+ if (Array.isArray(tmp)) {
61
+ const tmpFirstIndex = tmp.findIndex(configFinder);
62
+ if (tmpFirstIndex === -1) {
63
+ // Static config not found. Try factory functions
64
+ let configCandidate;
65
+ for (let i = 0, l = tmp.length; i < l; ++i) {
66
+ const f = tmp[i];
67
+ if (typeof f !== 'function') {
68
+ continue;
69
+ }
70
+ configCandidate = await f(contextName);
71
+ if (!isValidConfigFactoryResult(configCandidate)) {
72
+ continue;
73
+ }
74
+ tmp = configCandidate;
75
+ break;
76
+ }
77
+ if (Array.isArray(tmp)) {
78
+ throw new Error(`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.`);
79
+ }
80
+ }
81
+ else {
82
+ const tmpLastIndex = tmp.findLastIndex(configFinder);
83
+ if (tmpLastIndex !== tmpFirstIndex) {
84
+ throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
85
+ }
86
+ tmp = tmp[tmpFirstIndex];
87
+ }
88
+ }
89
+ else {
90
+ if (tmp instanceof Function) {
91
+ tmp = await tmp(contextName);
92
+ if (!isValidConfigFactoryResult(tmp)) {
93
+ throw new Error(`MikroORM config '${contextName}' was not what the function exported from '${path}' provided. Ensure it returns a config object with no name, or name matching the requested one.`);
94
+ }
95
+ }
96
+ else {
97
+ if (!configFinder(tmp)) {
98
+ throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
99
+ }
100
+ }
101
+ }
102
+ const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
103
+ return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig({}, esmConfigOptions, tmp, options, env));
104
+ }
105
+ static async getConfigFile(paths) {
35
106
  for (let path of paths) {
36
107
  path = Utils_1.Utils.absolutePath(path);
37
108
  path = Utils_1.Utils.normalizePath(path);
38
109
  if ((0, fs_extra_1.pathExistsSync)(path)) {
39
110
  const config = await Utils_1.Utils.dynamicImport(path);
40
111
  /* istanbul ignore next */
41
- let tmp = await (config.default ?? config);
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;
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(`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.`);
61
- }
62
- }
63
- else {
64
- const tmpLastIndex = tmp.findLastIndex(configFinder);
65
- if (tmpLastIndex !== tmpFirstIndex) {
66
- throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
67
- }
68
- tmp = tmp[tmpFirstIndex];
69
- }
70
- }
71
- else {
72
- if (tmp instanceof Function) {
73
- tmp = await tmp(contextName);
74
- if (!isValidConfigFactoryResult(tmp)) {
75
- throw new Error(`MikroORM config '${contextName}' was not what the function exported from '${path}' provided. Ensure it returns a config object with no name, or name matching the requested one.`);
76
- }
77
- }
78
- else {
79
- if (!configFinder(tmp)) {
80
- throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
81
- }
82
- }
83
- }
84
- const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
85
- return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig({}, esmConfigOptions, tmp, options, env));
112
+ return [path, await (config.default ?? config)];
86
113
  }
87
114
  }
88
- if (Utils_1.Utils.hasObjectKeys(env)) {
89
- return new Configuration_1.Configuration(Utils_1.Utils.mergeConfig({ contextName }, options, env));
90
- }
91
- throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
115
+ return [];
92
116
  }
93
117
  static getPackageConfig(basePath = process.cwd()) {
94
118
  if ((0, fs_extra_1.pathExistsSync)(`${basePath}/package.json`)) {