@mikro-orm/core 7.0.4 → 7.0.5-dev.0
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/EntityManager.d.ts +583 -884
- package/EntityManager.js +1895 -1922
- package/MikroORM.d.ts +74 -103
- package/MikroORM.js +179 -178
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +24 -30
- package/cache/FileCacheAdapter.js +78 -80
- package/cache/GeneratedCacheAdapter.d.ts +20 -18
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +20 -18
- package/cache/MemoryCacheAdapter.js +36 -35
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +84 -95
- package/connections/Connection.js +168 -165
- package/drivers/DatabaseDriver.d.ts +80 -186
- package/drivers/DatabaseDriver.js +443 -450
- package/drivers/IDatabaseDriver.d.ts +301 -440
- package/entity/BaseEntity.d.ts +83 -120
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +179 -212
- package/entity/Collection.js +721 -727
- package/entity/EntityAssigner.d.ts +77 -88
- package/entity/EntityAssigner.js +230 -231
- package/entity/EntityFactory.d.ts +54 -66
- package/entity/EntityFactory.js +383 -425
- package/entity/EntityHelper.d.ts +22 -34
- package/entity/EntityHelper.js +267 -280
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +723 -753
- package/entity/EntityRepository.d.ts +201 -316
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +82 -126
- package/entity/Reference.js +274 -278
- package/entity/WrappedEntity.d.ts +72 -115
- package/entity/WrappedEntity.js +166 -168
- package/entity/defineEntity.d.ts +636 -1315
- package/entity/defineEntity.js +518 -527
- package/entity/utils.d.ts +3 -13
- package/entity/utils.js +73 -71
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +253 -258
- package/enums.js +252 -251
- package/errors.d.ts +72 -114
- package/errors.js +253 -350
- package/events/EventManager.d.ts +14 -26
- package/events/EventManager.js +77 -79
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +8 -15
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +40 -23
- package/exceptions.js +52 -35
- package/hydration/Hydrator.d.ts +17 -42
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +17 -50
- package/hydration/ObjectHydrator.js +416 -481
- package/index.d.ts +2 -116
- package/index.js +1 -10
- package/logging/DefaultLogger.d.ts +32 -34
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +11 -13
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +10 -11
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +127 -211
- package/metadata/EntitySchema.js +398 -397
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1870 -1951
- package/metadata/MetadataProvider.d.ts +21 -24
- package/metadata/MetadataProvider.js +84 -82
- package/metadata/MetadataStorage.d.ts +32 -38
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +338 -381
- package/metadata/discover-entities.d.ts +2 -5
- package/metadata/discover-entities.js +37 -35
- package/metadata/types.d.ts +531 -615
- package/naming-strategy/AbstractNamingStrategy.d.ts +39 -54
- package/naming-strategy/AbstractNamingStrategy.js +85 -90
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
- package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
- package/naming-strategy/MongoNamingStrategy.d.ts +6 -6
- package/naming-strategy/MongoNamingStrategy.js +18 -18
- package/naming-strategy/NamingStrategy.d.ts +99 -109
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +4 -7
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +301 -310
- package/platforms/Platform.js +640 -663
- package/serialization/EntitySerializer.d.ts +26 -49
- package/serialization/EntitySerializer.js +218 -224
- package/serialization/EntityTransformer.d.ts +6 -10
- package/serialization/EntityTransformer.js +217 -219
- package/serialization/SerializationContext.d.ts +23 -27
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +10 -17
- package/types/BigIntType.js +37 -37
- package/types/BlobType.d.ts +3 -3
- package/types/BlobType.js +13 -13
- package/types/BooleanType.d.ts +4 -4
- package/types/BooleanType.js +12 -12
- package/types/CharacterType.d.ts +2 -2
- package/types/CharacterType.js +6 -6
- package/types/DateTimeType.d.ts +5 -5
- package/types/DateTimeType.js +15 -15
- package/types/DateType.d.ts +5 -5
- package/types/DateType.js +15 -15
- package/types/DecimalType.d.ts +7 -7
- package/types/DecimalType.js +26 -26
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +12 -12
- package/types/EnumArrayType.d.ts +5 -5
- package/types/EnumArrayType.js +24 -24
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +11 -11
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +9 -9
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +9 -9
- package/types/IntervalType.d.ts +4 -4
- package/types/IntervalType.js +12 -12
- package/types/JsonType.d.ts +8 -8
- package/types/JsonType.js +32 -32
- package/types/MediumIntType.d.ts +1 -1
- package/types/MediumIntType.js +3 -3
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +9 -9
- package/types/StringType.d.ts +4 -4
- package/types/StringType.js +12 -12
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +9 -9
- package/types/TimeType.d.ts +5 -5
- package/types/TimeType.js +17 -17
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +10 -10
- package/types/Type.d.ts +79 -83
- package/types/Type.js +82 -82
- package/types/Uint8ArrayType.d.ts +4 -4
- package/types/Uint8ArrayType.js +21 -21
- package/types/UnknownType.d.ts +4 -4
- package/types/UnknownType.js +12 -12
- package/types/UuidType.d.ts +5 -5
- package/types/UuidType.js +19 -19
- package/types/index.d.ts +49 -75
- package/types/index.js +26 -52
- package/typings.d.ts +737 -1250
- package/typings.js +231 -244
- package/unit-of-work/ChangeSet.d.ts +26 -26
- package/unit-of-work/ChangeSet.js +56 -56
- package/unit-of-work/ChangeSetComputer.d.ts +12 -12
- package/unit-of-work/ChangeSetComputer.js +170 -178
- package/unit-of-work/ChangeSetPersister.d.ts +44 -63
- package/unit-of-work/ChangeSetPersister.js +421 -442
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +88 -89
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +141 -181
- package/unit-of-work/UnitOfWork.js +1183 -1200
- package/utils/AbstractMigrator.d.ts +91 -111
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +34 -43
- package/utils/AbstractSchemaGenerator.js +122 -121
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +35 -34
- package/utils/Configuration.d.ts +808 -852
- package/utils/Configuration.js +344 -359
- package/utils/Cursor.d.ts +22 -40
- package/utils/Cursor.js +127 -135
- package/utils/DataloaderUtils.d.ts +43 -58
- package/utils/DataloaderUtils.js +198 -203
- package/utils/EntityComparator.d.ts +81 -98
- package/utils/EntityComparator.js +732 -828
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +51 -79
- package/utils/QueryHelper.js +361 -372
- package/utils/RawQueryFragment.d.ts +34 -50
- package/utils/RawQueryFragment.js +105 -107
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +53 -52
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +197 -199
- package/utils/Utils.d.ts +145 -204
- package/utils/Utils.js +812 -812
- package/utils/clone.js +113 -104
- package/utils/env-vars.js +88 -90
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +181 -180
- package/utils/upsert-utils.d.ts +5 -20
- package/utils/upsert-utils.js +116 -114
package/MikroORM.d.ts
CHANGED
|
@@ -3,16 +3,7 @@ import { type EntitySchema } from './metadata/EntitySchema.js';
|
|
|
3
3
|
import { MetadataStorage } from './metadata/MetadataStorage.js';
|
|
4
4
|
import { Configuration, type Options } from './utils/Configuration.js';
|
|
5
5
|
import type { EntityManager } from './EntityManager.js';
|
|
6
|
-
import type {
|
|
7
|
-
AnyEntity,
|
|
8
|
-
Constructor,
|
|
9
|
-
EntityClass,
|
|
10
|
-
EntityMetadata,
|
|
11
|
-
EntityName,
|
|
12
|
-
IEntityGenerator,
|
|
13
|
-
IMigrator,
|
|
14
|
-
ISeedManager,
|
|
15
|
-
} from './typings.js';
|
|
6
|
+
import type { AnyEntity, Constructor, EntityClass, EntityMetadata, EntityName, IEntityGenerator, IMigrator, ISeedManager } from './typings.js';
|
|
16
7
|
/** @internal */
|
|
17
8
|
export declare function loadOptionalDependencies(options: Partial<Options>): Promise<void>;
|
|
18
9
|
/**
|
|
@@ -43,101 +34,81 @@ export declare function loadOptionalDependencies(options: Partial<Options>): Pro
|
|
|
43
34
|
* await em.flush();
|
|
44
35
|
* ```
|
|
45
36
|
*/
|
|
46
|
-
export declare class MikroORM<
|
|
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
|
-
constructor(options: Partial<Options<Driver, EM, Entities>>);
|
|
85
|
-
/**
|
|
86
|
-
* Connects to the database.
|
|
87
|
-
*/
|
|
88
|
-
connect(): Promise<Driver>;
|
|
89
|
-
/**
|
|
90
|
-
* Reconnects, possibly to a different database.
|
|
91
|
-
*/
|
|
92
|
-
reconnect(options?: Partial<Options<Driver, EM, Entities>>): Promise<void>;
|
|
93
|
-
/**
|
|
94
|
-
* Checks whether the database connection is active.
|
|
95
|
-
*/
|
|
96
|
-
isConnected(): Promise<boolean>;
|
|
97
|
-
/**
|
|
98
|
-
* Checks whether the database connection is active, returns the reason if not.
|
|
99
|
-
*/
|
|
100
|
-
checkConnection(): Promise<
|
|
101
|
-
| {
|
|
37
|
+
export declare class MikroORM<Driver extends IDatabaseDriver = IDatabaseDriver, EM extends Driver[typeof EntityManagerType] & EntityManager<Driver> = Driver[typeof EntityManagerType] & EntityManager<Driver>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]> {
|
|
38
|
+
#private;
|
|
39
|
+
/** The global EntityManager instance. If you are using `RequestContext` helper, it will automatically pick the request specific context under the hood */
|
|
40
|
+
em: EM & {
|
|
41
|
+
'~entities'?: Entities;
|
|
42
|
+
};
|
|
43
|
+
/** The database driver instance used by this ORM. */
|
|
44
|
+
readonly driver: Driver;
|
|
45
|
+
/** The ORM configuration instance. */
|
|
46
|
+
readonly config: Configuration<Driver>;
|
|
47
|
+
/**
|
|
48
|
+
* Initialize the ORM, load entity metadata, create EntityManager and connect to the database.
|
|
49
|
+
* If you omit the `options` parameter, your CLI config will be used.
|
|
50
|
+
*/
|
|
51
|
+
static init<D extends IDatabaseDriver = IDatabaseDriver, EM extends D[typeof EntityManagerType] & EntityManager<D> = D[typeof EntityManagerType] & EntityManager<D>, Entities extends (string | EntityClass<AnyEntity> | EntitySchema)[] = (string | EntityClass<AnyEntity> | EntitySchema)[]>(options: Partial<Options<D, EM, Entities>>): Promise<MikroORM<D, EM, Entities>>;
|
|
52
|
+
/**
|
|
53
|
+
* Synchronous variant of the `init` method with some limitations:
|
|
54
|
+
* - folder-based discovery not supported
|
|
55
|
+
* - ORM extensions are not autoloaded
|
|
56
|
+
* - when metadata cache is enabled, `FileCacheAdapter` needs to be explicitly set in the config
|
|
57
|
+
*/
|
|
58
|
+
constructor(options: Partial<Options<Driver, EM, Entities>>);
|
|
59
|
+
/**
|
|
60
|
+
* Connects to the database.
|
|
61
|
+
*/
|
|
62
|
+
connect(): Promise<Driver>;
|
|
63
|
+
/**
|
|
64
|
+
* Reconnects, possibly to a different database.
|
|
65
|
+
*/
|
|
66
|
+
reconnect(options?: Partial<Options<Driver, EM, Entities>>): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Checks whether the database connection is active.
|
|
69
|
+
*/
|
|
70
|
+
isConnected(): Promise<boolean>;
|
|
71
|
+
/**
|
|
72
|
+
* Checks whether the database connection is active, returns the reason if not.
|
|
73
|
+
*/
|
|
74
|
+
checkConnection(): Promise<{
|
|
102
75
|
ok: true;
|
|
103
|
-
|
|
104
|
-
| {
|
|
76
|
+
} | {
|
|
105
77
|
ok: false;
|
|
106
78
|
reason: string;
|
|
107
79
|
error?: Error;
|
|
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
|
-
get entityGenerator(): IEntityGenerator;
|
|
80
|
+
}>;
|
|
81
|
+
/**
|
|
82
|
+
* Closes the database connection.
|
|
83
|
+
*/
|
|
84
|
+
close(force?: boolean): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the `MetadataStorage`.
|
|
87
|
+
*/
|
|
88
|
+
getMetadata(): MetadataStorage;
|
|
89
|
+
/**
|
|
90
|
+
* Gets the `EntityMetadata` instance when provided with the `entityName` parameter.
|
|
91
|
+
*/
|
|
92
|
+
getMetadata<Entity extends object>(entityName: EntityName<Entity>): EntityMetadata<Entity>;
|
|
93
|
+
private createEntityManager;
|
|
94
|
+
/**
|
|
95
|
+
* Allows dynamically discovering new entity by reference, handy for testing schema diffing.
|
|
96
|
+
*/
|
|
97
|
+
discoverEntity<T extends Constructor | EntitySchema>(entities: T | T[], reset?: EntityName | EntityName[]): void;
|
|
98
|
+
/**
|
|
99
|
+
* Gets the SchemaGenerator.
|
|
100
|
+
*/
|
|
101
|
+
get schema(): ReturnType<ReturnType<Driver['getPlatform']>['getSchemaGenerator']>;
|
|
102
|
+
/**
|
|
103
|
+
* Gets the SeedManager
|
|
104
|
+
*/
|
|
105
|
+
get seeder(): ISeedManager;
|
|
106
|
+
/**
|
|
107
|
+
* Gets the Migrator.
|
|
108
|
+
*/
|
|
109
|
+
get migrator(): IMigrator;
|
|
110
|
+
/**
|
|
111
|
+
* Gets the EntityGenerator.
|
|
112
|
+
*/
|
|
113
|
+
get entityGenerator(): IEntityGenerator;
|
|
143
114
|
}
|
package/MikroORM.js
CHANGED
|
@@ -5,40 +5,41 @@ import { loadEnvironmentVars } from './utils/env-vars.js';
|
|
|
5
5
|
import { Utils } from './utils/Utils.js';
|
|
6
6
|
import { colors } from './logging/colors.js';
|
|
7
7
|
async function tryRegisterExtension(name, pkg, extensions) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
try {
|
|
9
|
+
const url = import.meta.resolve(pkg);
|
|
10
|
+
const mod = await import(url);
|
|
11
|
+
if (mod[name]) {
|
|
12
|
+
extensions.push(mod[name]);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// not installed
|
|
17
|
+
}
|
|
17
18
|
}
|
|
18
19
|
/** @internal */
|
|
19
20
|
export async function loadOptionalDependencies(options) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
21
|
+
await import('@mikro-orm/core/fs-utils').then(m => m.fs.init()).catch(() => null);
|
|
22
|
+
const extensions = options.extensions ?? [];
|
|
23
|
+
const exists = (name) => extensions.some(ext => ext.name === name);
|
|
24
|
+
if (!exists('SeedManager')) {
|
|
25
|
+
await tryRegisterExtension('SeedManager', '@mikro-orm/seeder', extensions);
|
|
26
|
+
}
|
|
27
|
+
if (!exists('Migrator')) {
|
|
28
|
+
await tryRegisterExtension('Migrator', '@mikro-orm/migrations', extensions);
|
|
29
|
+
}
|
|
30
|
+
/* v8 ignore if */
|
|
31
|
+
if (!exists('Migrator')) {
|
|
32
|
+
await tryRegisterExtension('Migrator', '@mikro-orm/migrations-mongodb', extensions);
|
|
33
|
+
}
|
|
34
|
+
if (!exists('EntityGenerator')) {
|
|
35
|
+
await tryRegisterExtension('EntityGenerator', '@mikro-orm/entity-generator', extensions);
|
|
36
|
+
}
|
|
37
|
+
options.extensions = extensions;
|
|
38
|
+
const metadataCacheEnabled = options.metadataCache?.enabled || options.metadataProvider?.useCache?.();
|
|
39
|
+
if (metadataCacheEnabled) {
|
|
40
|
+
options.metadataCache ??= {};
|
|
41
|
+
options.metadataCache.adapter ??= await import('@mikro-orm/core/fs-utils').then(m => m.FileCacheAdapter);
|
|
42
|
+
}
|
|
42
43
|
}
|
|
43
44
|
/**
|
|
44
45
|
* The main class used to configure and bootstrap the ORM.
|
|
@@ -69,151 +70,151 @@ export async function loadOptionalDependencies(options) {
|
|
|
69
70
|
* ```
|
|
70
71
|
*/
|
|
71
72
|
export class MikroORM {
|
|
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
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
73
|
+
/** The global EntityManager instance. If you are using `RequestContext` helper, it will automatically pick the request specific context under the hood */
|
|
74
|
+
em;
|
|
75
|
+
/** The database driver instance used by this ORM. */
|
|
76
|
+
driver;
|
|
77
|
+
/** The ORM configuration instance. */
|
|
78
|
+
config;
|
|
79
|
+
#metadata;
|
|
80
|
+
#logger;
|
|
81
|
+
#discovery;
|
|
82
|
+
/**
|
|
83
|
+
* Initialize the ORM, load entity metadata, create EntityManager and connect to the database.
|
|
84
|
+
* If you omit the `options` parameter, your CLI config will be used.
|
|
85
|
+
*/
|
|
86
|
+
static async init(options) {
|
|
87
|
+
/* v8 ignore next */
|
|
88
|
+
if (!options) {
|
|
89
|
+
throw new Error(`options parameter is required`);
|
|
90
|
+
}
|
|
91
|
+
options = { ...options };
|
|
92
|
+
options.discovery ??= {};
|
|
93
|
+
options.discovery.skipSyncDiscovery ??= true;
|
|
94
|
+
await loadOptionalDependencies(options);
|
|
95
|
+
const orm = new this(options);
|
|
96
|
+
const preferTs = orm.config.get('preferTs', Utils.detectTypeScriptSupport());
|
|
97
|
+
orm.#metadata = await orm.#discovery.discover(preferTs);
|
|
98
|
+
orm.createEntityManager();
|
|
99
|
+
return orm;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Synchronous variant of the `init` method with some limitations:
|
|
103
|
+
* - folder-based discovery not supported
|
|
104
|
+
* - ORM extensions are not autoloaded
|
|
105
|
+
* - when metadata cache is enabled, `FileCacheAdapter` needs to be explicitly set in the config
|
|
106
|
+
*/
|
|
107
|
+
constructor(options) {
|
|
108
|
+
const env = loadEnvironmentVars();
|
|
109
|
+
options = options.preferEnvVars ? Utils.merge(options, env) : Utils.merge(env, options);
|
|
110
|
+
this.config = new Configuration(options);
|
|
111
|
+
const discovery = this.config.get('discovery');
|
|
112
|
+
this.driver = this.config.getDriver();
|
|
113
|
+
this.#logger = this.config.getLogger();
|
|
114
|
+
this.#logger.log('info', `MikroORM version: ${colors.green(Utils.getORMVersion())}`);
|
|
115
|
+
this.#discovery = new MetadataDiscovery(new MetadataStorage(), this.driver.getPlatform(), this.config);
|
|
116
|
+
this.driver.getPlatform().init(this);
|
|
117
|
+
for (const extension of this.config.get('extensions')) {
|
|
118
|
+
extension.register(this);
|
|
119
|
+
}
|
|
120
|
+
if (!discovery.skipSyncDiscovery) {
|
|
121
|
+
this.#metadata = this.#discovery.discoverSync();
|
|
122
|
+
this.createEntityManager();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Connects to the database.
|
|
127
|
+
*/
|
|
128
|
+
async connect() {
|
|
129
|
+
await this.driver.connect();
|
|
130
|
+
return this.driver;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Reconnects, possibly to a different database.
|
|
134
|
+
*/
|
|
135
|
+
async reconnect(options = {}) {
|
|
136
|
+
/* v8 ignore next */
|
|
137
|
+
for (const key of Utils.keys(options)) {
|
|
138
|
+
this.config.set(key, options[key]);
|
|
139
|
+
}
|
|
140
|
+
await this.driver.reconnect();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Checks whether the database connection is active.
|
|
144
|
+
*/
|
|
145
|
+
async isConnected() {
|
|
146
|
+
return this.driver.getConnection().isConnected();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Checks whether the database connection is active, returns the reason if not.
|
|
150
|
+
*/
|
|
151
|
+
async checkConnection() {
|
|
152
|
+
return this.driver.getConnection().checkConnection();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Closes the database connection.
|
|
156
|
+
*/
|
|
157
|
+
async close(force = false) {
|
|
158
|
+
await this.driver.close(force);
|
|
159
|
+
await this.config.getMetadataCacheAdapter()?.close?.();
|
|
160
|
+
await this.config.getResultCacheAdapter()?.close?.();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets the `MetadataStorage` (without parameters) or `EntityMetadata` instance when provided with the `entityName` parameter.
|
|
164
|
+
*/
|
|
165
|
+
getMetadata(entityName) {
|
|
166
|
+
if (entityName) {
|
|
167
|
+
return this.#metadata.get(entityName);
|
|
168
|
+
}
|
|
169
|
+
return this.#metadata;
|
|
170
|
+
}
|
|
171
|
+
createEntityManager() {
|
|
172
|
+
this.driver.setMetadata(this.#metadata);
|
|
173
|
+
this.em = this.driver.createEntityManager();
|
|
174
|
+
this.em.global = true;
|
|
175
|
+
this.#metadata.decorate(this.em);
|
|
176
|
+
this.driver.setMetadata(this.#metadata);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Allows dynamically discovering new entity by reference, handy for testing schema diffing.
|
|
180
|
+
*/
|
|
181
|
+
discoverEntity(entities, reset) {
|
|
182
|
+
for (const className of Utils.asArray(reset)) {
|
|
183
|
+
this.#metadata.reset(className);
|
|
184
|
+
this.#discovery.reset(className);
|
|
185
|
+
}
|
|
186
|
+
const tmp = this.#discovery.discoverReferences(Utils.asArray(entities));
|
|
187
|
+
const metadata = this.#discovery.processDiscoveredEntities(tmp);
|
|
188
|
+
for (const meta of metadata) {
|
|
189
|
+
this.#metadata.set(meta.class, meta);
|
|
190
|
+
meta.root = this.#metadata.get(meta.root.class);
|
|
191
|
+
}
|
|
192
|
+
this.#metadata.decorate(this.em);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Gets the SchemaGenerator.
|
|
196
|
+
*/
|
|
197
|
+
get schema() {
|
|
198
|
+
return this.config.getExtension('@mikro-orm/schema-generator');
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Gets the SeedManager
|
|
202
|
+
*/
|
|
203
|
+
get seeder() {
|
|
204
|
+
return this.driver.getPlatform().getExtension('SeedManager', '@mikro-orm/seeder', '@mikro-orm/seeder', this.em);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Gets the Migrator.
|
|
208
|
+
*/
|
|
209
|
+
get migrator() {
|
|
210
|
+
return this.driver.getPlatform().getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations', this.em);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Gets the EntityGenerator.
|
|
214
|
+
*/
|
|
215
|
+
get entityGenerator() {
|
|
216
|
+
return this.driver
|
|
217
|
+
.getPlatform()
|
|
218
|
+
.getExtension('EntityGenerator', '@mikro-orm/entity-generator', '@mikro-orm/entity-generator', this.em);
|
|
219
|
+
}
|
|
219
220
|
}
|
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ const author = await em.findOneOrFail(Author, 1, {
|
|
|
133
133
|
populate: ['books'],
|
|
134
134
|
});
|
|
135
135
|
author.name = 'Jon Snow II';
|
|
136
|
-
author.books.getItems().forEach(book =>
|
|
136
|
+
author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
|
|
137
137
|
author.books.add(orm.em.create(Book, { title: 'New Book', author }));
|
|
138
138
|
|
|
139
139
|
// Flush computes change sets and executes them in a single transaction
|