@mikro-orm/core 7.0.9 → 7.0.10-dev.1
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 +1899 -1926
- package/MikroORM.d.ts +74 -103
- package/MikroORM.js +178 -177
- 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 +181 -215
- package/entity/Collection.js +724 -730
- package/entity/EntityAssigner.d.ts +77 -88
- package/entity/EntityAssigner.js +230 -231
- package/entity/EntityFactory.d.ts +55 -67
- package/entity/EntityFactory.js +416 -457
- package/entity/EntityHelper.d.ts +23 -35
- package/entity/EntityHelper.js +279 -291
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +72 -98
- package/entity/EntityLoader.js +761 -792
- 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 +83 -127
- package/entity/Reference.js +277 -281
- package/entity/WrappedEntity.d.ts +72 -115
- package/entity/WrappedEntity.js +166 -168
- package/entity/defineEntity.d.ts +654 -1359
- 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 +418 -483
- 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 +130 -214
- package/metadata/EntitySchema.js +412 -411
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1879 -1957
- package/metadata/MetadataProvider.d.ts +26 -29
- package/metadata/MetadataProvider.js +97 -95
- 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 +303 -312
- package/platforms/Platform.js +675 -695
- 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 +741 -1254
- package/typings.js +233 -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 +179 -187
- package/unit-of-work/ChangeSetPersister.d.ts +50 -69
- package/unit-of-work/ChangeSetPersister.js +442 -465
- 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 +1222 -1236
- 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 +82 -99
- package/utils/EntityComparator.js +737 -829
- 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 +815 -815
- package/utils/clone.js +114 -105
- 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/entity/Reference.js
CHANGED
|
@@ -8,310 +8,306 @@ const referenceSymbol = Symbol('Reference');
|
|
|
8
8
|
const scalarReferenceSymbol = Symbol('ScalarReference');
|
|
9
9
|
/** Wrapper around an entity that provides lazy loading capabilities and identity-preserving reference semantics. */
|
|
10
10
|
export class Reference {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const pk = wrapped.hasPrimaryKey() ? '<' + wrapped.getSerializedPrimaryKey() + '>' : '';
|
|
177
|
-
const name = `Ref<${meta.className}${pk}>`;
|
|
178
|
-
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
179
|
-
}
|
|
11
|
+
entity;
|
|
12
|
+
property;
|
|
13
|
+
constructor(entity) {
|
|
14
|
+
this.entity = entity;
|
|
15
|
+
Object.defineProperty(this, referenceSymbol, { value: true, enumerable: false });
|
|
16
|
+
this.set(entity);
|
|
17
|
+
const meta = helper(this.entity).__meta;
|
|
18
|
+
meta.primaryKeys.forEach(primaryKey => {
|
|
19
|
+
Object.defineProperty(this, primaryKey, {
|
|
20
|
+
get() {
|
|
21
|
+
return this.entity[primaryKey];
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
if (meta.serializedPrimaryKey && meta.primaryKeys[0] !== meta.serializedPrimaryKey) {
|
|
26
|
+
Object.defineProperty(this, meta.serializedPrimaryKey, {
|
|
27
|
+
get() {
|
|
28
|
+
return helper(this.entity).getSerializedPrimaryKey();
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Creates a Reference wrapper for the given entity, preserving identity if one already exists. */
|
|
34
|
+
static create(entity) {
|
|
35
|
+
const unwrapped = Reference.unwrapReference(entity);
|
|
36
|
+
const ref = helper(entity).toReference();
|
|
37
|
+
if (unwrapped !== ref.unwrap()) {
|
|
38
|
+
ref.set(unwrapped);
|
|
39
|
+
}
|
|
40
|
+
return ref;
|
|
41
|
+
}
|
|
42
|
+
/** Creates a Reference wrapper for an entity identified by its primary key, wrapped in a Ref. */
|
|
43
|
+
static createFromPK(entityType, pk, options) {
|
|
44
|
+
const ref = this.createNakedFromPK(entityType, pk, options);
|
|
45
|
+
return helper(ref)?.toReference() ?? ref;
|
|
46
|
+
}
|
|
47
|
+
/** Creates an uninitialized entity reference by primary key without wrapping it in a Reference. */
|
|
48
|
+
static createNakedFromPK(entityType, pk, options) {
|
|
49
|
+
const factory = entityType.prototype.__factory;
|
|
50
|
+
if (!factory) {
|
|
51
|
+
// this can happen only if `ref()` is used as a property initializer, and the value is important only for the
|
|
52
|
+
// inference of defaults, so it's fine to return it directly without wrapping with `Reference` class
|
|
53
|
+
return pk;
|
|
54
|
+
}
|
|
55
|
+
const entity = factory.createReference(entityType, pk, {
|
|
56
|
+
merge: false,
|
|
57
|
+
convertCustomTypes: false,
|
|
58
|
+
...options,
|
|
59
|
+
});
|
|
60
|
+
const wrapped = helper(entity);
|
|
61
|
+
wrapped.__meta.primaryKeys.forEach(key => wrapped.__loadedProperties.add(key));
|
|
62
|
+
wrapped.__originalEntityData = factory.getComparator().prepareEntity(entity);
|
|
63
|
+
return entity;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Checks whether the argument is instance of `Reference` wrapper.
|
|
67
|
+
*/
|
|
68
|
+
static isReference(data) {
|
|
69
|
+
return data != null && Object.hasOwn(data, referenceSymbol);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Wraps the entity in a `Reference` wrapper if the property is defined as `ref`.
|
|
73
|
+
*/
|
|
74
|
+
static wrapReference(entity, prop) {
|
|
75
|
+
if (entity && prop.ref && !Reference.isReference(entity)) {
|
|
76
|
+
const ref = Reference.create(entity);
|
|
77
|
+
ref.property = prop;
|
|
78
|
+
return ref;
|
|
79
|
+
}
|
|
80
|
+
return entity;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns wrapped entity.
|
|
84
|
+
*/
|
|
85
|
+
static unwrapReference(ref) {
|
|
86
|
+
return Reference.isReference(ref) ? ref.unwrap() : ref;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded). Returns the entity.
|
|
90
|
+
* If the entity is not found in the database (e.g. it was deleted in the meantime, or currently active filters disallow loading of it)
|
|
91
|
+
* the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case.
|
|
92
|
+
*/
|
|
93
|
+
async load(options = {}) {
|
|
94
|
+
const wrapped = helper(this.entity);
|
|
95
|
+
if (!wrapped.__em) {
|
|
96
|
+
return this.entity;
|
|
97
|
+
}
|
|
98
|
+
options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property?.filters, options.filters) };
|
|
99
|
+
if (this.isInitialized() && !options.refresh && options.populate) {
|
|
100
|
+
await wrapped.__em.populate(this.entity, options.populate, options);
|
|
101
|
+
}
|
|
102
|
+
if (!this.isInitialized() || options.refresh) {
|
|
103
|
+
if (options.dataloader ??
|
|
104
|
+
[DataloaderType.ALL, DataloaderType.REFERENCE].includes(wrapped.__em.config.getDataloaderType())) {
|
|
105
|
+
const dataLoader = await wrapped.__em.getDataLoader('ref');
|
|
106
|
+
return dataLoader.load([this, options]);
|
|
107
|
+
}
|
|
108
|
+
return wrapped.init(options);
|
|
109
|
+
}
|
|
110
|
+
return this.entity;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
114
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
115
|
+
*/
|
|
116
|
+
async loadOrFail(options = {}) {
|
|
117
|
+
const ret = await this.load(options);
|
|
118
|
+
if (!ret) {
|
|
119
|
+
const wrapped = helper(this.entity);
|
|
120
|
+
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
121
|
+
const entityName = this.entity.constructor.name;
|
|
122
|
+
const where = wrapped.getPrimaryKey();
|
|
123
|
+
throw options.failHandler(entityName, where);
|
|
124
|
+
}
|
|
125
|
+
return ret;
|
|
126
|
+
}
|
|
127
|
+
set(entity) {
|
|
128
|
+
this.entity = Reference.unwrapReference(entity);
|
|
129
|
+
delete helper(this.entity).__reference;
|
|
130
|
+
}
|
|
131
|
+
/** Returns the underlying entity without checking initialization state. */
|
|
132
|
+
unwrap() {
|
|
133
|
+
return this.entity;
|
|
134
|
+
}
|
|
135
|
+
/** Returns the underlying entity, throwing an error if the reference is not initialized. */
|
|
136
|
+
getEntity() {
|
|
137
|
+
if (!this.isInitialized()) {
|
|
138
|
+
throw new Error(`Reference<${helper(this.entity).__meta.name}> ${helper(this.entity).getPrimaryKey()} not initialized`);
|
|
139
|
+
}
|
|
140
|
+
return this.entity;
|
|
141
|
+
}
|
|
142
|
+
/** Returns the value of a property on the underlying entity. Throws if the reference is not initialized. */
|
|
143
|
+
getProperty(prop) {
|
|
144
|
+
return this.getEntity()[prop];
|
|
145
|
+
}
|
|
146
|
+
/** Loads the entity if needed, then returns the value of the specified property. */
|
|
147
|
+
async loadProperty(prop, options) {
|
|
148
|
+
await this.loadOrFail(options);
|
|
149
|
+
return this.getEntity()[prop];
|
|
150
|
+
}
|
|
151
|
+
/** Returns whether the underlying entity has been fully loaded from the database. */
|
|
152
|
+
isInitialized() {
|
|
153
|
+
return helper(this.entity).__initialized;
|
|
154
|
+
}
|
|
155
|
+
/** Marks the underlying entity as populated or not for serialization purposes. */
|
|
156
|
+
populated(populated) {
|
|
157
|
+
helper(this.entity).populated(populated);
|
|
158
|
+
}
|
|
159
|
+
/** Serializes the underlying entity to a plain JSON object. */
|
|
160
|
+
toJSON(...args) {
|
|
161
|
+
return wrap(this.entity).toJSON(...args);
|
|
162
|
+
}
|
|
163
|
+
/** @ignore */
|
|
164
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
165
|
+
const object = { ...this };
|
|
166
|
+
const hidden = ['meta', 'property'];
|
|
167
|
+
hidden.forEach(k => delete object[k]);
|
|
168
|
+
const ret = inspect(object, { depth });
|
|
169
|
+
const wrapped = helper(this.entity);
|
|
170
|
+
const meta = wrapped.__meta;
|
|
171
|
+
/* v8 ignore next */
|
|
172
|
+
const pk = wrapped.hasPrimaryKey() ? '<' + wrapped.getSerializedPrimaryKey() + '>' : '';
|
|
173
|
+
const name = `Ref<${meta.className}${pk}>`;
|
|
174
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
175
|
+
}
|
|
180
176
|
}
|
|
181
177
|
/** Wrapper for lazy scalar properties that provides on-demand loading from the database. */
|
|
182
178
|
export class ScalarReference {
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
179
|
+
value;
|
|
180
|
+
entity;
|
|
181
|
+
#property;
|
|
182
|
+
#initialized;
|
|
183
|
+
constructor(value, initialized = value != null) {
|
|
184
|
+
this.value = value;
|
|
185
|
+
Object.defineProperty(this, scalarReferenceSymbol, { value: true, enumerable: false });
|
|
186
|
+
this.#initialized = initialized;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
190
|
+
* Returns either the whole entity, or the requested property.
|
|
191
|
+
*/
|
|
192
|
+
async load(options) {
|
|
193
|
+
const opts = typeof options === 'object' ? options : { prop: options };
|
|
194
|
+
if (!this.#initialized || opts.refresh) {
|
|
195
|
+
if (this.entity == null || this.#property == null) {
|
|
196
|
+
throw new Error('Cannot load scalar reference that is not bound to an entity property.');
|
|
197
|
+
}
|
|
198
|
+
await helper(this.entity).populate([this.#property], opts);
|
|
199
|
+
}
|
|
200
|
+
return this.value;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
204
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
205
|
+
*/
|
|
206
|
+
async loadOrFail(options = {}) {
|
|
207
|
+
const ret = await this.load(options);
|
|
208
|
+
if (ret == null) {
|
|
209
|
+
const wrapped = helper(this.entity);
|
|
210
|
+
options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
|
|
211
|
+
const entityName = this.entity.constructor.name;
|
|
212
|
+
throw NotFoundError.failedToLoadProperty(entityName, this.#property, wrapped.getPrimaryKey());
|
|
213
|
+
}
|
|
214
|
+
return ret;
|
|
215
|
+
}
|
|
216
|
+
/** Sets the scalar value and marks the reference as initialized. */
|
|
217
|
+
set(value) {
|
|
218
|
+
this.value = value;
|
|
219
|
+
this.#initialized = true;
|
|
220
|
+
}
|
|
221
|
+
/** Binds this scalar reference to a specific entity and property for lazy loading support. */
|
|
222
|
+
bind(entity, property) {
|
|
223
|
+
this.entity = entity;
|
|
224
|
+
this.#property = property;
|
|
225
|
+
Object.defineProperty(this, 'entity', { enumerable: false, value: entity });
|
|
226
|
+
}
|
|
227
|
+
/** Returns the current scalar value, or undefined if not yet loaded. */
|
|
228
|
+
unwrap() {
|
|
229
|
+
return this.value;
|
|
230
|
+
}
|
|
231
|
+
/** Returns whether the scalar value has been loaded. */
|
|
232
|
+
isInitialized() {
|
|
233
|
+
return this.#initialized;
|
|
234
|
+
}
|
|
235
|
+
static isScalarReference(data) {
|
|
236
|
+
return data != null && typeof data === 'object' && Object.hasOwn(data, scalarReferenceSymbol);
|
|
237
|
+
}
|
|
238
|
+
/** @ignore */
|
|
239
|
+
/* v8 ignore next */
|
|
240
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
241
|
+
return this.#initialized ? `Ref<${inspect(this.value)}>` : `Ref<?>`;
|
|
242
|
+
}
|
|
247
243
|
}
|
|
248
244
|
Object.defineProperties(Reference.prototype, {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
245
|
+
__meta: {
|
|
246
|
+
get() {
|
|
247
|
+
return this.entity.__meta;
|
|
248
|
+
},
|
|
252
249
|
},
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
250
|
+
__platform: {
|
|
251
|
+
get() {
|
|
252
|
+
return this.entity.__platform;
|
|
253
|
+
},
|
|
257
254
|
},
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
255
|
+
__helper: {
|
|
256
|
+
get() {
|
|
257
|
+
return this.entity.__helper;
|
|
258
|
+
},
|
|
262
259
|
},
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
260
|
+
$: {
|
|
261
|
+
get() {
|
|
262
|
+
return this.entity;
|
|
263
|
+
},
|
|
267
264
|
},
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
265
|
+
get: {
|
|
266
|
+
get() {
|
|
267
|
+
return () => this.entity;
|
|
268
|
+
},
|
|
272
269
|
},
|
|
273
|
-
},
|
|
274
270
|
});
|
|
275
271
|
Object.defineProperties(ScalarReference.prototype, {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
272
|
+
$: {
|
|
273
|
+
get() {
|
|
274
|
+
return this.value;
|
|
275
|
+
},
|
|
279
276
|
},
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
277
|
+
get: {
|
|
278
|
+
get() {
|
|
279
|
+
return () => this.value;
|
|
280
|
+
},
|
|
284
281
|
},
|
|
285
|
-
},
|
|
286
282
|
});
|
|
287
283
|
/**
|
|
288
284
|
* shortcut for `wrap(entity).toReference()`
|
|
289
285
|
*/
|
|
290
286
|
export function ref(entityOrType, pk) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
287
|
+
if (entityOrType == null) {
|
|
288
|
+
return entityOrType;
|
|
289
|
+
}
|
|
290
|
+
if (Utils.isEntity(entityOrType, true)) {
|
|
291
|
+
return helper(entityOrType).toReference();
|
|
292
|
+
}
|
|
293
|
+
if (Utils.isEntity(pk, true)) {
|
|
294
|
+
return helper(pk).toReference();
|
|
295
|
+
}
|
|
296
|
+
if (arguments.length === 1) {
|
|
297
|
+
return new ScalarReference(entityOrType, true);
|
|
298
|
+
}
|
|
299
|
+
if (pk == null) {
|
|
300
|
+
return pk;
|
|
301
|
+
}
|
|
302
|
+
return Reference.createFromPK(entityOrType, pk);
|
|
307
303
|
}
|
|
308
304
|
/**
|
|
309
305
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
310
306
|
*/
|
|
311
307
|
export function rel(entityType, pk) {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
308
|
+
if (pk == null || Utils.isEntity(pk)) {
|
|
309
|
+
return pk;
|
|
310
|
+
}
|
|
311
|
+
return Reference.createNakedFromPK(entityType, pk);
|
|
316
312
|
}
|
|
317
313
|
export { Reference as Ref };
|