@mikro-orm/core 7.3.0-dev.2 → 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 +158 -42
- package/cache/cache-key.d.ts +5 -0
- package/cache/cache-key.js +30 -0
- package/drivers/DatabaseDriver.d.ts +2 -0
- package/drivers/DatabaseDriver.js +16 -3
- package/drivers/IDatabaseDriver.d.ts +2 -0
- package/entity/EntityAssigner.js +15 -3
- package/entity/EntityFactory.d.ts +8 -0
- package/entity/EntityFactory.js +69 -3
- package/entity/EntityLoader.d.ts +1 -0
- package/entity/EntityLoader.js +66 -23
- package/entity/WrappedEntity.js +6 -1
- package/entity/defineEntity.d.ts +32 -19
- package/entity/defineEntity.js +9 -0
- package/entity/validators.js +4 -1
- package/errors.d.ts +7 -0
- package/errors.js +15 -0
- package/hydration/ObjectHydrator.js +6 -2
- package/metadata/MetadataDiscovery.d.ts +10 -0
- package/metadata/MetadataDiscovery.js +178 -30
- package/metadata/MetadataStorage.d.ts +8 -0
- package/metadata/MetadataStorage.js +47 -3
- package/metadata/MetadataValidator.js +2 -1
- package/metadata/types.d.ts +12 -0
- package/package.json +1 -1
- package/platforms/Platform.d.ts +6 -0
- package/platforms/Platform.js +26 -0
- package/serialization/EntitySerializer.js +56 -16
- package/serialization/EntityTransformer.js +4 -4
- package/serialization/SerializationContext.d.ts +2 -1
- package/serialization/SerializationContext.js +4 -3
- package/types/DecimalType.js +34 -1
- package/types/DoubleType.d.ts +7 -3
- package/types/DoubleType.js +8 -3
- package/typings.d.ts +26 -6
- package/typings.js +5 -2
- package/unit-of-work/ChangeSetComputer.js +22 -7
- package/unit-of-work/ChangeSetPersister.js +24 -5
- package/unit-of-work/CommitOrderCalculator.js +2 -1
- package/unit-of-work/IdentityMap.d.ts +3 -2
- package/unit-of-work/IdentityMap.js +30 -7
- package/unit-of-work/UnitOfWork.js +49 -9
- package/utils/Configuration.d.ts +12 -1
- package/utils/DataloaderUtils.js +19 -8
- package/utils/EntityComparator.d.ts +5 -0
- package/utils/EntityComparator.js +87 -19
- package/utils/QueryHelper.d.ts +14 -0
- package/utils/QueryHelper.js +90 -13
- package/utils/RawQueryFragment.d.ts +5 -1
- package/utils/RawQueryFragment.js +27 -19
- package/utils/TransactionManager.js +4 -0
- package/utils/Utils.d.ts +2 -0
- package/utils/Utils.js +24 -8
- package/utils/clone.js +5 -7
- package/utils/env-vars.js +2 -0
- package/utils/fs-utils.js +19 -15
- package/utils/upsert-utils.js +2 -1
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,12 +194,16 @@ 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
|
}
|
|
200
|
-
async populateScalar(meta, filtered, options) {
|
|
201
|
-
const pk = Utils.getPrimaryKeyHash(meta.primaryKeys);
|
|
202
|
-
const ids = Utils.unique(filtered.map(e => Utils.getPrimaryKeyValues(e, meta, true)));
|
|
204
|
+
async populateScalar(meta, filtered, options, key) {
|
|
205
|
+
const pk = (key ?? Utils.getPrimaryKeyHash(meta.primaryKeys));
|
|
206
|
+
const ids = Utils.unique(filtered.map(e => (key ? e[key] : Utils.getPrimaryKeyValues(e, meta, true))));
|
|
203
207
|
const where = this.mergePrimaryCondition(ids, pk, options, meta, this.#metadata, this.#driver.getPlatform());
|
|
204
208
|
const { filters, convertCustomTypes, lockMode, strategy, populateWhere, connectionType, logging, fields } = options;
|
|
205
209
|
await this.#em.find(meta.class, where, {
|
|
@@ -221,7 +225,8 @@ export class EntityLoader {
|
|
|
221
225
|
const needsFkLoad = [];
|
|
222
226
|
for (const entity of entities) {
|
|
223
227
|
const refValue = entity[prop.name];
|
|
224
|
-
|
|
228
|
+
// a `targetKey` reference is identified by its key, it might not have the PK yet
|
|
229
|
+
if (refValue && helper(refValue).getTargetKeyValue(prop.targetKey) != null) {
|
|
225
230
|
if ((ref && !options.refresh) || // :ref hint - already have reference
|
|
226
231
|
(!ref && helper(refValue).__initialized && !options.refresh) // already loaded
|
|
227
232
|
) {
|
|
@@ -246,7 +251,7 @@ export class EntityLoader {
|
|
|
246
251
|
if (!ref) {
|
|
247
252
|
for (const entity of needsFkLoad) {
|
|
248
253
|
const refValue = entity[prop.name];
|
|
249
|
-
if (refValue && helper(refValue).
|
|
254
|
+
if (refValue && helper(refValue).getTargetKeyValue(prop.targetKey) != null) {
|
|
250
255
|
toPopulate.push(entity);
|
|
251
256
|
}
|
|
252
257
|
}
|
|
@@ -268,7 +273,7 @@ export class EntityLoader {
|
|
|
268
273
|
const allItems = [];
|
|
269
274
|
await Promise.all([...groups].map(async ([discriminator, children]) => {
|
|
270
275
|
const targetMeta = this.#metadata.find(prop.discriminatorMap[discriminator]);
|
|
271
|
-
await this.populateScalar(targetMeta, children, options);
|
|
276
|
+
await this.populateScalar(targetMeta, children, options, prop.targetKey);
|
|
272
277
|
allItems.push(...children);
|
|
273
278
|
}));
|
|
274
279
|
return allItems;
|
|
@@ -311,23 +316,42 @@ export class EntityLoader {
|
|
|
311
316
|
}
|
|
312
317
|
initializeManyToMany(filtered, children, prop, field, customOrder, partial, readonly) {
|
|
313
318
|
if (prop.mappedBy) {
|
|
319
|
+
// group children by owner in one pass
|
|
320
|
+
const map = new Map(filtered.map(entity => [entity, []]));
|
|
321
|
+
for (const child of children) {
|
|
322
|
+
for (const owner of child[prop.mappedBy].getItems(false)) {
|
|
323
|
+
map.get(owner)?.push(child);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
314
326
|
for (const entity of filtered) {
|
|
315
|
-
|
|
316
|
-
entity[field].hydrate(items, true, partial, readonly);
|
|
327
|
+
entity[field].hydrate(map.get(entity), true, partial, readonly);
|
|
317
328
|
}
|
|
318
329
|
}
|
|
319
330
|
else {
|
|
320
331
|
// owning side of M:N without pivot table needs to be reordered
|
|
332
|
+
const childIndex = new Map(children.map((child, idx) => [child, idx]));
|
|
321
333
|
for (const entity of filtered) {
|
|
322
|
-
|
|
323
|
-
const items =
|
|
324
|
-
if (
|
|
325
|
-
items.sort((a, b) =>
|
|
334
|
+
// keep the order of references, or the order of loaded children when a custom order is used
|
|
335
|
+
const items = entity[prop.name].getItems(false).filter(item => childIndex.has(item));
|
|
336
|
+
if (customOrder) {
|
|
337
|
+
items.sort((a, b) => childIndex.get(a) - childIndex.get(b));
|
|
326
338
|
}
|
|
327
339
|
entity[field].hydrate(items, true, partial, readonly);
|
|
328
340
|
}
|
|
329
341
|
}
|
|
330
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
|
+
}
|
|
331
355
|
async findChildren(entities, prop, populate, options, ref) {
|
|
332
356
|
const children = Utils.unique(this.getChildReferences(entities, prop, options, ref, populate));
|
|
333
357
|
const meta = prop.targetMeta;
|
|
@@ -426,11 +450,13 @@ export class EntityLoader {
|
|
|
426
450
|
visited: options.visited,
|
|
427
451
|
};
|
|
428
452
|
if (populate.limit != null) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
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
|
+
}
|
|
434
460
|
}
|
|
435
461
|
const items = await this.#em.find(meta.class, where, findOptions);
|
|
436
462
|
// For targetKey relations, wire up loaded entities to parent references
|
|
@@ -476,6 +502,11 @@ export class EntityLoader {
|
|
|
476
502
|
for (const entity of entities) {
|
|
477
503
|
const ref = entity[prop.name];
|
|
478
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
|
+
}
|
|
479
510
|
continue;
|
|
480
511
|
}
|
|
481
512
|
const refKey = getKey(ref);
|
|
@@ -494,7 +525,7 @@ export class EntityLoader {
|
|
|
494
525
|
this.#em.getUnitOfWork().unmarkAsLoaded(item);
|
|
495
526
|
}
|
|
496
527
|
}
|
|
497
|
-
return { items, partial };
|
|
528
|
+
return { items, partial, partitions: findOptions._partitionLimit?.partitions };
|
|
498
529
|
}
|
|
499
530
|
mergePrimaryCondition(ids, pk, options, meta, metadata, platform) {
|
|
500
531
|
const cond1 = QueryHelper.processWhere({
|
|
@@ -672,12 +703,20 @@ export class EntityLoader {
|
|
|
672
703
|
const subCond = Utils.isPlainObject(where[prop.name]) ? { ...where[prop.name] } : {};
|
|
673
704
|
const meta2 = prop.targetMeta;
|
|
674
705
|
const pk = Utils.getPrimaryKeyHash(meta2.primaryKeys);
|
|
706
|
+
// `$not` wrapping entity conditions stays on the target entity, only operators on the relation itself map to its PK
|
|
707
|
+
const isRelationOperator = (key, value) => {
|
|
708
|
+
if (key === '$not' && Utils.isPlainObject(value)) {
|
|
709
|
+
return Utils.getObjectQueryKeys(value).every(k => Utils.isOperator(k, false));
|
|
710
|
+
}
|
|
711
|
+
return Utils.isOperator(key, false);
|
|
712
|
+
};
|
|
675
713
|
['$and', '$or'].forEach(op => {
|
|
676
714
|
if (where[op]) {
|
|
677
715
|
const child = where[op]
|
|
678
716
|
.map((cond) => cond[prop.name])
|
|
679
717
|
.filter((sub) => sub != null &&
|
|
680
|
-
!(Utils.isPlainObject(sub) &&
|
|
718
|
+
!(Utils.isPlainObject(sub) &&
|
|
719
|
+
Utils.getObjectQueryKeys(sub).every(key => isRelationOperator(key, sub[key]))))
|
|
681
720
|
.map((cond) => {
|
|
682
721
|
if (Utils.isPrimaryKey(cond)) {
|
|
683
722
|
return { [pk]: cond };
|
|
@@ -690,7 +729,7 @@ export class EntityLoader {
|
|
|
690
729
|
}
|
|
691
730
|
}
|
|
692
731
|
});
|
|
693
|
-
const operators = Object.keys(subCond).filter(key =>
|
|
732
|
+
const operators = Object.keys(subCond).filter(key => isRelationOperator(key, subCond[key]));
|
|
694
733
|
if (operators.length > 0) {
|
|
695
734
|
subCond[pk] = Utils.isPlainObject(subCond[pk]) ? { ...subCond[pk] } : (subCond[pk] ?? {});
|
|
696
735
|
operators.forEach(op => {
|
|
@@ -808,7 +847,7 @@ export class EntityLoader {
|
|
|
808
847
|
if ([ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(wrapped.__meta.properties[f].kind)) {
|
|
809
848
|
return entity[f].getItems(false).every((item) => this.isPropertyLoaded(item, r.join('.')));
|
|
810
849
|
}
|
|
811
|
-
return this.isPropertyLoaded(entity[f], r.join('.'));
|
|
850
|
+
return this.isPropertyLoaded(Reference.unwrapReference(entity[f]), r.join('.'));
|
|
812
851
|
}
|
|
813
852
|
filterReferences(entities, field, options, ref, populate) {
|
|
814
853
|
if (ref) {
|
|
@@ -850,7 +889,11 @@ export class EntityLoader {
|
|
|
850
889
|
if (value === null) {
|
|
851
890
|
return false;
|
|
852
891
|
}
|
|
853
|
-
|
|
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)) {
|
|
854
897
|
return true;
|
|
855
898
|
}
|
|
856
899
|
const target = Reference.unwrapReference(value);
|
package/entity/WrappedEntity.js
CHANGED
|
@@ -83,7 +83,12 @@ export class WrappedEntity {
|
|
|
83
83
|
if (!this.__em) {
|
|
84
84
|
throw ValidationError.entityNotManaged(this.entity);
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
// a reference known only by its `targetKey` has no PK, so look it up by the key instead
|
|
87
|
+
const key = this.hasPrimaryKey()
|
|
88
|
+
? undefined
|
|
89
|
+
: this.__meta.root.targetKeys?.find(k => this.entity[k] != null);
|
|
90
|
+
const where = key ? { [key]: this.entity[key] } : this.entity;
|
|
91
|
+
return this.__em.findOne(this.entity.constructor, where, {
|
|
87
92
|
...options,
|
|
88
93
|
refresh: true,
|
|
89
94
|
schema: this.__schema,
|
package/entity/defineEntity.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EntityManager } from '../EntityManager.js';
|
|
2
|
-
import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions } from '../metadata/types.js';
|
|
2
|
+
import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions, SequenceOptions } from '../metadata/types.js';
|
|
3
3
|
import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, LazyRef, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints, OptionalProps, ExtractDefineEntityProperties } from '../typings.js';
|
|
4
4
|
import type { Raw } from '../utils/RawQueryFragment.js';
|
|
5
5
|
import type { ScalarReference } from './Reference.js';
|
|
@@ -130,6 +130,7 @@ export interface PropertyChain<in out Value, in out Options> {
|
|
|
130
130
|
unique(unique?: boolean): PropertyChain<Value, Options>;
|
|
131
131
|
comment(comment: string): PropertyChain<Value, Options>;
|
|
132
132
|
collation(collation: string): PropertyChain<Value, Options>;
|
|
133
|
+
sequence(sequence: SequenceOptions | number): PropertyChain<Value, Options>;
|
|
133
134
|
accessor(accessor?: string | boolean): PropertyChain<Value, Options>;
|
|
134
135
|
eager(eager?: boolean): HasKind<Options, 'm:1' | '1:m' | '1:1' | 'm:n'> extends true ? PropertyChain<Value, Options> : never;
|
|
135
136
|
cascade(...cascade: Cascade[]): HasKind<Options, 'm:1' | '1:m' | '1:1' | 'm:n'> extends true ? PropertyChain<Value, Options> : never;
|
|
@@ -486,6 +487,12 @@ export declare class UniversalPropertyOptionsBuilder<in out Value, in out Option
|
|
|
486
487
|
* Emits a `COLLATE` clause on the column definition; the accepted collation names are dialect-specific.
|
|
487
488
|
*/
|
|
488
489
|
collation(collation: string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
|
|
490
|
+
/**
|
|
491
|
+
* Set the start value and increment of an autoincrement column. (SQL only)
|
|
492
|
+
* A number is a shorthand for `{ startWith }`.
|
|
493
|
+
* Applied only when the column is created (and by `orm.schema.clear()`), never diffed by the schema generator.
|
|
494
|
+
*/
|
|
495
|
+
sequence(sequence: SequenceOptions | number): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
|
|
489
496
|
/** mysql only */
|
|
490
497
|
extra(extra: string): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
|
|
491
498
|
/**
|
|
@@ -593,7 +600,7 @@ type EntityTarget = {
|
|
|
593
600
|
'~entity': any;
|
|
594
601
|
} | EntityClass;
|
|
595
602
|
declare const propertyBuilders: PropertyBuilders;
|
|
596
|
-
type PropertyBuildersOverrideKeys = 'bigint' | 'array' | 'decimal' | 'json' | 'string' | 'text' | 'datetime' | 'time' | 'enum';
|
|
603
|
+
type PropertyBuildersOverrideKeys = 'bigint' | 'array' | 'decimal' | 'double' | 'json' | 'string' | 'text' | 'datetime' | 'time' | 'enum';
|
|
597
604
|
/** Map of factory functions for creating type-safe property builders (scalars, enums, embeddables, and relations). */
|
|
598
605
|
export type PropertyBuilders = {
|
|
599
606
|
[K in Exclude<keyof typeof types, PropertyBuildersOverrideKeys>]: () => UniversalPropertyOptionsBuilder<InferPropertyValueType<(typeof types)[K]>, EmptyOptions, IncludeKeysForProperty>;
|
|
@@ -601,6 +608,7 @@ export type PropertyBuilders = {
|
|
|
601
608
|
bigint: <Mode extends 'bigint' | 'number' | 'string' = 'bigint'>(mode?: Mode) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.bigint<Mode>>, EmptyOptions, IncludeKeysForProperty>;
|
|
602
609
|
array: <T = string>(toJsValue?: (i: string) => T, toDbValue?: (i: T) => string) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.array<T>>, EmptyOptions, IncludeKeysForProperty>;
|
|
603
610
|
decimal: <Mode extends 'number' | 'string' = 'string'>(mode?: Mode) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.decimal<Mode>>, EmptyOptions, IncludeKeysForProperty>;
|
|
611
|
+
double: <Mode extends 'number' | 'string' = 'number'>(mode?: Mode) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.double<Mode>>, EmptyOptions, IncludeKeysForProperty>;
|
|
604
612
|
json: <T>() => UniversalPropertyOptionsBuilder<T, EmptyOptions, IncludeKeysForProperty>;
|
|
605
613
|
string: () => StringPropertyOptionsBuilder<InferPropertyValueType<typeof types.string>, EmptyOptions>;
|
|
606
614
|
text: () => StringPropertyOptionsBuilder<InferPropertyValueType<typeof types.text>, EmptyOptions>;
|
|
@@ -630,6 +638,21 @@ type AllKeys<TProperties, TBase> = keyof TProperties | (IsNever<TBase> extends t
|
|
|
630
638
|
type PartialWhere<TProperties, TBase> = string | FilterQuery<{
|
|
631
639
|
[K in AllKeys<TProperties, TBase> & string]?: unknown;
|
|
632
640
|
}>;
|
|
641
|
+
type DefineEntityFilterMap = Dictionary<{
|
|
642
|
+
name: string;
|
|
643
|
+
cond: Dictionary | ((args: Dictionary, type: 'read' | 'update' | 'delete', em: any, options?: FindOptions<any, any, any, any> | FindOneOptions<any, any, any, any>, entityName?: string) => MaybePromise<FilterQuery<any>>);
|
|
644
|
+
default?: boolean;
|
|
645
|
+
entity?: EntityName<any> | EntityName<any>[];
|
|
646
|
+
args?: boolean;
|
|
647
|
+
strict?: boolean;
|
|
648
|
+
rls?: boolean | {
|
|
649
|
+
setting?: string;
|
|
650
|
+
};
|
|
651
|
+
}>;
|
|
652
|
+
type DefineEntityFilterMapLoose = Dictionary<{
|
|
653
|
+
name: string;
|
|
654
|
+
cond: Dictionary;
|
|
655
|
+
}>;
|
|
633
656
|
/** Metadata descriptor for `defineEntity()`, combining entity options with property definitions. */
|
|
634
657
|
export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false, TDiscriminatorColumn extends string | undefined = undefined, TDiscriminatorValue extends string | number | undefined = undefined, TBaseDiscriminatorColumn extends string | undefined = undefined, TEmbeddable extends boolean = false, TForceUndefined extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminator' | 'discriminatorColumn' | 'discriminatorValue' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'triggers' | 'repository' | 'filters' | 'orderBy'> {
|
|
635
658
|
name: TName;
|
|
@@ -641,20 +664,10 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
|
|
|
641
664
|
'~discriminatorColumn'?: TBaseDiscriminatorColumn;
|
|
642
665
|
});
|
|
643
666
|
properties: TProperties | ((properties: PropertyBuilders) => TProperties);
|
|
644
|
-
primaryKeys?: TPK & InferPrimaryKeyConstraint<TProperties
|
|
667
|
+
primaryKeys?: TPK & NoInfer<InferPrimaryKeyConstraint<TProperties>>[];
|
|
645
668
|
hooks?: DefineEntityHooks;
|
|
646
669
|
repository?: () => TRepository;
|
|
647
|
-
filters?:
|
|
648
|
-
name: string;
|
|
649
|
-
cond: Dictionary | ((args: Dictionary, type: 'read' | 'update' | 'delete', em: any, options?: FindOptions<any, any, any, any> | FindOneOptions<any, any, any, any>, entityName?: string) => MaybePromise<FilterQuery<any>>);
|
|
650
|
-
default?: boolean;
|
|
651
|
-
entity?: EntityName<any> | EntityName<any>[];
|
|
652
|
-
args?: boolean;
|
|
653
|
-
strict?: boolean;
|
|
654
|
-
rls?: boolean | {
|
|
655
|
-
setting?: string;
|
|
656
|
-
};
|
|
657
|
-
}>;
|
|
670
|
+
filters?: DefineEntityFilterMap | IsNever<TRepository, never, DefineEntityFilterMapLoose>;
|
|
658
671
|
forceObject?: TForceObject;
|
|
659
672
|
/** Hydrate `null` as `undefined` for this entity and infer nullable properties as `T | undefined`. */
|
|
660
673
|
forceUndefined?: TForceUndefined;
|
|
@@ -669,16 +682,16 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
|
|
|
669
682
|
/** Alias for `discriminator`. */
|
|
670
683
|
discriminatorColumn?: TDiscriminatorColumn;
|
|
671
684
|
discriminatorValue?: TDiscriminatorValue;
|
|
672
|
-
versionProperty?: AllKeys<TProperties, TBase
|
|
673
|
-
concurrencyCheckKeys?: Set<AllKeys<TProperties, TBase
|
|
674
|
-
serializedPrimaryKey?: AllKeys<TProperties, TBase
|
|
685
|
+
versionProperty?: NoInfer<AllKeys<TProperties, TBase>>;
|
|
686
|
+
concurrencyCheckKeys?: Set<NoInfer<AllKeys<TProperties, TBase>>>;
|
|
687
|
+
serializedPrimaryKey?: NoInfer<AllKeys<TProperties, TBase>>;
|
|
675
688
|
indexes?: {
|
|
676
689
|
properties?: NoInfer<AllKeys<TProperties, TBase>> | NoInfer<AllKeys<TProperties, TBase>>[];
|
|
677
690
|
name?: string;
|
|
678
691
|
type?: string;
|
|
679
692
|
options?: Dictionary;
|
|
680
693
|
expression?: string | IndexCallback<InferEntityFromProperties<TProperties, TPK, TBase>>;
|
|
681
|
-
where?: PartialWhere<TProperties, TBase
|
|
694
|
+
where?: NoInfer<PartialWhere<TProperties, TBase>>;
|
|
682
695
|
columns?: IndexColumnOptions[];
|
|
683
696
|
include?: NoInfer<AllKeys<TProperties, TBase>> | NoInfer<AllKeys<TProperties, TBase>>[];
|
|
684
697
|
fillFactor?: number;
|
|
@@ -691,7 +704,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
|
|
|
691
704
|
name?: string;
|
|
692
705
|
options?: Dictionary;
|
|
693
706
|
expression?: string | IndexCallback<InferEntityFromProperties<TProperties, TPK, TBase>>;
|
|
694
|
-
where?: PartialWhere<TProperties, TBase
|
|
707
|
+
where?: NoInfer<PartialWhere<TProperties, TBase>>;
|
|
695
708
|
deferMode?: DeferMode | `${DeferMode}`;
|
|
696
709
|
columns?: IndexColumnOptions[];
|
|
697
710
|
include?: NoInfer<AllKeys<TProperties, TBase>> | NoInfer<AllKeys<TProperties, TBase>>[];
|
package/entity/defineEntity.js
CHANGED
|
@@ -323,6 +323,14 @@ export class UniversalPropertyOptionsBuilder {
|
|
|
323
323
|
collation(collation) {
|
|
324
324
|
return this.assignOptions({ collation });
|
|
325
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Set the start value and increment of an autoincrement column. (SQL only)
|
|
328
|
+
* A number is a shorthand for `{ startWith }`.
|
|
329
|
+
* Applied only when the column is created (and by `orm.schema.clear()`), never diffed by the schema generator.
|
|
330
|
+
*/
|
|
331
|
+
sequence(sequence) {
|
|
332
|
+
return this.assignOptions({ sequence: typeof sequence === 'number' ? { startWith: sequence } : sequence });
|
|
333
|
+
}
|
|
326
334
|
/** mysql only */
|
|
327
335
|
extra(extra) {
|
|
328
336
|
return this.assignOptions({ extra });
|
|
@@ -511,6 +519,7 @@ const propertyBuilders = {
|
|
|
511
519
|
bigint: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.bigint(mode) }),
|
|
512
520
|
array: (toJsValue = i => i, toDbValue = i => i) => new UniversalPropertyOptionsBuilder({ type: new types.array(toJsValue, toDbValue) }),
|
|
513
521
|
decimal: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.decimal(mode) }),
|
|
522
|
+
double: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.double(mode) }),
|
|
514
523
|
json: () => new UniversalPropertyOptionsBuilder({ type: types.json }),
|
|
515
524
|
string: () => new StringPropertyOptionsBuilder({
|
|
516
525
|
type: types.string,
|
package/entity/validators.js
CHANGED
|
@@ -24,7 +24,10 @@ export function validateProperty(prop, givenValue, entity) {
|
|
|
24
24
|
}
|
|
25
25
|
function getValue(o, prop) {
|
|
26
26
|
if (prop.embedded && prop.embedded[0] in o) {
|
|
27
|
-
|
|
27
|
+
const embeddable = o[prop.embedded[0]];
|
|
28
|
+
const variantProp = embeddable?.__meta?.properties[prop.embedded[1]];
|
|
29
|
+
// a same-named property of a different kind in another polymorphic variant
|
|
30
|
+
return variantProp && !Utils.isSameKind(variantProp, prop) ? undefined : embeddable?.[prop.embedded[1]];
|
|
28
31
|
}
|
|
29
32
|
/* v8 ignore next */
|
|
30
33
|
if (prop.ref) {
|
package/errors.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class ValidationError<T extends AnyEntity = AnyEntity> extends Er
|
|
|
10
10
|
static fromWrongPropertyType(entity: AnyEntity, property: string, expectedType: string, givenType: string, givenValue: string): ValidationError;
|
|
11
11
|
static fromWrongRepositoryType(entityName: string, repoType: string, method: string): ValidationError;
|
|
12
12
|
static fromMergeWithoutPK(meta: EntityMetadata): ValidationError;
|
|
13
|
+
static referenceWithoutPK(entity: AnyEntity): ValidationError;
|
|
13
14
|
static transactionRequired(): ValidationError;
|
|
14
15
|
static entityNotManaged(entity: AnyEntity): ValidationError;
|
|
15
16
|
static notEntity(owner: AnyEntity, prop: EntityProperty, data: any): ValidationError;
|
|
@@ -90,6 +91,12 @@ export declare class MetadataError<T extends AnyEntity = AnyEntity> extends Vali
|
|
|
90
91
|
static triggersNotSupportedByDriver(meta: EntityMetadata): MetadataError;
|
|
91
92
|
/** Thrown when row level security is declared on an entity using a driver that does not support it. */
|
|
92
93
|
static rowLevelSecurityNotSupportedByDriver(meta: EntityMetadata): MetadataError;
|
|
94
|
+
/** Thrown when the `sequence` option is declared on a property that is not autoincrement. */
|
|
95
|
+
static sequenceWithoutAutoincrement(meta: EntityMetadata, prop: EntityProperty): MetadataError;
|
|
96
|
+
/** Thrown when the `sequence` option is combined with an identity option list in `generated`. */
|
|
97
|
+
static sequenceWithIdentityOptions(meta: EntityMetadata, prop: EntityProperty): MetadataError;
|
|
98
|
+
/** Thrown when `sequence.incrementBy` is declared on a platform without a per-column increment. */
|
|
99
|
+
static sequenceIncrementNotSupported(meta: EntityMetadata, prop: EntityProperty, platform: string): MetadataError;
|
|
93
100
|
/** Thrown when row level security is declared on a non-root entity of an STI hierarchy. */
|
|
94
101
|
static rowLevelSecurityOnNonRootStiEntity(meta: EntityMetadata): MetadataError;
|
|
95
102
|
/** Thrown when two policies on the same entity are given the same explicit name. */
|
package/errors.js
CHANGED
|
@@ -28,6 +28,9 @@ export class ValidationError extends Error {
|
|
|
28
28
|
static fromMergeWithoutPK(meta) {
|
|
29
29
|
return new ValidationError(`You cannot merge entity '${meta.className}' without identifier!`);
|
|
30
30
|
}
|
|
31
|
+
static referenceWithoutPK(entity) {
|
|
32
|
+
return new ValidationError(`Entity '${entity.constructor.name}' is only known by an alternate key and has no primary key, load it first`, entity);
|
|
33
|
+
}
|
|
31
34
|
static transactionRequired() {
|
|
32
35
|
return new ValidationError('An open transaction is required for this operation');
|
|
33
36
|
}
|
|
@@ -293,6 +296,18 @@ export class MetadataError extends ValidationError {
|
|
|
293
296
|
static rowLevelSecurityNotSupportedByDriver(meta) {
|
|
294
297
|
return new MetadataError(`Entity ${meta.className} declares row level security which is not supported by the current driver. Row level security is only available with the PostgreSQL driver. To test a PostgreSQL app without a server, use the \`@mikro-orm/pglite\` driver instead of sqlite.`);
|
|
295
298
|
}
|
|
299
|
+
/** Thrown when the `sequence` option is declared on a property that is not autoincrement. */
|
|
300
|
+
static sequenceWithoutAutoincrement(meta, prop) {
|
|
301
|
+
return MetadataError.fromMessage(meta, prop, `defines the 'sequence' option, but is not an autoincrement property`);
|
|
302
|
+
}
|
|
303
|
+
/** Thrown when the `sequence` option is combined with an identity option list in `generated`. */
|
|
304
|
+
static sequenceWithIdentityOptions(meta, prop) {
|
|
305
|
+
return MetadataError.fromMessage(meta, prop, `defines the 'sequence' option together with identity options in 'generated', use only one of them`);
|
|
306
|
+
}
|
|
307
|
+
/** Thrown when `sequence.incrementBy` is declared on a platform without a per-column increment. */
|
|
308
|
+
static sequenceIncrementNotSupported(meta, prop, platform) {
|
|
309
|
+
return MetadataError.fromMessage(meta, prop, `defines 'sequence.incrementBy', but ${platform} does not support a per-column increment`);
|
|
310
|
+
}
|
|
296
311
|
/** Thrown when row level security is declared on a non-root entity of an STI hierarchy. */
|
|
297
312
|
static rowLevelSecurityOnNonRootStiEntity(meta) {
|
|
298
313
|
return new MetadataError(`Entity ${meta.className} declares row level security, but it is part of a single table inheritance hierarchy. Declare policies on the root entity ${meta.root.className} instead, as the whole hierarchy shares a single table.`);
|
|
@@ -207,7 +207,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
207
207
|
if (prop.customType?.ensureComparable(meta, prop)) {
|
|
208
208
|
registerCustomType(prop, this.safeKey(prop.name), 'convertToDatabaseValue', context);
|
|
209
209
|
ret.push(` if (data${dataKey} != null && typeof data${dataKey} !== 'object' && convertCustomTypes) {`);
|
|
210
|
-
ret.push(` data${dataKey} = convertToDatabaseValue_${this.safeKey(prop.name)}(entity${entityKey}.__helper.
|
|
210
|
+
ret.push(` data${dataKey} = convertToDatabaseValue_${this.safeKey(prop.name)}(entity${entityKey}.__helper.getTargetKeyValue(${prop.targetKey ? this.quote(prop.targetKey) : ''}));`);
|
|
211
211
|
ret.push(` }`);
|
|
212
212
|
}
|
|
213
213
|
return ret;
|
|
@@ -313,6 +313,10 @@ export class ObjectHydrator extends Hydrator {
|
|
|
313
313
|
? dataKey + this.wrap(childProp.embedded[1])
|
|
314
314
|
: this.wrap(childProp.name);
|
|
315
315
|
const prop2 = childMeta.properties[childProp.embedded[1]];
|
|
316
|
+
// a same-named property of a different kind in another variant
|
|
317
|
+
if (prop2 && !Utils.isSameKind(prop2, childProp)) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
316
320
|
const prop3 = {
|
|
317
321
|
...prop2,
|
|
318
322
|
name: childProp.name,
|
|
@@ -404,7 +408,7 @@ export class ObjectHydrator extends Hydrator {
|
|
|
404
408
|
const code = `// compiled hydrator for entity ${meta.className} (${type + normalizeAccessors ? ' normalized' : ''})\n` +
|
|
405
409
|
`return function(entity, data, factory, newEntity, convertCustomTypes, schema, parentSchema, normalizeAccessors) {\n` +
|
|
406
410
|
`${lines.join('\n')}\n}`;
|
|
407
|
-
const fnKey = `hydrator-${meta.
|
|
411
|
+
const fnKey = `hydrator-${meta.compiledName}-${type}-${normalizeAccessors}`;
|
|
408
412
|
const hydrator = Utils.createFunction(context, code, this.config.get('compiledFunctions'), fnKey);
|
|
409
413
|
this.#hydrators[key].set(meta.class, hydrator);
|
|
410
414
|
return hydrator;
|
|
@@ -29,11 +29,21 @@ export declare class MetadataDiscovery {
|
|
|
29
29
|
private initFieldName;
|
|
30
30
|
private initManyToOneFieldName;
|
|
31
31
|
private initManyToManyFieldName;
|
|
32
|
+
/**
|
|
33
|
+
* Walks up the TPT hierarchy to the entity whose M:N declaration this property inherits. A child redeclaring
|
|
34
|
+
* the property with a different target, pivot table, join columns or fixed order keeps its own pivot table.
|
|
35
|
+
*/
|
|
36
|
+
private getPivotDeclaringMeta;
|
|
32
37
|
private initManyToManyFields;
|
|
33
38
|
private isExplicitTableName;
|
|
34
39
|
private initManyToOneFields;
|
|
35
40
|
private initOneToManyFields;
|
|
36
41
|
private processEntity;
|
|
42
|
+
/**
|
|
43
|
+
* Groups the relations of all entities by target class name in one pass, so `findReferencingProperties()`
|
|
44
|
+
* does not go over the relations of every entity for each entity. Lists keep the order of `metadata` and `relations`.
|
|
45
|
+
*/
|
|
46
|
+
private indexReferencingProperties;
|
|
37
47
|
private findReferencingProperties;
|
|
38
48
|
private initFactoryField;
|
|
39
49
|
private ensureCorrectFKOrderInPivotEntity;
|