@mikro-orm/core 6.3.14-dev.2 → 6.3.14-dev.20

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.
@@ -177,6 +177,19 @@ export interface PropertyOptions<Owner> {
177
177
  * ```
178
178
  */
179
179
  getter?: boolean;
180
+ /**
181
+ * When defining a property over a method (not a getter, a regular function), you can use this option to point
182
+ * to the method name.
183
+ *
184
+ * @example
185
+ * ```
186
+ * @Property({ getter: true })
187
+ * getFullName() {
188
+ * return this.firstName + this.lastName;
189
+ * }
190
+ * ```
191
+ */
192
+ getterName?: keyof Owner;
180
193
  /**
181
194
  * Set to define serialized primary key for MongoDB. (virtual)
182
195
  * Alias for `@SerializedPrimaryKey()` decorator.
@@ -69,6 +69,9 @@ class EntityFactory {
69
69
  if (options.convertCustomTypes) {
70
70
  for (const prop of meta.props) {
71
71
  if (prop.customType?.ensureComparable(meta, prop) && data[prop.name]) {
72
+ if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils_1.Utils.isPlainObject(data[prop.name])) {
73
+ data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
74
+ }
72
75
  data[prop.name] = prop.customType.convertToDatabaseValue(data[prop.name], this.platform, { key: prop.name, mode: 'hydration' });
73
76
  }
74
77
  }
@@ -176,10 +176,15 @@ class EntityHelper {
176
176
  inverse.addWithoutPropagation(owner);
177
177
  (0, wrap_1.helper)(owner).__em?.getUnitOfWork().cancelOrphanRemoval(owner);
178
178
  }
179
- if (prop.kind === enums_1.ReferenceKind.ONE_TO_ONE && entity && (!prop.owner || (0, wrap_1.helper)(entity).__initialized)) {
179
+ if (prop.kind === enums_1.ReferenceKind.ONE_TO_ONE) {
180
180
  if ((value != null && Reference_1.Reference.unwrapReference(inverse) !== owner) ||
181
- (value == null && entity[prop2.name] != null)) {
182
- EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
181
+ (value == null && entity?.[prop2.name] != null)) {
182
+ if (entity && (!prop.owner || (0, wrap_1.helper)(entity).__initialized)) {
183
+ EntityHelper.propagateOneToOne(entity, owner, prop, prop2, value, old);
184
+ }
185
+ if (old && prop.orphanRemoval) {
186
+ (0, wrap_1.helper)(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
187
+ }
183
188
  }
184
189
  }
185
190
  }
@@ -203,9 +208,6 @@ class EntityHelper {
203
208
  else {
204
209
  entity[prop2.name] = Reference_1.Reference.wrapReference(owner, prop);
205
210
  }
206
- if (old && prop.orphanRemoval) {
207
- (0, wrap_1.helper)(old).__em?.getUnitOfWork().scheduleOrphanRemoval(old);
208
- }
209
211
  if (old?.[prop2.name] != null) {
210
212
  delete (0, wrap_1.helper)(old).__data[prop2.name];
211
213
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.3.14-dev.2",
3
+ "version": "6.3.14-dev.20",
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",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.2.0",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.3.14-dev.2",
67
+ "mikro-orm": "6.3.14-dev.20",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
@@ -8,6 +8,7 @@ export declare class DecimalType extends Type<string | number, string> {
8
8
  mode?: ("number" | "string") | undefined;
9
9
  constructor(mode?: ("number" | "string") | undefined);
10
10
  convertToJSValue(value: string): number | string;
11
+ compareValues(a: string, b: string): boolean;
11
12
  getColumnType(prop: EntityProperty, platform: Platform): string;
12
13
  compareAsType(): string;
13
14
  }
@@ -17,6 +17,9 @@ class DecimalType extends Type_1.Type {
17
17
  }
18
18
  return value;
19
19
  }
20
+ compareValues(a, b) {
21
+ return String(a) === String(b);
22
+ }
20
23
  getColumnType(prop, platform) {
21
24
  return platform.getDecimalTypeDeclarationSQL(prop);
22
25
  }
@@ -23,7 +23,7 @@ export declare class DataloaderUtils {
23
23
  static groupInversedOrMappedKeysByEntityAndOpts(collsWithOpts: readonly [Collection<any>, Omit<InitCollectionOptions<any, any>, 'dataloader'>?][]): Map<string, Map<string, Set<Primary<any>>>>;
24
24
  /**
25
25
  * Turn the entity+options map into actual queries.
26
- * The keys are the entity names + a strigified 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.
26
+ * 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.
27
27
  * We must populate the inverse side of the relationship in order to be able to later retrieve the PK(s) from its item(s).
28
28
  * 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.
29
29
  */
@@ -33,7 +33,7 @@ export declare class DataloaderUtils {
33
33
  * First checks if the Entity type matches, then retrieves the inverse side of the relationship
34
34
  * where the filtering will be done in order to match the target collection.
35
35
  */
36
- static getColFilter<T, S extends T>(collection: Collection<any, object>): (result: T) => result is S;
36
+ static getColFilter<T, S extends T>(collection: Collection<any>): (result: T) => result is S;
37
37
  /**
38
38
  * Returns the collection dataloader batchLoadFn, which aggregates collections by entity,
39
39
  * makes one query per entity and maps each input collection to the corresponding result.
@@ -4,6 +4,7 @@ exports.DataloaderUtils = void 0;
4
4
  const Collection_1 = require("../entity/Collection");
5
5
  const wrap_1 = require("../entity/wrap");
6
6
  const enums_1 = require("../enums");
7
+ const Reference_1 = require("../entity/Reference");
7
8
  class DataloaderUtils {
8
9
  /**
9
10
  * Groups identified references by entity and returns a Map with the
@@ -92,7 +93,7 @@ class DataloaderUtils {
92
93
  }
93
94
  /**
94
95
  * Turn the entity+options map into actual queries.
95
- * The keys are the entity names + a strigified 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
+ * 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
97
  * 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
98
  * 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
  */
@@ -139,20 +140,21 @@ class DataloaderUtils {
139
140
  // There is no need to check if Entity matches because we already matched the key which is entity+options.
140
141
  // This is the inverse side of the relationship where the filtering will be done in order to match the target collection
141
142
  // Either inversedBy or mappedBy exist because we already checked in groupInversedOrMappedKeysByEntity
142
- const refOrCol = result[(collection.property.inversedBy ?? collection.property.mappedBy)];
143
- if (refOrCol instanceof Collection_1.Collection) {
144
- // The inverse side is a Collection
145
- // We keep the result if any of PKs of the inverse side matches the PK of the collection owner
146
- for (const item of refOrCol.getItems()) {
147
- if ((0, wrap_1.helper)(item).getSerializedPrimaryKey() === (0, wrap_1.helper)(collection.owner).getSerializedPrimaryKey()) {
143
+ const inverseProp = collection.property.inversedBy ?? collection.property.mappedBy;
144
+ const target = Reference_1.Reference.unwrapReference(result[inverseProp]);
145
+ if (target instanceof Collection_1.Collection) {
146
+ for (const item of target) {
147
+ if (item === collection.owner) {
148
148
  return true;
149
149
  }
150
150
  }
151
151
  }
152
- else {
153
- // The inverse side is a Reference
154
- // We keep the result if the PK of the inverse side matches the PK of the collection owner
155
- return (0, wrap_1.helper)(refOrCol).getSerializedPrimaryKey() === (0, wrap_1.helper)(collection.owner).getSerializedPrimaryKey();
152
+ else if (target) {
153
+ return target === collection.owner;
154
+ }
155
+ // FIXME https://github.com/mikro-orm/mikro-orm/issues/6031
156
+ if (!target && collection.property.kind === enums_1.ReferenceKind.MANY_TO_MANY) {
157
+ throw new Error(`Inverse side is required for M:N relations with dataloader: ${collection.owner.constructor.name}.${collection.property.name}`);
156
158
  }
157
159
  return false;
158
160
  };
package/utils/Utils.js CHANGED
@@ -50,7 +50,12 @@ function compareObjects(a, b) {
50
50
  return a.sql === b.sql && compareArrays(a.params, b.params);
51
51
  }
52
52
  if ((a instanceof Date && b instanceof Date)) {
53
- return a.toISOString() === b.toISOString();
53
+ const timeA = a.getTime();
54
+ const timeB = b.getTime();
55
+ if (isNaN(timeA) || isNaN(timeB)) {
56
+ throw new Error('Comparing invalid dates is not supported');
57
+ }
58
+ return timeA === timeB;
54
59
  }
55
60
  if ((typeof a === 'function' && typeof b === 'function') ||
56
61
  (typeof a === 'object' && a.client && ['Ref', 'Raw'].includes(a.constructor.name) && typeof b === 'object' && b.client && ['Ref', 'Raw'].includes(b.constructor.name)) || // knex qb
package/utils/clone.js CHANGED
@@ -75,7 +75,7 @@ function clone(parent, respectCustomCloneMethod = true) {
75
75
  child.lastIndex = parent.lastIndex;
76
76
  }
77
77
  }
78
- else if (parent instanceof Date || (typeof parent === 'object' && parent.constructor.name === 'Date')) {
78
+ else if (parent instanceof Date) {
79
79
  child = new Date(parent.getTime());
80
80
  }
81
81
  else if (Buffer.isBuffer(parent)) {