@mikro-orm/cli 7.1.4-dev.7 → 7.1.4-dev.9
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/CLIHelper.js +3 -51
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +3 -3
- package/searchConfiguration.d.ts +12 -0
- package/searchConfiguration.js +63 -0
package/CLIHelper.js
CHANGED
|
@@ -4,6 +4,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
4
4
|
import yargs from 'yargs';
|
|
5
5
|
import { colors, Configuration, loadEnvironmentVars, loadOptionalDependencies, MikroORM, Utils, } from '@mikro-orm/core';
|
|
6
6
|
import { fs } from '@mikro-orm/core/fs-utils';
|
|
7
|
+
import { searchConfiguration } from './searchConfiguration.js';
|
|
7
8
|
/**
|
|
8
9
|
* @internal
|
|
9
10
|
*/
|
|
@@ -24,13 +25,6 @@ export class CLIHelper {
|
|
|
24
25
|
}
|
|
25
26
|
contextName ??= process.env.MIKRO_ORM_CONTEXT_NAME ?? 'default';
|
|
26
27
|
const env = await this.loadEnvironmentVars();
|
|
27
|
-
// oxfmt-ignore
|
|
28
|
-
const configFinder = (cfg) => {
|
|
29
|
-
return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : contextName === 'default');
|
|
30
|
-
};
|
|
31
|
-
const isValidConfigFactoryResult = (cfg) => {
|
|
32
|
-
return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName);
|
|
33
|
-
};
|
|
34
28
|
const result = await this.getConfigFile(paths);
|
|
35
29
|
if (!result[0]) {
|
|
36
30
|
if (Utils.hasObjectKeys(env)) {
|
|
@@ -40,50 +34,8 @@ export class CLIHelper {
|
|
|
40
34
|
}
|
|
41
35
|
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
|
|
42
36
|
}
|
|
43
|
-
const path = result
|
|
44
|
-
|
|
45
|
-
if (Array.isArray(tmp)) {
|
|
46
|
-
const tmpFirstIndex = tmp.findIndex(configFinder);
|
|
47
|
-
if (tmpFirstIndex === -1) {
|
|
48
|
-
// Static config not found. Try factory functions
|
|
49
|
-
let configCandidate;
|
|
50
|
-
for (let i = 0, l = tmp.length; i < l; ++i) {
|
|
51
|
-
const f = tmp[i];
|
|
52
|
-
if (typeof f !== 'function') {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
configCandidate = await f(contextName);
|
|
56
|
-
if (!isValidConfigFactoryResult(configCandidate)) {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
tmp = configCandidate;
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
if (Array.isArray(tmp)) {
|
|
63
|
-
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.`);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
const tmpLastIndex = tmp.findLastIndex(configFinder);
|
|
68
|
-
if (tmpLastIndex !== tmpFirstIndex) {
|
|
69
|
-
throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
|
|
70
|
-
}
|
|
71
|
-
tmp = tmp[tmpFirstIndex];
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
if (tmp instanceof Function) {
|
|
76
|
-
tmp = await tmp(contextName);
|
|
77
|
-
if (!isValidConfigFactoryResult(tmp)) {
|
|
78
|
-
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.`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
if (!configFinder(tmp)) {
|
|
83
|
-
throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
37
|
+
const [path, configExports] = result;
|
|
38
|
+
const tmp = await searchConfiguration(configExports, contextName, path);
|
|
87
39
|
const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
|
|
88
40
|
await loadOptionalDependencies(tmp);
|
|
89
41
|
const preferEnvVars = options.preferEnvVars ?? tmp.preferEnvVars;
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/cli",
|
|
3
|
-
"version": "7.1.4-dev.
|
|
3
|
+
"version": "7.1.4-dev.9",
|
|
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
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"copy": "node ../../scripts/copy.mjs"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@mikro-orm/core": "7.1.4-dev.
|
|
54
|
-
"mikro-orm": "7.1.4-dev.
|
|
53
|
+
"@mikro-orm/core": "7.1.4-dev.9",
|
|
54
|
+
"mikro-orm": "7.1.4-dev.9",
|
|
55
55
|
"yargs": "17.7.2"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type AnyEntity, type EntityClass, type EntityManager, type EntityManagerType, type EntitySchema, type IDatabaseDriver, type Options } from '@mikro-orm/core';
|
|
2
|
+
/**
|
|
3
|
+
* Search a config file for a given contextName.
|
|
4
|
+
*
|
|
5
|
+
* @param configExports The exports from a config file. Importing is left to the caller.
|
|
6
|
+
* @param contextName The context name to search the config for.
|
|
7
|
+
* @param path Path to display in error messages. Callers can use this to match the path the exports were imported from.
|
|
8
|
+
*
|
|
9
|
+
* @return A promise that resolves with The matching configuration options, as defined in the configExports.
|
|
10
|
+
* The caller is responsible for further adjustments or dependency imports.
|
|
11
|
+
*/
|
|
12
|
+
export declare function searchConfiguration<Driver extends IDatabaseDriver = IDatabaseDriver, EM extends EntityManager<Driver> & Driver[typeof EntityManagerType] = EntityManager<Driver> & Driver[typeof EntityManagerType], Entities extends readonly (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(configExports: unknown, contextName?: string, path?: string): Promise<Options<Driver, EM, Entities>>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search a config file for a given contextName.
|
|
3
|
+
*
|
|
4
|
+
* @param configExports The exports from a config file. Importing is left to the caller.
|
|
5
|
+
* @param contextName The context name to search the config for.
|
|
6
|
+
* @param path Path to display in error messages. Callers can use this to match the path the exports were imported from.
|
|
7
|
+
*
|
|
8
|
+
* @return A promise that resolves with The matching configuration options, as defined in the configExports.
|
|
9
|
+
* The caller is responsible for further adjustments or dependency imports.
|
|
10
|
+
*/
|
|
11
|
+
export async function searchConfiguration(configExports, contextName = 'default', path = '') {
|
|
12
|
+
let tmp = configExports;
|
|
13
|
+
// oxfmt-ignore
|
|
14
|
+
const configFinder = (cfg) => {
|
|
15
|
+
return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : contextName === 'default');
|
|
16
|
+
};
|
|
17
|
+
const isValidConfigFactoryResult = (cfg) => {
|
|
18
|
+
return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName);
|
|
19
|
+
};
|
|
20
|
+
if (Array.isArray(tmp)) {
|
|
21
|
+
const tmpFirstIndex = tmp.findIndex(configFinder);
|
|
22
|
+
if (tmpFirstIndex === -1) {
|
|
23
|
+
// Static config not found. Try factory functions
|
|
24
|
+
let configCandidate;
|
|
25
|
+
for (let i = 0, l = tmp.length; i < l; ++i) {
|
|
26
|
+
const f = tmp[i];
|
|
27
|
+
if (typeof f !== 'function') {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
configCandidate = await f(contextName);
|
|
31
|
+
if (!isValidConfigFactoryResult(configCandidate)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
tmp = configCandidate;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(tmp)) {
|
|
38
|
+
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.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const tmpLastIndex = tmp.findLastIndex(configFinder);
|
|
43
|
+
if (tmpLastIndex !== tmpFirstIndex) {
|
|
44
|
+
throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
|
|
45
|
+
}
|
|
46
|
+
tmp = tmp[tmpFirstIndex];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
if (tmp instanceof Function) {
|
|
51
|
+
tmp = await tmp(contextName);
|
|
52
|
+
if (!isValidConfigFactoryResult(tmp)) {
|
|
53
|
+
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.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
if (!configFinder(tmp)) {
|
|
58
|
+
throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return tmp;
|
|
63
|
+
}
|