-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
ConfigurationLoader.ts
202 lines (167 loc) · 7.55 KB
/
ConfigurationLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import dotenv from 'dotenv';
import { pathExists, realpath } from 'fs-extra';
import { isAbsolute, join } from 'path';
import type { IDatabaseDriver } from '../drivers';
import type { Options } from './Configuration';
import { Configuration } from './Configuration';
import { Utils } from './Utils';
import type { Dictionary } from '../typings';
/**
* @internal
*/
export class ConfigurationLoader {
static async getConfiguration<D extends IDatabaseDriver = IDatabaseDriver>(validate = true, options?: Partial<Options>): Promise<Configuration<D>> {
const paths = await ConfigurationLoader.getConfigPaths();
const env = ConfigurationLoader.loadEnvironmentVars(options);
for (let path of paths) {
path = Utils.absolutePath(path);
path = Utils.normalizePath(path);
if (await pathExists(path)) {
const config = await Utils.dynamicImport(path);
/* istanbul ignore next */
let tmp = config.default ?? config;
if (tmp instanceof Function) {
tmp = tmp();
}
if (tmp instanceof Promise) {
tmp = await tmp;
}
return new Configuration({ ...tmp, ...options, ...env }, validate);
}
}
if (Utils.hasObjectKeys(env)) {
return new Configuration({ ...options, ...env }, validate);
}
throw new Error(`MikroORM config file not found in ['${paths.join(`', '`)}']`);
}
static async getPackageConfig(basePath = process.cwd()): Promise<Dictionary> {
if (await pathExists(`${basePath}/package.json`)) {
return import(`${basePath}/package.json`);
}
const parentFolder = await realpath(`${basePath}/..`);
// we reached the root folder
if (basePath === parentFolder) {
return {};
}
return this.getPackageConfig(parentFolder);
}
static async getSettings(): Promise<Settings> {
const config = await ConfigurationLoader.getPackageConfig();
const settings = config['mikro-orm'] || {};
const bool = (v: string) => ['true', 't', '1'].includes(v.toLowerCase());
settings.useTsNode = process.env.MIKRO_ORM_CLI_USE_TS_NODE ? bool(process.env.MIKRO_ORM_CLI_USE_TS_NODE) : settings.useTsNode;
settings.tsConfigPath = process.env.MIKRO_ORM_CLI_TS_CONFIG_PATH ?? settings.tsConfigPath;
return settings;
}
static async getConfigPaths(): Promise<string[]> {
const paths: string[] = [];
const settings = await ConfigurationLoader.getSettings();
if (process.env.MIKRO_ORM_CLI) {
paths.push(process.env.MIKRO_ORM_CLI);
}
paths.push(...(settings.configPaths || []));
if (settings.useTsNode) {
paths.push('./mikro-orm.config.ts');
}
paths.push('./mikro-orm.config.js');
const tsNode = Utils.detectTsNode();
return paths.filter(p => p.endsWith('.js') || tsNode);
}
static async registerTsNode(configPath = 'tsconfig.json'): Promise<void> {
const tsConfigPath = isAbsolute(configPath) ? configPath : join(process.cwd(), configPath);
const tsNode = Utils.tryRequire({
module: 'ts-node',
from: tsConfigPath,
warning: 'ts-node not installed, support for working with TS files might not work',
});
/* istanbul ignore next */
if (!tsNode) {
return;
}
const { options } = tsNode.register({
project: tsConfigPath,
transpileOnly: true,
compilerOptions: {
module: 'commonjs',
},
}).config;
if (Object.entries(options?.paths ?? {}).length > 0) {
Utils.requireFrom('tsconfig-paths', tsConfigPath).register({
baseUrl: options.baseUrl,
paths: options.paths,
});
}
}
static loadEnvironmentVars<D extends IDatabaseDriver>(options?: Options<D> | Configuration<D>): Partial<Options<D>> {
const baseDir = options instanceof Configuration ? options.get('baseDir') : options?.baseDir;
const path = process.env.MIKRO_ORM_ENV ?? ((baseDir ?? process.cwd()) + '/.env');
dotenv.config({ path });
const ret: Dictionary = {};
const array = (v: string) => v.split(',').map(vv => vv.trim());
const bool = (v: string) => ['true', 't', '1'].includes(v.toLowerCase());
const num = (v: string) => +v;
const read = (o: Dictionary, envKey: string, key: string, mapper: (v: string) => unknown = v => v) => {
if (!(envKey in process.env)) {
return;
}
const val = process.env[envKey]!;
o[key] = mapper(val);
};
const cleanup = (o: Dictionary, k: string) => Utils.hasObjectKeys(o[k]) ? {} : delete o[k];
read(ret, 'MIKRO_ORM_BASE_DIR', 'baseDir');
read(ret, 'MIKRO_ORM_TYPE', 'type');
read(ret, 'MIKRO_ORM_ENTITIES', 'entities', array);
read(ret, 'MIKRO_ORM_ENTITIES_TS', 'entitiesTs', array);
read(ret, 'MIKRO_ORM_CLIENT_URL', 'clientUrl');
read(ret, 'MIKRO_ORM_HOST', 'host');
read(ret, 'MIKRO_ORM_PORT', 'port', num);
read(ret, 'MIKRO_ORM_USER', 'user');
read(ret, 'MIKRO_ORM_PASSWORD', 'password');
read(ret, 'MIKRO_ORM_DB_NAME', 'dbName');
read(ret, 'MIKRO_ORM_LOAD_STRATEGY', 'loadStrategy');
read(ret, 'MIKRO_ORM_BATCH_SIZE', 'batchSize', num);
read(ret, 'MIKRO_ORM_USE_BATCH_INSERTS', 'useBatchInserts', bool);
read(ret, 'MIKRO_ORM_USE_BATCH_UPDATES', 'useBatchUpdates', bool);
read(ret, 'MIKRO_ORM_STRICT', 'strict', bool);
read(ret, 'MIKRO_ORM_VALIDATE', 'validate', bool);
read(ret, 'MIKRO_ORM_ALLOW_GLOBAL_CONTEXT', 'allowGlobalContext', bool);
read(ret, 'MIKRO_ORM_AUTO_JOIN_ONE_TO_ONE_OWNER', 'autoJoinOneToOneOwner', bool);
read(ret, 'MIKRO_ORM_PROPAGATE_TO_ONE_OWNER', 'propagateToOneOwner', bool);
read(ret, 'MIKRO_ORM_POPULATE_AFTER_FLUSH', 'populateAfterFlush', bool);
read(ret, 'MIKRO_ORM_FORCE_ENTITY_CONSTRUCTOR', 'forceEntityConstructor', bool);
read(ret, 'MIKRO_ORM_FORCE_UNDEFINED', 'forceUndefined', bool);
read(ret, 'MIKRO_ORM_FORCE_UTC_TIMEZONE', 'forceUtcTimezone', bool);
read(ret, 'MIKRO_ORM_TIMEZONE', 'timezone');
read(ret, 'MIKRO_ORM_ENSURE_INDEXES', 'ensureIndexes', bool);
read(ret, 'MIKRO_ORM_IMPLICIT_TRANSACTIONS', 'implicitTransactions', bool);
read(ret, 'MIKRO_ORM_DEBUG', 'debug', bool);
read(ret, 'MIKRO_ORM_VERBOSE', 'verbose', bool);
ret.discovery = {};
read(ret.discovery, 'MIKRO_ORM_DISCOVERY_WARN_WHEN_NO_ENTITIES', 'warnWhenNoEntities', bool);
read(ret.discovery, 'MIKRO_ORM_DISCOVERY_REQUIRE_ENTITIES_ARRAY', 'requireEntitiesArray', bool);
read(ret.discovery, 'MIKRO_ORM_DISCOVERY_ALWAYS_ANALYSE_PROPERTIES', 'alwaysAnalyseProperties', bool);
read(ret.discovery, 'MIKRO_ORM_DISCOVERY_DISABLE_DYNAMIC_FILE_ACCESS', 'disableDynamicFileAccess', bool);
cleanup(ret, 'discovery');
ret.migrations = {};
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_TABLE_NAME', 'tableName');
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_PATH', 'path');
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_GLOB', 'glob');
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_TRANSACTIONAL', 'transactional', bool);
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_DISABLE_FOREIGN_KEYS', 'disableForeignKeys', bool);
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_ALL_OR_NOTHING', 'allOrNothing', bool);
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_DROP_TABLES', 'dropTables', bool);
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_SAFE', 'safe', bool);
read(ret.migrations, 'MIKRO_ORM_MIGRATIONS_EMIT', 'emit');
cleanup(ret, 'migrations');
ret.seeder = {};
read(ret.seeder, 'MIKRO_ORM_SEEDER_PATH', 'path');
read(ret.seeder, 'MIKRO_ORM_SEEDER_DEFAULT_SEEDER', 'defaultSeeder');
cleanup(ret, 'seeder');
return ret;
}
}
export interface Settings {
useTsNode?: boolean;
tsConfigPath?: string;
configPaths?: string[];
}