@mikro-orm/core 7.0.4-dev.8 → 7.0.4
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 +884 -583
- package/EntityManager.js +1922 -1895
- package/MikroORM.d.ts +103 -74
- package/MikroORM.js +178 -179
- package/README.md +1 -1
- package/cache/CacheAdapter.d.ts +36 -36
- package/cache/FileCacheAdapter.d.ts +30 -24
- package/cache/FileCacheAdapter.js +80 -78
- package/cache/GeneratedCacheAdapter.d.ts +18 -20
- package/cache/GeneratedCacheAdapter.js +30 -30
- package/cache/MemoryCacheAdapter.d.ts +18 -20
- package/cache/MemoryCacheAdapter.js +35 -36
- package/cache/NullCacheAdapter.d.ts +16 -16
- package/cache/NullCacheAdapter.js +24 -24
- package/connections/Connection.d.ts +95 -84
- package/connections/Connection.js +165 -168
- package/drivers/DatabaseDriver.d.ts +186 -80
- package/drivers/DatabaseDriver.js +450 -443
- package/drivers/IDatabaseDriver.d.ts +440 -301
- package/entity/BaseEntity.d.ts +120 -83
- package/entity/BaseEntity.js +43 -43
- package/entity/Collection.d.ts +212 -179
- package/entity/Collection.js +727 -721
- package/entity/EntityAssigner.d.ts +88 -77
- package/entity/EntityAssigner.js +231 -230
- package/entity/EntityFactory.d.ts +66 -54
- package/entity/EntityFactory.js +425 -383
- package/entity/EntityHelper.d.ts +34 -22
- package/entity/EntityHelper.js +280 -267
- package/entity/EntityIdentifier.d.ts +4 -4
- package/entity/EntityIdentifier.js +10 -10
- package/entity/EntityLoader.d.ts +98 -72
- package/entity/EntityLoader.js +753 -723
- package/entity/EntityRepository.d.ts +316 -201
- package/entity/EntityRepository.js +213 -213
- package/entity/PolymorphicRef.d.ts +5 -5
- package/entity/PolymorphicRef.js +10 -10
- package/entity/Reference.d.ts +126 -82
- package/entity/Reference.js +278 -274
- package/entity/WrappedEntity.d.ts +115 -72
- package/entity/WrappedEntity.js +168 -166
- package/entity/defineEntity.d.ts +1315 -636
- package/entity/defineEntity.js +527 -518
- package/entity/utils.d.ts +13 -3
- package/entity/utils.js +71 -73
- package/entity/validators.js +43 -43
- package/entity/wrap.js +8 -8
- package/enums.d.ts +258 -253
- package/enums.js +251 -252
- package/errors.d.ts +114 -72
- package/errors.js +350 -253
- package/events/EventManager.d.ts +26 -14
- package/events/EventManager.js +79 -77
- package/events/EventSubscriber.d.ts +29 -29
- package/events/TransactionEventBroadcaster.d.ts +15 -8
- package/events/TransactionEventBroadcaster.js +14 -14
- package/exceptions.d.ts +23 -40
- package/exceptions.js +35 -52
- package/hydration/Hydrator.d.ts +42 -17
- package/hydration/Hydrator.js +43 -43
- package/hydration/ObjectHydrator.d.ts +50 -17
- package/hydration/ObjectHydrator.js +481 -416
- package/index.d.ts +116 -2
- package/index.js +10 -1
- package/logging/DefaultLogger.d.ts +34 -32
- package/logging/DefaultLogger.js +86 -86
- package/logging/Logger.d.ts +41 -41
- package/logging/SimpleLogger.d.ts +13 -11
- package/logging/SimpleLogger.js +22 -22
- package/logging/colors.d.ts +6 -6
- package/logging/colors.js +11 -10
- package/logging/inspect.js +7 -7
- package/metadata/EntitySchema.d.ts +211 -127
- package/metadata/EntitySchema.js +397 -398
- package/metadata/MetadataDiscovery.d.ts +114 -114
- package/metadata/MetadataDiscovery.js +1951 -1863
- package/metadata/MetadataProvider.d.ts +24 -21
- package/metadata/MetadataProvider.js +82 -84
- package/metadata/MetadataStorage.d.ts +38 -32
- package/metadata/MetadataStorage.js +118 -118
- package/metadata/MetadataValidator.d.ts +39 -39
- package/metadata/MetadataValidator.js +381 -338
- package/metadata/discover-entities.d.ts +5 -2
- package/metadata/discover-entities.js +35 -27
- package/metadata/types.d.ts +615 -531
- package/naming-strategy/AbstractNamingStrategy.d.ts +54 -39
- package/naming-strategy/AbstractNamingStrategy.js +90 -85
- 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 +109 -99
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +7 -7
- package/naming-strategy/UnderscoreNamingStrategy.js +21 -21
- package/not-supported.js +7 -4
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -1
- package/platforms/ExceptionConverter.js +4 -4
- package/platforms/Platform.d.ts +310 -299
- package/platforms/Platform.js +663 -636
- package/serialization/EntitySerializer.d.ts +49 -26
- package/serialization/EntitySerializer.js +224 -218
- package/serialization/EntityTransformer.d.ts +10 -6
- package/serialization/EntityTransformer.js +219 -217
- package/serialization/SerializationContext.d.ts +27 -23
- package/serialization/SerializationContext.js +105 -105
- package/types/ArrayType.d.ts +8 -8
- package/types/ArrayType.js +33 -33
- package/types/BigIntType.d.ts +17 -10
- 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 +83 -79
- 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 +75 -49
- package/types/index.js +52 -26
- package/typings.d.ts +1250 -737
- package/typings.js +244 -231
- 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 +178 -170
- package/unit-of-work/ChangeSetPersister.d.ts +63 -44
- package/unit-of-work/ChangeSetPersister.js +442 -421
- package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
- package/unit-of-work/CommitOrderCalculator.js +89 -88
- package/unit-of-work/IdentityMap.d.ts +31 -31
- package/unit-of-work/IdentityMap.js +105 -105
- package/unit-of-work/UnitOfWork.d.ts +181 -141
- package/unit-of-work/UnitOfWork.js +1200 -1183
- package/utils/AbstractMigrator.d.ts +111 -91
- package/utils/AbstractMigrator.js +275 -275
- package/utils/AbstractSchemaGenerator.d.ts +43 -34
- package/utils/AbstractSchemaGenerator.js +121 -122
- package/utils/AsyncContext.d.ts +3 -3
- package/utils/AsyncContext.js +34 -35
- package/utils/Configuration.d.ts +852 -808
- package/utils/Configuration.js +359 -344
- package/utils/Cursor.d.ts +40 -22
- package/utils/Cursor.js +135 -127
- package/utils/DataloaderUtils.d.ts +58 -43
- package/utils/DataloaderUtils.js +203 -198
- package/utils/EntityComparator.d.ts +98 -81
- package/utils/EntityComparator.js +828 -728
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +3 -3
- package/utils/QueryHelper.d.ts +79 -51
- package/utils/QueryHelper.js +372 -361
- package/utils/RawQueryFragment.d.ts +50 -34
- package/utils/RawQueryFragment.js +107 -105
- package/utils/RequestContext.d.ts +32 -32
- package/utils/RequestContext.js +52 -53
- package/utils/TransactionContext.d.ts +16 -16
- package/utils/TransactionContext.js +27 -27
- package/utils/TransactionManager.d.ts +58 -58
- package/utils/TransactionManager.js +199 -197
- package/utils/Utils.d.ts +204 -145
- package/utils/Utils.js +812 -810
- package/utils/clone.js +104 -113
- package/utils/env-vars.js +90 -88
- package/utils/fs-utils.d.ts +15 -15
- package/utils/fs-utils.js +180 -181
- package/utils/upsert-utils.d.ts +20 -5
- package/utils/upsert-utils.js +114 -116
package/utils/DataloaderUtils.js
CHANGED
|
@@ -3,15 +3,15 @@ import { helper } from '../entity/wrap.js';
|
|
|
3
3
|
import { Reference } from '../entity/Reference.js';
|
|
4
4
|
import { Utils } from './Utils.js';
|
|
5
5
|
export class DataloaderUtils {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
static DataLoader;
|
|
7
|
+
/**
|
|
8
|
+
* Groups identified references by entity and returns a Map with the
|
|
9
|
+
* class name as the index and the corresponding primary keys as the value.
|
|
10
|
+
*/
|
|
11
|
+
static groupPrimaryKeysByEntityAndOpts(refsWithOpts) {
|
|
12
|
+
const map = new Map();
|
|
13
|
+
for (const [ref, opts] of refsWithOpts) {
|
|
14
|
+
/* The key is a combination of the uniqueName (a unique table name based identifier) and a stringified version if the load options because we want
|
|
15
15
|
to map each combination of entities/options into separate find queries in order to return accurate results.
|
|
16
16
|
This could be further optimized finding the "lowest common denominator" among the different options
|
|
17
17
|
for each Entity and firing a single query for each Entity instead of Entity+options combination.
|
|
@@ -24,210 +24,215 @@ export class DataloaderUtils {
|
|
|
24
24
|
Thus such approach should probably be configurable, if not opt-in.
|
|
25
25
|
NOTE: meta + opts multi maps (https://github.com/martian17/ds-js) might be a more elegant way
|
|
26
26
|
to implement this but not necessarily faster. */
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
return map;
|
|
27
|
+
const key = `${helper(ref).__meta.uniqueName}|${JSON.stringify(opts ?? {})}`;
|
|
28
|
+
let primaryKeysSet = map.get(key);
|
|
29
|
+
if (primaryKeysSet == null) {
|
|
30
|
+
primaryKeysSet = new Set();
|
|
31
|
+
map.set(key, primaryKeysSet);
|
|
32
|
+
}
|
|
33
|
+
primaryKeysSet.add(helper(ref).getPrimaryKey());
|
|
36
34
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
35
|
+
return map;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns the reference dataloader batchLoadFn, which aggregates references by entity,
|
|
39
|
+
* makes one query per entity and maps each input reference to the corresponding result.
|
|
40
|
+
*/
|
|
41
|
+
static getRefBatchLoadFn(em) {
|
|
42
|
+
return async refsWithOpts => {
|
|
43
|
+
const groupedIdsMap = DataloaderUtils.groupPrimaryKeysByEntityAndOpts(refsWithOpts);
|
|
44
|
+
const promises = Array.from(groupedIdsMap).map(([key, idsSet]) => {
|
|
45
|
+
const uniqueName = key.substring(0, key.indexOf('|'));
|
|
46
|
+
const opts = JSON.parse(key.substring(key.indexOf('|') + 1));
|
|
47
|
+
const meta = em.getMetadata().getByUniqueName(uniqueName);
|
|
48
|
+
return em.find(meta.class, Array.from(idsSet), opts);
|
|
49
|
+
});
|
|
50
|
+
await Promise.all(promises);
|
|
51
|
+
/* Instead of assigning each find result to the original reference we use a shortcut
|
|
52
52
|
which takes advantage of the already existing Mikro-ORM caching mechanism:
|
|
53
53
|
when it calls ref.unwrap it will automatically retrieve the entity
|
|
54
54
|
from the cache (it will hit the cache because of the previous find query).
|
|
55
55
|
This trick won't be possible for collections where we will be forced to map the results. */
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
56
|
+
return refsWithOpts.map(([ref]) => ref.unwrap());
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Groups collections by entity and returns a Map whose keys are the entity names and whose values are filter Maps
|
|
61
|
+
* which we can use to narrow down the find query to return just the items of the collections that have been dataloaded.
|
|
62
|
+
* The entries of the filter Map will be used as the values of an $or operator so we end up with a query per entity.
|
|
63
|
+
*/
|
|
64
|
+
static groupInversedOrMappedKeysByEntityAndOpts(collsWithOpts) {
|
|
65
|
+
const entitiesMap = new Map();
|
|
66
|
+
for (const [col, opts] of collsWithOpts) {
|
|
67
|
+
/*
|
|
68
68
|
We first get the entity name of the Collection and together with its options (see groupPrimaryKeysByEntityAndOpts
|
|
69
69
|
for a full explanation) we use it as the key of the first Map.
|
|
70
70
|
With that we know that we have to look for entities of this type (and with the same options) in order to fulfill the collection.
|
|
71
71
|
The value is another Map which we can use to filter the find query to get results pertaining to the collections that have been dataloaded:
|
|
72
72
|
its keys are the props we are going to filter to and its values are the corresponding PKs.
|
|
73
73
|
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
return entitiesMap;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Turn the entity+options map into actual queries.
|
|
95
|
-
* The keys are the entity names + a stringified version of the options and the values are filter Maps which will be used as the values of an $or operator so we end up with a query per entity+opts.
|
|
96
|
-
* We must populate the inverse side of the relationship in order to be able to later retrieve the PK(s) from its item(s).
|
|
97
|
-
* Together with the query the promises will also return the key which can be used to narrow down the results pertaining to a certain set of options.
|
|
98
|
-
*/
|
|
99
|
-
static entitiesAndOptsMapToQueries(entitiesAndOptsMap, em) {
|
|
100
|
-
return Array.from(entitiesAndOptsMap, async ([key, filterMap]) => {
|
|
101
|
-
const uniqueName = key.substring(0, key.indexOf('|'));
|
|
102
|
-
const opts = JSON.parse(key.substring(key.indexOf('|') + 1));
|
|
103
|
-
const meta = em.getMetadata().getByUniqueName(uniqueName);
|
|
104
|
-
const res = await em.find(meta.class, opts?.where != null && Object.keys(opts.where).length > 0
|
|
105
|
-
? {
|
|
106
|
-
$and: [
|
|
107
|
-
{
|
|
108
|
-
$or: Array.from(filterMap.entries()).map(([prop, pks]) => {
|
|
109
|
-
return { [prop]: Array.from(pks) };
|
|
110
|
-
}),
|
|
111
|
-
},
|
|
112
|
-
opts.where,
|
|
113
|
-
],
|
|
114
|
-
}
|
|
115
|
-
: {
|
|
116
|
-
// The entries of the filter Map will be used as the values of the $or operator
|
|
117
|
-
$or: Array.from(filterMap.entries()).map(([prop, pks]) => {
|
|
118
|
-
return { [prop]: Array.from(pks) };
|
|
119
|
-
}),
|
|
120
|
-
}, {
|
|
121
|
-
...opts,
|
|
122
|
-
// We need to populate the inverse side of the relationship in order to be able to later retrieve the PK(s) from its item(s)
|
|
123
|
-
populate: [
|
|
124
|
-
...(opts.populate === false ? [] : (opts.populate ?? [])),
|
|
125
|
-
...Array.from(filterMap.keys()).filter(
|
|
126
|
-
// We need to do so only if the inverse side is a collection, because we can already retrieve the PK from a reference without having to load it
|
|
127
|
-
prop => meta.properties[prop]?.ref !== true),
|
|
128
|
-
],
|
|
129
|
-
});
|
|
130
|
-
return [key, res];
|
|
131
|
-
});
|
|
74
|
+
const key = `${col.property.targetMeta.uniqueName}|${JSON.stringify(opts ?? {})}`;
|
|
75
|
+
let filterMap = entitiesMap.get(key); // We are going to use this map to filter the entities pertaining to the collections that have been dataloaded.
|
|
76
|
+
if (filterMap == null) {
|
|
77
|
+
filterMap = new Map();
|
|
78
|
+
entitiesMap.set(key, filterMap);
|
|
79
|
+
}
|
|
80
|
+
// The Collection dataloader relies on the inverse side of the relationship (inversedBy/mappedBy), which is going to be
|
|
81
|
+
// the key of the filter Map and it's the prop that we use to filter the results pertaining to the Collection.
|
|
82
|
+
const inversedProp = col.property.inversedBy ?? col.property.mappedBy; // Many to Many vs One to Many
|
|
83
|
+
let primaryKeys = filterMap.get(inversedProp);
|
|
84
|
+
if (primaryKeys == null) {
|
|
85
|
+
primaryKeys = new Set();
|
|
86
|
+
filterMap.set(inversedProp, primaryKeys);
|
|
87
|
+
}
|
|
88
|
+
// This is the PK that in conjunction with the filter Map key (the prop) will lead to this specific Collection
|
|
89
|
+
primaryKeys.add(helper(col.owner).getPrimaryKey());
|
|
132
90
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
91
|
+
return entitiesMap;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Turn the entity+options map into actual queries.
|
|
95
|
+
* The keys are the entity names + a stringified version of the options and the values are filter Maps which will be used as the values of an $or operator so we end up with a query per entity+opts.
|
|
96
|
+
* We must populate the inverse side of the relationship in order to be able to later retrieve the PK(s) from its item(s).
|
|
97
|
+
* Together with the query the promises will also return the key which can be used to narrow down the results pertaining to a certain set of options.
|
|
98
|
+
*/
|
|
99
|
+
static entitiesAndOptsMapToQueries(entitiesAndOptsMap, em) {
|
|
100
|
+
return Array.from(entitiesAndOptsMap, async ([key, filterMap]) => {
|
|
101
|
+
const uniqueName = key.substring(0, key.indexOf('|'));
|
|
102
|
+
const opts = JSON.parse(key.substring(key.indexOf('|') + 1));
|
|
103
|
+
const meta = em.getMetadata().getByUniqueName(uniqueName);
|
|
104
|
+
const res = await em.find(
|
|
105
|
+
meta.class,
|
|
106
|
+
opts?.where != null && Object.keys(opts.where).length > 0
|
|
107
|
+
? {
|
|
108
|
+
$and: [
|
|
109
|
+
{
|
|
110
|
+
$or: Array.from(filterMap.entries()).map(([prop, pks]) => {
|
|
111
|
+
return { [prop]: Array.from(pks) };
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
opts.where,
|
|
115
|
+
],
|
|
151
116
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
const value = groups.get(key) ?? [];
|
|
191
|
-
value.push([col, opts ?? {}]);
|
|
192
|
-
groups.set(key, value);
|
|
193
|
-
}
|
|
194
|
-
const ret = [];
|
|
195
|
-
for (const group of groups.values()) {
|
|
196
|
-
const prop = group[0][0].property;
|
|
197
|
-
const options = {};
|
|
198
|
-
const wrap = (cond) => ({ [prop.name]: cond });
|
|
199
|
-
const orderBy = Utils.asArray(group[0][1]?.orderBy).map(o => wrap(o));
|
|
200
|
-
const populate = wrap(group[0][1]?.populate);
|
|
201
|
-
const owners = group.map(c => c[0].owner);
|
|
202
|
-
const $or = [];
|
|
203
|
-
// a bit of a hack, but we need to prefix the key, since we have only a column name, not a property name
|
|
204
|
-
const alias = em.config.getNamingStrategy().aliasName(Utils.className(prop.pivotEntity), 0);
|
|
205
|
-
const fk = `${alias}.${Utils.getPrimaryKeyHash(prop.joinColumns)}`;
|
|
206
|
-
for (const c of group) {
|
|
207
|
-
$or.push({ $and: [c[1]?.where ?? {}, { [fk]: c[0].owner }] });
|
|
208
|
-
options.refresh ??= c[1]?.refresh;
|
|
209
|
-
}
|
|
210
|
-
options.where = wrap({ $or });
|
|
211
|
-
const r = await em
|
|
212
|
-
.getEntityLoader()
|
|
213
|
-
.findChildrenFromPivotTable(owners, prop, options, orderBy, populate, group[0][1]?.ref);
|
|
214
|
-
ret.push(...r);
|
|
215
|
-
}
|
|
216
|
-
return ret;
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
static async getDataLoader() {
|
|
220
|
-
if (this.DataLoader) {
|
|
221
|
-
return this.DataLoader;
|
|
117
|
+
: {
|
|
118
|
+
// The entries of the filter Map will be used as the values of the $or operator
|
|
119
|
+
$or: Array.from(filterMap.entries()).map(([prop, pks]) => {
|
|
120
|
+
return { [prop]: Array.from(pks) };
|
|
121
|
+
}),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
...opts,
|
|
125
|
+
// We need to populate the inverse side of the relationship in order to be able to later retrieve the PK(s) from its item(s)
|
|
126
|
+
populate: [
|
|
127
|
+
...(opts.populate === false ? [] : (opts.populate ?? [])),
|
|
128
|
+
...Array.from(filterMap.keys()).filter(
|
|
129
|
+
// We need to do so only if the inverse side is a collection, because we can already retrieve the PK from a reference without having to load it
|
|
130
|
+
prop => meta.properties[prop]?.ref !== true,
|
|
131
|
+
),
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
return [key, res];
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Creates a filter which returns the results pertaining to a certain collection.
|
|
140
|
+
* First checks if the Entity type matches, then retrieves the inverse side of the relationship
|
|
141
|
+
* where the filtering will be done in order to match the target collection.
|
|
142
|
+
*/
|
|
143
|
+
static getColFilter(collection) {
|
|
144
|
+
return result => {
|
|
145
|
+
// There is no need to check if Entity matches because we already matched the key which is entity+options.
|
|
146
|
+
// This is the inverse side of the relationship where the filtering will be done in order to match the target collection
|
|
147
|
+
// Either inversedBy or mappedBy exist because we already checked in groupInversedOrMappedKeysByEntity
|
|
148
|
+
const inverseProp = collection.property.inversedBy ?? collection.property.mappedBy;
|
|
149
|
+
const target = Reference.unwrapReference(result[inverseProp]);
|
|
150
|
+
if (target instanceof Collection) {
|
|
151
|
+
for (const item of target) {
|
|
152
|
+
if (item === collection.owner) {
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
222
155
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
156
|
+
} else if (target) {
|
|
157
|
+
return target === collection.owner;
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Returns the 1:M collection dataloader batchLoadFn, which aggregates collections by entity,
|
|
164
|
+
* makes one query per entity and maps each input collection to the corresponding result.
|
|
165
|
+
*/
|
|
166
|
+
static getColBatchLoadFn(em) {
|
|
167
|
+
return async collsWithOpts => {
|
|
168
|
+
const entitiesAndOptsMap = DataloaderUtils.groupInversedOrMappedKeysByEntityAndOpts(collsWithOpts);
|
|
169
|
+
const promises = DataloaderUtils.entitiesAndOptsMapToQueries(entitiesAndOptsMap, em);
|
|
170
|
+
const resultsMap = new Map(await Promise.all(promises));
|
|
171
|
+
// We need to filter the results in order to map each input collection
|
|
172
|
+
// to a subset of each query matching the collection items.
|
|
173
|
+
return collsWithOpts.map(([col, opts]) => {
|
|
174
|
+
const key = `${col.property.targetMeta.uniqueName}|${JSON.stringify(opts ?? {})}`;
|
|
175
|
+
const entities = resultsMap.get(key);
|
|
176
|
+
if (entities == null) {
|
|
177
|
+
// Should never happen
|
|
178
|
+
/* v8 ignore next */
|
|
179
|
+
throw new Error('Cannot match results');
|
|
227
180
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
181
|
+
return entities.filter(DataloaderUtils.getColFilter(col));
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Returns the M:N collection dataloader batchLoadFn, which aggregates collections by entity,
|
|
187
|
+
* makes one query per entity and maps each input collection to the corresponding result.
|
|
188
|
+
*/
|
|
189
|
+
static getManyToManyColBatchLoadFn(em) {
|
|
190
|
+
return async collsWithOpts => {
|
|
191
|
+
const groups = new Map();
|
|
192
|
+
for (const [col, opts] of collsWithOpts) {
|
|
193
|
+
const key = `${col.property.targetMeta.uniqueName}.${col.property.name}|${JSON.stringify(opts ?? {})}`;
|
|
194
|
+
const value = groups.get(key) ?? [];
|
|
195
|
+
value.push([col, opts ?? {}]);
|
|
196
|
+
groups.set(key, value);
|
|
197
|
+
}
|
|
198
|
+
const ret = [];
|
|
199
|
+
for (const group of groups.values()) {
|
|
200
|
+
const prop = group[0][0].property;
|
|
201
|
+
const options = {};
|
|
202
|
+
const wrap = cond => ({ [prop.name]: cond });
|
|
203
|
+
const orderBy = Utils.asArray(group[0][1]?.orderBy).map(o => wrap(o));
|
|
204
|
+
const populate = wrap(group[0][1]?.populate);
|
|
205
|
+
const owners = group.map(c => c[0].owner);
|
|
206
|
+
const $or = [];
|
|
207
|
+
// a bit of a hack, but we need to prefix the key, since we have only a column name, not a property name
|
|
208
|
+
const alias = em.config.getNamingStrategy().aliasName(Utils.className(prop.pivotEntity), 0);
|
|
209
|
+
const fk = `${alias}.${Utils.getPrimaryKeyHash(prop.joinColumns)}`;
|
|
210
|
+
for (const c of group) {
|
|
211
|
+
$or.push({ $and: [c[1]?.where ?? {}, { [fk]: c[0].owner }] });
|
|
212
|
+
options.refresh ??= c[1]?.refresh;
|
|
231
213
|
}
|
|
214
|
+
options.where = wrap({ $or });
|
|
215
|
+
const r = await em
|
|
216
|
+
.getEntityLoader()
|
|
217
|
+
.findChildrenFromPivotTable(owners, prop, options, orderBy, populate, group[0][1]?.ref);
|
|
218
|
+
ret.push(...r);
|
|
219
|
+
}
|
|
220
|
+
return ret;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
static async getDataLoader() {
|
|
224
|
+
if (this.DataLoader) {
|
|
225
|
+
return this.DataLoader;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const mod = await import('dataloader');
|
|
229
|
+
const DataLoader = mod.default;
|
|
230
|
+
return (this.DataLoader ??= DataLoader);
|
|
231
|
+
} catch {
|
|
232
|
+
/* v8 ignore next */
|
|
233
|
+
throw new Error(
|
|
234
|
+
"DataLoader is not found, make sure `dataloader` package is installed in your project's dependencies.",
|
|
235
|
+
);
|
|
232
236
|
}
|
|
237
|
+
}
|
|
233
238
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
EntityData,
|
|
3
|
+
EntityDictionary,
|
|
4
|
+
EntityMetadata,
|
|
5
|
+
EntityName,
|
|
6
|
+
EntityProperty,
|
|
7
|
+
IMetadataStorage,
|
|
8
|
+
Primary,
|
|
9
|
+
} from '../typings.js';
|
|
2
10
|
import type { Platform } from '../platforms/Platform.js';
|
|
3
11
|
import type { Configuration } from './Configuration.js';
|
|
4
|
-
type Comparator<T> = (
|
|
12
|
+
type Comparator<T> = (
|
|
13
|
+
a: T,
|
|
14
|
+
b: T,
|
|
15
|
+
options?: {
|
|
5
16
|
includeInverseSides?: boolean;
|
|
6
|
-
}
|
|
17
|
+
},
|
|
18
|
+
) => EntityData<T>;
|
|
7
19
|
type ResultMapper<T> = (result: EntityData<T>) => EntityData<T> | null;
|
|
8
20
|
type SnapshotGenerator<T> = (entity: T) => EntityData<T>;
|
|
9
21
|
type PkGetter<T> = (entity: T) => Primary<T>;
|
|
@@ -11,83 +23,88 @@ type PkSerializer<T> = (entity: T) => string;
|
|
|
11
23
|
type CompositeKeyPart = string | CompositeKeyPart[];
|
|
12
24
|
/** @internal Generates and caches JIT-compiled functions for comparing, snapshotting, and mapping entity data. */
|
|
13
25
|
export declare class EntityComparator {
|
|
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
|
-
|
|
26
|
+
#private;
|
|
27
|
+
constructor(metadata: IMetadataStorage, platform: Platform, config?: Configuration);
|
|
28
|
+
/**
|
|
29
|
+
* Computes difference between two entities.
|
|
30
|
+
*/
|
|
31
|
+
diffEntities<T extends object>(
|
|
32
|
+
entityName: EntityName<T>,
|
|
33
|
+
a: EntityData<T>,
|
|
34
|
+
b: EntityData<T>,
|
|
35
|
+
options?: {
|
|
36
|
+
includeInverseSides?: boolean;
|
|
37
|
+
},
|
|
38
|
+
): EntityData<T>;
|
|
39
|
+
/** Returns true if two entity snapshots are identical (no differences). */
|
|
40
|
+
matching<T extends object>(entityName: EntityName<T>, a: EntityData<T>, b: EntityData<T>): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Removes ORM specific code from entities and prepares it for serializing. Used before change set computation.
|
|
43
|
+
* References will be mapped to primary keys, collections to arrays of primary keys.
|
|
44
|
+
*/
|
|
45
|
+
prepareEntity<T extends object>(entity: T): EntityData<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Maps database columns to properties.
|
|
48
|
+
*/
|
|
49
|
+
mapResult<T>(meta: EntityMetadata<T>, result: EntityDictionary<T>): EntityData<T>;
|
|
50
|
+
/**
|
|
51
|
+
* @internal Highly performance-sensitive method.
|
|
52
|
+
*/
|
|
53
|
+
getPkGetter<T>(meta: EntityMetadata<T>): PkGetter<T>;
|
|
54
|
+
/**
|
|
55
|
+
* @internal Highly performance-sensitive method.
|
|
56
|
+
*/
|
|
57
|
+
getPkGetterConverted<T>(meta: EntityMetadata<T>): PkGetter<T>;
|
|
58
|
+
/**
|
|
59
|
+
* @internal Highly performance-sensitive method.
|
|
60
|
+
*/
|
|
61
|
+
getPkSerializer<T>(meta: EntityMetadata<T>): PkSerializer<T>;
|
|
62
|
+
/**
|
|
63
|
+
* @internal Highly performance-sensitive method.
|
|
64
|
+
*/
|
|
65
|
+
getSnapshotGenerator<T>(entityName: EntityName<T>): SnapshotGenerator<T>;
|
|
66
|
+
/**
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
propName(name: string, parent?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* @internal respects nested composite keys, e.g. `[1, [2, 3]]`
|
|
72
|
+
*/
|
|
73
|
+
createCompositeKeyArray(prop: EntityProperty, parents?: EntityProperty[]): string;
|
|
74
|
+
/**
|
|
75
|
+
* @internal
|
|
76
|
+
*/
|
|
77
|
+
formatCompositeKeyPart(part: CompositeKeyPart): string;
|
|
78
|
+
/**
|
|
79
|
+
* @internal Highly performance-sensitive method.
|
|
80
|
+
*/
|
|
81
|
+
getResultMapper<T>(meta: EntityMetadata<T>): ResultMapper<T>;
|
|
82
|
+
private getPropertyCondition;
|
|
83
|
+
private getEmbeddedArrayPropertySnapshot;
|
|
84
|
+
/**
|
|
85
|
+
* we need to serialize only object embeddables, and only the top level ones, so root object embeddable
|
|
86
|
+
* properties and first child nested object embeddables with inlined parent
|
|
87
|
+
*/
|
|
88
|
+
private shouldSerialize;
|
|
89
|
+
private getEmbeddedPropertySnapshot;
|
|
90
|
+
private registerCustomType;
|
|
91
|
+
private getPropertySnapshot;
|
|
92
|
+
/**
|
|
93
|
+
* @internal Highly performance-sensitive method.
|
|
94
|
+
*/
|
|
95
|
+
getEntityComparator<T extends object>(entityName: EntityName<T>): Comparator<T>;
|
|
96
|
+
private getGenericComparator;
|
|
97
|
+
private getPropertyComparator;
|
|
98
|
+
private wrap;
|
|
99
|
+
private safeKey;
|
|
100
|
+
/**
|
|
101
|
+
* Sets the toArray helper in the context if not already set.
|
|
102
|
+
* Used for converting composite PKs to arrays.
|
|
103
|
+
*/
|
|
104
|
+
private setToArrayHelper;
|
|
105
|
+
/**
|
|
106
|
+
* perf: used to generate list of comparable properties during discovery, so we speed up the runtime comparison
|
|
107
|
+
*/
|
|
108
|
+
static isComparable<T>(prop: EntityProperty<T>, root: EntityMetadata): boolean;
|
|
92
109
|
}
|
|
93
110
|
export {};
|