@mikro-orm/core 7.3.0-dev.20 → 7.3.0-dev.21
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.js +27 -10
- package/cache/cache-key.d.ts +5 -0
- package/cache/cache-key.js +30 -0
- package/drivers/IDatabaseDriver.d.ts +2 -0
- package/entity/EntityLoader.d.ts +1 -0
- package/entity/EntityLoader.js +35 -8
- package/package.json +1 -1
- package/serialization/EntitySerializer.js +4 -4
- package/serialization/EntityTransformer.js +4 -4
- package/serialization/SerializationContext.d.ts +2 -1
- package/serialization/SerializationContext.js +4 -3
- package/unit-of-work/UnitOfWork.js +4 -2
- package/utils/DataloaderUtils.js +19 -8
- package/utils/Utils.js +1 -1
package/EntityManager.js
CHANGED
|
@@ -19,6 +19,7 @@ import { TransactionEventBroadcaster } from './events/TransactionEventBroadcaste
|
|
|
19
19
|
import { MetadataError, OptimisticLockError, ValidationError } from './errors.js';
|
|
20
20
|
import { applyPopulateHints, getLoadingStrategy } from './entity/utils.js';
|
|
21
21
|
import { TransactionManager } from './utils/TransactionManager.js';
|
|
22
|
+
import { scopeCacheKey, stringifyCacheKey } from './cache/cache-key.js';
|
|
22
23
|
/**
|
|
23
24
|
* The EntityManager is the central access point to ORM functionality. It is a facade to all different ORM subsystems
|
|
24
25
|
* such as UnitOfWork, Query Language, and Repository API.
|
|
@@ -211,9 +212,14 @@ export class EntityManager {
|
|
|
211
212
|
await em.tryFlush(entityName, options);
|
|
212
213
|
const where = (await em.processWhere(entityName, options.where ?? {}, options, 'read'));
|
|
213
214
|
validateParams(where);
|
|
214
|
-
options.orderBy = options.orderBy || {};
|
|
215
|
-
options.populate = (await em.preparePopulate(entityName, options));
|
|
216
215
|
const meta = this.metadata.get(entityName);
|
|
216
|
+
if (meta.orderBy) {
|
|
217
|
+
options.orderBy = QueryHelper.mergeOrderBy(options.orderBy, meta.orderBy);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
options.orderBy ??= {};
|
|
221
|
+
}
|
|
222
|
+
options.populate = (await em.preparePopulate(entityName, options));
|
|
217
223
|
em.validateIndexUsage(meta, options.where ?? {}, options);
|
|
218
224
|
options = { ...options };
|
|
219
225
|
// save the original hint value so we know it was infer/all
|
|
@@ -857,6 +863,10 @@ export class EntityManager {
|
|
|
857
863
|
return em.lockAndPopulate(meta, entity, where, options);
|
|
858
864
|
}
|
|
859
865
|
validateParams(where);
|
|
866
|
+
// before the cache key so cached results share the merged orderBy
|
|
867
|
+
if (meta.orderBy) {
|
|
868
|
+
options.orderBy = QueryHelper.mergeOrderBy(options.orderBy, meta.orderBy);
|
|
869
|
+
}
|
|
860
870
|
options.populate = (await em.preparePopulate(entityName, options));
|
|
861
871
|
const cacheKey = em.cacheKey(entityName, options, 'em.findOne', where);
|
|
862
872
|
const cached = await em.tryCache(entityName, options.cache, cacheKey, options.refresh, true, options.schema);
|
|
@@ -1308,7 +1318,7 @@ export class EntityManager {
|
|
|
1308
1318
|
// an empty condition identifies no row, it must not turn into a reload condition matching every row
|
|
1309
1319
|
const getRowUniqueFields = (where) => Utils.isPlainObject(where) && !Utils.hasObjectKeys(where) ? uniqueFields : getUniqueFields(where);
|
|
1310
1320
|
const platform = this.getPlatform();
|
|
1311
|
-
const returning = getOnConflictReturningFields(meta,
|
|
1321
|
+
const returning = getOnConflictReturningFields(meta, allData[0], uniqueFields, options, platform);
|
|
1312
1322
|
if (options.onConflictWhere && res.rows?.length !== allData.length) {
|
|
1313
1323
|
returning.push(...meta.comparableProps.filter(p => !p.lazy && !p.embeddable).map(p => p.name));
|
|
1314
1324
|
}
|
|
@@ -1831,6 +1841,7 @@ export class EntityManager {
|
|
|
1831
1841
|
options.populateWhere = this.createPopulateWhere({ ...where }, options);
|
|
1832
1842
|
options.populateFilter = await this.getJoinedFilters(meta, options);
|
|
1833
1843
|
validateParams(where);
|
|
1844
|
+
em.validateIndexUsage(meta, where, options);
|
|
1834
1845
|
delete options.orderBy;
|
|
1835
1846
|
await em.processUnionWhere(entityName, options, 'read');
|
|
1836
1847
|
const cacheKey = em.cacheKey(entityName, options, 'em.count', where);
|
|
@@ -2388,7 +2399,17 @@ export class EntityManager {
|
|
|
2388
2399
|
}
|
|
2389
2400
|
let autoRefresh;
|
|
2390
2401
|
if (options.fields) {
|
|
2391
|
-
|
|
2402
|
+
// inline embeddables track only their leaf properties as loaded, not the parent
|
|
2403
|
+
const isLoaded = (prop) => helper(entity).__loadedProperties.has(prop.name) ||
|
|
2404
|
+
(prop.kind === ReferenceKind.EMBEDDED && !prop.object && Object.values(prop.embeddedProps).every(isLoaded));
|
|
2405
|
+
autoRefresh = options.fields.some(field => {
|
|
2406
|
+
const [first, ...rest] = field.split('.');
|
|
2407
|
+
let prop = meta.properties[first];
|
|
2408
|
+
for (const part of rest) {
|
|
2409
|
+
prop = prop?.kind === ReferenceKind.EMBEDDED && !prop.object ? prop.embeddedProps[part] : undefined;
|
|
2410
|
+
}
|
|
2411
|
+
return !prop || !isLoaded(prop);
|
|
2412
|
+
});
|
|
2392
2413
|
}
|
|
2393
2414
|
else {
|
|
2394
2415
|
autoRefresh = meta.comparableProps.some(prop => {
|
|
@@ -2458,11 +2479,7 @@ export class EntityManager {
|
|
|
2458
2479
|
const em = this.getContext();
|
|
2459
2480
|
// a named cache key (`config[0]`) discards the computed `key`, which already carries the session context — so
|
|
2460
2481
|
// scope it here too, otherwise a fork's rows would be served to another session context under the same name
|
|
2461
|
-
const cacheKey = Array.isArray(config)
|
|
2462
|
-
? em.#sessionContext
|
|
2463
|
-
? `${config[0]}|${JSON.stringify(em.#sessionContext)}`
|
|
2464
|
-
: config[0]
|
|
2465
|
-
: JSON.stringify(key);
|
|
2482
|
+
const cacheKey = Array.isArray(config) ? scopeCacheKey(config[0], em.#sessionContext) : stringifyCacheKey(key);
|
|
2466
2483
|
const cached = await em.#resultCache.get(cacheKey);
|
|
2467
2484
|
if (!cached) {
|
|
2468
2485
|
return { key: cacheKey, data: cached };
|
|
@@ -2515,7 +2532,7 @@ export class EntityManager {
|
|
|
2515
2532
|
await em.#resultCache.remove(cacheKey);
|
|
2516
2533
|
// named keys are scoped by the session context (see `tryCache`), so clear this context's variant too
|
|
2517
2534
|
if (em.#sessionContext) {
|
|
2518
|
-
await em.#resultCache.remove(
|
|
2535
|
+
await em.#resultCache.remove(scopeCacheKey(cacheKey, em.#sessionContext));
|
|
2519
2536
|
}
|
|
2520
2537
|
}
|
|
2521
2538
|
/**
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { SessionContext } from '../typings.js';
|
|
2
|
+
/** Serializes a result cache or dataloader batch key, telling apart all values a query can contain. */
|
|
3
|
+
export declare function stringifyCacheKey(key: unknown): string;
|
|
4
|
+
/** Scopes a named result cache key by the session context, so rows are not served across session contexts. */
|
|
5
|
+
export declare function scopeCacheKey(name: string, sessionContext?: SessionContext): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { isRaw, isSubquery, Raw } from '../utils/RawQueryFragment.js';
|
|
2
|
+
import { Utils } from '../utils/Utils.js';
|
|
3
|
+
// JSON loses RegExp patterns, `raw()` params and `raw()` keys (symbols), and throws on bigints and sub-queries; `this[key]` is the value before `toJSON()`
|
|
4
|
+
function replacer(key, value) {
|
|
5
|
+
const original = isSubquery(this[key]) ? this[key].toRaw() : this[key];
|
|
6
|
+
if (typeof original === 'bigint') {
|
|
7
|
+
return { bigint: original.toString() };
|
|
8
|
+
}
|
|
9
|
+
if (original instanceof RegExp) {
|
|
10
|
+
return { regexp: original.source, flags: original.flags };
|
|
11
|
+
}
|
|
12
|
+
if (isRaw(original)) {
|
|
13
|
+
return { sql: original.sql, params: original.params };
|
|
14
|
+
}
|
|
15
|
+
if (Raw.hasObjectFragments(value)) {
|
|
16
|
+
return Utils.getObjectQueryKeys(value).map(k => [
|
|
17
|
+
Raw.getKnownFragment(k) ?? k,
|
|
18
|
+
value[k],
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
/** Serializes a result cache or dataloader batch key, telling apart all values a query can contain. */
|
|
24
|
+
export function stringifyCacheKey(key) {
|
|
25
|
+
return JSON.stringify(key, replacer);
|
|
26
|
+
}
|
|
27
|
+
/** Scopes a named result cache key by the session context, so rows are not served across session contexts. */
|
|
28
|
+
export function scopeCacheKey(name, sessionContext) {
|
|
29
|
+
return sessionContext ? `${name}|${JSON.stringify(sessionContext)}` : name;
|
|
30
|
+
}
|
|
@@ -326,6 +326,8 @@ export interface CountOptions<T extends object, P extends string = never> extend
|
|
|
326
326
|
flushMode?: FlushMode | `${FlushMode}`;
|
|
327
327
|
/** SQL: appended to FROM clause (e.g. `'force index(my_index)'`); MongoDB: index name or spec passed as `hint`. */
|
|
328
328
|
indexHint?: string | Dictionary;
|
|
329
|
+
/** @see FindOptions.using */
|
|
330
|
+
using?: IndexName<T> | IndexName<T>[];
|
|
329
331
|
/** sql only */
|
|
330
332
|
comments?: string | string[];
|
|
331
333
|
/** sql only */
|
package/entity/EntityLoader.d.ts
CHANGED
package/entity/EntityLoader.js
CHANGED
|
@@ -186,7 +186,7 @@ export class EntityLoader {
|
|
|
186
186
|
if (prop.polymorphic && prop.polymorphTargets) {
|
|
187
187
|
return this.populatePolymorphic(entities, prop, options, !!ref);
|
|
188
188
|
}
|
|
189
|
-
const { items, partial } = await this.findChildren(options.filtered ?? entities, prop, populate, {
|
|
189
|
+
const { items, partial, partitions } = await this.findChildren(options.filtered ?? entities, prop, populate, {
|
|
190
190
|
...options,
|
|
191
191
|
where,
|
|
192
192
|
orderBy: mergedOrderBy,
|
|
@@ -194,6 +194,10 @@ export class EntityLoader {
|
|
|
194
194
|
const hasLimit = populate.limit != null;
|
|
195
195
|
const isPartial = partial || hasLimit;
|
|
196
196
|
const customOrder = mergedOrderBy.length > 0 || !!prop.orderBy || !!prop.targetMeta?.orderBy;
|
|
197
|
+
if (partitions) {
|
|
198
|
+
this.initializePartitions(filtered, items, field, partitions);
|
|
199
|
+
return items;
|
|
200
|
+
}
|
|
197
201
|
this.initializeCollections(filtered, prop, field, items, customOrder, isPartial, hasLimit);
|
|
198
202
|
return items;
|
|
199
203
|
}
|
|
@@ -336,6 +340,18 @@ export class EntityLoader {
|
|
|
336
340
|
}
|
|
337
341
|
}
|
|
338
342
|
}
|
|
343
|
+
initializePartitions(filtered, children, field, partitions) {
|
|
344
|
+
// a child belongs only to the owners whose slice returned it, not to every owner in its array
|
|
345
|
+
const key = (entity) => helper(entity).getSerializedPrimaryKey();
|
|
346
|
+
const owners = new Map(filtered.map(entity => [key(entity), []]));
|
|
347
|
+
const items = new Map(children.map(child => [key(child), child]));
|
|
348
|
+
for (const [owner, child] of partitions) {
|
|
349
|
+
owners.get('' + owner)?.push(items.get('' + child));
|
|
350
|
+
}
|
|
351
|
+
for (const entity of filtered) {
|
|
352
|
+
entity[field].hydrate(owners.get(key(entity)), true, true, true);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
339
355
|
async findChildren(entities, prop, populate, options, ref) {
|
|
340
356
|
const children = Utils.unique(this.getChildReferences(entities, prop, options, ref, populate));
|
|
341
357
|
const meta = prop.targetMeta;
|
|
@@ -434,11 +450,13 @@ export class EntityLoader {
|
|
|
434
450
|
visited: options.visited,
|
|
435
451
|
};
|
|
436
452
|
if (populate.limit != null) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
453
|
+
// the owners of an M:N without a pivot table are stored as an array, the driver slices per owner
|
|
454
|
+
const owners = prop.kind === ReferenceKind.MANY_TO_MANY && ownerProp ? ids : undefined;
|
|
455
|
+
findOptions._partitionLimit = { partitionBy: fk, limit: populate.limit, offset: populate.offset, owners };
|
|
456
|
+
// a cached result would not carry the per-owner partitions the driver writes back
|
|
457
|
+
if (owners) {
|
|
458
|
+
findOptions.cache = false;
|
|
459
|
+
}
|
|
442
460
|
}
|
|
443
461
|
const items = await this.#em.find(meta.class, where, findOptions);
|
|
444
462
|
// For targetKey relations, wire up loaded entities to parent references
|
|
@@ -484,6 +502,11 @@ export class EntityLoader {
|
|
|
484
502
|
for (const entity of entities) {
|
|
485
503
|
const ref = entity[prop.name];
|
|
486
504
|
if (ref == null) {
|
|
505
|
+
// an inverse 1:1 with no matching row is resolved as empty, mark it so it is not queried again
|
|
506
|
+
if (isInverseOneToOne && !partial && childrenMap.has(getKey(entity))) {
|
|
507
|
+
entity[prop.name] = nullVal;
|
|
508
|
+
helper(entity).__loadedProperties.add(prop.name);
|
|
509
|
+
}
|
|
487
510
|
continue;
|
|
488
511
|
}
|
|
489
512
|
const refKey = getKey(ref);
|
|
@@ -502,7 +525,7 @@ export class EntityLoader {
|
|
|
502
525
|
this.#em.getUnitOfWork().unmarkAsLoaded(item);
|
|
503
526
|
}
|
|
504
527
|
}
|
|
505
|
-
return { items, partial };
|
|
528
|
+
return { items, partial, partitions: findOptions._partitionLimit?.partitions };
|
|
506
529
|
}
|
|
507
530
|
mergePrimaryCondition(ids, pk, options, meta, metadata, platform) {
|
|
508
531
|
const cond1 = QueryHelper.processWhere({
|
|
@@ -866,7 +889,11 @@ export class EntityLoader {
|
|
|
866
889
|
if (value === null) {
|
|
867
890
|
return false;
|
|
868
891
|
}
|
|
869
|
-
|
|
892
|
+
// with `forceUndefined`, an empty relation that was already loaded is `undefined` instead of `null`
|
|
893
|
+
if (value === undefined) {
|
|
894
|
+
return !helper(e).__loadedProperties.has(prop.name);
|
|
895
|
+
}
|
|
896
|
+
if (!Utils.isEntity(value, true)) {
|
|
870
897
|
return true;
|
|
871
898
|
}
|
|
872
899
|
const target = Reference.unwrapReference(value);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.3.0-dev.
|
|
3
|
+
"version": "7.3.0-dev.21",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -76,12 +76,12 @@ export class EntitySerializer {
|
|
|
76
76
|
const meta = wrapped.__meta;
|
|
77
77
|
let contextCreated = false;
|
|
78
78
|
options = prepareOptions(options);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
let root = wrapped.__serializationContext.root;
|
|
80
|
+
if (!root) {
|
|
81
|
+
root = new SerializationContext();
|
|
82
82
|
contextCreated = true;
|
|
83
83
|
}
|
|
84
|
-
|
|
84
|
+
SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
|
|
85
85
|
const ret = {};
|
|
86
86
|
const props = new Set();
|
|
87
87
|
if (meta.serializedPrimaryKey && !meta.compositePK) {
|
|
@@ -21,12 +21,12 @@ export class EntityTransformer {
|
|
|
21
21
|
if (!wrapped) {
|
|
22
22
|
return entity;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
SerializationContext.
|
|
24
|
+
let root = wrapped.__serializationContext.root;
|
|
25
|
+
if (!root) {
|
|
26
|
+
root = new SerializationContext(wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
|
|
27
27
|
contextCreated = true;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
SerializationContext.propagate(root, entity, isVisible);
|
|
30
30
|
const meta = wrapped.__meta;
|
|
31
31
|
const ret = {};
|
|
32
32
|
const props = new Set();
|
|
@@ -18,7 +18,8 @@ export declare class SerializationContext<T extends object> {
|
|
|
18
18
|
/** Cleans up the serialization context by removing root references from all tracked entities. */
|
|
19
19
|
close(): void;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Propagates the context to the entity and its direct relations, so nested serialization calls pick it up.
|
|
22
|
+
* Runs for every serialized entity, so the context spreads along the serialized path instead of the whole graph.
|
|
22
23
|
*/
|
|
23
24
|
static propagate(root: SerializationContext<any>, entity: AnyEntity, isVisible: (meta: EntityMetadata, prop: string) => boolean): void;
|
|
24
25
|
/** Checks whether a property is explicitly listed in the populate hints for the current path. */
|
|
@@ -47,7 +47,8 @@ export class SerializationContext {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Propagates the context to the entity and its direct relations, so nested serialization calls pick it up.
|
|
51
|
+
* Runs for every serialized entity, so the context spreads along the serialized path instead of the whole graph.
|
|
51
52
|
*/
|
|
52
53
|
static propagate(root, entity, isVisible) {
|
|
53
54
|
root.register(entity);
|
|
@@ -59,14 +60,14 @@ export class SerializationContext {
|
|
|
59
60
|
const target = entity[key];
|
|
60
61
|
if (Utils.isEntity(target, true)) {
|
|
61
62
|
if (!target.__helper.__serializationContext.root) {
|
|
62
|
-
|
|
63
|
+
root.register(target);
|
|
63
64
|
}
|
|
64
65
|
continue;
|
|
65
66
|
}
|
|
66
67
|
if (Utils.isCollection(target)) {
|
|
67
68
|
for (const item of target.getItems(false)) {
|
|
68
69
|
if (!item.__helper.__serializationContext.root) {
|
|
69
|
-
|
|
70
|
+
root.register(item);
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
}
|
|
@@ -395,7 +395,7 @@ export class UnitOfWork {
|
|
|
395
395
|
this.#persistStack.delete(entity);
|
|
396
396
|
this.#identityMap.delete(entity);
|
|
397
397
|
}
|
|
398
|
-
//
|
|
398
|
+
// unlink loaded inverse-side items only when the FK is `set null`, other delete rules are left to the database
|
|
399
399
|
const entityMeta = helper(entity).__meta;
|
|
400
400
|
for (const prop of entityMeta.bidirectionalRelations) {
|
|
401
401
|
const inverseProp = prop.mappedBy || prop.inversedBy;
|
|
@@ -410,7 +410,9 @@ export class UnitOfWork {
|
|
|
410
410
|
: inverseTarget.class !== entityMeta.class) {
|
|
411
411
|
continue;
|
|
412
412
|
}
|
|
413
|
-
if (prop.kind === ReferenceKind.ONE_TO_MANY &&
|
|
413
|
+
if (prop.kind === ReferenceKind.ONE_TO_MANY &&
|
|
414
|
+
prop2.deleteRule === 'set null' &&
|
|
415
|
+
Utils.isCollection(relation)) {
|
|
414
416
|
for (const item of relation.getItems(false)) {
|
|
415
417
|
delete item[inverseProp];
|
|
416
418
|
}
|
package/utils/DataloaderUtils.js
CHANGED
|
@@ -3,6 +3,9 @@ import { helper } from '../entity/wrap.js';
|
|
|
3
3
|
import { Reference } from '../entity/Reference.js';
|
|
4
4
|
import { ReferenceKind } from '../enums.js';
|
|
5
5
|
import { Utils } from './Utils.js';
|
|
6
|
+
import { stringifyCacheKey } from '../cache/cache-key.js';
|
|
7
|
+
// the original options of each group key, JSON cannot restore values like RegExp or `raw()` from the key
|
|
8
|
+
const groupOptions = new WeakMap();
|
|
6
9
|
export class DataloaderUtils {
|
|
7
10
|
static DataLoader;
|
|
8
11
|
/**
|
|
@@ -11,6 +14,8 @@ export class DataloaderUtils {
|
|
|
11
14
|
*/
|
|
12
15
|
static groupPrimaryKeysByEntityAndOpts(refsWithOpts) {
|
|
13
16
|
const map = new Map();
|
|
17
|
+
const options = new Map();
|
|
18
|
+
groupOptions.set(map, options);
|
|
14
19
|
for (const [ref, opts] of refsWithOpts) {
|
|
15
20
|
/* 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
|
|
16
21
|
to map each combination of entities/options into separate find queries in order to return accurate results.
|
|
@@ -25,7 +30,8 @@ export class DataloaderUtils {
|
|
|
25
30
|
Thus such approach should probably be configurable, if not opt-in.
|
|
26
31
|
NOTE: meta + opts multi maps (https://github.com/martian17/ds-js) might be a more elegant way
|
|
27
32
|
to implement this but not necessarily faster. */
|
|
28
|
-
const key = `${helper(ref).__meta.uniqueName}|${
|
|
33
|
+
const key = `${helper(ref).__meta.uniqueName}|${stringifyCacheKey(opts ?? {})}`;
|
|
34
|
+
options.set(key, opts ?? {});
|
|
29
35
|
let primaryKeysSet = map.get(key);
|
|
30
36
|
if (primaryKeysSet == null) {
|
|
31
37
|
primaryKeysSet = new Set();
|
|
@@ -44,7 +50,7 @@ export class DataloaderUtils {
|
|
|
44
50
|
const groupedIdsMap = DataloaderUtils.groupPrimaryKeysByEntityAndOpts(refsWithOpts);
|
|
45
51
|
const promises = Array.from(groupedIdsMap).map(([key, idsSet]) => {
|
|
46
52
|
const uniqueName = key.substring(0, key.indexOf('|'));
|
|
47
|
-
const opts =
|
|
53
|
+
const opts = groupOptions.get(groupedIdsMap).get(key);
|
|
48
54
|
const meta = em.getMetadata().getByUniqueName(uniqueName);
|
|
49
55
|
return em.find(meta.class, Array.from(idsSet), opts);
|
|
50
56
|
});
|
|
@@ -64,6 +70,8 @@ export class DataloaderUtils {
|
|
|
64
70
|
*/
|
|
65
71
|
static groupInversedOrMappedKeysByEntityAndOpts(collsWithOpts) {
|
|
66
72
|
const entitiesMap = new Map();
|
|
73
|
+
const options = new Map();
|
|
74
|
+
groupOptions.set(entitiesMap, options);
|
|
67
75
|
for (const [col, opts] of collsWithOpts) {
|
|
68
76
|
/*
|
|
69
77
|
We first get the entity name of the Collection and together with its options (see groupPrimaryKeysByEntityAndOpts
|
|
@@ -72,7 +80,8 @@ export class DataloaderUtils {
|
|
|
72
80
|
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:
|
|
73
81
|
its keys are the props we are going to filter to and its values are the corresponding PKs.
|
|
74
82
|
*/
|
|
75
|
-
const key = `${col.property.targetMeta.uniqueName}|${
|
|
83
|
+
const key = `${col.property.targetMeta.uniqueName}|${stringifyCacheKey(opts ?? {})}`;
|
|
84
|
+
options.set(key, opts ?? {});
|
|
76
85
|
let filterMap = entitiesMap.get(key); // We are going to use this map to filter the entities pertaining to the collections that have been dataloaded.
|
|
77
86
|
if (filterMap == null) {
|
|
78
87
|
filterMap = new Map();
|
|
@@ -100,7 +109,9 @@ export class DataloaderUtils {
|
|
|
100
109
|
static entitiesAndOptsMapToQueries(entitiesAndOptsMap, em) {
|
|
101
110
|
return Array.from(entitiesAndOptsMap, async ([key, filterMap]) => {
|
|
102
111
|
const uniqueName = key.substring(0, key.indexOf('|'));
|
|
103
|
-
const opts =
|
|
112
|
+
const opts = groupOptions
|
|
113
|
+
.get(entitiesAndOptsMap)
|
|
114
|
+
?.get(key) ?? JSON.parse(key.substring(key.indexOf('|') + 1));
|
|
104
115
|
const meta = em.getMetadata().getByUniqueName(uniqueName);
|
|
105
116
|
const res = await em.find(meta.class, opts?.where != null && Object.keys(opts.where).length > 0
|
|
106
117
|
? {
|
|
@@ -168,7 +179,7 @@ export class DataloaderUtils {
|
|
|
168
179
|
// We need to filter the results in order to map each input collection
|
|
169
180
|
// to a subset of each query matching the collection items.
|
|
170
181
|
return collsWithOpts.map(([col, opts]) => {
|
|
171
|
-
const key = `${col.property.targetMeta.uniqueName}|${
|
|
182
|
+
const key = `${col.property.targetMeta.uniqueName}|${stringifyCacheKey(opts ?? {})}`;
|
|
172
183
|
const entities = resultsMap.get(key);
|
|
173
184
|
if (entities == null) {
|
|
174
185
|
// Should never happen
|
|
@@ -187,7 +198,7 @@ export class DataloaderUtils {
|
|
|
187
198
|
return async (collsWithOpts) => {
|
|
188
199
|
const groups = new Map();
|
|
189
200
|
for (const [col, opts] of collsWithOpts) {
|
|
190
|
-
const key = `${col.property.targetMeta.uniqueName}.${col.property.name}|${
|
|
201
|
+
const key = `${col.property.targetMeta.uniqueName}.${col.property.name}|${stringifyCacheKey(opts ?? {})}`;
|
|
191
202
|
const value = groups.get(key) ?? [];
|
|
192
203
|
value.push([col, opts ?? {}]);
|
|
193
204
|
groups.set(key, value);
|
|
@@ -252,7 +263,7 @@ export class DataloaderUtils {
|
|
|
252
263
|
// sharing a relation with the same property name (e.g. `Author.books` and `Publisher.books`)
|
|
253
264
|
// don't collide into a single batch that would use one owner's FK mapping for the other.
|
|
254
265
|
const ownerUniqueName = helper(col.owner).__meta.uniqueName;
|
|
255
|
-
const key = `${ownerUniqueName}.${prop.name}|${
|
|
266
|
+
const key = `${ownerUniqueName}.${prop.name}|${stringifyCacheKey(opts ?? {})}`;
|
|
256
267
|
keys.push(key);
|
|
257
268
|
let group = groups.get(key);
|
|
258
269
|
if (!group) {
|
|
@@ -260,7 +271,7 @@ export class DataloaderUtils {
|
|
|
260
271
|
groups.set(key, group);
|
|
261
272
|
}
|
|
262
273
|
const pk = helper(col.owner).getPrimaryKey();
|
|
263
|
-
group.ownerPKs.set(
|
|
274
|
+
group.ownerPKs.set(stringifyCacheKey(pk), pk);
|
|
264
275
|
}
|
|
265
276
|
const promises = Array.from(groups.entries()).map(async ([key, group]) => {
|
|
266
277
|
const allPKs = Array.from(group.ownerPKs.values());
|
package/utils/Utils.js
CHANGED
|
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
|
|
|
153
153
|
/** Collection of general-purpose utility methods used throughout the ORM. */
|
|
154
154
|
export class Utils {
|
|
155
155
|
static PK_SEPARATOR = '~~~';
|
|
156
|
-
static #ORM_VERSION = '7.3.0-dev.
|
|
156
|
+
static #ORM_VERSION = '7.3.0-dev.21';
|
|
157
157
|
/** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
|
|
158
158
|
static getRlsSettingName(filterName, argName) {
|
|
159
159
|
return `mikro.${filterName}.${argName}`;
|