@mikro-orm/core 6.4.1-dev.9 → 6.4.2-dev.0
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/entity/EntityAssigner.d.ts +49 -0
- package/entity/EntityFactory.js +6 -3
- package/metadata/MetadataDiscovery.js +2 -2
- package/package.json +4 -4
- package/typings.d.ts +1 -0
- package/typings.js +3 -0
- package/unit-of-work/ChangeSet.js +8 -0
- package/unit-of-work/UnitOfWork.js +1 -1
- package/utils/EntityComparator.js +13 -1
|
@@ -17,14 +17,63 @@ export declare class EntityAssigner {
|
|
|
17
17
|
}
|
|
18
18
|
export declare const assign: typeof EntityAssigner.assign;
|
|
19
19
|
export interface AssignOptions<Convert extends boolean> {
|
|
20
|
+
/**
|
|
21
|
+
* Allows disabling processing of nested relations. When disabled, an object payload in place of a relation always
|
|
22
|
+
* results in an `INSERT` query. To assign a value of the relation, use the foreign key instead of an object.
|
|
23
|
+
* Defaults to `true`.
|
|
24
|
+
*/
|
|
20
25
|
updateNestedEntities?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* When assigning to a relation property with object payload and `updateNestedEntities` enabled (default), you can
|
|
28
|
+
* control how a payload without a primary key is handled. By default, it is considered as a new object, resulting
|
|
29
|
+
* in an `INSERT` query. Use `updateByPrimaryKey: false` to allow assigning the data on an existing relation instead.
|
|
30
|
+
* Defaults to `true`.
|
|
31
|
+
*/
|
|
21
32
|
updateByPrimaryKey?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* When you have some properties in the payload that are not represented by an entity property mapping, you can skip
|
|
35
|
+
* such unknown properties via `onlyProperties: true`. Defaults to `false`.
|
|
36
|
+
*/
|
|
22
37
|
onlyProperties?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* With `onlyOwnProperties` enabled, to-many relations are skipped, and payloads of to-one relations are converted
|
|
40
|
+
* to foreign keys. Defaults to `false`.
|
|
41
|
+
*/
|
|
23
42
|
onlyOwnProperties?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* `assign` excepts runtime values for properties using custom types. To be able to assign raw database values, you
|
|
45
|
+
* can enable the `convertCustomTypes` option. Defaults to `false`.
|
|
46
|
+
*/
|
|
24
47
|
convertCustomTypes?: Convert;
|
|
48
|
+
/**
|
|
49
|
+
* When assigning to a JSON property, the value is replaced. Use `mergeObjectProperties: true` to enable deep merging
|
|
50
|
+
* of the payload with the existing value. Defaults to `false`.
|
|
51
|
+
*/
|
|
25
52
|
mergeObjectProperties?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* When assigning to an embedded property, the values are deeply merged with the existing data.
|
|
55
|
+
* Use `mergeEmbeddedProperties: false` to replace them instead. Defaults to `true`.
|
|
56
|
+
*/
|
|
26
57
|
mergeEmbeddedProperties?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* When assigning to a relation property, if the value is a POJO and `updateByPrimaryKey` is enabled, we check if
|
|
60
|
+
* the target exists in the identity map based on its primary key and call `assign` on it recursively. If there is
|
|
61
|
+
* no primary key provided, or the entity is not present in the context, such an entity is considered as new
|
|
62
|
+
* (resulting in `INSERT` query), created via `em.create()`. You can use `merge: true` to use `em.merge()` instead,
|
|
63
|
+
* which means there won't be any query used for persisting the relation. Defaults to `false`.
|
|
64
|
+
*/
|
|
27
65
|
merge?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* When assigning to a to-many relation properties (`Collection`) with `updateNestedEntities` and `updateByPrimaryKey`
|
|
68
|
+
* enabled (default), you can use this option to override the relation schema. This is used only when trying to find
|
|
69
|
+
* the entity reference in the current context. If it is not found, we create the relation entity using the target
|
|
70
|
+
* entity schema. The value is automatically inferred from the target entity.
|
|
71
|
+
*/
|
|
28
72
|
schema?: string;
|
|
73
|
+
/**
|
|
74
|
+
* When using the static `assign()` helper, you can pass the EntityManager instance explicitly via the `em` option.
|
|
75
|
+
* This is only needed when you try to assign a relation property. The value is automatically inferred from the target
|
|
76
|
+
* entity when it is managed, or when you use `em.assign()` instead.
|
|
77
|
+
*/
|
|
29
78
|
em?: EntityManager;
|
|
30
79
|
}
|
package/entity/EntityFactory.js
CHANGED
|
@@ -283,18 +283,21 @@ class EntityFactory {
|
|
|
283
283
|
extractConstructorParams(meta, data, options) {
|
|
284
284
|
return meta.constructorParams.map(k => {
|
|
285
285
|
if (meta.properties[k] && [enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(meta.properties[k].kind) && data[k]) {
|
|
286
|
-
const
|
|
286
|
+
const pk = Reference_1.Reference.unwrapReference(data[k]);
|
|
287
|
+
const entity = this.unitOfWork.getById(meta.properties[k].type, pk, options.schema);
|
|
287
288
|
if (entity) {
|
|
288
289
|
return entity;
|
|
289
290
|
}
|
|
290
291
|
if (Utils_1.Utils.isEntity(data[k])) {
|
|
291
292
|
return data[k];
|
|
292
293
|
}
|
|
293
|
-
|
|
294
|
+
const nakedPk = Utils_1.Utils.extractPK(data[k], meta.properties[k].targetMeta, true);
|
|
295
|
+
if (Utils_1.Utils.isObject(data[k]) && !nakedPk) {
|
|
294
296
|
return this.create(meta.properties[k].type, data[k], options);
|
|
295
297
|
}
|
|
296
298
|
const { newEntity, initialized, ...rest } = options;
|
|
297
|
-
|
|
299
|
+
const target = this.createReference(meta.properties[k].type, nakedPk, rest);
|
|
300
|
+
return Reference_1.Reference.wrapReference(target, meta.properties[k]);
|
|
298
301
|
}
|
|
299
302
|
if (meta.properties[k]?.kind === enums_1.ReferenceKind.EMBEDDED && data[k]) {
|
|
300
303
|
/* istanbul ignore next */
|
|
@@ -56,13 +56,13 @@ class MetadataDiscovery {
|
|
|
56
56
|
this.logger.log('discovery', `ORM entity discovery started, using ${colors_1.colors.cyan(this.metadataProvider.constructor.name)} in sync mode`);
|
|
57
57
|
this.findEntities(preferTsNode, true);
|
|
58
58
|
for (const meta of this.discovered) {
|
|
59
|
-
this.config.get('discovery').onMetadata?.(meta, this.platform);
|
|
59
|
+
void this.config.get('discovery').onMetadata?.(meta, this.platform);
|
|
60
60
|
}
|
|
61
61
|
this.processDiscoveredEntities(this.discovered);
|
|
62
62
|
const diff = Date.now() - startTime;
|
|
63
63
|
this.logger.log('discovery', `- entity discovery finished, found ${colors_1.colors.green('' + this.discovered.length)} entities, took ${colors_1.colors.green(`${diff} ms`)}`);
|
|
64
64
|
const storage = this.mapDiscoveredEntities();
|
|
65
|
-
this.config.get('discovery').afterDiscovered?.(storage, this.platform);
|
|
65
|
+
void this.config.get('discovery').afterDiscovered?.(storage, this.platform);
|
|
66
66
|
return storage;
|
|
67
67
|
}
|
|
68
68
|
mapDiscoveredEntities() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "6.4.
|
|
3
|
+
"version": "6.4.2-dev.0",
|
|
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
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -59,12 +59,12 @@
|
|
|
59
59
|
"access": "public"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"dataloader": "2.2.
|
|
63
|
-
"dotenv": "16.4.
|
|
62
|
+
"dataloader": "2.2.3",
|
|
63
|
+
"dotenv": "16.4.7",
|
|
64
64
|
"esprima": "4.0.1",
|
|
65
65
|
"fs-extra": "11.2.0",
|
|
66
66
|
"globby": "11.1.0",
|
|
67
|
-
"mikro-orm": "6.4.
|
|
67
|
+
"mikro-orm": "6.4.2-dev.0",
|
|
68
68
|
"reflect-metadata": "0.2.2"
|
|
69
69
|
}
|
|
70
70
|
}
|
package/typings.d.ts
CHANGED
|
@@ -388,6 +388,7 @@ export declare class EntityMetadata<T = any> {
|
|
|
388
388
|
addProperty(prop: Partial<EntityProperty<T>>, sync?: boolean): void;
|
|
389
389
|
removeProperty(name: string, sync?: boolean): void;
|
|
390
390
|
getPrimaryProps(): EntityProperty<T>[];
|
|
391
|
+
getPrimaryProp(): EntityProperty<T>;
|
|
391
392
|
get tableName(): string;
|
|
392
393
|
set tableName(name: string);
|
|
393
394
|
sync(initIndexes?: boolean): void;
|
package/typings.js
CHANGED
|
@@ -31,6 +31,14 @@ class ChangeSet {
|
|
|
31
31
|
else {
|
|
32
32
|
this.primaryKey = this.originalEntity[this.meta.primaryKeys[0]];
|
|
33
33
|
}
|
|
34
|
+
if (!this.meta.compositePK
|
|
35
|
+
&& this.meta.getPrimaryProp().targetMeta?.compositePK
|
|
36
|
+
&& typeof this.primaryKey === 'object'
|
|
37
|
+
&& this.primaryKey !== null) {
|
|
38
|
+
this.primaryKey = this.meta.getPrimaryProp().targetMeta.primaryKeys.map(childPK => {
|
|
39
|
+
return this.primaryKey[childPK];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
34
42
|
if (object && this.primaryKey != null) {
|
|
35
43
|
return Utils_1.Utils.primaryKeyToObject(this.meta, this.primaryKey);
|
|
36
44
|
}
|
|
@@ -774,7 +774,7 @@ class UnitOfWork {
|
|
|
774
774
|
findExtraUpdates(changeSet, props) {
|
|
775
775
|
for (const prop of props) {
|
|
776
776
|
const ref = changeSet.entity[prop.name];
|
|
777
|
-
if (!ref) {
|
|
777
|
+
if (!ref || prop.deferMode === enums_1.DeferMode.INITIALLY_DEFERRED) {
|
|
778
778
|
continue;
|
|
779
779
|
}
|
|
780
780
|
if (Utils_1.Utils.isCollection(ref)) {
|
|
@@ -74,7 +74,19 @@ class EntityComparator {
|
|
|
74
74
|
else {
|
|
75
75
|
const pk = meta.primaryKeys[0];
|
|
76
76
|
if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
|
|
77
|
-
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference))
|
|
77
|
+
lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) {`);
|
|
78
|
+
lines.push(` const pk = entity${this.wrap(pk)}.__helper.getPrimaryKey();`);
|
|
79
|
+
if (meta.properties[pk].targetMeta.compositePK) {
|
|
80
|
+
lines.push(` if (typeof pk === 'object') {`);
|
|
81
|
+
lines.push(` return [`);
|
|
82
|
+
for (const childPK of meta.properties[pk].targetMeta.primaryKeys) {
|
|
83
|
+
lines.push(` pk${this.wrap(childPK)},`);
|
|
84
|
+
}
|
|
85
|
+
lines.push(` ];`);
|
|
86
|
+
lines.push(` }`);
|
|
87
|
+
}
|
|
88
|
+
lines.push(` return pk;`);
|
|
89
|
+
lines.push(` }`);
|
|
78
90
|
}
|
|
79
91
|
lines.push(` return entity${this.wrap(pk)};`);
|
|
80
92
|
}
|