@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.100
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 +96 -58
- package/EntityManager.js +465 -395
- package/MikroORM.d.ts +45 -35
- package/MikroORM.js +109 -160
- package/README.md +3 -2
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +2 -2
- package/cache/FileCacheAdapter.js +20 -27
- package/cache/GeneratedCacheAdapter.d.ts +2 -3
- package/cache/GeneratedCacheAdapter.js +1 -7
- package/cache/MemoryCacheAdapter.d.ts +1 -1
- package/cache/MemoryCacheAdapter.js +1 -5
- package/cache/NullCacheAdapter.d.ts +1 -1
- package/cache/NullCacheAdapter.js +1 -5
- package/cache/index.d.ts +4 -5
- package/cache/index.js +4 -21
- package/connections/Connection.d.ts +22 -14
- package/connections/Connection.js +27 -23
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/drivers/DatabaseDriver.d.ts +25 -15
- package/drivers/DatabaseDriver.js +77 -64
- package/drivers/IDatabaseDriver.d.ts +40 -16
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/BaseEntity.d.ts +6 -7
- package/entity/BaseEntity.js +16 -23
- package/entity/Collection.d.ts +98 -34
- package/entity/Collection.js +466 -131
- package/entity/EntityAssigner.d.ts +3 -3
- package/entity/EntityAssigner.js +67 -64
- package/entity/EntityFactory.d.ts +10 -3
- package/entity/EntityFactory.js +112 -91
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +66 -53
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +8 -7
- package/entity/EntityLoader.js +161 -128
- package/entity/EntityRepository.d.ts +8 -8
- package/entity/EntityRepository.js +7 -11
- package/entity/Reference.d.ts +10 -13
- package/entity/Reference.js +64 -46
- package/entity/WrappedEntity.d.ts +12 -17
- package/entity/WrappedEntity.js +22 -31
- package/entity/defineEntity.d.ts +568 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +14 -13
- package/entity/index.js +14 -29
- package/entity/utils.d.ts +8 -1
- package/entity/utils.js +22 -13
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +24 -9
- package/enums.js +50 -41
- package/errors.d.ts +11 -3
- package/errors.js +42 -32
- package/events/EventManager.d.ts +5 -4
- package/events/EventManager.js +26 -22
- package/events/EventSubscriber.d.ts +8 -5
- package/events/EventSubscriber.js +1 -2
- package/events/TransactionEventBroadcaster.d.ts +3 -3
- package/events/TransactionEventBroadcaster.js +1 -5
- package/events/index.d.ts +3 -3
- package/events/index.js +3 -19
- package/exceptions.js +18 -39
- package/hydration/Hydrator.d.ts +5 -5
- package/hydration/Hydrator.js +2 -6
- package/hydration/ObjectHydrator.d.ts +7 -7
- package/hydration/ObjectHydrator.js +58 -50
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +20 -46
- package/logging/DefaultLogger.d.ts +2 -2
- package/logging/DefaultLogger.js +10 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +3 -3
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +5 -4
- package/logging/index.js +5 -20
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +16 -0
- package/metadata/EntitySchema.d.ts +14 -10
- package/metadata/EntitySchema.js +78 -64
- package/metadata/MetadataDiscovery.d.ts +11 -14
- package/metadata/MetadataDiscovery.js +278 -317
- package/metadata/MetadataProvider.d.ts +13 -4
- package/metadata/MetadataProvider.js +47 -8
- package/metadata/MetadataStorage.d.ts +2 -7
- package/metadata/MetadataStorage.js +19 -35
- package/metadata/MetadataValidator.d.ts +3 -10
- package/metadata/MetadataValidator.js +51 -64
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/metadata/types.d.ts +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +7 -3
- package/naming-strategy/AbstractNamingStrategy.js +11 -9
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +2 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -1
- package/naming-strategy/MongoNamingStrategy.js +2 -6
- package/naming-strategy/NamingStrategy.d.ts +12 -2
- package/naming-strategy/NamingStrategy.js +1 -2
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -1
- package/naming-strategy/UnderscoreNamingStrategy.js +2 -6
- package/naming-strategy/index.d.ts +5 -5
- package/naming-strategy/index.js +5 -21
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +19 -20
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +15 -22
- package/platforms/Platform.js +58 -88
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +4 -2
- package/serialization/EntitySerializer.js +64 -51
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +48 -42
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +24 -25
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +6 -11
- package/types/BigIntType.d.ts +12 -9
- package/types/BigIntType.js +6 -6
- package/types/BlobType.d.ts +3 -4
- package/types/BlobType.js +2 -11
- package/types/BooleanType.d.ts +5 -4
- package/types/BooleanType.js +5 -6
- package/types/CharacterType.d.ts +3 -3
- package/types/CharacterType.js +2 -6
- package/types/DateTimeType.d.ts +3 -3
- package/types/DateTimeType.js +2 -6
- package/types/DateType.d.ts +3 -3
- package/types/DateType.js +2 -6
- package/types/DecimalType.d.ts +9 -7
- package/types/DecimalType.js +5 -8
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +4 -7
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +4 -10
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +2 -6
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +2 -6
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +2 -6
- package/types/IntervalType.d.ts +3 -3
- package/types/IntervalType.js +2 -6
- package/types/JsonType.d.ts +4 -4
- package/types/JsonType.js +9 -8
- package/types/MediumIntType.d.ts +3 -3
- package/types/MediumIntType.js +2 -6
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +2 -6
- package/types/StringType.d.ts +3 -3
- package/types/StringType.js +2 -6
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +2 -6
- package/types/TimeType.d.ts +3 -3
- package/types/TimeType.js +4 -8
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +3 -6
- package/types/Type.d.ts +4 -6
- package/types/Type.js +6 -10
- package/types/Uint8ArrayType.d.ts +3 -4
- package/types/Uint8ArrayType.js +3 -12
- package/types/UnknownType.d.ts +3 -3
- package/types/UnknownType.js +2 -6
- package/types/UuidType.d.ts +3 -3
- package/types/UuidType.js +2 -6
- package/types/index.d.ts +25 -25
- package/types/index.js +52 -79
- package/typings.d.ts +134 -93
- package/typings.js +67 -65
- package/unit-of-work/ChangeSet.d.ts +1 -4
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -9
- package/unit-of-work/ChangeSetComputer.js +36 -38
- package/unit-of-work/ChangeSetPersister.d.ts +11 -9
- package/unit-of-work/ChangeSetPersister.js +100 -65
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -1
- package/unit-of-work/CommitOrderCalculator.js +6 -10
- package/unit-of-work/IdentityMap.d.ts +1 -1
- package/unit-of-work/IdentityMap.js +1 -5
- package/unit-of-work/UnitOfWork.d.ts +16 -8
- package/unit-of-work/UnitOfWork.js +266 -209
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +11 -11
- package/utils/AbstractSchemaGenerator.js +21 -20
- package/utils/Configuration.d.ts +774 -224
- package/utils/Configuration.js +166 -216
- package/utils/ConfigurationLoader.d.ts +1 -53
- package/utils/ConfigurationLoader.js +1 -367
- package/utils/Cursor.d.ts +6 -9
- package/utils/Cursor.js +25 -25
- package/utils/DataloaderUtils.d.ts +18 -8
- package/utils/DataloaderUtils.js +63 -21
- package/utils/EntityComparator.d.ts +9 -5
- package/utils/EntityComparator.js +155 -108
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +12 -4
- package/utils/QueryHelper.js +110 -53
- package/utils/RawQueryFragment.d.ts +37 -14
- package/utils/RawQueryFragment.js +50 -33
- package/utils/RequestContext.d.ts +2 -2
- package/utils/RequestContext.js +3 -7
- package/utils/TransactionContext.d.ts +1 -1
- package/utils/TransactionContext.js +4 -8
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +16 -100
- package/utils/Utils.js +114 -332
- package/utils/clone.js +7 -11
- package/utils/env-vars.d.ts +3 -0
- package/utils/env-vars.js +87 -0
- package/utils/fs-utils.d.ts +12 -0
- package/utils/fs-utils.js +97 -0
- package/utils/index.d.ts +14 -13
- package/utils/index.js +14 -29
- package/utils/upsert-utils.d.ts +8 -3
- package/utils/upsert-utils.js +57 -10
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -16
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -33
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -14
- package/decorators/Embedded.d.ts +0 -18
- package/decorators/Embedded.js +0 -20
- package/decorators/Entity.d.ts +0 -18
- package/decorators/Entity.js +0 -16
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -19
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -11
- package/decorators/Formula.d.ts +0 -5
- package/decorators/Formula.js +0 -18
- package/decorators/Indexed.d.ts +0 -17
- package/decorators/Indexed.js +0 -24
- package/decorators/ManyToMany.d.ts +0 -40
- package/decorators/ManyToMany.js +0 -16
- package/decorators/ManyToOne.d.ts +0 -30
- package/decorators/ManyToOne.js +0 -16
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -20
- package/decorators/OneToOne.d.ts +0 -24
- package/decorators/OneToOne.js +0 -10
- package/decorators/PrimaryKey.d.ts +0 -9
- package/decorators/PrimaryKey.js +0 -23
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -34
- package/decorators/Transactional.d.ts +0 -13
- package/decorators/Transactional.js +0 -31
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -59
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -36
- package/entity/ArrayCollection.d.ts +0 -116
- package/entity/ArrayCollection.js +0 -399
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -154
- package/index.mjs +0 -199
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -48
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -31
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const errors_1 = require("../errors");
|
|
5
|
-
const Utils_1 = require("../utils/Utils");
|
|
6
|
-
class EntityRepository {
|
|
1
|
+
import { ValidationError } from '../errors.js';
|
|
2
|
+
import { Utils } from '../utils/Utils.js';
|
|
3
|
+
export class EntityRepository {
|
|
7
4
|
em;
|
|
8
5
|
entityName;
|
|
9
6
|
constructor(em, entityName) {
|
|
@@ -191,7 +188,7 @@ class EntityRepository {
|
|
|
191
188
|
return this.getEntityManager().count(this.entityName, where, options);
|
|
192
189
|
}
|
|
193
190
|
getEntityName() {
|
|
194
|
-
return
|
|
191
|
+
return Utils.className(this.entityName);
|
|
195
192
|
}
|
|
196
193
|
/**
|
|
197
194
|
* Returns the underlying EntityManager instance
|
|
@@ -200,15 +197,14 @@ class EntityRepository {
|
|
|
200
197
|
return this.em;
|
|
201
198
|
}
|
|
202
199
|
validateRepositoryType(entities, method) {
|
|
203
|
-
entities =
|
|
200
|
+
entities = Utils.asArray(entities);
|
|
204
201
|
if (entities.length === 0) {
|
|
205
202
|
return;
|
|
206
203
|
}
|
|
207
204
|
const entityName = entities[0].constructor.name;
|
|
208
|
-
const repoType =
|
|
205
|
+
const repoType = Utils.className(this.entityName);
|
|
209
206
|
if (entityName && repoType !== entityName) {
|
|
210
|
-
throw
|
|
207
|
+
throw ValidationError.fromWrongRepositoryType(entityName, repoType, method);
|
|
211
208
|
}
|
|
212
209
|
}
|
|
213
210
|
}
|
|
214
|
-
exports.EntityRepository = EntityRepository;
|
package/entity/Reference.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver';
|
|
1
|
+
import type { AddEager, AddOptional, Dictionary, EntityClass, EntityKey, EntityProperty, Loaded, LoadedReference, Primary, Ref } from '../typings.js';
|
|
2
|
+
import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver.js';
|
|
4
3
|
export declare class Reference<T extends object> {
|
|
5
4
|
private entity;
|
|
5
|
+
private property?;
|
|
6
6
|
constructor(entity: T);
|
|
7
7
|
static create<T extends object>(entity: T | Ref<T>): Ref<T>;
|
|
8
8
|
static createFromPK<T extends object>(entityType: EntityClass<T>, pk: Primary<T>, options?: {
|
|
@@ -42,8 +42,6 @@ export declare class Reference<T extends object> {
|
|
|
42
42
|
isInitialized(): boolean;
|
|
43
43
|
populated(populated?: boolean): void;
|
|
44
44
|
toJSON(...args: any[]): Dictionary;
|
|
45
|
-
/** @ignore */
|
|
46
|
-
[inspect.custom](depth?: number): string;
|
|
47
45
|
}
|
|
48
46
|
export declare class ScalarReference<Value> {
|
|
49
47
|
private value?;
|
|
@@ -56,12 +54,15 @@ export declare class ScalarReference<Value> {
|
|
|
56
54
|
* Returns either the whole entity, or the requested property.
|
|
57
55
|
*/
|
|
58
56
|
load(options?: Omit<LoadReferenceOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value | undefined>;
|
|
57
|
+
/**
|
|
58
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
59
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
60
|
+
*/
|
|
61
|
+
loadOrFail(options?: Omit<LoadReferenceOrFailOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value>;
|
|
59
62
|
set(value: Value): void;
|
|
60
63
|
bind<Entity extends object>(entity: Entity, property: EntityKey<Entity>): void;
|
|
61
64
|
unwrap(): Value | undefined;
|
|
62
65
|
isInitialized(): boolean;
|
|
63
|
-
/** @ignore */
|
|
64
|
-
[inspect.custom](): string;
|
|
65
66
|
}
|
|
66
67
|
export interface LoadReferenceOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends FindOneOptions<T, P, F, E> {
|
|
67
68
|
dataloader?: boolean;
|
|
@@ -72,15 +73,11 @@ export interface LoadReferenceOrFailOptions<T extends object, P extends string =
|
|
|
72
73
|
/**
|
|
73
74
|
* shortcut for `wrap(entity).toReference()`
|
|
74
75
|
*/
|
|
75
|
-
export declare function ref<
|
|
76
|
+
export declare function ref<I extends unknown | Ref<unknown> | undefined | null, T extends I & {}>(entity: I): Ref<T> & LoadedReference<Loaded<T, AddEager<T>>> | AddOptional<typeof entity>;
|
|
76
77
|
/**
|
|
77
78
|
* shortcut for `Reference.createFromPK(entityType, pk)`
|
|
78
79
|
*/
|
|
79
|
-
export declare function ref<T, PKV extends Primary<T> = Primary<T>>(entityType: EntityClass<T>, pk
|
|
80
|
-
/**
|
|
81
|
-
* shortcut for `wrap(entity).toReference()`
|
|
82
|
-
*/
|
|
83
|
-
export declare function ref<T>(value: T | Ref<T>): Ref<T> & LoadedReference<Loaded<T, AddEager<T>>>;
|
|
80
|
+
export declare function ref<I extends unknown | undefined | null, T, PKV extends Primary<T> = Primary<T>>(entityType: EntityClass<T>, pk: I): Ref<T> | AddOptional<typeof pk>;
|
|
84
81
|
/**
|
|
85
82
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
86
83
|
*/
|
package/entity/Reference.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const wrap_1 = require("./wrap");
|
|
9
|
-
const Utils_1 = require("../utils/Utils");
|
|
10
|
-
class Reference {
|
|
1
|
+
import { DataloaderType } from '../enums.js';
|
|
2
|
+
import { helper, wrap } from './wrap.js';
|
|
3
|
+
import { Utils } from '../utils/Utils.js';
|
|
4
|
+
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
5
|
+
import { NotFoundError } from '../errors.js';
|
|
6
|
+
import { inspect } from '../logging/inspect.js';
|
|
7
|
+
export class Reference {
|
|
11
8
|
entity;
|
|
9
|
+
property;
|
|
12
10
|
constructor(entity) {
|
|
13
11
|
this.entity = entity;
|
|
14
12
|
this.set(entity);
|
|
15
|
-
const meta =
|
|
13
|
+
const meta = helper(this.entity).__meta;
|
|
16
14
|
meta.primaryKeys.forEach(primaryKey => {
|
|
17
15
|
Object.defineProperty(this, primaryKey, {
|
|
18
16
|
get() {
|
|
@@ -23,14 +21,14 @@ class Reference {
|
|
|
23
21
|
if (meta.serializedPrimaryKey && meta.primaryKeys[0] !== meta.serializedPrimaryKey) {
|
|
24
22
|
Object.defineProperty(this, meta.serializedPrimaryKey, {
|
|
25
23
|
get() {
|
|
26
|
-
return
|
|
24
|
+
return helper(this.entity).getSerializedPrimaryKey();
|
|
27
25
|
},
|
|
28
26
|
});
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
static create(entity) {
|
|
32
30
|
const unwrapped = Reference.unwrapReference(entity);
|
|
33
|
-
const ref =
|
|
31
|
+
const ref = helper(entity).toReference();
|
|
34
32
|
if (unwrapped !== ref.unwrap()) {
|
|
35
33
|
ref.set(unwrapped);
|
|
36
34
|
}
|
|
@@ -38,16 +36,21 @@ class Reference {
|
|
|
38
36
|
}
|
|
39
37
|
static createFromPK(entityType, pk, options) {
|
|
40
38
|
const ref = this.createNakedFromPK(entityType, pk, options);
|
|
41
|
-
return
|
|
39
|
+
return helper(ref)?.toReference() ?? ref;
|
|
42
40
|
}
|
|
43
41
|
static createNakedFromPK(entityType, pk, options) {
|
|
44
42
|
const factory = entityType.prototype.__factory;
|
|
43
|
+
if (!factory) {
|
|
44
|
+
// this can happen only if `ref()` is used as a property initializer, and the value is important only for the
|
|
45
|
+
// inference of defaults, so it's fine to return it directly without wrapping with `Reference` class
|
|
46
|
+
return pk;
|
|
47
|
+
}
|
|
45
48
|
const entity = factory.createReference(entityType, pk, {
|
|
46
49
|
merge: false,
|
|
47
50
|
convertCustomTypes: false,
|
|
48
51
|
...options,
|
|
49
52
|
});
|
|
50
|
-
const wrapped =
|
|
53
|
+
const wrapped = helper(entity);
|
|
51
54
|
wrapped.__meta.primaryKeys.forEach(key => wrapped.__loadedProperties.add(key));
|
|
52
55
|
wrapped.__originalEntityData = factory.getComparator().prepareEntity(entity);
|
|
53
56
|
return entity;
|
|
@@ -63,7 +66,9 @@ class Reference {
|
|
|
63
66
|
*/
|
|
64
67
|
static wrapReference(entity, prop) {
|
|
65
68
|
if (entity && prop.ref && !Reference.isReference(entity)) {
|
|
66
|
-
|
|
69
|
+
const ref = Reference.create(entity);
|
|
70
|
+
ref.property = prop;
|
|
71
|
+
return ref;
|
|
67
72
|
}
|
|
68
73
|
return entity;
|
|
69
74
|
}
|
|
@@ -79,17 +84,18 @@ class Reference {
|
|
|
79
84
|
* the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case.
|
|
80
85
|
*/
|
|
81
86
|
async load(options = {}) {
|
|
82
|
-
const wrapped =
|
|
87
|
+
const wrapped = helper(this.entity);
|
|
83
88
|
if (!wrapped.__em) {
|
|
84
89
|
return this.entity;
|
|
85
90
|
}
|
|
91
|
+
options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property?.filters, options.filters) };
|
|
86
92
|
if (this.isInitialized() && !options.refresh && options.populate) {
|
|
87
93
|
await wrapped.__em.populate(this.entity, options.populate, options);
|
|
88
94
|
}
|
|
89
95
|
if (!this.isInitialized() || options.refresh) {
|
|
90
|
-
if (options.dataloader ?? [
|
|
91
|
-
|
|
92
|
-
return
|
|
96
|
+
if (options.dataloader ?? [DataloaderType.ALL, DataloaderType.REFERENCE].includes(wrapped.__em.config.getDataloaderType())) {
|
|
97
|
+
const dataLoader = await wrapped.__em.getDataLoader('ref');
|
|
98
|
+
return dataLoader.load([this, options]);
|
|
93
99
|
}
|
|
94
100
|
return wrapped.init(options);
|
|
95
101
|
}
|
|
@@ -102,7 +108,7 @@ class Reference {
|
|
|
102
108
|
async loadOrFail(options = {}) {
|
|
103
109
|
const ret = await this.load(options);
|
|
104
110
|
if (!ret) {
|
|
105
|
-
const wrapped =
|
|
111
|
+
const wrapped = helper(this.entity);
|
|
106
112
|
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
107
113
|
const entityName = this.entity.constructor.name;
|
|
108
114
|
const where = wrapped.getPrimaryKey();
|
|
@@ -112,14 +118,14 @@ class Reference {
|
|
|
112
118
|
}
|
|
113
119
|
set(entity) {
|
|
114
120
|
this.entity = Reference.unwrapReference(entity);
|
|
115
|
-
delete
|
|
121
|
+
delete helper(this.entity).__reference;
|
|
116
122
|
}
|
|
117
123
|
unwrap() {
|
|
118
124
|
return this.entity;
|
|
119
125
|
}
|
|
120
126
|
getEntity() {
|
|
121
127
|
if (!this.isInitialized()) {
|
|
122
|
-
throw new Error(`Reference<${
|
|
128
|
+
throw new Error(`Reference<${helper(this.entity).__meta.name}> ${helper(this.entity).getPrimaryKey()} not initialized`);
|
|
123
129
|
}
|
|
124
130
|
return this.entity;
|
|
125
131
|
}
|
|
@@ -131,31 +137,29 @@ class Reference {
|
|
|
131
137
|
return this.getEntity()[prop];
|
|
132
138
|
}
|
|
133
139
|
isInitialized() {
|
|
134
|
-
return
|
|
140
|
+
return helper(this.entity).__initialized;
|
|
135
141
|
}
|
|
136
142
|
populated(populated) {
|
|
137
|
-
|
|
143
|
+
helper(this.entity).populated(populated);
|
|
138
144
|
}
|
|
139
145
|
toJSON(...args) {
|
|
140
|
-
return
|
|
146
|
+
return wrap(this.entity).toJSON(...args);
|
|
141
147
|
}
|
|
142
|
-
/* istanbul ignore next */
|
|
143
148
|
/** @ignore */
|
|
144
|
-
[
|
|
149
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
145
150
|
const object = { ...this };
|
|
146
|
-
const hidden = ['meta'];
|
|
151
|
+
const hidden = ['meta', 'property'];
|
|
147
152
|
hidden.forEach(k => delete object[k]);
|
|
148
|
-
const ret =
|
|
149
|
-
const wrapped =
|
|
153
|
+
const ret = inspect(object, { depth });
|
|
154
|
+
const wrapped = helper(this.entity);
|
|
150
155
|
const meta = wrapped.__meta;
|
|
156
|
+
/* v8 ignore next */
|
|
151
157
|
const pk = wrapped.hasPrimaryKey() ? '<' + wrapped.getSerializedPrimaryKey() + '>' : '';
|
|
152
158
|
const name = `Ref<${meta.className}${pk}>`;
|
|
153
159
|
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
154
160
|
}
|
|
155
161
|
}
|
|
156
|
-
|
|
157
|
-
exports.Ref = Reference;
|
|
158
|
-
class ScalarReference {
|
|
162
|
+
export class ScalarReference {
|
|
159
163
|
value;
|
|
160
164
|
initialized;
|
|
161
165
|
entity;
|
|
@@ -174,10 +178,24 @@ class ScalarReference {
|
|
|
174
178
|
if (this.entity == null || this.property == null) {
|
|
175
179
|
throw new Error('Cannot load scalar reference that is not bound to an entity property.');
|
|
176
180
|
}
|
|
177
|
-
await
|
|
181
|
+
await helper(this.entity).populate([this.property], opts);
|
|
178
182
|
}
|
|
179
183
|
return this.value;
|
|
180
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
187
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
188
|
+
*/
|
|
189
|
+
async loadOrFail(options = {}) {
|
|
190
|
+
const ret = await this.load(options);
|
|
191
|
+
if (ret == null) {
|
|
192
|
+
const wrapped = helper(this.entity);
|
|
193
|
+
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
194
|
+
const entityName = this.entity.constructor.name;
|
|
195
|
+
throw NotFoundError.failedToLoadProperty(entityName, this.property, wrapped.getPrimaryKey());
|
|
196
|
+
}
|
|
197
|
+
return ret;
|
|
198
|
+
}
|
|
181
199
|
set(value) {
|
|
182
200
|
this.value = value;
|
|
183
201
|
this.initialized = true;
|
|
@@ -193,13 +211,12 @@ class ScalarReference {
|
|
|
193
211
|
isInitialized() {
|
|
194
212
|
return this.initialized;
|
|
195
213
|
}
|
|
196
|
-
/* istanbul ignore next */
|
|
197
214
|
/** @ignore */
|
|
198
|
-
|
|
199
|
-
|
|
215
|
+
/* v8 ignore next */
|
|
216
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
217
|
+
return this.initialized ? `Ref<${inspect(this.value)}>` : `Ref<?>`;
|
|
200
218
|
}
|
|
201
219
|
}
|
|
202
|
-
exports.ScalarReference = ScalarReference;
|
|
203
220
|
Object.defineProperties(Reference.prototype, {
|
|
204
221
|
__reference: { value: true, enumerable: false },
|
|
205
222
|
__meta: { get() { return this.entity.__meta; } },
|
|
@@ -216,15 +233,15 @@ Object.defineProperties(ScalarReference.prototype, {
|
|
|
216
233
|
/**
|
|
217
234
|
* shortcut for `wrap(entity).toReference()`
|
|
218
235
|
*/
|
|
219
|
-
function ref(entityOrType, pk) {
|
|
236
|
+
export function ref(entityOrType, pk) {
|
|
220
237
|
if (entityOrType == null) {
|
|
221
238
|
return entityOrType;
|
|
222
239
|
}
|
|
223
|
-
if (
|
|
224
|
-
return
|
|
240
|
+
if (Utils.isEntity(entityOrType, true)) {
|
|
241
|
+
return helper(entityOrType).toReference();
|
|
225
242
|
}
|
|
226
|
-
if (
|
|
227
|
-
return
|
|
243
|
+
if (Utils.isEntity(pk, true)) {
|
|
244
|
+
return helper(pk).toReference();
|
|
228
245
|
}
|
|
229
246
|
if (arguments.length === 1) {
|
|
230
247
|
return new ScalarReference(entityOrType, true);
|
|
@@ -237,9 +254,10 @@ function ref(entityOrType, pk) {
|
|
|
237
254
|
/**
|
|
238
255
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
239
256
|
*/
|
|
240
|
-
function rel(entityType, pk) {
|
|
241
|
-
if (pk == null ||
|
|
257
|
+
export function rel(entityType, pk) {
|
|
258
|
+
if (pk == null || Utils.isEntity(pk)) {
|
|
242
259
|
return pk;
|
|
243
260
|
}
|
|
244
261
|
return Reference.createNakedFromPK(entityType, pk);
|
|
245
262
|
}
|
|
263
|
+
export { Reference as Ref };
|
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
import type { PopulatePath } from '../enums';
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import type {
|
|
8
|
-
import type {
|
|
9
|
-
import type
|
|
10
|
-
import {
|
|
11
|
-
import type { FindOneOptions, LoadHint } from '../drivers/IDatabaseDriver';
|
|
1
|
+
import type { PopulatePath } from '../enums.js';
|
|
2
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
3
|
+
import type { Dictionary, EntityData, EntityDictionary, EntityMetadata, IHydrator, EntityKey, PopulateOptions, Primary, AutoPath, Ref, AddEager, LoadedReference, EntityDTO, Loaded, FromEntityType, IsSubset, MergeSelected } from '../typings.js';
|
|
4
|
+
import { Reference } from './Reference.js';
|
|
5
|
+
import { type AssignOptions } from './EntityAssigner.js';
|
|
6
|
+
import type { EntityLoaderOptions } from './EntityLoader.js';
|
|
7
|
+
import type { EntityIdentifier } from './EntityIdentifier.js';
|
|
8
|
+
import type { SerializationContext } from '../serialization/SerializationContext.js';
|
|
9
|
+
import { type SerializeOptions } from '../serialization/EntitySerializer.js';
|
|
10
|
+
import type { FindOneOptions, LoadHint } from '../drivers/IDatabaseDriver.js';
|
|
12
11
|
export declare class WrappedEntity<Entity extends object> {
|
|
13
12
|
__initialized: boolean;
|
|
14
|
-
__touched: boolean;
|
|
15
13
|
__populated?: boolean;
|
|
16
14
|
__managed?: boolean;
|
|
17
15
|
__onLoadFired?: boolean;
|
|
@@ -41,7 +39,6 @@ export declare class WrappedEntity<Entity extends object> {
|
|
|
41
39
|
private readonly pkGetterConverted?;
|
|
42
40
|
constructor(entity: Entity, hydrator: IHydrator, pkGetter?: (e: Entity) => Primary<Entity>, pkSerializer?: (e: Entity) => string, pkGetterConverted?: (e: Entity) => Primary<Entity>);
|
|
43
41
|
isInitialized(): boolean;
|
|
44
|
-
isTouched(): boolean;
|
|
45
42
|
isManaged(): boolean;
|
|
46
43
|
populated(populated?: boolean | undefined): void;
|
|
47
44
|
setSerializationContext<Hint extends string = never, Fields extends string = '*', Exclude extends string = never>(options: LoadHint<Entity, Hint, Fields, Exclude>): void;
|
|
@@ -61,9 +58,7 @@ export declare class WrappedEntity<Entity extends object> {
|
|
|
61
58
|
setPrimaryKey(id: Primary<Entity> | null): void;
|
|
62
59
|
getSerializedPrimaryKey(): string;
|
|
63
60
|
get __meta(): EntityMetadata<Entity>;
|
|
64
|
-
get __platform(): import("
|
|
65
|
-
get __config(): import("
|
|
61
|
+
get __platform(): import("@mikro-orm/sql").Platform;
|
|
62
|
+
get __config(): import("@mikro-orm/sql").Configuration<import("../drivers/IDatabaseDriver.js").IDatabaseDriver<import("@mikro-orm/sql").Connection>, EntityManager<import("../drivers/IDatabaseDriver.js").IDatabaseDriver<import("@mikro-orm/sql").Connection>>>;
|
|
66
63
|
get __primaryKeys(): Primary<Entity>[];
|
|
67
|
-
/** @ignore */
|
|
68
|
-
[inspect.custom](): string;
|
|
69
64
|
}
|
package/entity/WrappedEntity.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const wrap_1 = require("./wrap");
|
|
11
|
-
const EntitySerializer_1 = require("../serialization/EntitySerializer");
|
|
12
|
-
const utils_1 = require("./utils");
|
|
13
|
-
class WrappedEntity {
|
|
1
|
+
import { Reference } from './Reference.js';
|
|
2
|
+
import { EntityTransformer } from '../serialization/EntityTransformer.js';
|
|
3
|
+
import { EntityAssigner } from './EntityAssigner.js';
|
|
4
|
+
import { Utils } from '../utils/Utils.js';
|
|
5
|
+
import { ValidationError } from '../errors.js';
|
|
6
|
+
import { helper } from './wrap.js';
|
|
7
|
+
import { EntitySerializer } from '../serialization/EntitySerializer.js';
|
|
8
|
+
import { expandDotPaths } from './utils.js';
|
|
9
|
+
export class WrappedEntity {
|
|
14
10
|
constructor(entity, hydrator, pkGetter, pkSerializer, pkGetterConverted) {
|
|
15
11
|
this.entity = entity;
|
|
16
12
|
this.hydrator = hydrator;
|
|
@@ -18,7 +14,6 @@ class WrappedEntity {
|
|
|
18
14
|
this.pkSerializer = pkSerializer;
|
|
19
15
|
this.pkGetterConverted = pkGetterConverted;
|
|
20
16
|
this.__initialized = true;
|
|
21
|
-
this.__touched = false;
|
|
22
17
|
this.__serializationContext = {};
|
|
23
18
|
this.__loadedProperties = new Set();
|
|
24
19
|
this.__data = {};
|
|
@@ -27,9 +22,6 @@ class WrappedEntity {
|
|
|
27
22
|
isInitialized() {
|
|
28
23
|
return this.__initialized;
|
|
29
24
|
}
|
|
30
|
-
isTouched() {
|
|
31
|
-
return this.__touched;
|
|
32
|
-
}
|
|
33
25
|
isManaged() {
|
|
34
26
|
return !!this.__managed;
|
|
35
27
|
}
|
|
@@ -39,7 +31,7 @@ class WrappedEntity {
|
|
|
39
31
|
setSerializationContext(options) {
|
|
40
32
|
const exclude = options.exclude ?? [];
|
|
41
33
|
const context = this.__serializationContext;
|
|
42
|
-
const populate =
|
|
34
|
+
const populate = expandDotPaths(this.__meta, options.populate);
|
|
43
35
|
context.populate = context.populate ? context.populate.concat(populate) : populate;
|
|
44
36
|
context.exclude = context.exclude ? context.exclude.concat(exclude) : exclude;
|
|
45
37
|
if (context.fields && options.fields) {
|
|
@@ -53,17 +45,17 @@ class WrappedEntity {
|
|
|
53
45
|
}
|
|
54
46
|
}
|
|
55
47
|
toReference() {
|
|
56
|
-
this.__reference ??= new
|
|
48
|
+
this.__reference ??= new Reference(this.entity);
|
|
57
49
|
return this.__reference;
|
|
58
50
|
}
|
|
59
51
|
toObject(ignoreFields) {
|
|
60
|
-
return
|
|
52
|
+
return EntityTransformer.toObject(this.entity, ignoreFields);
|
|
61
53
|
}
|
|
62
54
|
serialize(options) {
|
|
63
|
-
return
|
|
55
|
+
return EntitySerializer.serialize(this.entity, options);
|
|
64
56
|
}
|
|
65
57
|
toPOJO() {
|
|
66
|
-
return
|
|
58
|
+
return EntityTransformer.toObject(this.entity, [], true);
|
|
67
59
|
}
|
|
68
60
|
toJSON(...args) {
|
|
69
61
|
// toJSON methods is added to the prototype during discovery to support automatic serialization via JSON.stringify()
|
|
@@ -73,17 +65,17 @@ class WrappedEntity {
|
|
|
73
65
|
if ('assign' in this.entity) {
|
|
74
66
|
return this.entity.assign(data, options);
|
|
75
67
|
}
|
|
76
|
-
return
|
|
68
|
+
return EntityAssigner.assign(this.entity, data, options);
|
|
77
69
|
}
|
|
78
70
|
async init(options) {
|
|
79
71
|
if (!this.__em) {
|
|
80
|
-
throw
|
|
72
|
+
throw ValidationError.entityNotManaged(this.entity);
|
|
81
73
|
}
|
|
82
74
|
return this.__em.findOne(this.entity.constructor.name, this.entity, { ...options, refresh: true, schema: this.__schema });
|
|
83
75
|
}
|
|
84
76
|
async populate(populate, options = {}) {
|
|
85
77
|
if (!this.__em) {
|
|
86
|
-
throw
|
|
78
|
+
throw ValidationError.entityNotManaged(this.entity);
|
|
87
79
|
}
|
|
88
80
|
// @ts-ignore hard to type
|
|
89
81
|
await this.__em.populate(this.entity, populate, options);
|
|
@@ -99,7 +91,7 @@ class WrappedEntity {
|
|
|
99
91
|
return null;
|
|
100
92
|
}
|
|
101
93
|
if (this.__pk != null && this.__meta.compositePK) {
|
|
102
|
-
return
|
|
94
|
+
return Utils.getCompositeKeyValue(this.__pk, this.__meta, convertCustomTypes ? 'convertToDatabaseValue' : false, this.__platform);
|
|
103
95
|
}
|
|
104
96
|
if (convertCustomTypes && this.__pk != null && prop.customType) {
|
|
105
97
|
return prop.customType.convertToDatabaseValue(this.__pk, this.__platform);
|
|
@@ -118,8 +110,8 @@ class WrappedEntity {
|
|
|
118
110
|
if (this.__meta.compositePK) {
|
|
119
111
|
return this.__meta.primaryKeys.reduce((ret, pk) => {
|
|
120
112
|
const child = this.entity[pk];
|
|
121
|
-
if (
|
|
122
|
-
const childPk =
|
|
113
|
+
if (Utils.isEntity(child, true)) {
|
|
114
|
+
const childPk = helper(child).getPrimaryKeys(convertCustomTypes);
|
|
123
115
|
ret.push(...childPk);
|
|
124
116
|
}
|
|
125
117
|
else {
|
|
@@ -153,11 +145,10 @@ class WrappedEntity {
|
|
|
153
145
|
return this.__em?.config ?? this.entity.__config;
|
|
154
146
|
}
|
|
155
147
|
get __primaryKeys() {
|
|
156
|
-
return
|
|
148
|
+
return Utils.getPrimaryKeyValues(this.entity, this.__meta);
|
|
157
149
|
}
|
|
158
150
|
/** @ignore */
|
|
159
|
-
[
|
|
151
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
160
152
|
return `[WrappedEntity<${this.__meta.className}>]`;
|
|
161
153
|
}
|
|
162
154
|
}
|
|
163
|
-
exports.WrappedEntity = WrappedEntity;
|