@mikro-orm/core 6.4.17-dev.89 → 6.4.17-dev.90

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.
@@ -174,8 +174,8 @@ class EntityFactory {
174
174
  if (Array.isArray(id)) {
175
175
  id = Utils_1.Utils.getPrimaryKeyCondFromArray(id, meta);
176
176
  }
177
- const pks = Utils_1.Utils.getOrderedPrimaryKeys(id, meta, this.platform, options.convertCustomTypes);
178
- const exists = this.unitOfWork.getById(entityName, pks, schema);
177
+ const pks = Utils_1.Utils.getOrderedPrimaryKeys(id, meta, this.platform);
178
+ const exists = this.unitOfWork.getById(entityName, pks, schema, options.convertCustomTypes);
179
179
  if (exists) {
180
180
  return exists;
181
181
  }
@@ -265,7 +265,7 @@ class EntityFactory {
265
265
  if (!Array.isArray(data) && meta.primaryKeys.some(pk => data[pk] == null)) {
266
266
  return undefined;
267
267
  }
268
- const pks = Utils_1.Utils.getOrderedPrimaryKeys(data, meta, this.platform);
268
+ const pks = Utils_1.Utils.getOrderedPrimaryKeys(data, meta, this.platform, options.convertCustomTypes);
269
269
  return this.unitOfWork.getById(meta.className, pks, schema);
270
270
  }
271
271
  processDiscriminatorColumn(meta, data) {
@@ -299,7 +299,7 @@ class EntityFactory {
299
299
  return meta.constructorParams.map(k => {
300
300
  if (meta.properties[k] && [enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(meta.properties[k].kind) && data[k]) {
301
301
  const pk = Reference_1.Reference.unwrapReference(data[k]);
302
- const entity = this.unitOfWork.getById(meta.properties[k].type, pk, options.schema);
302
+ const entity = this.unitOfWork.getById(meta.properties[k].type, pk, options.schema, true);
303
303
  if (entity) {
304
304
  return entity;
305
305
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.4.17-dev.89",
3
+ "version": "6.4.17-dev.90",
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.3.1",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.4.17-dev.89",
67
+ "mikro-orm": "6.4.17-dev.90",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
package/typings.d.ts CHANGED
@@ -401,7 +401,7 @@ export declare class EntityMetadata<T = any> {
401
401
  constructor(meta?: Partial<EntityMetadata>);
402
402
  addProperty(prop: Partial<EntityProperty<T>>, sync?: boolean): void;
403
403
  removeProperty(name: string, sync?: boolean): void;
404
- getPrimaryProps(): EntityProperty<T>[];
404
+ getPrimaryProps(flatten?: boolean): EntityProperty<T>[];
405
405
  getPrimaryProp(): EntityProperty<T>;
406
406
  createColumnMappingObject(): Dictionary<any>;
407
407
  get tableName(): string;
package/typings.js CHANGED
@@ -47,8 +47,17 @@ class EntityMetadata {
47
47
  this.sync();
48
48
  }
49
49
  }
50
- getPrimaryProps() {
51
- return this.primaryKeys.map(pk => this.properties[pk]);
50
+ getPrimaryProps(flatten = false) {
51
+ const pks = this.primaryKeys.map(pk => this.properties[pk]);
52
+ if (flatten) {
53
+ return pks.flatMap(pk => {
54
+ if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(pk.kind)) {
55
+ return pk.targetMeta.getPrimaryProps(true);
56
+ }
57
+ return [pk];
58
+ });
59
+ }
60
+ return pks;
52
61
  }
53
62
  getPrimaryProp() {
54
63
  return this.properties[this.primaryKeys[0]];
@@ -331,7 +331,7 @@ class ChangeSetPersister {
331
331
  });
332
332
  const data = await this.driver.find(meta.className, { [pk]: { $in: pks } }, options);
333
333
  const map = new Map();
334
- data.forEach(item => map.set(utils_1.Utils.getCompositeKeyHash(item, meta, true, this.platform, true), item));
334
+ data.forEach(item => map.set(utils_1.Utils.getCompositeKeyHash(item, meta, false, this.platform, true), item));
335
335
  for (const changeSet of changeSets) {
336
336
  const data = map.get((0, entity_1.helper)(changeSet.entity).getSerializedPrimaryKey());
337
337
  this.hydrator.hydrate(changeSet.entity, meta, data, this.factory, 'full', false, true);
@@ -38,7 +38,7 @@ export declare class UnitOfWork {
38
38
  /**
39
39
  * Returns entity from the identity map. For composite keys, you need to pass an array of PKs in the same order as they are defined in `meta.primaryKeys`.
40
40
  */
41
- getById<T extends object>(entityName: string, id: Primary<T> | Primary<T>[], schema?: string): T | undefined;
41
+ getById<T extends object>(entityName: string, id: Primary<T> | Primary<T>[], schema?: string, convertCustomTypes?: boolean): T | undefined;
42
42
  tryGetById<T extends object>(entityName: string, where: FilterQuery<T>, schema?: string, strict?: boolean): T | null;
43
43
  /**
44
44
  * Returns map of all managed entities.
@@ -124,7 +124,7 @@ class UnitOfWork {
124
124
  /**
125
125
  * Returns entity from the identity map. For composite keys, you need to pass an array of PKs in the same order as they are defined in `meta.primaryKeys`.
126
126
  */
127
- getById(entityName, id, schema) {
127
+ getById(entityName, id, schema, convertCustomTypes) {
128
128
  if (id == null || (Array.isArray(id) && id.length === 0)) {
129
129
  return undefined;
130
130
  }
@@ -134,7 +134,16 @@ class UnitOfWork {
134
134
  hash = '' + id;
135
135
  }
136
136
  else {
137
- const keys = Array.isArray(id) ? Utils_1.Utils.flatten(id) : [id];
137
+ let keys = Array.isArray(id) ? Utils_1.Utils.flatten(id) : [id];
138
+ keys = meta.getPrimaryProps(true).map((p, i) => {
139
+ if (!convertCustomTypes && p.customType) {
140
+ return p.customType.convertToDatabaseValue(keys[i], this.platform, {
141
+ key: p.name,
142
+ mode: 'hydration',
143
+ });
144
+ }
145
+ return keys[i];
146
+ });
138
147
  hash = Utils_1.Utils.getPrimaryKeyHash(keys);
139
148
  }
140
149
  schema ??= meta.schema ?? this.em.config.getSchema();
@@ -158,6 +158,7 @@ class EntityComparator {
158
158
  const lines = [];
159
159
  const context = new Map();
160
160
  context.set('getCompositeKeyValue', (val) => Utils_1.Utils.flatten(Utils_1.Utils.getCompositeKeyValue(val, meta, 'convertToDatabaseValue', this.platform)));
161
+ context.set('getPrimaryKeyHash', (val) => Utils_1.Utils.getPrimaryKeyHash(Utils_1.Utils.asArray(val)));
161
162
  if (meta.primaryKeys.length > 1) {
162
163
  lines.push(` const pks = entity.__helper.__pk ? getCompositeKeyValue(entity.__helper.__pk) : [`);
163
164
  meta.primaryKeys.forEach(pk => {
@@ -173,14 +174,23 @@ class EntityComparator {
173
174
  }
174
175
  else {
175
176
  const pk = meta.primaryKeys[0];
176
- if (meta.properties[pk].kind !== enums_1.ReferenceKind.SCALAR) {
177
+ const prop = meta.properties[pk];
178
+ if (prop.kind !== enums_1.ReferenceKind.SCALAR) {
177
179
  lines.push(` if (entity${this.wrap(pk)} != null && (entity${this.wrap(pk)}.__entity || entity${this.wrap(pk)}.__reference)) return entity${this.wrap(pk)}.__helper.getSerializedPrimaryKey();`);
178
180
  }
179
181
  const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey);
180
182
  if (serializedPrimaryKey) {
181
183
  lines.push(` return '' + entity.${serializedPrimaryKey.name};`);
182
184
  }
183
- lines.push(` return '' + entity.${meta.primaryKeys[0]};`);
185
+ else if (prop.customType) {
186
+ const convertorKey = this.registerCustomType(meta.properties[pk], context);
187
+ const idx = this.tmpIndex++;
188
+ lines.push(` const val_${idx} = convertToDatabaseValue_${convertorKey}(entity${this.wrap(pk)});`);
189
+ lines.push(` return getPrimaryKeyHash(val_${idx});`);
190
+ }
191
+ else {
192
+ lines.push(` return '' + entity${this.wrap(pk)};`);
193
+ }
184
194
  }
185
195
  const code = `// compiled pk serializer for entity ${meta.className}\n`
186
196
  + `return function(entity) {\n${lines.join('\n')}\n}`;