@mikro-orm/core 7.0.0-dev.32 → 7.0.0-dev.321
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 +71 -63
- package/EntityManager.js +365 -283
- package/MikroORM.d.ts +44 -35
- package/MikroORM.js +109 -142
- package/README.md +7 -4
- package/cache/FileCacheAdapter.d.ts +1 -2
- package/cache/FileCacheAdapter.js +19 -14
- package/cache/GeneratedCacheAdapter.d.ts +0 -1
- package/cache/GeneratedCacheAdapter.js +0 -2
- package/cache/index.d.ts +1 -2
- package/cache/index.js +0 -2
- package/connections/Connection.d.ts +12 -5
- package/connections/Connection.js +37 -15
- package/drivers/DatabaseDriver.d.ts +25 -18
- package/drivers/DatabaseDriver.js +144 -45
- package/drivers/IDatabaseDriver.d.ts +118 -23
- package/entity/BaseEntity.d.ts +63 -4
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +101 -29
- package/entity/Collection.js +473 -115
- package/entity/EntityAssigner.js +37 -25
- package/entity/EntityFactory.d.ts +7 -1
- package/entity/EntityFactory.js +116 -64
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +69 -27
- package/entity/EntityLoader.d.ts +11 -10
- package/entity/EntityLoader.js +264 -102
- package/entity/EntityRepository.d.ts +28 -8
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +2 -6
- package/entity/Reference.js +52 -19
- package/entity/WrappedEntity.d.ts +3 -8
- package/entity/WrappedEntity.js +6 -7
- package/entity/defineEntity.d.ts +525 -311
- package/entity/defineEntity.js +134 -290
- package/entity/index.d.ts +2 -2
- package/entity/index.js +2 -2
- package/entity/utils.d.ts +6 -1
- package/entity/utils.js +46 -11
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +66 -0
- package/enums.d.ts +8 -6
- package/enums.js +13 -17
- package/errors.d.ts +20 -10
- package/errors.js +63 -31
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +24 -13
- package/events/index.d.ts +1 -1
- package/events/index.js +0 -1
- package/exceptions.js +9 -2
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +105 -46
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +3 -4
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +5 -7
- package/logging/index.d.ts +2 -1
- package/logging/index.js +1 -1
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +47 -23
- package/metadata/EntitySchema.js +103 -34
- package/metadata/MetadataDiscovery.d.ts +64 -9
- package/metadata/MetadataDiscovery.js +867 -354
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +71 -2
- package/metadata/MetadataStorage.d.ts +13 -11
- package/metadata/MetadataStorage.js +72 -41
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +214 -44
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +0 -1
- package/metadata/types.d.ts +577 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +26 -5
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/naming-strategy/index.d.ts +1 -1
- package/naming-strategy/index.js +0 -1
- package/not-supported.d.ts +2 -0
- package/not-supported.js +8 -0
- package/package.json +47 -36
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +33 -15
- package/platforms/Platform.js +125 -69
- package/serialization/EntitySerializer.d.ts +6 -3
- package/serialization/EntitySerializer.js +53 -29
- package/serialization/EntityTransformer.js +33 -21
- package/serialization/SerializationContext.d.ts +6 -6
- package/serialization/SerializationContext.js +4 -4
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.js +2 -2
- package/types/DoubleType.js +1 -1
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/UuidType.d.ts +2 -0
- package/types/UuidType.js +14 -2
- package/types/index.d.ts +3 -2
- package/typings.d.ts +427 -170
- package/typings.js +100 -45
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +8 -9
- package/unit-of-work/ChangeSetComputer.d.ts +3 -8
- package/unit-of-work/ChangeSetComputer.js +49 -26
- package/unit-of-work/ChangeSetPersister.d.ts +13 -12
- package/unit-of-work/ChangeSetPersister.js +107 -44
- package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
- package/unit-of-work/CommitOrderCalculator.js +17 -15
- package/unit-of-work/IdentityMap.d.ts +12 -0
- package/unit-of-work/IdentityMap.js +39 -1
- package/unit-of-work/UnitOfWork.d.ts +34 -4
- package/unit-of-work/UnitOfWork.js +294 -107
- package/utils/AbstractMigrator.d.ts +101 -0
- package/utils/AbstractMigrator.js +303 -0
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +30 -18
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +796 -211
- package/utils/Configuration.js +160 -197
- package/utils/ConfigurationLoader.d.ts +1 -52
- package/utils/ConfigurationLoader.js +1 -330
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +29 -14
- package/utils/DataloaderUtils.d.ts +10 -5
- package/utils/DataloaderUtils.js +42 -22
- package/utils/EntityComparator.d.ts +16 -9
- package/utils/EntityComparator.js +202 -96
- package/utils/QueryHelper.d.ts +34 -7
- package/utils/QueryHelper.js +183 -72
- package/utils/RawQueryFragment.d.ts +28 -34
- package/utils/RawQueryFragment.js +37 -72
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.js +11 -7
- package/utils/Utils.d.ts +16 -127
- package/utils/Utils.js +106 -401
- package/utils/clone.js +13 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +98 -0
- package/utils/fs-utils.d.ts +34 -0
- package/utils/fs-utils.js +193 -0
- package/utils/index.d.ts +1 -3
- package/utils/index.js +1 -3
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +51 -5
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -12
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -33
- package/decorators/Entity.js +0 -12
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -4
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -19
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -42
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -34
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -28
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -8
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -14
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -407
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
package/MikroORM.d.ts
CHANGED
|
@@ -3,13 +3,42 @@ 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 { Constructor, EntityMetadata, EntityName, IEntityGenerator, IMigrator, ISeedManager } from './typings.js';
|
|
6
|
+
import type { AnyEntity, Constructor, EntityClass, EntityMetadata, EntityName, IEntityGenerator, IMigrator, ISeedManager } from './typings.js';
|
|
7
|
+
/** @internal */
|
|
8
|
+
export declare function loadOptionalDependencies(options: Options): Promise<void>;
|
|
7
9
|
/**
|
|
8
|
-
*
|
|
10
|
+
* The main class used to configure and bootstrap the ORM.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // import from driver package
|
|
15
|
+
* import { MikroORM, defineEntity, p } from '@mikro-orm/sqlite';
|
|
16
|
+
*
|
|
17
|
+
* const User = defineEntity({
|
|
18
|
+
* name: 'User',
|
|
19
|
+
* properties: {
|
|
20
|
+
* id: p.integer().primary(),
|
|
21
|
+
* name: p.string(),
|
|
22
|
+
* },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const orm = new MikroORM({
|
|
26
|
+
* entities: [User],
|
|
27
|
+
* dbName: 'my.db',
|
|
28
|
+
* });
|
|
29
|
+
* await orm.schema.update();
|
|
30
|
+
*
|
|
31
|
+
* const em = orm.em.fork();
|
|
32
|
+
* const u1 = em.create(User, { name: 'John' });
|
|
33
|
+
* const u2 = em.create(User, { name: 'Ben' });
|
|
34
|
+
* await em.flush();
|
|
35
|
+
* ```
|
|
9
36
|
*/
|
|
10
|
-
export declare class MikroORM<Driver extends IDatabaseDriver = IDatabaseDriver, EM extends EntityManager = Driver[typeof EntityManagerType] & EntityManager> {
|
|
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)[]> {
|
|
11
38
|
/** The global EntityManager instance. If you are using `RequestContext` helper, it will automatically pick the request specific context under the hood */
|
|
12
|
-
em: EM
|
|
39
|
+
em: EM & {
|
|
40
|
+
'~entities'?: Entities;
|
|
41
|
+
};
|
|
13
42
|
readonly driver: Driver;
|
|
14
43
|
readonly config: Configuration<Driver>;
|
|
15
44
|
private metadata;
|
|
@@ -19,16 +48,14 @@ export declare class MikroORM<Driver extends IDatabaseDriver = IDatabaseDriver,
|
|
|
19
48
|
* Initialize the ORM, load entity metadata, create EntityManager and connect to the database.
|
|
20
49
|
* If you omit the `options` parameter, your CLI config will be used.
|
|
21
50
|
*/
|
|
22
|
-
static init<D extends IDatabaseDriver = IDatabaseDriver, EM extends EntityManager = D[typeof EntityManagerType] & EntityManager>(options
|
|
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: Options<D, EM, Entities>): Promise<MikroORM<D, EM, Entities>>;
|
|
23
52
|
/**
|
|
24
53
|
* Synchronous variant of the `init` method with some limitations:
|
|
25
|
-
* -
|
|
26
|
-
* -
|
|
27
|
-
* -
|
|
28
|
-
* - no check for mismatched package versions
|
|
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
|
|
29
57
|
*/
|
|
30
|
-
|
|
31
|
-
constructor(options: Options<Driver, EM>);
|
|
58
|
+
constructor(options: Options<Driver, EM, Entities>);
|
|
32
59
|
/**
|
|
33
60
|
* Connects to the database.
|
|
34
61
|
*/
|
|
@@ -36,13 +63,13 @@ export declare class MikroORM<Driver extends IDatabaseDriver = IDatabaseDriver,
|
|
|
36
63
|
/**
|
|
37
64
|
* Reconnects, possibly to a different database.
|
|
38
65
|
*/
|
|
39
|
-
reconnect(options?: Options): Promise<void>;
|
|
66
|
+
reconnect(options?: Partial<Options<Driver, EM, Entities>>): Promise<void>;
|
|
40
67
|
/**
|
|
41
68
|
* Checks whether the database connection is active.
|
|
42
69
|
*/
|
|
43
70
|
isConnected(): Promise<boolean>;
|
|
44
71
|
/**
|
|
45
|
-
* Checks whether the database connection is active, returns .
|
|
72
|
+
* Checks whether the database connection is active, returns the reason if not.
|
|
46
73
|
*/
|
|
47
74
|
checkConnection(): Promise<{
|
|
48
75
|
ok: true;
|
|
@@ -63,43 +90,25 @@ export declare class MikroORM<Driver extends IDatabaseDriver = IDatabaseDriver,
|
|
|
63
90
|
* Gets the `EntityMetadata` instance when provided with the `entityName` parameter.
|
|
64
91
|
*/
|
|
65
92
|
getMetadata<Entity extends object>(entityName: EntityName<Entity>): EntityMetadata<Entity>;
|
|
66
|
-
discoverEntities(): Promise<void>;
|
|
67
|
-
discoverEntitiesSync(): void;
|
|
68
93
|
private createEntityManager;
|
|
69
94
|
/**
|
|
70
95
|
* Allows dynamically discovering new entity by reference, handy for testing schema diffing.
|
|
71
96
|
*/
|
|
72
|
-
discoverEntity<T extends Constructor | EntitySchema>(entities: T | T[], reset?:
|
|
97
|
+
discoverEntity<T extends Constructor | EntitySchema>(entities: T | T[], reset?: EntityName | EntityName[]): void;
|
|
73
98
|
/**
|
|
74
99
|
* Gets the SchemaGenerator.
|
|
75
100
|
*/
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Gets the EntityGenerator.
|
|
79
|
-
*/
|
|
80
|
-
getEntityGenerator<T extends IEntityGenerator = IEntityGenerator>(): T;
|
|
81
|
-
/**
|
|
82
|
-
* Gets the Migrator.
|
|
83
|
-
*/
|
|
84
|
-
getMigrator<T extends IMigrator = IMigrator>(): T;
|
|
101
|
+
get schema(): ReturnType<ReturnType<Driver['getPlatform']>['getSchemaGenerator']>;
|
|
85
102
|
/**
|
|
86
103
|
* Gets the SeedManager
|
|
87
104
|
*/
|
|
88
|
-
getSeeder<T extends ISeedManager = ISeedManager>(): T;
|
|
89
|
-
/**
|
|
90
|
-
* Shortcut for `orm.getSchemaGenerator()`
|
|
91
|
-
*/
|
|
92
|
-
get schema(): ReturnType<ReturnType<Driver["getPlatform"]>["getSchemaGenerator"]>;
|
|
93
|
-
/**
|
|
94
|
-
* Shortcut for `orm.getSeeder()`
|
|
95
|
-
*/
|
|
96
105
|
get seeder(): ISeedManager;
|
|
97
106
|
/**
|
|
98
|
-
*
|
|
107
|
+
* Gets the Migrator.
|
|
99
108
|
*/
|
|
100
109
|
get migrator(): IMigrator;
|
|
101
110
|
/**
|
|
102
|
-
*
|
|
111
|
+
* Gets the EntityGenerator.
|
|
103
112
|
*/
|
|
104
113
|
get entityGenerator(): IEntityGenerator;
|
|
105
114
|
}
|
package/MikroORM.js
CHANGED
|
@@ -1,14 +1,73 @@
|
|
|
1
1
|
import { MetadataDiscovery } from './metadata/MetadataDiscovery.js';
|
|
2
2
|
import { MetadataStorage } from './metadata/MetadataStorage.js';
|
|
3
|
-
import { MetadataValidator } from './metadata/MetadataValidator.js';
|
|
4
|
-
import { ReflectMetadataProvider } from './metadata/ReflectMetadataProvider.js';
|
|
5
3
|
import { Configuration } from './utils/Configuration.js';
|
|
6
|
-
import {
|
|
4
|
+
import { loadEnvironmentVars } from './utils/env-vars.js';
|
|
7
5
|
import { Utils } from './utils/Utils.js';
|
|
8
6
|
import { colors } from './logging/colors.js';
|
|
9
|
-
|
|
7
|
+
async function tryRegisterExtension(name, pkg, extensions) {
|
|
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
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** @internal */
|
|
20
|
+
export async function loadOptionalDependencies(options) {
|
|
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
|
+
}
|
|
43
|
+
}
|
|
10
44
|
/**
|
|
11
|
-
*
|
|
45
|
+
* The main class used to configure and bootstrap the ORM.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // import from driver package
|
|
50
|
+
* import { MikroORM, defineEntity, p } from '@mikro-orm/sqlite';
|
|
51
|
+
*
|
|
52
|
+
* const User = defineEntity({
|
|
53
|
+
* name: 'User',
|
|
54
|
+
* properties: {
|
|
55
|
+
* id: p.integer().primary(),
|
|
56
|
+
* name: p.string(),
|
|
57
|
+
* },
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* const orm = new MikroORM({
|
|
61
|
+
* entities: [User],
|
|
62
|
+
* dbName: 'my.db',
|
|
63
|
+
* });
|
|
64
|
+
* await orm.schema.update();
|
|
65
|
+
*
|
|
66
|
+
* const em = orm.em.fork();
|
|
67
|
+
* const u1 = em.create(User, { name: 'John' });
|
|
68
|
+
* const u2 = em.create(User, { name: 'Ben' });
|
|
69
|
+
* await em.flush();
|
|
70
|
+
* ```
|
|
12
71
|
*/
|
|
13
72
|
export class MikroORM {
|
|
14
73
|
/** The global EntityManager instance. If you are using `RequestContext` helper, it will automatically pick the request specific context under the hood */
|
|
@@ -23,103 +82,56 @@ export class MikroORM {
|
|
|
23
82
|
* If you omit the `options` parameter, your CLI config will be used.
|
|
24
83
|
*/
|
|
25
84
|
static async init(options) {
|
|
26
|
-
|
|
27
|
-
const coreVersion = ConfigurationLoader.checkPackageVersion();
|
|
28
|
-
const env = await ConfigurationLoader.loadEnvironmentVars();
|
|
85
|
+
/* v8 ignore next */
|
|
29
86
|
if (!options) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
const orm = new MikroORM(options);
|
|
42
|
-
orm.logger.log('info', `MikroORM version: ${colors.green(coreVersion)}`);
|
|
43
|
-
// we need to allow global context here as we are not in a scope of requests yet
|
|
44
|
-
const allowGlobalContext = orm.config.get('allowGlobalContext');
|
|
45
|
-
orm.config.set('allowGlobalContext', true);
|
|
46
|
-
await orm.discoverEntities();
|
|
47
|
-
orm.config.set('allowGlobalContext', allowGlobalContext);
|
|
48
|
-
orm.driver.getPlatform().init(orm);
|
|
49
|
-
if (orm.config.get('connect')) {
|
|
50
|
-
await orm.connect();
|
|
51
|
-
}
|
|
52
|
-
for (const extension of orm.config.get('extensions')) {
|
|
53
|
-
extension.register(orm);
|
|
54
|
-
}
|
|
55
|
-
if (orm.config.get('connect') && orm.config.get('ensureIndexes')) {
|
|
56
|
-
await orm.getSchemaGenerator().ensureIndexes();
|
|
57
|
-
}
|
|
87
|
+
throw new Error(`options parameter is required`);
|
|
88
|
+
}
|
|
89
|
+
options = { ...options };
|
|
90
|
+
options.discovery ??= {};
|
|
91
|
+
options.discovery.skipSyncDiscovery ??= true;
|
|
92
|
+
await loadOptionalDependencies(options);
|
|
93
|
+
const orm = new this(options);
|
|
94
|
+
const preferTs = orm.config.get('preferTs', Utils.detectTypeScriptSupport());
|
|
95
|
+
orm.metadata = await orm.discovery.discover(preferTs);
|
|
96
|
+
orm.createEntityManager();
|
|
58
97
|
return orm;
|
|
59
98
|
}
|
|
60
99
|
/**
|
|
61
100
|
* Synchronous variant of the `init` method with some limitations:
|
|
62
|
-
* -
|
|
63
|
-
* -
|
|
64
|
-
* -
|
|
65
|
-
* - no check for mismatched package versions
|
|
101
|
+
* - folder-based discovery not supported
|
|
102
|
+
* - ORM extensions are not autoloaded
|
|
103
|
+
* - when metadata cache is enabled, `FileCacheAdapter` needs to be explicitly set in the config
|
|
66
104
|
*/
|
|
67
|
-
static initSync(options) {
|
|
68
|
-
ConfigurationLoader.registerDotenv(options);
|
|
69
|
-
const env = ConfigurationLoader.loadEnvironmentVarsSync();
|
|
70
|
-
options = Utils.merge(options, env);
|
|
71
|
-
if ('DRIVER' in this && !options.driver) {
|
|
72
|
-
options.driver = this.DRIVER;
|
|
73
|
-
}
|
|
74
|
-
const orm = new MikroORM(options);
|
|
75
|
-
// we need to allow global context here as we are not in a scope of requests yet
|
|
76
|
-
const allowGlobalContext = orm.config.get('allowGlobalContext');
|
|
77
|
-
orm.config.set('allowGlobalContext', true);
|
|
78
|
-
orm.discoverEntitiesSync();
|
|
79
|
-
orm.config.set('allowGlobalContext', allowGlobalContext);
|
|
80
|
-
orm.driver.getPlatform().init(orm);
|
|
81
|
-
for (const extension of orm.config.get('extensions')) {
|
|
82
|
-
extension.register(orm);
|
|
83
|
-
}
|
|
84
|
-
return orm;
|
|
85
|
-
}
|
|
86
105
|
constructor(options) {
|
|
106
|
+
const env = loadEnvironmentVars();
|
|
107
|
+
options = options.preferEnvVars ? Utils.merge(options, env) : Utils.merge(env, options);
|
|
87
108
|
this.config = new Configuration(options);
|
|
88
109
|
const discovery = this.config.get('discovery');
|
|
89
|
-
if (discovery.disableDynamicFileAccess) {
|
|
90
|
-
this.config.set('metadataProvider', ReflectMetadataProvider);
|
|
91
|
-
this.config.set('metadataCache', { adapter: NullCacheAdapter });
|
|
92
|
-
discovery.requireEntitiesArray = true;
|
|
93
|
-
}
|
|
94
110
|
this.driver = this.config.getDriver();
|
|
95
111
|
this.logger = this.config.getLogger();
|
|
112
|
+
this.logger.log('info', `MikroORM version: ${colors.green(Utils.getORMVersion())}`);
|
|
96
113
|
this.discovery = new MetadataDiscovery(new MetadataStorage(), this.driver.getPlatform(), this.config);
|
|
114
|
+
this.driver.getPlatform().init(this);
|
|
115
|
+
for (const extension of this.config.get('extensions')) {
|
|
116
|
+
extension.register(this);
|
|
117
|
+
}
|
|
118
|
+
if (!discovery.skipSyncDiscovery) {
|
|
119
|
+
this.metadata = this.discovery.discoverSync();
|
|
120
|
+
this.createEntityManager();
|
|
121
|
+
}
|
|
97
122
|
}
|
|
98
123
|
/**
|
|
99
124
|
* Connects to the database.
|
|
100
125
|
*/
|
|
101
126
|
async connect() {
|
|
102
|
-
|
|
103
|
-
const clientUrl = connection.getClientUrl();
|
|
104
|
-
const dbName = this.config.get('dbName');
|
|
105
|
-
const db = dbName + (clientUrl ? ' on ' + clientUrl : '');
|
|
106
|
-
if (this.config.get('ensureDatabase')) {
|
|
107
|
-
const options = this.config.get('ensureDatabase');
|
|
108
|
-
await this.schema.ensureDatabase(typeof options === 'boolean' ? {} : { ...options, forceCheck: true });
|
|
109
|
-
}
|
|
110
|
-
if (await this.isConnected()) {
|
|
111
|
-
this.logger.log('info', `MikroORM successfully connected to database ${colors.green(db)}`);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
this.logger.error('info', `MikroORM failed to connect to database ${db}`);
|
|
115
|
-
}
|
|
127
|
+
await this.driver.connect();
|
|
116
128
|
return this.driver;
|
|
117
129
|
}
|
|
118
130
|
/**
|
|
119
131
|
* Reconnects, possibly to a different database.
|
|
120
132
|
*/
|
|
121
133
|
async reconnect(options = {}) {
|
|
122
|
-
/* v8 ignore next
|
|
134
|
+
/* v8 ignore next */
|
|
123
135
|
for (const key of Utils.keys(options)) {
|
|
124
136
|
this.config.set(key, options[key]);
|
|
125
137
|
}
|
|
@@ -132,7 +144,7 @@ export class MikroORM {
|
|
|
132
144
|
return this.driver.getConnection().isConnected();
|
|
133
145
|
}
|
|
134
146
|
/**
|
|
135
|
-
* Checks whether the database connection is active, returns .
|
|
147
|
+
* Checks whether the database connection is active, returns the reason if not.
|
|
136
148
|
*/
|
|
137
149
|
async checkConnection() {
|
|
138
150
|
return this.driver.getConnection().checkConnection();
|
|
@@ -141,34 +153,19 @@ export class MikroORM {
|
|
|
141
153
|
* Closes the database connection.
|
|
142
154
|
*/
|
|
143
155
|
async close(force = false) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (this.config.getMetadataCacheAdapter()?.close) {
|
|
148
|
-
await this.config.getMetadataCacheAdapter().close();
|
|
149
|
-
}
|
|
150
|
-
if (this.config.getResultCacheAdapter()?.close) {
|
|
151
|
-
await this.config.getResultCacheAdapter().close();
|
|
152
|
-
}
|
|
156
|
+
await this.driver.close(force);
|
|
157
|
+
await this.config.getMetadataCacheAdapter()?.close?.();
|
|
158
|
+
await this.config.getResultCacheAdapter()?.close?.();
|
|
153
159
|
}
|
|
154
160
|
/**
|
|
155
161
|
* Gets the `MetadataStorage` (without parameters) or `EntityMetadata` instance when provided with the `entityName` parameter.
|
|
156
162
|
*/
|
|
157
163
|
getMetadata(entityName) {
|
|
158
164
|
if (entityName) {
|
|
159
|
-
entityName = Utils.className(entityName);
|
|
160
165
|
return this.metadata.get(entityName);
|
|
161
166
|
}
|
|
162
167
|
return this.metadata;
|
|
163
168
|
}
|
|
164
|
-
async discoverEntities() {
|
|
165
|
-
this.metadata = await this.discovery.discover(this.config.get('preferTs'));
|
|
166
|
-
this.createEntityManager();
|
|
167
|
-
}
|
|
168
|
-
discoverEntitiesSync() {
|
|
169
|
-
this.metadata = this.discovery.discoverSync(this.config.get('preferTs'));
|
|
170
|
-
this.createEntityManager();
|
|
171
|
-
}
|
|
172
169
|
createEntityManager() {
|
|
173
170
|
this.driver.setMetadata(this.metadata);
|
|
174
171
|
this.em = this.driver.createEntityManager();
|
|
@@ -180,72 +177,42 @@ export class MikroORM {
|
|
|
180
177
|
* Allows dynamically discovering new entity by reference, handy for testing schema diffing.
|
|
181
178
|
*/
|
|
182
179
|
discoverEntity(entities, reset) {
|
|
183
|
-
entities = Utils.asArray(entities);
|
|
184
180
|
for (const className of Utils.asArray(reset)) {
|
|
185
181
|
this.metadata.reset(className);
|
|
186
182
|
this.discovery.reset(className);
|
|
187
183
|
}
|
|
188
|
-
const tmp = this.discovery.discoverReferences(entities);
|
|
189
|
-
const options = this.config.get('discovery');
|
|
190
|
-
new MetadataValidator().validateDiscovered([...Object.values(this.metadata.getAll()), ...tmp], options);
|
|
184
|
+
const tmp = this.discovery.discoverReferences(Utils.asArray(entities));
|
|
191
185
|
const metadata = this.discovery.processDiscoveredEntities(tmp);
|
|
192
|
-
|
|
193
|
-
this.metadata.set(meta.
|
|
194
|
-
meta.root = this.metadata.get(meta.root.
|
|
195
|
-
}
|
|
186
|
+
for (const meta of metadata) {
|
|
187
|
+
this.metadata.set(meta.class, meta);
|
|
188
|
+
meta.root = this.metadata.get(meta.root.class);
|
|
189
|
+
}
|
|
196
190
|
this.metadata.decorate(this.em);
|
|
197
191
|
}
|
|
198
192
|
/**
|
|
199
193
|
* Gets the SchemaGenerator.
|
|
200
194
|
*/
|
|
201
|
-
getSchemaGenerator() {
|
|
202
|
-
const extension = this.config.getExtension('@mikro-orm/schema-generator');
|
|
203
|
-
if (extension) {
|
|
204
|
-
return extension;
|
|
205
|
-
}
|
|
206
|
-
/* v8 ignore next 2 */
|
|
207
|
-
throw new Error(`SchemaGenerator extension not registered.`);
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* Gets the EntityGenerator.
|
|
211
|
-
*/
|
|
212
|
-
getEntityGenerator() {
|
|
213
|
-
return this.driver.getPlatform().getExtension('EntityGenerator', '@mikro-orm/entity-generator', '@mikro-orm/entity-generator', this.em);
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Gets the Migrator.
|
|
217
|
-
*/
|
|
218
|
-
getMigrator() {
|
|
219
|
-
return this.driver.getPlatform().getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations', this.em);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Gets the SeedManager
|
|
223
|
-
*/
|
|
224
|
-
getSeeder() {
|
|
225
|
-
return this.driver.getPlatform().getExtension('SeedManager', '@mikro-orm/seeder', '@mikro-orm/seeder', this.em);
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Shortcut for `orm.getSchemaGenerator()`
|
|
229
|
-
*/
|
|
230
195
|
get schema() {
|
|
231
|
-
return this.
|
|
196
|
+
return this.config.getExtension('@mikro-orm/schema-generator');
|
|
232
197
|
}
|
|
233
198
|
/**
|
|
234
|
-
*
|
|
199
|
+
* Gets the SeedManager
|
|
235
200
|
*/
|
|
236
201
|
get seeder() {
|
|
237
|
-
return this.
|
|
202
|
+
return this.driver.getPlatform().getExtension('SeedManager', '@mikro-orm/seeder', '@mikro-orm/seeder', this.em);
|
|
238
203
|
}
|
|
239
204
|
/**
|
|
240
|
-
*
|
|
205
|
+
* Gets the Migrator.
|
|
241
206
|
*/
|
|
242
207
|
get migrator() {
|
|
243
|
-
return this.
|
|
208
|
+
return this.driver.getPlatform().getExtension('Migrator', '@mikro-orm/migrator', '@mikro-orm/migrations', this.em);
|
|
244
209
|
}
|
|
245
210
|
/**
|
|
246
|
-
*
|
|
211
|
+
* Gets the EntityGenerator.
|
|
247
212
|
*/
|
|
248
213
|
get entityGenerator() {
|
|
249
|
-
return this.
|
|
214
|
+
return this.driver
|
|
215
|
+
.getPlatform()
|
|
216
|
+
.getExtension('EntityGenerator', '@mikro-orm/entity-generator', '@mikro-orm/entity-generator', this.em);
|
|
250
217
|
}
|
|
251
218
|
}
|
package/README.md
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
<a href="https://mikro-orm.io"><img src="https://raw.githubusercontent.com/mikro-orm/mikro-orm/master/docs/static/img/logo-readme.svg?sanitize=true" alt="MikroORM" /></a>
|
|
3
3
|
</h1>
|
|
4
4
|
|
|
5
|
-
TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL
|
|
5
|
+
TypeScript ORM for Node.js based on Data Mapper, [Unit of Work](https://mikro-orm.io/docs/unit-of-work/) and [Identity Map](https://mikro-orm.io/docs/identity-map/) patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL, SQLite (including libSQL), MSSQL and Oracle databases.
|
|
6
6
|
|
|
7
7
|
> Heavily inspired by [Doctrine](https://www.doctrine-project.org/) and [Hibernate](https://hibernate.org/).
|
|
8
8
|
|
|
9
|
-
[](https://
|
|
10
|
-
[](https://
|
|
9
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
10
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
11
11
|
[](https://discord.gg/w8bjxFHS7X)
|
|
12
|
-
[](https://
|
|
12
|
+
[](https://npmx.dev/package/@mikro-orm/core)
|
|
13
13
|
[](https://coveralls.io/r/mikro-orm/mikro-orm?branch=master)
|
|
14
14
|
[](https://github.com/mikro-orm/mikro-orm/actions?workflow=tests)
|
|
15
15
|
|
|
@@ -181,6 +181,7 @@ yarn add @mikro-orm/core @mikro-orm/mysql # for mysql/mariadb
|
|
|
181
181
|
yarn add @mikro-orm/core @mikro-orm/mariadb # for mysql/mariadb
|
|
182
182
|
yarn add @mikro-orm/core @mikro-orm/postgresql # for postgresql
|
|
183
183
|
yarn add @mikro-orm/core @mikro-orm/mssql # for mssql
|
|
184
|
+
yarn add @mikro-orm/core @mikro-orm/oracledb # for oracle
|
|
184
185
|
yarn add @mikro-orm/core @mikro-orm/sqlite # for sqlite
|
|
185
186
|
yarn add @mikro-orm/core @mikro-orm/libsql # for libsql
|
|
186
187
|
```
|
|
@@ -381,6 +382,8 @@ See also the list of contributors who [participated](https://github.com/mikro-or
|
|
|
381
382
|
|
|
382
383
|
Please ⭐️ this repository if this project helped you!
|
|
383
384
|
|
|
385
|
+
> If you'd like to support my open-source work, consider sponsoring me directly at [github.com/sponsors/b4nan](https://github.com/sponsors/b4nan).
|
|
386
|
+
|
|
384
387
|
## 📝 License
|
|
385
388
|
|
|
386
389
|
Copyright © 2018 [Martin Adámek](https://github.com/b4nan).
|
|
@@ -3,13 +3,12 @@ export declare class FileCacheAdapter implements SyncCacheAdapter {
|
|
|
3
3
|
private readonly options;
|
|
4
4
|
private readonly baseDir;
|
|
5
5
|
private readonly pretty;
|
|
6
|
-
private readonly hashAlgorithm;
|
|
7
6
|
private readonly VERSION;
|
|
8
7
|
private cache;
|
|
9
8
|
constructor(options: {
|
|
10
9
|
cacheDir: string;
|
|
11
10
|
combined?: boolean | string;
|
|
12
|
-
}, baseDir: string, pretty?: boolean
|
|
11
|
+
} | undefined, baseDir: string, pretty?: boolean);
|
|
13
12
|
/**
|
|
14
13
|
* @inheritDoc
|
|
15
14
|
*/
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { globSync } from 'tinyglobby';
|
|
2
1
|
import { existsSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs';
|
|
2
|
+
import { fs } from '../utils/fs-utils.js';
|
|
3
3
|
import { Utils } from '../utils/Utils.js';
|
|
4
4
|
export class FileCacheAdapter {
|
|
5
5
|
options;
|
|
6
6
|
baseDir;
|
|
7
7
|
pretty;
|
|
8
|
-
hashAlgorithm;
|
|
9
8
|
VERSION = Utils.getORMVersion();
|
|
10
9
|
cache = {};
|
|
11
|
-
constructor(options, baseDir, pretty = false
|
|
10
|
+
constructor(options = {}, baseDir, pretty = false) {
|
|
12
11
|
this.options = options;
|
|
13
12
|
this.baseDir = baseDir;
|
|
14
13
|
this.pretty = pretty;
|
|
15
|
-
this.
|
|
14
|
+
this.options.cacheDir ??= process.cwd() + '/temp';
|
|
16
15
|
}
|
|
17
16
|
/**
|
|
18
17
|
* @inheritDoc
|
|
@@ -22,7 +21,7 @@ export class FileCacheAdapter {
|
|
|
22
21
|
if (!existsSync(path)) {
|
|
23
22
|
return null;
|
|
24
23
|
}
|
|
25
|
-
const payload =
|
|
24
|
+
const payload = fs.readJSONSync(path);
|
|
26
25
|
const hash = this.getHash(payload.origin);
|
|
27
26
|
if (!hash || payload.hash !== hash) {
|
|
28
27
|
return null;
|
|
@@ -53,32 +52,38 @@ export class FileCacheAdapter {
|
|
|
53
52
|
*/
|
|
54
53
|
clear() {
|
|
55
54
|
const path = this.path('*');
|
|
56
|
-
const files =
|
|
57
|
-
|
|
55
|
+
const files = fs.glob(path);
|
|
56
|
+
for (const file of files) {
|
|
57
|
+
/* v8 ignore next */
|
|
58
|
+
try {
|
|
59
|
+
unlinkSync(file);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// ignore if file is already gone
|
|
63
|
+
}
|
|
64
|
+
}
|
|
58
65
|
this.cache = {};
|
|
59
66
|
}
|
|
60
67
|
combine() {
|
|
61
68
|
if (!this.options.combined) {
|
|
62
69
|
return;
|
|
63
70
|
}
|
|
64
|
-
let path = typeof this.options.combined === 'string'
|
|
65
|
-
|
|
66
|
-
: './metadata.json';
|
|
67
|
-
path = Utils.normalizePath(this.options.cacheDir, path);
|
|
71
|
+
let path = typeof this.options.combined === 'string' ? this.options.combined : './metadata.json';
|
|
72
|
+
path = fs.normalizePath(this.options.cacheDir, path);
|
|
68
73
|
this.options.combined = path; // override in the options, so we can log it from the CLI in `cache:generate` command
|
|
69
74
|
writeFileSync(path, JSON.stringify(this.cache, null, this.pretty ? 2 : undefined));
|
|
70
75
|
return path;
|
|
71
76
|
}
|
|
72
77
|
path(name) {
|
|
73
|
-
|
|
78
|
+
fs.ensureDir(this.options.cacheDir);
|
|
74
79
|
return `${this.options.cacheDir}/${name}.json`;
|
|
75
80
|
}
|
|
76
81
|
getHash(origin) {
|
|
77
|
-
origin =
|
|
82
|
+
origin = fs.absolutePath(origin, this.baseDir);
|
|
78
83
|
if (!existsSync(origin)) {
|
|
79
84
|
return null;
|
|
80
85
|
}
|
|
81
86
|
const contents = readFileSync(origin);
|
|
82
|
-
return Utils.hash(contents.toString() + this.VERSION
|
|
87
|
+
return Utils.hash(contents.toString() + this.VERSION);
|
|
83
88
|
}
|
|
84
89
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { CacheAdapter } from './CacheAdapter.js';
|
|
2
2
|
import type { Dictionary } from '../typings.js';
|
|
3
3
|
export declare class GeneratedCacheAdapter implements CacheAdapter {
|
|
4
|
-
private readonly options;
|
|
5
4
|
private readonly data;
|
|
6
5
|
constructor(options: {
|
|
7
6
|
data: Dictionary;
|
package/cache/index.d.ts
CHANGED