@yandjin-mikro-orm/core 6.1.4-rc-sti-changes-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 +553 -0
- package/EntityManager.js +1736 -0
- package/LICENSE +21 -0
- package/MikroORM.d.ts +102 -0
- package/MikroORM.js +258 -0
- package/README.md +383 -0
- package/cache/CacheAdapter.d.ts +40 -0
- package/cache/CacheAdapter.js +2 -0
- package/cache/FileCacheAdapter.d.ts +31 -0
- package/cache/FileCacheAdapter.js +90 -0
- package/cache/GeneratedCacheAdapter.d.ts +25 -0
- package/cache/GeneratedCacheAdapter.js +38 -0
- package/cache/MemoryCacheAdapter.d.ts +24 -0
- package/cache/MemoryCacheAdapter.js +44 -0
- package/cache/NullCacheAdapter.d.ts +19 -0
- package/cache/NullCacheAdapter.js +30 -0
- package/cache/index.d.ts +5 -0
- package/cache/index.js +21 -0
- package/connections/Connection.d.ts +85 -0
- package/connections/Connection.js +128 -0
- package/connections/index.d.ts +1 -0
- package/connections/index.js +17 -0
- package/decorators/Check.d.ts +3 -0
- package/decorators/Check.js +17 -0
- package/decorators/CreateRequestContext.d.ts +2 -0
- package/decorators/CreateRequestContext.js +31 -0
- package/decorators/Embeddable.d.ts +8 -0
- package/decorators/Embeddable.js +15 -0
- package/decorators/Embedded.d.ts +13 -0
- package/decorators/Embedded.js +21 -0
- package/decorators/EnsureRequestContext.d.ts +2 -0
- package/decorators/EnsureRequestContext.js +26 -0
- package/decorators/Entity.d.ts +18 -0
- package/decorators/Entity.js +17 -0
- package/decorators/Enum.d.ts +9 -0
- package/decorators/Enum.js +20 -0
- package/decorators/Filter.d.ts +2 -0
- package/decorators/Filter.js +12 -0
- package/decorators/Formula.d.ts +5 -0
- package/decorators/Formula.js +19 -0
- package/decorators/Indexed.d.ts +12 -0
- package/decorators/Indexed.js +25 -0
- package/decorators/ManyToMany.d.ts +21 -0
- package/decorators/ManyToMany.js +17 -0
- package/decorators/ManyToOne.d.ts +15 -0
- package/decorators/ManyToOne.js +17 -0
- package/decorators/OneToMany.d.ts +18 -0
- package/decorators/OneToMany.js +21 -0
- package/decorators/OneToOne.d.ts +12 -0
- package/decorators/OneToOne.js +11 -0
- package/decorators/PrimaryKey.d.ts +8 -0
- package/decorators/PrimaryKey.js +24 -0
- package/decorators/Property.d.ts +218 -0
- package/decorators/Property.js +35 -0
- package/decorators/Subscriber.d.ts +1 -0
- package/decorators/Subscriber.js +2 -0
- package/decorators/hooks.d.ts +16 -0
- package/decorators/hooks.js +60 -0
- package/decorators/index.d.ts +17 -0
- package/decorators/index.js +36 -0
- package/drivers/DatabaseDriver.d.ts +72 -0
- package/drivers/DatabaseDriver.js +358 -0
- package/drivers/IDatabaseDriver.d.ts +193 -0
- package/drivers/IDatabaseDriver.js +4 -0
- package/drivers/index.d.ts +2 -0
- package/drivers/index.js +18 -0
- package/entity/ArrayCollection.d.ts +113 -0
- package/entity/ArrayCollection.js +386 -0
- package/entity/BaseEntity.d.ts +22 -0
- package/entity/BaseEntity.js +47 -0
- package/entity/Collection.d.ts +104 -0
- package/entity/Collection.js +373 -0
- package/entity/EntityAssigner.d.ts +28 -0
- package/entity/EntityAssigner.js +226 -0
- package/entity/EntityFactory.d.ts +41 -0
- package/entity/EntityFactory.js +302 -0
- package/entity/EntityHelper.d.ts +29 -0
- package/entity/EntityHelper.js +250 -0
- package/entity/EntityIdentifier.d.ts +10 -0
- package/entity/EntityIdentifier.js +19 -0
- package/entity/EntityLoader.d.ts +65 -0
- package/entity/EntityLoader.js +579 -0
- package/entity/EntityRepository.d.ts +161 -0
- package/entity/EntityRepository.js +207 -0
- package/entity/EntityValidator.d.ts +19 -0
- package/entity/EntityValidator.js +152 -0
- package/entity/Reference.d.ts +89 -0
- package/entity/Reference.js +242 -0
- package/entity/WrappedEntity.d.ts +67 -0
- package/entity/WrappedEntity.js +146 -0
- package/entity/index.d.ts +13 -0
- package/entity/index.js +29 -0
- package/entity/wrap.d.ts +15 -0
- package/entity/wrap.js +26 -0
- package/enums.d.ts +160 -0
- package/enums.js +169 -0
- package/errors.d.ts +65 -0
- package/errors.js +222 -0
- package/events/EventManager.d.ts +17 -0
- package/events/EventManager.js +77 -0
- package/events/EventSubscriber.d.ts +39 -0
- package/events/EventSubscriber.js +2 -0
- package/events/TransactionEventBroadcaster.d.ts +11 -0
- package/events/TransactionEventBroadcaster.js +17 -0
- package/events/index.d.ts +3 -0
- package/events/index.js +19 -0
- package/exceptions.d.ts +104 -0
- package/exceptions.js +130 -0
- package/hydration/Hydrator.d.ts +23 -0
- package/hydration/Hydrator.js +51 -0
- package/hydration/ObjectHydrator.d.ts +24 -0
- package/hydration/ObjectHydrator.js +332 -0
- package/hydration/index.d.ts +2 -0
- package/hydration/index.js +18 -0
- package/index.d.ts +25 -0
- package/index.js +50 -0
- package/index.mjs +192 -0
- package/logging/DefaultLogger.d.ts +32 -0
- package/logging/DefaultLogger.js +90 -0
- package/logging/Logger.d.ts +56 -0
- package/logging/Logger.js +2 -0
- package/logging/SimpleLogger.d.ts +17 -0
- package/logging/SimpleLogger.js +31 -0
- package/logging/colors.d.ts +9 -0
- package/logging/colors.js +20 -0
- package/logging/index.d.ts +4 -0
- package/logging/index.js +20 -0
- package/metadata/EntitySchema.d.ts +74 -0
- package/metadata/EntitySchema.js +293 -0
- package/metadata/MetadataDiscovery.d.ts +71 -0
- package/metadata/MetadataDiscovery.js +1244 -0
- package/metadata/MetadataProvider.d.ts +11 -0
- package/metadata/MetadataProvider.js +23 -0
- package/metadata/MetadataStorage.d.ts +22 -0
- package/metadata/MetadataStorage.js +87 -0
- package/metadata/MetadataValidator.d.ts +24 -0
- package/metadata/MetadataValidator.js +213 -0
- package/metadata/ReflectMetadataProvider.d.ts +8 -0
- package/metadata/ReflectMetadataProvider.js +48 -0
- package/metadata/index.d.ts +6 -0
- package/metadata/index.js +22 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +18 -0
- package/naming-strategy/AbstractNamingStrategy.js +48 -0
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +12 -0
- package/naming-strategy/EntityCaseNamingStrategy.js +32 -0
- package/naming-strategy/MongoNamingStrategy.d.ts +9 -0
- package/naming-strategy/MongoNamingStrategy.js +25 -0
- package/naming-strategy/NamingStrategy.d.ts +52 -0
- package/naming-strategy/NamingStrategy.js +2 -0
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +10 -0
- package/naming-strategy/UnderscoreNamingStrategy.js +28 -0
- package/naming-strategy/index.d.ts +5 -0
- package/naming-strategy/index.js +21 -0
- package/package.json +70 -0
- package/platforms/ExceptionConverter.d.ts +5 -0
- package/platforms/ExceptionConverter.js +11 -0
- package/platforms/Platform.d.ts +201 -0
- package/platforms/Platform.js +452 -0
- package/platforms/index.d.ts +2 -0
- package/platforms/index.js +18 -0
- package/serialization/EntitySerializer.d.ts +34 -0
- package/serialization/EntitySerializer.js +206 -0
- package/serialization/EntityTransformer.d.ts +8 -0
- package/serialization/EntityTransformer.js +192 -0
- package/serialization/SerializationContext.d.ts +30 -0
- package/serialization/SerializationContext.js +111 -0
- package/serialization/index.d.ts +3 -0
- package/serialization/index.js +19 -0
- package/types/ArrayType.d.ts +13 -0
- package/types/ArrayType.js +47 -0
- package/types/BigIntType.d.ts +16 -0
- package/types/BigIntType.js +45 -0
- package/types/BlobType.d.ts +10 -0
- package/types/BlobType.js +27 -0
- package/types/BooleanType.d.ts +8 -0
- package/types/BooleanType.js +16 -0
- package/types/DateTimeType.d.ts +8 -0
- package/types/DateTimeType.js +16 -0
- package/types/DateType.d.ts +8 -0
- package/types/DateType.js +16 -0
- package/types/DecimalType.d.ts +11 -0
- package/types/DecimalType.js +22 -0
- package/types/DoubleType.d.ts +11 -0
- package/types/DoubleType.js +22 -0
- package/types/EnumArrayType.d.ts +9 -0
- package/types/EnumArrayType.js +32 -0
- package/types/EnumType.d.ts +8 -0
- package/types/EnumType.js +16 -0
- package/types/FloatType.d.ts +8 -0
- package/types/FloatType.js +16 -0
- package/types/IntegerType.d.ts +8 -0
- package/types/IntegerType.js +16 -0
- package/types/IntervalType.d.ts +8 -0
- package/types/IntervalType.js +16 -0
- package/types/JsonType.d.ts +13 -0
- package/types/JsonType.js +34 -0
- package/types/MediumIntType.d.ts +6 -0
- package/types/MediumIntType.js +10 -0
- package/types/SmallIntType.d.ts +8 -0
- package/types/SmallIntType.js +16 -0
- package/types/StringType.d.ts +8 -0
- package/types/StringType.js +16 -0
- package/types/TextType.d.ts +8 -0
- package/types/TextType.js +16 -0
- package/types/TimeType.d.ts +9 -0
- package/types/TimeType.js +23 -0
- package/types/TinyIntType.d.ts +8 -0
- package/types/TinyIntType.js +16 -0
- package/types/Type.d.ts +64 -0
- package/types/Type.js +84 -0
- package/types/Uint8ArrayType.d.ts +11 -0
- package/types/Uint8ArrayType.js +37 -0
- package/types/UnknownType.d.ts +7 -0
- package/types/UnknownType.js +13 -0
- package/types/UuidType.d.ts +7 -0
- package/types/UuidType.js +13 -0
- package/types/index.d.ts +75 -0
- package/types/index.js +77 -0
- package/typings.d.ts +767 -0
- package/typings.js +198 -0
- package/unit-of-work/ChangeSet.d.ts +34 -0
- package/unit-of-work/ChangeSet.js +62 -0
- package/unit-of-work/ChangeSetComputer.d.ts +26 -0
- package/unit-of-work/ChangeSetComputer.js +153 -0
- package/unit-of-work/ChangeSetPersister.d.ts +50 -0
- package/unit-of-work/ChangeSetPersister.js +361 -0
- package/unit-of-work/CommitOrderCalculator.d.ts +62 -0
- package/unit-of-work/CommitOrderCalculator.js +113 -0
- package/unit-of-work/IdentityMap.d.ts +17 -0
- package/unit-of-work/IdentityMap.js +84 -0
- package/unit-of-work/UnitOfWork.d.ts +124 -0
- package/unit-of-work/UnitOfWork.js +1013 -0
- package/unit-of-work/index.d.ts +6 -0
- package/unit-of-work/index.js +22 -0
- package/utils/AbstractSchemaGenerator.d.ts +38 -0
- package/utils/AbstractSchemaGenerator.js +101 -0
- package/utils/Configuration.d.ts +390 -0
- package/utils/Configuration.js +357 -0
- package/utils/ConfigurationLoader.d.ts +29 -0
- package/utils/ConfigurationLoader.js +282 -0
- package/utils/Cursor.d.ts +77 -0
- package/utils/Cursor.js +169 -0
- package/utils/DataloaderUtils.d.ts +43 -0
- package/utils/DataloaderUtils.js +194 -0
- package/utils/EntityComparator.d.ts +73 -0
- package/utils/EntityComparator.js +568 -0
- package/utils/NullHighlighter.d.ts +4 -0
- package/utils/NullHighlighter.js +9 -0
- package/utils/QueryHelper.d.ts +28 -0
- package/utils/QueryHelper.js +228 -0
- package/utils/RawQueryFragment.d.ts +96 -0
- package/utils/RawQueryFragment.js +188 -0
- package/utils/RequestContext.d.ts +34 -0
- package/utils/RequestContext.js +54 -0
- package/utils/TransactionContext.d.ts +19 -0
- package/utils/TransactionContext.js +34 -0
- package/utils/Utils.d.ts +274 -0
- package/utils/Utils.js +1073 -0
- package/utils/clone.d.ts +6 -0
- package/utils/clone.js +127 -0
- package/utils/index.d.ts +13 -0
- package/utils/index.js +29 -0
- package/utils/upsert-utils.d.ts +6 -0
- package/utils/upsert-utils.js +33 -0
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Collection = void 0;
|
|
4
|
+
const ArrayCollection_1 = require("./ArrayCollection");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const enums_1 = require("../enums");
|
|
8
|
+
const Reference_1 = require("./Reference");
|
|
9
|
+
const wrap_1 = require("./wrap");
|
|
10
|
+
class Collection extends ArrayCollection_1.ArrayCollection {
|
|
11
|
+
readonly;
|
|
12
|
+
_populated;
|
|
13
|
+
_em;
|
|
14
|
+
// this is for some reason needed for TS, otherwise it can fail with `Type instantiation is excessively deep and possibly infinite.`
|
|
15
|
+
_snapshot;
|
|
16
|
+
constructor(owner, items, initialized = true) {
|
|
17
|
+
super(owner, items);
|
|
18
|
+
this.initialized = !!items || initialized;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates new Collection instance, assigns it to the owning entity and sets the items to it (propagating them to their inverse sides)
|
|
22
|
+
*/
|
|
23
|
+
static create(owner, prop, items, initialized) {
|
|
24
|
+
const coll = new Collection(owner, undefined, initialized);
|
|
25
|
+
coll.property = (0, wrap_1.helper)(owner).__meta.properties[prop];
|
|
26
|
+
owner[prop] = coll;
|
|
27
|
+
if (items) {
|
|
28
|
+
coll.set(items);
|
|
29
|
+
}
|
|
30
|
+
return coll;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Ensures the collection is loaded first (without reloading it if it already is loaded).
|
|
34
|
+
* Returns the Collection instance (itself), works the same as `Reference.load()`.
|
|
35
|
+
*/
|
|
36
|
+
async load(options = {}) {
|
|
37
|
+
if (this.isInitialized(true) && !options.refresh) {
|
|
38
|
+
const em = this.getEntityManager(this.items, false);
|
|
39
|
+
await em?.populate(this.items, options.populate, options);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
await this.init(options);
|
|
43
|
+
}
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Initializes the collection and returns the items
|
|
48
|
+
*/
|
|
49
|
+
async loadItems(options) {
|
|
50
|
+
await this.load(options);
|
|
51
|
+
return super.getItems();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the count of collection items from database instead of counting loaded items.
|
|
55
|
+
* The value is cached (unless you use the `where` option), use `refresh: true` to force reload it.
|
|
56
|
+
*/
|
|
57
|
+
async loadCount(options = {}) {
|
|
58
|
+
options = typeof options === 'boolean' ? { refresh: options } : options;
|
|
59
|
+
const { refresh, where, ...countOptions } = options;
|
|
60
|
+
if (!refresh && !where && utils_1.Utils.isDefined(this._count)) {
|
|
61
|
+
return this._count;
|
|
62
|
+
}
|
|
63
|
+
const em = this.getEntityManager();
|
|
64
|
+
if (!em.getPlatform().usesPivotTable() && this.property.kind === enums_1.ReferenceKind.MANY_TO_MANY && this.property.owner) {
|
|
65
|
+
return this._count = this.length;
|
|
66
|
+
}
|
|
67
|
+
const cond = this.createLoadCountCondition(where ?? {});
|
|
68
|
+
const count = await em.count(this.property.type, cond, countOptions);
|
|
69
|
+
if (!where) {
|
|
70
|
+
this._count = count;
|
|
71
|
+
}
|
|
72
|
+
return count;
|
|
73
|
+
}
|
|
74
|
+
async matching(options) {
|
|
75
|
+
const em = this.getEntityManager();
|
|
76
|
+
const { where, ctx, ...opts } = options;
|
|
77
|
+
opts.orderBy = this.createOrderBy(opts.orderBy);
|
|
78
|
+
let items;
|
|
79
|
+
if (this.property.kind === enums_1.ReferenceKind.MANY_TO_MANY && em.getPlatform().usesPivotTable()) {
|
|
80
|
+
const cond = await em.applyFilters(this.property.type, where, options.filters ?? {}, 'read');
|
|
81
|
+
const map = await em.getDriver().loadFromPivotTable(this.property, [(0, wrap_1.helper)(this.owner).__primaryKeys], cond, opts.orderBy, ctx, options);
|
|
82
|
+
items = map[(0, wrap_1.helper)(this.owner).getSerializedPrimaryKey()].map((item) => em.merge(this.property.type, item, { convertCustomTypes: true }));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
items = await em.find(this.property.type, this.createCondition(where), opts);
|
|
86
|
+
}
|
|
87
|
+
if (options.store) {
|
|
88
|
+
this.hydrate(items, true);
|
|
89
|
+
this.populated();
|
|
90
|
+
this.readonly = true;
|
|
91
|
+
}
|
|
92
|
+
return items;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Returns the items (the collection must be initialized)
|
|
96
|
+
*/
|
|
97
|
+
getItems(check = true) {
|
|
98
|
+
if (check) {
|
|
99
|
+
this.checkInitialized();
|
|
100
|
+
}
|
|
101
|
+
return super.getItems();
|
|
102
|
+
}
|
|
103
|
+
toJSON() {
|
|
104
|
+
if (!this.isInitialized()) {
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
return super.toJSON();
|
|
108
|
+
}
|
|
109
|
+
add(entity, ...entities) {
|
|
110
|
+
entities = utils_1.Utils.asArray(entity).concat(entities);
|
|
111
|
+
const unwrapped = entities.map(i => Reference_1.Reference.unwrapReference(i));
|
|
112
|
+
unwrapped.forEach(entity => this.validateItemType(entity));
|
|
113
|
+
this.modify('add', unwrapped);
|
|
114
|
+
this.cancelOrphanRemoval(unwrapped);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* @inheritDoc
|
|
118
|
+
*/
|
|
119
|
+
remove(entity, ...entities) {
|
|
120
|
+
if (entity instanceof Function) {
|
|
121
|
+
for (const item of this.items) {
|
|
122
|
+
if (entity(item)) {
|
|
123
|
+
this.remove(item);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
entities = utils_1.Utils.asArray(entity).concat(entities);
|
|
129
|
+
const unwrapped = entities.map(i => Reference_1.Reference.unwrapReference(i));
|
|
130
|
+
this.modify('remove', unwrapped);
|
|
131
|
+
const em = this.getEntityManager(unwrapped, false);
|
|
132
|
+
if (this.property.orphanRemoval && em) {
|
|
133
|
+
for (const item of unwrapped) {
|
|
134
|
+
em.getUnitOfWork().scheduleOrphanRemoval(item);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
contains(item, check = true) {
|
|
139
|
+
if (check) {
|
|
140
|
+
this.checkInitialized();
|
|
141
|
+
}
|
|
142
|
+
return super.contains(item);
|
|
143
|
+
}
|
|
144
|
+
count() {
|
|
145
|
+
this.checkInitialized();
|
|
146
|
+
return super.count();
|
|
147
|
+
}
|
|
148
|
+
isEmpty() {
|
|
149
|
+
this.checkInitialized();
|
|
150
|
+
return super.isEmpty();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* @inheritDoc
|
|
154
|
+
*/
|
|
155
|
+
slice(start, end) {
|
|
156
|
+
this.checkInitialized();
|
|
157
|
+
return super.slice(start, end);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @inheritDoc
|
|
161
|
+
*/
|
|
162
|
+
exists(cb) {
|
|
163
|
+
this.checkInitialized();
|
|
164
|
+
return super.exists(cb);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @inheritDoc
|
|
168
|
+
*/
|
|
169
|
+
find(cb) {
|
|
170
|
+
this.checkInitialized();
|
|
171
|
+
return super.find(cb);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* @inheritDoc
|
|
175
|
+
*/
|
|
176
|
+
filter(cb) {
|
|
177
|
+
this.checkInitialized();
|
|
178
|
+
return super.filter(cb);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* @inheritDoc
|
|
182
|
+
*/
|
|
183
|
+
map(mapper) {
|
|
184
|
+
this.checkInitialized();
|
|
185
|
+
return super.map(mapper);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @inheritDoc
|
|
189
|
+
*/
|
|
190
|
+
indexBy(key, valueKey) {
|
|
191
|
+
this.checkInitialized();
|
|
192
|
+
return super.indexBy(key, valueKey);
|
|
193
|
+
}
|
|
194
|
+
shouldPopulate(populated) {
|
|
195
|
+
if (!this.isInitialized(true)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (this._populated != null) {
|
|
199
|
+
return this._populated;
|
|
200
|
+
}
|
|
201
|
+
return !!populated;
|
|
202
|
+
}
|
|
203
|
+
populated(populated = true) {
|
|
204
|
+
this._populated = populated;
|
|
205
|
+
}
|
|
206
|
+
async init(options = {}) {
|
|
207
|
+
if (this.dirty) {
|
|
208
|
+
const items = [...this.items];
|
|
209
|
+
this.dirty = false;
|
|
210
|
+
await this.init(options);
|
|
211
|
+
items.forEach(i => this.add(i));
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
const em = this.getEntityManager();
|
|
215
|
+
if (options.dataloader ?? [enums_1.DataloaderType.ALL, enums_1.DataloaderType.COLLECTION].includes(utils_1.DataloaderUtils.getDataloaderType(em.config.get('dataloader')))) {
|
|
216
|
+
const order = [...this.items]; // copy order of references
|
|
217
|
+
const customOrder = !!options.orderBy;
|
|
218
|
+
// eslint-disable-next-line dot-notation
|
|
219
|
+
const items = await em['colLoader'].load([this, options]);
|
|
220
|
+
if (!customOrder) {
|
|
221
|
+
this.reorderItems(items, order);
|
|
222
|
+
}
|
|
223
|
+
this.items.clear();
|
|
224
|
+
let i = 0;
|
|
225
|
+
for (const item of items) {
|
|
226
|
+
this.items.add(item);
|
|
227
|
+
this[i++] = item;
|
|
228
|
+
}
|
|
229
|
+
this.initialized = true;
|
|
230
|
+
this.dirty = false;
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
const populate = Array.isArray(options.populate)
|
|
234
|
+
? options.populate.map(f => f === '*' ? f : `${this.property.name}.${f}`)
|
|
235
|
+
: [`${this.property.name}${options.ref ? ':ref' : ''}`];
|
|
236
|
+
const schema = this.property.targetMeta.schema === '*'
|
|
237
|
+
? (0, wrap_1.helper)(this.owner).__schema
|
|
238
|
+
: undefined;
|
|
239
|
+
await em.populate(this.owner, populate, {
|
|
240
|
+
...options,
|
|
241
|
+
refresh: true,
|
|
242
|
+
connectionType: options.connectionType,
|
|
243
|
+
schema,
|
|
244
|
+
where: { [this.property.name]: options.where },
|
|
245
|
+
orderBy: { [this.property.name]: options.orderBy },
|
|
246
|
+
});
|
|
247
|
+
return this;
|
|
248
|
+
}
|
|
249
|
+
getEntityManager(items = [], required = true) {
|
|
250
|
+
const wrapped = (0, wrap_1.helper)(this.owner);
|
|
251
|
+
let em = (this._em ?? wrapped.__em);
|
|
252
|
+
if (!em) {
|
|
253
|
+
for (const i of this.items) {
|
|
254
|
+
if (i && (0, wrap_1.helper)(i).__em) {
|
|
255
|
+
em = (0, wrap_1.helper)(i).__em;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const i of items) {
|
|
260
|
+
if (i && (0, wrap_1.helper)(i).__em) {
|
|
261
|
+
em = (0, wrap_1.helper)(i).__em;
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (em) {
|
|
267
|
+
Object.defineProperty(this, '_em', { value: em });
|
|
268
|
+
}
|
|
269
|
+
if (!em && required) {
|
|
270
|
+
throw errors_1.ValidationError.entityNotManaged(this.owner);
|
|
271
|
+
}
|
|
272
|
+
return em;
|
|
273
|
+
}
|
|
274
|
+
createCondition(cond = {}) {
|
|
275
|
+
if (this.property.kind === enums_1.ReferenceKind.ONE_TO_MANY) {
|
|
276
|
+
cond[this.property.mappedBy] = (0, wrap_1.helper)(this.owner).getPrimaryKey();
|
|
277
|
+
}
|
|
278
|
+
else { // MANY_TO_MANY
|
|
279
|
+
this.createManyToManyCondition(cond);
|
|
280
|
+
}
|
|
281
|
+
return cond;
|
|
282
|
+
}
|
|
283
|
+
createOrderBy(orderBy = []) {
|
|
284
|
+
if (utils_1.Utils.isEmpty(orderBy) && this.property.orderBy) {
|
|
285
|
+
orderBy = this.property.orderBy;
|
|
286
|
+
}
|
|
287
|
+
return utils_1.Utils.asArray(orderBy);
|
|
288
|
+
}
|
|
289
|
+
createManyToManyCondition(cond) {
|
|
290
|
+
const dict = cond;
|
|
291
|
+
if (this.property.owner || this.property.pivotTable) {
|
|
292
|
+
// we know there is at least one item as it was checked in load method
|
|
293
|
+
const pk = this.property.targetMeta.primaryKeys[0];
|
|
294
|
+
dict[pk] = { $in: [] };
|
|
295
|
+
this.items.forEach(item => dict[pk].$in.push((0, wrap_1.helper)(item).getPrimaryKey()));
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
dict[this.property.mappedBy] = (0, wrap_1.helper)(this.owner).getPrimaryKey();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
createLoadCountCondition(cond) {
|
|
302
|
+
const wrapped = (0, wrap_1.helper)(this.owner);
|
|
303
|
+
const val = wrapped.__meta.compositePK ? { $in: wrapped.__primaryKeys } : wrapped.getPrimaryKey();
|
|
304
|
+
const dict = cond;
|
|
305
|
+
if (this.property.kind === enums_1.ReferenceKind.ONE_TO_MANY) {
|
|
306
|
+
dict[this.property.mappedBy] = val;
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
const key = this.property.owner ? this.property.inversedBy : this.property.mappedBy;
|
|
310
|
+
dict[key] = val;
|
|
311
|
+
}
|
|
312
|
+
return cond;
|
|
313
|
+
}
|
|
314
|
+
modify(method, items) {
|
|
315
|
+
if (method === 'remove') {
|
|
316
|
+
this.checkInitialized();
|
|
317
|
+
}
|
|
318
|
+
this.validateModification(items);
|
|
319
|
+
super[method](items);
|
|
320
|
+
this.setDirty();
|
|
321
|
+
}
|
|
322
|
+
checkInitialized() {
|
|
323
|
+
if (!this.isInitialized()) {
|
|
324
|
+
throw new Error(`Collection<${this.property.type}> of entity ${this.owner.constructor.name}[${(0, wrap_1.helper)(this.owner).getSerializedPrimaryKey()}] not initialized`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* re-orders items after searching with `$in` operator
|
|
329
|
+
*/
|
|
330
|
+
reorderItems(items, order) {
|
|
331
|
+
if (this.property.kind === enums_1.ReferenceKind.MANY_TO_MANY && this.property.owner) {
|
|
332
|
+
items.sort((a, b) => order.indexOf(a) - order.indexOf(b));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
cancelOrphanRemoval(items) {
|
|
336
|
+
const em = this.getEntityManager(items, false);
|
|
337
|
+
if (!em) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
for (const item of items) {
|
|
341
|
+
em.getUnitOfWork().cancelOrphanRemoval(item);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
validateItemType(item) {
|
|
345
|
+
if (!utils_1.Utils.isEntity(item)) {
|
|
346
|
+
throw errors_1.ValidationError.notEntity(this.owner, this.property, item);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
validateModification(items) {
|
|
350
|
+
if (this.readonly) {
|
|
351
|
+
throw errors_1.ValidationError.cannotModifyReadonlyCollection(this.owner, this.property);
|
|
352
|
+
}
|
|
353
|
+
// currently we allow persisting to inverse sides only in SQL drivers
|
|
354
|
+
if (this.property.pivotTable || !this.property.mappedBy) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
const check = (item) => {
|
|
358
|
+
if (!item || (0, wrap_1.helper)(item).__initialized) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
return !item[this.property.mappedBy] && this.property.kind === enums_1.ReferenceKind.MANY_TO_MANY;
|
|
362
|
+
};
|
|
363
|
+
// throw if we are modifying inverse side of M:N collection when owning side is initialized (would be ignored when persisting)
|
|
364
|
+
if (items.find(item => check(item))) {
|
|
365
|
+
throw errors_1.ValidationError.cannotModifyInverseCollection(this.owner, this.property);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
exports.Collection = Collection;
|
|
370
|
+
Object.defineProperties(Collection.prototype, {
|
|
371
|
+
$: { get() { return this; } },
|
|
372
|
+
get: { get() { return () => this; } },
|
|
373
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { EntityManager } from '../EntityManager';
|
|
2
|
+
import type { EntityData, EntityDTO, EntityProperty, IsSubset, FromEntityType, MergeSelected } from '../typings';
|
|
3
|
+
export declare class EntityAssigner {
|
|
4
|
+
static assign<Entity extends object, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Data extends EntityData<Naked> | Partial<EntityDTO<Naked>> = EntityData<Naked> | Partial<EntityDTO<Naked>>>(entity: Entity, data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions): MergeSelected<Entity, Naked, keyof Data & string>;
|
|
5
|
+
private static assignProperty;
|
|
6
|
+
/**
|
|
7
|
+
* auto-wire 1:1 inverse side with owner as in no-sql drivers it can't be joined
|
|
8
|
+
* also makes sure the link is bidirectional when creating new entities from nested structures
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
static autoWireOneToOne<T extends object, O extends object>(prop: EntityProperty<O, T>, entity: O): void;
|
|
12
|
+
private static validateEM;
|
|
13
|
+
private static assignReference;
|
|
14
|
+
private static assignCollection;
|
|
15
|
+
private static assignEmbeddable;
|
|
16
|
+
private static createCollectionItem;
|
|
17
|
+
}
|
|
18
|
+
export declare const assign: typeof EntityAssigner.assign;
|
|
19
|
+
export interface AssignOptions {
|
|
20
|
+
updateNestedEntities?: boolean;
|
|
21
|
+
updateByPrimaryKey?: boolean;
|
|
22
|
+
onlyProperties?: boolean;
|
|
23
|
+
convertCustomTypes?: boolean;
|
|
24
|
+
mergeObjectProperties?: boolean;
|
|
25
|
+
merge?: boolean;
|
|
26
|
+
schema?: string;
|
|
27
|
+
em?: EntityManager;
|
|
28
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assign = exports.EntityAssigner = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const Collection_1 = require("./Collection");
|
|
6
|
+
const Utils_1 = require("../utils/Utils");
|
|
7
|
+
const Reference_1 = require("./Reference");
|
|
8
|
+
const enums_1 = require("../enums");
|
|
9
|
+
const EntityValidator_1 = require("./EntityValidator");
|
|
10
|
+
const wrap_1 = require("./wrap");
|
|
11
|
+
const EntityHelper_1 = require("./EntityHelper");
|
|
12
|
+
const validator = new EntityValidator_1.EntityValidator(false);
|
|
13
|
+
class EntityAssigner {
|
|
14
|
+
static assign(entity, data, options = {}) {
|
|
15
|
+
let opts = options;
|
|
16
|
+
if (opts.visited?.has(entity)) {
|
|
17
|
+
return entity;
|
|
18
|
+
}
|
|
19
|
+
EntityHelper_1.EntityHelper.ensurePropagation(entity);
|
|
20
|
+
opts.visited ??= new Set();
|
|
21
|
+
opts.visited.add(entity);
|
|
22
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
23
|
+
opts = {
|
|
24
|
+
updateNestedEntities: true,
|
|
25
|
+
updateByPrimaryKey: true,
|
|
26
|
+
mergeObjectProperties: true,
|
|
27
|
+
schema: wrapped.__schema,
|
|
28
|
+
...opts, // allow overriding the defaults
|
|
29
|
+
};
|
|
30
|
+
const meta = wrapped.__meta;
|
|
31
|
+
const props = meta.properties;
|
|
32
|
+
Object.keys(data).forEach(prop => {
|
|
33
|
+
return EntityAssigner.assignProperty(entity, prop, props, data, {
|
|
34
|
+
...opts,
|
|
35
|
+
em: opts.em || wrapped.__em,
|
|
36
|
+
platform: wrapped.__platform,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
return entity;
|
|
40
|
+
}
|
|
41
|
+
static assignProperty(entity, propName, props, data, options) {
|
|
42
|
+
if (options.onlyProperties && !(propName in props)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let value = data[propName];
|
|
46
|
+
const prop = { ...props[propName], name: propName };
|
|
47
|
+
if (propName in props && !prop.nullable && value == null) {
|
|
48
|
+
throw new Error(`You must pass a non-${value} value to the property ${propName} of entity ${entity.constructor.name}.`);
|
|
49
|
+
}
|
|
50
|
+
// create collection instance if its missing so old items can be deleted with orphan removal
|
|
51
|
+
if ([enums_1.ReferenceKind.MANY_TO_MANY, enums_1.ReferenceKind.ONE_TO_MANY].includes(prop?.kind) && entity[prop.name] == null) {
|
|
52
|
+
entity[prop.name] = Collection_1.Collection.create(entity, prop.name, undefined, (0, wrap_1.helper)(entity).isInitialized());
|
|
53
|
+
}
|
|
54
|
+
if (prop && Utils_1.Utils.isCollection(entity[prop.name])) {
|
|
55
|
+
return EntityAssigner.assignCollection(entity, entity[prop.name], value, prop, options.em, options);
|
|
56
|
+
}
|
|
57
|
+
const customType = prop?.customType;
|
|
58
|
+
if (options.convertCustomTypes && customType && prop.kind === enums_1.ReferenceKind.SCALAR && !Utils_1.Utils.isEntity(data)) {
|
|
59
|
+
value = customType.convertToJSValue(value, options.platform);
|
|
60
|
+
}
|
|
61
|
+
if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop?.kind) && value != null) {
|
|
62
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
63
|
+
if (options.updateNestedEntities && entity.hasOwnProperty(propName) && Utils_1.Utils.isEntity(entity[propName], true) && Utils_1.Utils.isPlainObject(value)) {
|
|
64
|
+
const unwrappedEntity = Reference_1.Reference.unwrapReference(entity[propName]);
|
|
65
|
+
const wrapped = (0, wrap_1.helper)(unwrappedEntity);
|
|
66
|
+
if (options.updateByPrimaryKey) {
|
|
67
|
+
const pk = Utils_1.Utils.extractPK(value, prop.targetMeta);
|
|
68
|
+
if (pk) {
|
|
69
|
+
const ref = options.em.getReference(prop.type, pk, options);
|
|
70
|
+
// if the PK differs, we want to change the target entity, not update it
|
|
71
|
+
const wrappedChild = (0, wrap_1.helper)(ref);
|
|
72
|
+
const sameTarget = wrappedChild.getSerializedPrimaryKey() === wrapped.getSerializedPrimaryKey();
|
|
73
|
+
if (wrappedChild.__managed && wrappedChild.isInitialized() && sameTarget) {
|
|
74
|
+
return EntityAssigner.assign(ref, value, options);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return EntityAssigner.assignReference(entity, value, prop, options.em, options);
|
|
78
|
+
}
|
|
79
|
+
if (wrapped.__managed && (0, wrap_1.wrap)(unwrappedEntity).isInitialized()) {
|
|
80
|
+
return EntityAssigner.assign(unwrappedEntity, value, options);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return EntityAssigner.assignReference(entity, value, prop, options.em, options);
|
|
84
|
+
}
|
|
85
|
+
if (prop.kind === enums_1.ReferenceKind.SCALAR && enums_1.SCALAR_TYPES.includes(prop.runtimeType) && (prop.setter || !prop.getter)) {
|
|
86
|
+
return entity[prop.name] = validator.validateProperty(prop, value, entity);
|
|
87
|
+
}
|
|
88
|
+
if (prop.kind === enums_1.ReferenceKind.EMBEDDED && EntityAssigner.validateEM(options.em)) {
|
|
89
|
+
return EntityAssigner.assignEmbeddable(entity, value, prop, options.em, options);
|
|
90
|
+
}
|
|
91
|
+
if (options.mergeObjectProperties && Utils_1.Utils.isPlainObject(entity[propName]) && Utils_1.Utils.isPlainObject(value)) {
|
|
92
|
+
entity[propName] ??= {};
|
|
93
|
+
entity[propName] = Utils_1.Utils.merge({}, entity[propName], value);
|
|
94
|
+
}
|
|
95
|
+
else if (!prop || prop.setter || !prop.getter) {
|
|
96
|
+
entity[propName] = value;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* auto-wire 1:1 inverse side with owner as in no-sql drivers it can't be joined
|
|
101
|
+
* also makes sure the link is bidirectional when creating new entities from nested structures
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
static autoWireOneToOne(prop, entity) {
|
|
105
|
+
const ref = entity[prop.name];
|
|
106
|
+
if (prop.kind !== enums_1.ReferenceKind.ONE_TO_ONE || !Utils_1.Utils.isEntity(ref)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const meta2 = (0, wrap_1.helper)(ref).__meta;
|
|
110
|
+
const prop2 = meta2.properties[prop.inversedBy || prop.mappedBy];
|
|
111
|
+
/* istanbul ignore next */
|
|
112
|
+
if (prop2 && !ref[prop2.name]) {
|
|
113
|
+
if (Reference_1.Reference.isReference(ref)) {
|
|
114
|
+
ref.unwrap()[prop2.name] = Reference_1.Reference.wrapReference(entity, prop2);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
ref[prop2.name] = Reference_1.Reference.wrapReference(entity, prop2);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
static validateEM(em) {
|
|
122
|
+
if (!em) {
|
|
123
|
+
throw new Error(`To use assign() on not managed entities, explicitly provide EM instance: wrap(entity).assign(data, { em: orm.em })`);
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
static assignReference(entity, value, prop, em, options) {
|
|
128
|
+
if (Utils_1.Utils.isEntity(value, true)) {
|
|
129
|
+
entity[prop.name] = Reference_1.Reference.wrapReference(value, prop);
|
|
130
|
+
}
|
|
131
|
+
else if (Utils_1.Utils.isPrimaryKey(value, true) && EntityAssigner.validateEM(em)) {
|
|
132
|
+
entity[prop.name] = prop.mapToPk ? value : Reference_1.Reference.wrapReference(em.getReference(prop.type, value, options), prop);
|
|
133
|
+
}
|
|
134
|
+
else if (Utils_1.Utils.isPlainObject(value) && options.merge && EntityAssigner.validateEM(em)) {
|
|
135
|
+
entity[prop.name] = Reference_1.Reference.wrapReference(em.merge(prop.type, value, options), prop);
|
|
136
|
+
}
|
|
137
|
+
else if (Utils_1.Utils.isPlainObject(value) && EntityAssigner.validateEM(em)) {
|
|
138
|
+
entity[prop.name] = Reference_1.Reference.wrapReference(em.create(prop.type, value, options), prop);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const name = entity.constructor.name;
|
|
142
|
+
throw new Error(`Invalid reference value provided for '${name}.${prop.name}' in ${name}.assign(): ${JSON.stringify(value)}`);
|
|
143
|
+
}
|
|
144
|
+
EntityAssigner.autoWireOneToOne(prop, entity);
|
|
145
|
+
}
|
|
146
|
+
static assignCollection(entity, collection, value, prop, em, options) {
|
|
147
|
+
const invalid = [];
|
|
148
|
+
const items = Utils_1.Utils.asArray(value).map((item, idx) => {
|
|
149
|
+
// try to propagate missing owning side reference to the payload first
|
|
150
|
+
const prop2 = prop.targetMeta?.properties[prop.mappedBy];
|
|
151
|
+
if (Utils_1.Utils.isPlainObject(item) && prop2 && item[prop2.name] == null) {
|
|
152
|
+
item = { ...item, [prop2.name]: Reference_1.Reference.wrapReference(entity, prop2) };
|
|
153
|
+
}
|
|
154
|
+
if (options.updateNestedEntities && options.updateByPrimaryKey && Utils_1.Utils.isPlainObject(item)) {
|
|
155
|
+
const pk = Utils_1.Utils.extractPK(item, prop.targetMeta);
|
|
156
|
+
if (pk && EntityAssigner.validateEM(em)) {
|
|
157
|
+
const ref = em.getUnitOfWork().getById(prop.type, pk, options.schema);
|
|
158
|
+
/* istanbul ignore else */
|
|
159
|
+
if (ref) {
|
|
160
|
+
return EntityAssigner.assign(ref, item, options);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return this.createCollectionItem(item, em, prop, invalid, options);
|
|
164
|
+
}
|
|
165
|
+
/* istanbul ignore next */
|
|
166
|
+
if (options.updateNestedEntities && !options.updateByPrimaryKey && collection[idx] && (0, wrap_1.helper)(collection[idx])?.isInitialized()) {
|
|
167
|
+
return EntityAssigner.assign(collection[idx], item, options);
|
|
168
|
+
}
|
|
169
|
+
return this.createCollectionItem(item, em, prop, invalid, options);
|
|
170
|
+
});
|
|
171
|
+
if (invalid.length > 0) {
|
|
172
|
+
const name = entity.constructor.name;
|
|
173
|
+
throw new Error(`Invalid collection values provided for '${name}.${prop.name}' in ${name}.assign(): ${(0, util_1.inspect)(invalid)}`);
|
|
174
|
+
}
|
|
175
|
+
if (Array.isArray(value)) {
|
|
176
|
+
collection.set(items);
|
|
177
|
+
}
|
|
178
|
+
else { // append to the collection in case of assigning a single value instead of array
|
|
179
|
+
collection.add(items);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
static assignEmbeddable(entity, value, prop, em, options) {
|
|
183
|
+
const propName = prop.embedded ? prop.embedded[1] : prop.name;
|
|
184
|
+
if (value == null) {
|
|
185
|
+
entity[propName] = value;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// if the value is not an array, we just push, otherwise we replace the array
|
|
189
|
+
if (prop.array && (Array.isArray(value) || entity[propName] == null)) {
|
|
190
|
+
entity[propName] = [];
|
|
191
|
+
}
|
|
192
|
+
if (prop.array) {
|
|
193
|
+
return Utils_1.Utils.asArray(value).forEach(item => {
|
|
194
|
+
const tmp = {};
|
|
195
|
+
this.assignEmbeddable(tmp, item, { ...prop, array: false }, em, options);
|
|
196
|
+
entity[propName].push(...Object.values(tmp));
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
const create = () => EntityAssigner.validateEM(em) && em.getEntityFactory().createEmbeddable(prop.type, value, {
|
|
200
|
+
convertCustomTypes: options.convertCustomTypes,
|
|
201
|
+
newEntity: options.mergeObjectProperties ? !('propName' in entity) : true,
|
|
202
|
+
});
|
|
203
|
+
entity[propName] = (options.mergeObjectProperties ? (entity[propName] || create()) : create());
|
|
204
|
+
Object.keys(value).forEach(key => {
|
|
205
|
+
EntityAssigner.assignProperty(entity[propName], key, prop.embeddedProps, value, options);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
static createCollectionItem(item, em, prop, invalid, options) {
|
|
209
|
+
if (Utils_1.Utils.isEntity(item)) {
|
|
210
|
+
return item;
|
|
211
|
+
}
|
|
212
|
+
if (Utils_1.Utils.isPrimaryKey(item) && EntityAssigner.validateEM(em)) {
|
|
213
|
+
return em.getReference(prop.type, item, options);
|
|
214
|
+
}
|
|
215
|
+
if (Utils_1.Utils.isPlainObject(item) && options.merge && EntityAssigner.validateEM(em)) {
|
|
216
|
+
return em.merge(prop.type, item, options);
|
|
217
|
+
}
|
|
218
|
+
if (Utils_1.Utils.isPlainObject(item) && EntityAssigner.validateEM(em)) {
|
|
219
|
+
return em.create(prop.type, item, options);
|
|
220
|
+
}
|
|
221
|
+
invalid.push(item);
|
|
222
|
+
return item;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.EntityAssigner = EntityAssigner;
|
|
226
|
+
exports.assign = EntityAssigner.assign;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { EntityData, EntityMetadata, EntityName, New, Primary } from '../typings';
|
|
2
|
+
import type { EntityManager } from '../EntityManager';
|
|
3
|
+
import type { EntityComparator } from '../utils/EntityComparator';
|
|
4
|
+
export interface FactoryOptions {
|
|
5
|
+
initialized?: boolean;
|
|
6
|
+
newEntity?: boolean;
|
|
7
|
+
merge?: boolean;
|
|
8
|
+
refresh?: boolean;
|
|
9
|
+
convertCustomTypes?: boolean;
|
|
10
|
+
recomputeSnapshot?: boolean;
|
|
11
|
+
schema?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class EntityFactory {
|
|
14
|
+
private readonly em;
|
|
15
|
+
private readonly driver;
|
|
16
|
+
private readonly platform;
|
|
17
|
+
private readonly config;
|
|
18
|
+
private readonly metadata;
|
|
19
|
+
private readonly hydrator;
|
|
20
|
+
private readonly eventManager;
|
|
21
|
+
private readonly comparator;
|
|
22
|
+
constructor(em: EntityManager);
|
|
23
|
+
create<T extends object, P extends string = string>(entityName: EntityName<T>, data: EntityData<T>, options?: FactoryOptions): New<T, P>;
|
|
24
|
+
mergeData<T extends object>(meta: EntityMetadata<T>, entity: T, data: EntityData<T>, options?: FactoryOptions): void;
|
|
25
|
+
createReference<T extends object>(entityName: EntityName<T>, id: Primary<T> | Primary<T>[] | Record<string, Primary<T>>, options?: Pick<FactoryOptions, 'merge' | 'convertCustomTypes' | 'schema'>): T;
|
|
26
|
+
createEmbeddable<T extends object>(entityName: EntityName<T>, data: EntityData<T>, options?: Pick<FactoryOptions, 'newEntity' | 'convertCustomTypes'>): T;
|
|
27
|
+
getComparator(): EntityComparator;
|
|
28
|
+
private createEntity;
|
|
29
|
+
private hydrate;
|
|
30
|
+
private findEntity;
|
|
31
|
+
private processDiscriminatorColumn;
|
|
32
|
+
/**
|
|
33
|
+
* denormalize PK to value required by driver (e.g. ObjectId)
|
|
34
|
+
*/
|
|
35
|
+
private denormalizePrimaryKey;
|
|
36
|
+
/**
|
|
37
|
+
* returns parameters for entity constructor, creating references from plain ids
|
|
38
|
+
*/
|
|
39
|
+
private extractConstructorParams;
|
|
40
|
+
private get unitOfWork();
|
|
41
|
+
}
|