@mikro-orm/core 7.0.0-dev.64 → 7.0.0-dev.65
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/MikroORM.js +1 -1
- package/package.json +2 -2
- package/utils/ConfigurationLoader.d.ts +3 -30
- package/utils/ConfigurationLoader.js +19 -200
package/MikroORM.js
CHANGED
|
@@ -37,7 +37,7 @@ export class MikroORM {
|
|
|
37
37
|
* - no support for folder based discovery
|
|
38
38
|
*/
|
|
39
39
|
constructor(options) {
|
|
40
|
-
const env = ConfigurationLoader.
|
|
40
|
+
const env = ConfigurationLoader.loadEnvironmentVars();
|
|
41
41
|
const coreVersion = ConfigurationLoader.checkPackageVersion();
|
|
42
42
|
options = Utils.merge(options, env);
|
|
43
43
|
this.config = new Configuration(options);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "7.0.0-dev.
|
|
4
|
+
"version": "7.0.0-dev.65",
|
|
5
5
|
"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.",
|
|
6
6
|
"exports": {
|
|
7
7
|
"./package.json": "./package.json",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"mikro-orm": "7.0.0-dev.
|
|
55
|
+
"mikro-orm": "7.0.0-dev.65"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"dataloader": "2.2.3"
|
|
@@ -1,40 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { EntityManagerType, IDatabaseDriver } from '../drivers/IDatabaseDriver.js';
|
|
1
|
+
import type { IDatabaseDriver } from '../drivers/IDatabaseDriver.js';
|
|
3
2
|
import type { Dictionary } from '../typings.js';
|
|
4
|
-
import {
|
|
3
|
+
import { type Options } from './Configuration.js';
|
|
5
4
|
/**
|
|
6
5
|
* @internal
|
|
7
6
|
*/
|
|
8
7
|
export declare class ConfigurationLoader {
|
|
9
|
-
|
|
10
|
-
* Gets a named configuration
|
|
11
|
-
*
|
|
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
|
-
* @param options Additional options to augment the final configuration with.
|
|
15
|
-
*/
|
|
16
|
-
static getConfiguration<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>>(contextName?: string, paths?: string[], options?: Partial<Options>): Promise<Configuration<D, EM>>;
|
|
17
|
-
static getConfigFile(paths: string[]): Promise<[string, unknown] | []>;
|
|
8
|
+
static loadEnvironmentVars<D extends IDatabaseDriver>(): Partial<Options<D>>;
|
|
18
9
|
static getPackageConfig(basePath?: string): Dictionary;
|
|
19
|
-
static getSettings(): Settings;
|
|
20
|
-
static getConfigPaths(): string[];
|
|
21
|
-
static isESM(): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Tries to register TS support in the following order: swc, tsx, jiti, tsimp
|
|
24
|
-
* Use `MIKRO_ORM_CLI_TS_LOADER` env var to set the loader explicitly.
|
|
25
|
-
* This method is used only in CLI context.
|
|
26
|
-
*/
|
|
27
|
-
static registerTypeScriptSupport(configPath?: string, tsLoader?: 'swc' | 'tsx' | 'jiti' | 'tsimp' | 'auto'): Promise<boolean>;
|
|
28
|
-
static loadEnvironmentVars<D extends IDatabaseDriver>(): Promise<Partial<Options<D>>>;
|
|
29
|
-
static loadEnvironmentVarsSync<D extends IDatabaseDriver>(): Partial<Options<D>>;
|
|
30
10
|
static getORMPackages(): Set<string>;
|
|
31
11
|
static getORMPackageVersion(name: string): string | undefined;
|
|
32
12
|
static checkPackageVersion(): string;
|
|
33
13
|
}
|
|
34
|
-
export interface Settings {
|
|
35
|
-
verbose?: boolean;
|
|
36
|
-
preferTs?: boolean;
|
|
37
|
-
tsLoader?: 'swc' | 'tsx' | 'jiti' | 'tsimp' | 'auto';
|
|
38
|
-
tsConfigPath?: string;
|
|
39
|
-
configPaths?: string[];
|
|
40
|
-
}
|
|
@@ -1,209 +1,11 @@
|
|
|
1
1
|
import { realpathSync } from 'node:fs';
|
|
2
|
-
import { colors } from '../logging/colors.js';
|
|
3
|
-
import { Configuration } from './Configuration.js';
|
|
4
2
|
import { Utils } from './Utils.js';
|
|
3
|
+
import { colors } from '../logging/colors.js';
|
|
5
4
|
/**
|
|
6
5
|
* @internal
|
|
7
6
|
*/
|
|
8
7
|
export class ConfigurationLoader {
|
|
9
|
-
|
|
10
|
-
* Gets a named configuration
|
|
11
|
-
*
|
|
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
|
-
* @param options Additional options to augment the final configuration with.
|
|
15
|
-
*/
|
|
16
|
-
static async getConfiguration(contextName = 'default', paths = ConfigurationLoader.getConfigPaths(), options = {}) {
|
|
17
|
-
const env = await this.loadEnvironmentVars();
|
|
18
|
-
const configFinder = (cfg) => {
|
|
19
|
-
return typeof cfg === 'object' && cfg !== null && ('contextName' in cfg ? cfg.contextName === contextName : (contextName === 'default'));
|
|
20
|
-
};
|
|
21
|
-
const isValidConfigFactoryResult = (cfg) => {
|
|
22
|
-
return typeof cfg === 'object' && cfg !== null && (!('contextName' in cfg) || cfg.contextName === contextName);
|
|
23
|
-
};
|
|
24
|
-
const result = await this.getConfigFile(paths);
|
|
25
|
-
if (!result[0]) {
|
|
26
|
-
if (Utils.hasObjectKeys(env)) {
|
|
27
|
-
return new Configuration(Utils.mergeConfig({ contextName }, options, env));
|
|
28
|
-
}
|
|
29
|
-
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
|
|
30
|
-
}
|
|
31
|
-
const path = result[0];
|
|
32
|
-
let tmp = result[1];
|
|
33
|
-
if (Array.isArray(tmp)) {
|
|
34
|
-
const tmpFirstIndex = tmp.findIndex(configFinder);
|
|
35
|
-
if (tmpFirstIndex === -1) {
|
|
36
|
-
// Static config not found. Try factory functions
|
|
37
|
-
let configCandidate;
|
|
38
|
-
for (let i = 0, l = tmp.length; i < l; ++i) {
|
|
39
|
-
const f = tmp[i];
|
|
40
|
-
if (typeof f !== 'function') {
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
configCandidate = await f(contextName);
|
|
44
|
-
if (!isValidConfigFactoryResult(configCandidate)) {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
tmp = configCandidate;
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
50
|
-
if (Array.isArray(tmp)) {
|
|
51
|
-
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.`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
const tmpLastIndex = tmp.findLastIndex(configFinder);
|
|
56
|
-
if (tmpLastIndex !== tmpFirstIndex) {
|
|
57
|
-
throw new Error(`MikroORM config '${contextName}' is not unique within the array exported by '${path}' (first occurrence index: ${tmpFirstIndex}; last occurrence index: ${tmpLastIndex})`);
|
|
58
|
-
}
|
|
59
|
-
tmp = tmp[tmpFirstIndex];
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
if (tmp instanceof Function) {
|
|
64
|
-
tmp = await tmp(contextName);
|
|
65
|
-
if (!isValidConfigFactoryResult(tmp)) {
|
|
66
|
-
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.`);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
if (!configFinder(tmp)) {
|
|
71
|
-
throw new Error(`MikroORM config '${contextName}' was not what the default export from '${path}' provided.`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
const esmConfigOptions = this.isESM() ? { entityGenerator: { esmImport: true } } : {};
|
|
76
|
-
return new Configuration(Utils.mergeConfig({}, esmConfigOptions, tmp, options, env));
|
|
77
|
-
}
|
|
78
|
-
static async getConfigFile(paths) {
|
|
79
|
-
for (let path of paths) {
|
|
80
|
-
path = Utils.absolutePath(path);
|
|
81
|
-
path = Utils.normalizePath(path);
|
|
82
|
-
if (Utils.pathExists(path)) {
|
|
83
|
-
const config = await Utils.dynamicImport(path);
|
|
84
|
-
/* v8 ignore next */
|
|
85
|
-
return [path, await (config.default ?? config)];
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return [];
|
|
89
|
-
}
|
|
90
|
-
static getPackageConfig(basePath = process.cwd()) {
|
|
91
|
-
if (Utils.pathExists(`${basePath}/package.json`)) {
|
|
92
|
-
/* v8 ignore next 5 */
|
|
93
|
-
try {
|
|
94
|
-
return Utils.readJSONSync(`${basePath}/package.json`);
|
|
95
|
-
}
|
|
96
|
-
catch {
|
|
97
|
-
return {};
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
const parentFolder = realpathSync(`${basePath}/..`);
|
|
101
|
-
// we reached the root folder
|
|
102
|
-
if (basePath === parentFolder) {
|
|
103
|
-
return {};
|
|
104
|
-
}
|
|
105
|
-
return this.getPackageConfig(parentFolder);
|
|
106
|
-
}
|
|
107
|
-
static getSettings() {
|
|
108
|
-
const config = ConfigurationLoader.getPackageConfig();
|
|
109
|
-
const settings = { ...config['mikro-orm'] };
|
|
110
|
-
const bool = (v) => ['true', 't', '1'].includes(v.toLowerCase());
|
|
111
|
-
settings.preferTs = process.env.MIKRO_ORM_CLI_PREFER_TS != null ? bool(process.env.MIKRO_ORM_CLI_PREFER_TS) : settings.preferTs;
|
|
112
|
-
settings.tsLoader = process.env.MIKRO_ORM_CLI_TS_LOADER ?? settings.tsLoader;
|
|
113
|
-
settings.tsConfigPath = process.env.MIKRO_ORM_CLI_TS_CONFIG_PATH ?? settings.tsConfigPath;
|
|
114
|
-
settings.verbose = process.env.MIKRO_ORM_CLI_VERBOSE != null ? bool(process.env.MIKRO_ORM_CLI_VERBOSE) : settings.verbose;
|
|
115
|
-
if (process.env.MIKRO_ORM_CLI_CONFIG?.endsWith('.ts')) {
|
|
116
|
-
settings.preferTs = true;
|
|
117
|
-
}
|
|
118
|
-
return settings;
|
|
119
|
-
}
|
|
120
|
-
static getConfigPaths() {
|
|
121
|
-
const settings = ConfigurationLoader.getSettings();
|
|
122
|
-
const typeScriptSupport = settings.preferTs ?? Utils.detectTypeScriptSupport();
|
|
123
|
-
const paths = [];
|
|
124
|
-
if (process.env.MIKRO_ORM_CLI_CONFIG) {
|
|
125
|
-
paths.push(process.env.MIKRO_ORM_CLI_CONFIG);
|
|
126
|
-
}
|
|
127
|
-
paths.push(...(settings.configPaths || []));
|
|
128
|
-
if (typeScriptSupport) {
|
|
129
|
-
paths.push('./src/mikro-orm.config.ts');
|
|
130
|
-
paths.push('./mikro-orm.config.ts');
|
|
131
|
-
}
|
|
132
|
-
const distDir = Utils.pathExists(process.cwd() + '/dist');
|
|
133
|
-
const buildDir = Utils.pathExists(process.cwd() + '/build');
|
|
134
|
-
/* v8 ignore next */
|
|
135
|
-
const path = distDir ? 'dist' : (buildDir ? 'build' : 'src');
|
|
136
|
-
paths.push(`./${path}/mikro-orm.config.js`);
|
|
137
|
-
paths.push('./mikro-orm.config.js');
|
|
138
|
-
/* v8 ignore next */
|
|
139
|
-
return Utils.unique(paths).filter(p => !p.match(/\.[mc]?ts$/) || typeScriptSupport);
|
|
140
|
-
}
|
|
141
|
-
static isESM() {
|
|
142
|
-
const config = ConfigurationLoader.getPackageConfig();
|
|
143
|
-
const type = config?.type ?? '';
|
|
144
|
-
return type === 'module';
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Tries to register TS support in the following order: swc, tsx, jiti, tsimp
|
|
148
|
-
* Use `MIKRO_ORM_CLI_TS_LOADER` env var to set the loader explicitly.
|
|
149
|
-
* This method is used only in CLI context.
|
|
150
|
-
*/
|
|
151
|
-
static async registerTypeScriptSupport(configPath = 'tsconfig.json', tsLoader) {
|
|
152
|
-
/* v8 ignore next 3 */
|
|
153
|
-
if (process.versions.bun) {
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
process.env.SWC_NODE_PROJECT ??= configPath;
|
|
157
|
-
process.env.TSIMP_PROJECT ??= configPath;
|
|
158
|
-
process.env.MIKRO_ORM_CLI_ALWAYS_ALLOW_TS ??= '1';
|
|
159
|
-
const isEsm = this.isESM();
|
|
160
|
-
/* v8 ignore next */
|
|
161
|
-
const importMethod = isEsm ? 'tryImport' : 'tryRequire';
|
|
162
|
-
const explicitLoader = tsLoader ?? process.env.MIKRO_ORM_CLI_TS_LOADER ?? 'auto';
|
|
163
|
-
const loaders = {
|
|
164
|
-
swc: { esm: '@swc-node/register/esm-register', cjs: '@swc-node/register' },
|
|
165
|
-
tsx: { esm: 'tsx/esm/api', cjs: 'tsx/cjs/api', cb: (tsx) => tsx.register({ tsconfig: configPath }) },
|
|
166
|
-
jiti: { esm: 'jiti/register', cjs: 'jiti/register', cb: () => Utils.setDynamicImportProvider(id => import(id).then(mod => mod?.default ?? mod)) },
|
|
167
|
-
tsimp: { esm: 'tsimp/import', cjs: 'tsimp/import' },
|
|
168
|
-
};
|
|
169
|
-
for (const loader of Utils.keys(loaders)) {
|
|
170
|
-
if (explicitLoader !== 'auto' && loader !== explicitLoader) {
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
const { esm, cjs, cb } = loaders[loader];
|
|
174
|
-
/* v8 ignore next */
|
|
175
|
-
const module = isEsm ? esm : cjs;
|
|
176
|
-
const mod = await Utils[importMethod]({ module });
|
|
177
|
-
if (mod) {
|
|
178
|
-
cb?.(mod);
|
|
179
|
-
process.env.MIKRO_ORM_CLI_TS_LOADER = loader;
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
// eslint-disable-next-line no-console
|
|
184
|
-
console.warn('Neither `swc`, `tsx`, `jiti` nor `tsimp` found in the project dependencies, support for working with TypeScript files might not work. To use `swc`, you need to install both `@swc-node/register` and `@swc/core`.');
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
static async loadEnvironmentVars() {
|
|
188
|
-
const ret = this.loadEnvironmentVarsSync();
|
|
189
|
-
// only to keep some sort of back compatibility with those using env vars only, to support `MIKRO_ORM_TYPE`
|
|
190
|
-
const PLATFORMS = {
|
|
191
|
-
mongo: { className: 'MongoDriver', module: '@mikro-orm/mongodb' },
|
|
192
|
-
mysql: { className: 'MySqlDriver', module: '@mikro-orm/mysql' },
|
|
193
|
-
mssql: { className: 'MsSqlDriver', module: '@mikro-orm/mssql' },
|
|
194
|
-
mariadb: { className: 'MariaDbDriver', module: '@mikro-orm/mariadb' },
|
|
195
|
-
postgresql: { className: 'PostgreSqlDriver', module: '@mikro-orm/postgresql' },
|
|
196
|
-
sqlite: { className: 'SqliteDriver', module: '@mikro-orm/sqlite' },
|
|
197
|
-
libsql: { className: 'LibSqlDriver', module: '@mikro-orm/libsql' },
|
|
198
|
-
};
|
|
199
|
-
if (process.env.MIKRO_ORM_TYPE) {
|
|
200
|
-
const val = process.env.MIKRO_ORM_TYPE;
|
|
201
|
-
const driver = await import(PLATFORMS[val].module);
|
|
202
|
-
ret.driver = driver[PLATFORMS[val].className];
|
|
203
|
-
}
|
|
204
|
-
return ret;
|
|
205
|
-
}
|
|
206
|
-
static loadEnvironmentVarsSync() {
|
|
8
|
+
static loadEnvironmentVars() {
|
|
207
9
|
const ret = {};
|
|
208
10
|
const array = (v) => v.split(',').map(vv => vv.trim());
|
|
209
11
|
const bool = (v) => ['true', 't', '1'].includes(v.toLowerCase());
|
|
@@ -280,6 +82,23 @@ export class ConfigurationLoader {
|
|
|
280
82
|
cleanup(ret, 'seeder');
|
|
281
83
|
return ret;
|
|
282
84
|
}
|
|
85
|
+
static getPackageConfig(basePath = process.cwd()) {
|
|
86
|
+
if (Utils.pathExists(`${basePath}/package.json`)) {
|
|
87
|
+
/* v8 ignore next 5 */
|
|
88
|
+
try {
|
|
89
|
+
return Utils.readJSONSync(`${basePath}/package.json`);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const parentFolder = realpathSync(`${basePath}/..`);
|
|
96
|
+
// we reached the root folder
|
|
97
|
+
if (basePath === parentFolder) {
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
return this.getPackageConfig(parentFolder);
|
|
101
|
+
}
|
|
283
102
|
static getORMPackages() {
|
|
284
103
|
const pkg = this.getPackageConfig();
|
|
285
104
|
return new Set([
|