@mikro-orm/core 7.0.0-dev.115 → 7.0.0-dev.117

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 CHANGED
@@ -571,13 +571,13 @@ export class EntityManager {
571
571
  let found = false;
572
572
  for (const e of fork.unitOfWork.getIdentityMap()) {
573
573
  const ref = em.getReference(e.constructor, helper(e).getPrimaryKey());
574
- const data = helper(e).serialize({ ignoreSerializers: true, includeHidden: true });
574
+ const data = helper(e).serialize({ ignoreSerializers: true, includeHidden: true, convertCustomTypes: true });
575
575
  em.config.getHydrator(this.metadata).hydrate(ref, helper(ref).__meta, data, em.entityFactory, 'full', false, true);
576
576
  Utils.merge(helper(ref).__originalEntityData, this.comparator.prepareEntity(e));
577
577
  found ||= ref === entity;
578
578
  }
579
579
  if (!found) {
580
- const data = helper(reloaded).serialize({ ignoreSerializers: true, includeHidden: true });
580
+ const data = helper(reloaded).serialize({ ignoreSerializers: true, includeHidden: true, convertCustomTypes: true });
581
581
  em.config.getHydrator(this.metadata).hydrate(entity, wrapped.__meta, data, em.entityFactory, 'full', false, true);
582
582
  Utils.merge(wrapped.__originalEntityData, this.comparator.prepareEntity(reloaded));
583
583
  }
@@ -822,7 +822,7 @@ export class MetadataDiscovery {
822
822
  const glue = object ? '~' : '_';
823
823
  for (const prop of Object.values(embeddable.properties)) {
824
824
  const name = (embeddedProp.embeddedPath?.join(glue) ?? embeddedProp.fieldNames[0] + glue) + prop.name;
825
- meta.properties[name] = Utils.copy(prop, false);
825
+ meta.properties[name] = Utils.copy(prop);
826
826
  meta.properties[name].name = name;
827
827
  meta.properties[name].embedded = [embeddedProp.name, prop.name];
828
828
  meta.propertyOrder.set(name, (order += 0.01));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.115",
4
+ "version": "7.0.0-dev.117",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -4,6 +4,7 @@ export declare class EntitySerializer {
4
4
  static serialize<T extends object, P extends string = never, E extends string = never>(entity: T, options?: SerializeOptions<T, P, E>): EntityDTO<Loaded<T, P>>;
5
5
  private static propertyName;
6
6
  private static processProperty;
7
+ private static processCustomType;
7
8
  private static extractChildOptions;
8
9
  private static processEntity;
9
10
  private static processCollection;
@@ -23,6 +24,8 @@ export interface SerializeOptions<T, P extends string = never, E extends string
23
24
  skipNull?: boolean;
24
25
  /** Only include properties for a specific group. If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. */
25
26
  groups?: string[];
27
+ /** Convert custom types to their database representation. By default, the `Type.toJSON` method is invoked instead. */
28
+ convertCustomTypes?: boolean;
26
29
  }
27
30
  /**
28
31
  * Converts entity instance to POJO, converting the `Collection`s to arrays and unwrapping the `Reference` wrapper, while respecting the serialization options.
@@ -159,12 +159,20 @@ export class EntitySerializer {
159
159
  return helper(value).toJSON();
160
160
  }
161
161
  }
162
- const customType = property?.customType;
163
- if (customType) {
164
- return customType.toJSON(value, wrapped.__platform);
162
+ if (property.customType) {
163
+ return this.processCustomType(value, property, wrapped.__platform, options.convertCustomTypes);
165
164
  }
166
165
  return wrapped.__platform.normalizePrimaryKey(value);
167
166
  }
167
+ static processCustomType(value, prop, platform, convertCustomTypes) {
168
+ if (!prop.customType) {
169
+ return value;
170
+ }
171
+ if (convertCustomTypes) {
172
+ return prop.customType.convertToDatabaseValue(value, platform, { mode: 'serialization' });
173
+ }
174
+ return prop.customType.toJSON(value, platform);
175
+ }
168
176
  static extractChildOptions(options, prop) {
169
177
  return {
170
178
  ...options,
@@ -183,10 +191,7 @@ export class EntitySerializer {
183
191
  if (expand) {
184
192
  return this.serialize(child, childOptions);
185
193
  }
186
- let pk = wrapped.getPrimaryKey();
187
- if (prop.customType) {
188
- pk = prop.customType.toJSON(pk, wrapped.__platform);
189
- }
194
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
190
195
  if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
191
196
  return Utils.primaryKeyToObject(meta, pk, visible);
192
197
  }
@@ -210,10 +215,7 @@ export class EntitySerializer {
210
215
  if (populated || !wrapped.__managed) {
211
216
  return this.serialize(item, this.extractChildOptions(options, prop.name));
212
217
  }
213
- let pk = wrapped.getPrimaryKey();
214
- if (prop.customType) {
215
- pk = prop.customType.toJSON(pk, wrapped.__platform);
216
- }
218
+ const pk = this.processCustomType(wrapped.getPrimaryKey(), prop, wrapped.__platform, options.convertCustomTypes);
217
219
  if (options.forceObject || wrapped.__config.get('serialization').forceObject) {
218
220
  return Utils.primaryKeyToObject(wrapped.__meta, pk);
219
221
  }
@@ -3,6 +3,7 @@ import { ReferenceKind } from '../enums.js';
3
3
  import { compareArrays, compareBooleans, compareBuffers, compareObjects, equals, parseJsonSafe, Utils } from './Utils.js';
4
4
  import { JsonType } from '../types/JsonType.js';
5
5
  import { Raw } from './RawQueryFragment.js';
6
+ import { EntityIdentifier } from '../entity/EntityIdentifier.js';
6
7
  export class EntityComparator {
7
8
  metadata;
8
9
  platform;
@@ -517,8 +518,11 @@ export class EntityComparator {
517
518
  return val;
518
519
  };
519
520
  context.set('toArray', toArray);
521
+ context.set('EntityIdentifier', EntityIdentifier);
520
522
  ret += ` if (entity${entityKey} === null) {\n`;
521
523
  ret += ` ret${dataKey} = null;\n`;
524
+ ret += ` } else if (entity${entityKey}?.__helper.__identifier && !entity${entityKey}.__helper.hasPrimaryKey()) {\n`;
525
+ ret += ` ret${dataKey} = entity${entityKey}?.__helper.__identifier;\n`;
522
526
  ret += ` } else if (typeof entity${entityKey} !== 'undefined') {\n`;
523
527
  ret += ` ret${dataKey} = toArray(entity${entityKey}.__helper.getPrimaryKey(true));\n`;
524
528
  ret += ` }\n`;
@@ -13,6 +13,7 @@ export declare class RawQueryFragment {
13
13
  [Symbol.toPrimitive](hint: 'string'): RawQueryFragmentSymbol;
14
14
  get [Symbol.toStringTag](): string;
15
15
  toJSON(): string;
16
+ clone(): this;
16
17
  static isKnownFragmentSymbol(key: unknown): key is RawQueryFragmentSymbol;
17
18
  static hasObjectFragments(object: unknown): boolean;
18
19
  static isKnownFragment(key: unknown): key is RawQueryFragment | symbol;
@@ -33,6 +33,9 @@ export class RawQueryFragment {
33
33
  toJSON() {
34
34
  return `raw('${this.sql}')`;
35
35
  }
36
+ clone() {
37
+ return this;
38
+ }
36
39
  static isKnownFragmentSymbol(key) {
37
40
  return typeof key === 'symbol' && this.#rawQueryReferences.has(key);
38
41
  }
package/utils/Utils.js CHANGED
@@ -123,7 +123,7 @@ export function parseJsonSafe(value) {
123
123
  }
124
124
  export class Utils {
125
125
  static PK_SEPARATOR = '~~~';
126
- static #ORM_VERSION = '7.0.0-dev.115';
126
+ static #ORM_VERSION = '7.0.0-dev.117';
127
127
  /**
128
128
  * Checks if the argument is instance of `Object`. Returns false for arrays.
129
129
  */