@mikro-orm/core 7.0.0-dev.33 → 7.0.0-dev.331

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.
Files changed (218) hide show
  1. package/EntityManager.d.ts +70 -75
  2. package/EntityManager.js +487 -402
  3. package/MikroORM.d.ts +45 -38
  4. package/MikroORM.js +123 -156
  5. package/README.md +7 -4
  6. package/cache/FileCacheAdapter.d.ts +2 -7
  7. package/cache/FileCacheAdapter.js +35 -30
  8. package/cache/GeneratedCacheAdapter.d.ts +1 -2
  9. package/cache/GeneratedCacheAdapter.js +6 -8
  10. package/cache/MemoryCacheAdapter.d.ts +1 -2
  11. package/cache/MemoryCacheAdapter.js +8 -8
  12. package/cache/index.d.ts +1 -2
  13. package/cache/index.js +0 -2
  14. package/connections/Connection.d.ts +12 -5
  15. package/connections/Connection.js +37 -15
  16. package/drivers/DatabaseDriver.d.ts +25 -18
  17. package/drivers/DatabaseDriver.js +144 -45
  18. package/drivers/IDatabaseDriver.d.ts +118 -23
  19. package/entity/BaseEntity.d.ts +63 -4
  20. package/entity/BaseEntity.js +0 -3
  21. package/entity/Collection.d.ts +95 -31
  22. package/entity/Collection.js +487 -139
  23. package/entity/EntityAssigner.js +37 -25
  24. package/entity/EntityFactory.d.ts +8 -9
  25. package/entity/EntityFactory.js +152 -100
  26. package/entity/EntityHelper.d.ts +2 -2
  27. package/entity/EntityHelper.js +69 -27
  28. package/entity/EntityLoader.d.ts +12 -13
  29. package/entity/EntityLoader.js +286 -125
  30. package/entity/EntityRepository.d.ts +28 -8
  31. package/entity/EntityRepository.js +8 -2
  32. package/entity/PolymorphicRef.d.ts +12 -0
  33. package/entity/PolymorphicRef.js +18 -0
  34. package/entity/Reference.d.ts +3 -8
  35. package/entity/Reference.js +62 -29
  36. package/entity/WrappedEntity.d.ts +7 -10
  37. package/entity/WrappedEntity.js +6 -7
  38. package/entity/defineEntity.d.ts +472 -313
  39. package/entity/defineEntity.js +134 -290
  40. package/entity/index.d.ts +2 -2
  41. package/entity/index.js +2 -2
  42. package/entity/utils.d.ts +6 -1
  43. package/entity/utils.js +46 -11
  44. package/entity/validators.d.ts +11 -0
  45. package/entity/validators.js +66 -0
  46. package/enums.d.ts +8 -6
  47. package/enums.js +13 -17
  48. package/errors.d.ts +26 -16
  49. package/errors.js +63 -31
  50. package/events/EventManager.d.ts +3 -5
  51. package/events/EventManager.js +37 -26
  52. package/events/index.d.ts +1 -1
  53. package/events/index.js +0 -1
  54. package/exceptions.js +9 -2
  55. package/hydration/Hydrator.js +1 -2
  56. package/hydration/ObjectHydrator.d.ts +5 -6
  57. package/hydration/ObjectHydrator.js +109 -50
  58. package/index.d.ts +2 -2
  59. package/index.js +1 -2
  60. package/logging/DefaultLogger.d.ts +1 -1
  61. package/logging/DefaultLogger.js +3 -4
  62. package/logging/SimpleLogger.d.ts +1 -1
  63. package/logging/colors.d.ts +1 -1
  64. package/logging/colors.js +4 -6
  65. package/logging/index.d.ts +2 -1
  66. package/logging/index.js +1 -1
  67. package/logging/inspect.d.ts +2 -0
  68. package/logging/inspect.js +11 -0
  69. package/metadata/EntitySchema.d.ts +47 -23
  70. package/metadata/EntitySchema.js +103 -34
  71. package/metadata/MetadataDiscovery.d.ts +65 -18
  72. package/metadata/MetadataDiscovery.js +940 -424
  73. package/metadata/MetadataProvider.d.ts +11 -2
  74. package/metadata/MetadataProvider.js +71 -2
  75. package/metadata/MetadataStorage.d.ts +11 -13
  76. package/metadata/MetadataStorage.js +79 -48
  77. package/metadata/MetadataValidator.d.ts +32 -9
  78. package/metadata/MetadataValidator.js +214 -44
  79. package/metadata/discover-entities.d.ts +5 -0
  80. package/metadata/discover-entities.js +40 -0
  81. package/metadata/index.d.ts +1 -1
  82. package/metadata/index.js +0 -1
  83. package/metadata/types.d.ts +577 -0
  84. package/metadata/types.js +1 -0
  85. package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
  86. package/naming-strategy/AbstractNamingStrategy.js +26 -5
  87. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  88. package/naming-strategy/EntityCaseNamingStrategy.js +7 -6
  89. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  90. package/naming-strategy/MongoNamingStrategy.js +6 -6
  91. package/naming-strategy/NamingStrategy.d.ts +28 -4
  92. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  93. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  94. package/naming-strategy/index.d.ts +1 -1
  95. package/naming-strategy/index.js +0 -1
  96. package/not-supported.d.ts +2 -0
  97. package/not-supported.js +8 -0
  98. package/package.json +47 -36
  99. package/platforms/ExceptionConverter.js +1 -1
  100. package/platforms/Platform.d.ts +33 -15
  101. package/platforms/Platform.js +125 -69
  102. package/serialization/EntitySerializer.d.ts +6 -3
  103. package/serialization/EntitySerializer.js +54 -30
  104. package/serialization/EntityTransformer.js +37 -22
  105. package/serialization/SerializationContext.d.ts +10 -14
  106. package/serialization/SerializationContext.js +24 -19
  107. package/types/ArrayType.d.ts +1 -1
  108. package/types/ArrayType.js +2 -3
  109. package/types/BigIntType.js +1 -1
  110. package/types/BlobType.d.ts +0 -1
  111. package/types/BlobType.js +0 -3
  112. package/types/BooleanType.d.ts +1 -0
  113. package/types/BooleanType.js +3 -0
  114. package/types/DecimalType.js +2 -2
  115. package/types/DoubleType.js +1 -1
  116. package/types/EnumArrayType.js +1 -2
  117. package/types/JsonType.d.ts +1 -1
  118. package/types/JsonType.js +7 -2
  119. package/types/TinyIntType.js +1 -1
  120. package/types/Type.d.ts +2 -4
  121. package/types/Type.js +3 -3
  122. package/types/Uint8ArrayType.d.ts +0 -1
  123. package/types/Uint8ArrayType.js +1 -4
  124. package/types/UuidType.d.ts +2 -0
  125. package/types/UuidType.js +14 -2
  126. package/types/index.d.ts +3 -2
  127. package/typings.d.ts +427 -170
  128. package/typings.js +100 -45
  129. package/unit-of-work/ChangeSet.d.ts +4 -6
  130. package/unit-of-work/ChangeSet.js +8 -9
  131. package/unit-of-work/ChangeSetComputer.d.ts +2 -12
  132. package/unit-of-work/ChangeSetComputer.js +61 -38
  133. package/unit-of-work/ChangeSetPersister.d.ts +10 -17
  134. package/unit-of-work/ChangeSetPersister.js +136 -73
  135. package/unit-of-work/CommitOrderCalculator.d.ts +13 -14
  136. package/unit-of-work/CommitOrderCalculator.js +22 -20
  137. package/unit-of-work/IdentityMap.d.ts +12 -3
  138. package/unit-of-work/IdentityMap.js +51 -13
  139. package/unit-of-work/UnitOfWork.d.ts +39 -23
  140. package/unit-of-work/UnitOfWork.js +441 -246
  141. package/utils/AbstractMigrator.d.ts +101 -0
  142. package/utils/AbstractMigrator.js +303 -0
  143. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  144. package/utils/AbstractSchemaGenerator.js +30 -18
  145. package/utils/AsyncContext.d.ts +6 -0
  146. package/utils/AsyncContext.js +42 -0
  147. package/utils/Configuration.d.ts +647 -185
  148. package/utils/Configuration.js +215 -252
  149. package/utils/ConfigurationLoader.d.ts +1 -52
  150. package/utils/ConfigurationLoader.js +1 -330
  151. package/utils/Cursor.d.ts +3 -6
  152. package/utils/Cursor.js +32 -17
  153. package/utils/DataloaderUtils.d.ts +10 -5
  154. package/utils/DataloaderUtils.js +42 -22
  155. package/utils/EntityComparator.d.ts +21 -21
  156. package/utils/EntityComparator.js +224 -118
  157. package/utils/QueryHelper.d.ts +34 -7
  158. package/utils/QueryHelper.js +183 -72
  159. package/utils/RawQueryFragment.d.ts +28 -34
  160. package/utils/RawQueryFragment.js +37 -72
  161. package/utils/RequestContext.js +2 -2
  162. package/utils/TransactionContext.js +2 -2
  163. package/utils/TransactionManager.js +11 -8
  164. package/utils/Utils.d.ts +16 -127
  165. package/utils/Utils.js +104 -402
  166. package/utils/clone.js +13 -23
  167. package/utils/env-vars.d.ts +7 -0
  168. package/utils/env-vars.js +98 -0
  169. package/utils/fs-utils.d.ts +20 -0
  170. package/utils/fs-utils.js +193 -0
  171. package/utils/index.d.ts +1 -3
  172. package/utils/index.js +1 -3
  173. package/utils/upsert-utils.d.ts +9 -4
  174. package/utils/upsert-utils.js +51 -5
  175. package/decorators/Check.d.ts +0 -3
  176. package/decorators/Check.js +0 -13
  177. package/decorators/CreateRequestContext.d.ts +0 -3
  178. package/decorators/CreateRequestContext.js +0 -32
  179. package/decorators/Embeddable.d.ts +0 -8
  180. package/decorators/Embeddable.js +0 -11
  181. package/decorators/Embedded.d.ts +0 -12
  182. package/decorators/Embedded.js +0 -18
  183. package/decorators/Entity.d.ts +0 -33
  184. package/decorators/Entity.js +0 -12
  185. package/decorators/Enum.d.ts +0 -9
  186. package/decorators/Enum.js +0 -16
  187. package/decorators/Filter.d.ts +0 -2
  188. package/decorators/Filter.js +0 -8
  189. package/decorators/Formula.d.ts +0 -4
  190. package/decorators/Formula.js +0 -15
  191. package/decorators/Indexed.d.ts +0 -19
  192. package/decorators/Indexed.js +0 -20
  193. package/decorators/ManyToMany.d.ts +0 -42
  194. package/decorators/ManyToMany.js +0 -14
  195. package/decorators/ManyToOne.d.ts +0 -34
  196. package/decorators/ManyToOne.js +0 -14
  197. package/decorators/OneToMany.d.ts +0 -28
  198. package/decorators/OneToMany.js +0 -17
  199. package/decorators/OneToOne.d.ts +0 -28
  200. package/decorators/OneToOne.js +0 -7
  201. package/decorators/PrimaryKey.d.ts +0 -8
  202. package/decorators/PrimaryKey.js +0 -20
  203. package/decorators/Property.d.ts +0 -250
  204. package/decorators/Property.js +0 -32
  205. package/decorators/Transactional.d.ts +0 -14
  206. package/decorators/Transactional.js +0 -28
  207. package/decorators/hooks.d.ts +0 -16
  208. package/decorators/hooks.js +0 -47
  209. package/decorators/index.d.ts +0 -17
  210. package/decorators/index.js +0 -17
  211. package/entity/ArrayCollection.d.ts +0 -118
  212. package/entity/ArrayCollection.js +0 -407
  213. package/entity/EntityValidator.d.ts +0 -19
  214. package/entity/EntityValidator.js +0 -150
  215. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  216. package/metadata/ReflectMetadataProvider.js +0 -44
  217. package/utils/resolveContextProvider.d.ts +0 -10
  218. package/utils/resolveContextProvider.js +0 -28
@@ -1,40 +1,67 @@
1
1
  export class IdentityMap {
2
- defaultSchema;
2
+ #defaultSchema;
3
+ #registry = new Map();
4
+ /** Tracks alternate key hashes for each entity so we can clean them up on delete */
5
+ #alternateKeys = new WeakMap();
3
6
  constructor(defaultSchema) {
4
- this.defaultSchema = defaultSchema;
7
+ this.#defaultSchema = defaultSchema;
5
8
  }
6
- registry = new Map();
7
9
  store(item) {
8
10
  this.getStore(item.__meta.root).set(this.getPkHash(item), item);
9
11
  }
12
+ /**
13
+ * Stores an entity under an alternate key (non-PK property).
14
+ * This allows looking up entities by unique properties that are not the primary key.
15
+ */
16
+ storeByKey(item, key, value, schema) {
17
+ const hash = this.getKeyHash(key, value, schema);
18
+ this.getStore(item.__meta.root).set(hash, item);
19
+ // Track this alternate key so we can clean it up when the entity is deleted
20
+ let keys = this.#alternateKeys.get(item);
21
+ if (!keys) {
22
+ keys = new Set();
23
+ this.#alternateKeys.set(item, keys);
24
+ }
25
+ keys.add(hash);
26
+ }
10
27
  delete(item) {
11
- this.getStore(item.__meta.root).delete(this.getPkHash(item));
28
+ const meta = item.__meta.root;
29
+ const store = this.getStore(meta);
30
+ store.delete(this.getPkHash(item));
31
+ // Also delete any alternate key entries for this entity
32
+ const altKeys = this.#alternateKeys.get(item);
33
+ if (altKeys) {
34
+ for (const hash of altKeys) {
35
+ store.delete(hash);
36
+ }
37
+ this.#alternateKeys.delete(item);
38
+ }
12
39
  }
13
40
  getByHash(meta, hash) {
14
41
  const store = this.getStore(meta);
15
42
  return store.has(hash) ? store.get(hash) : undefined;
16
43
  }
17
44
  getStore(meta) {
18
- const store = this.registry.get(meta.class);
45
+ const store = this.#registry.get(meta.class);
19
46
  if (store) {
20
47
  return store;
21
48
  }
22
49
  const newStore = new Map();
23
- this.registry.set(meta.class, newStore);
50
+ this.#registry.set(meta.class, newStore);
24
51
  return newStore;
25
52
  }
26
53
  clear() {
27
- this.registry.clear();
54
+ this.#registry.clear();
28
55
  }
29
56
  values() {
30
57
  const ret = [];
31
- for (const store of this.registry.values()) {
58
+ for (const store of this.#registry.values()) {
32
59
  ret.push(...store.values());
33
60
  }
34
61
  return ret;
35
62
  }
36
63
  *[Symbol.iterator]() {
37
- for (const store of this.registry.values()) {
64
+ for (const store of this.#registry.values()) {
38
65
  for (const item of store.values()) {
39
66
  yield item;
40
67
  }
@@ -42,7 +69,7 @@ export class IdentityMap {
42
69
  }
43
70
  keys() {
44
71
  const ret = [];
45
- for (const [cls, store] of this.registry) {
72
+ for (const [cls, store] of this.#registry) {
46
73
  ret.push(...[...store.keys()].map(hash => `${cls.name}-${hash}`));
47
74
  }
48
75
  return ret;
@@ -52,18 +79,29 @@ export class IdentityMap {
52
79
  */
53
80
  get(hash) {
54
81
  const [name, id] = hash.split('-', 2);
55
- const cls = [...this.registry.keys()].find(k => k.name === name);
82
+ const cls = [...this.#registry.keys()].find(k => k.name === name);
56
83
  if (!cls) {
57
84
  return undefined;
58
85
  }
59
- const store = this.registry.get(cls);
86
+ const store = this.#registry.get(cls);
60
87
  return store.has(id) ? store.get(id) : undefined;
61
88
  }
62
89
  getPkHash(item) {
63
90
  const wrapped = item.__helper;
64
91
  const meta = wrapped.__meta;
65
92
  const hash = wrapped.getSerializedPrimaryKey();
66
- const schema = wrapped.__schema ?? meta.root.schema ?? this.defaultSchema;
93
+ const schema = wrapped.__schema ?? meta.root.schema ?? this.#defaultSchema;
94
+ if (schema) {
95
+ return schema + ':' + hash;
96
+ }
97
+ return hash;
98
+ }
99
+ /**
100
+ * Creates a hash for an alternate key lookup.
101
+ * Format: `[key]value` or `schema:[key]value`
102
+ */
103
+ getKeyHash(key, value, schema) {
104
+ const hash = `[${key}]${value}`;
67
105
  if (schema) {
68
106
  return schema + ':' + hash;
69
107
  }
@@ -1,4 +1,4 @@
1
- import type { AnyEntity, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary } from '../typings.js';
1
+ import type { AnyEntity, EntityData, EntityMetadata, EntityName, EntityProperty, FilterQuery, Primary } from '../typings.js';
2
2
  import { Collection } from '../entity/Collection.js';
3
3
  import { Reference } from '../entity/Reference.js';
4
4
  import { ChangeSet, ChangeSetType } from './ChangeSet.js';
@@ -7,27 +7,15 @@ import type { EntityManager } from '../EntityManager.js';
7
7
  import { IdentityMap } from './IdentityMap.js';
8
8
  import type { LockOptions } from '../drivers/IDatabaseDriver.js';
9
9
  export declare class UnitOfWork {
10
- private readonly em;
11
- /** map of references to managed entities */
12
- private readonly identityMap;
13
- private readonly persistStack;
14
- private readonly removeStack;
15
- private readonly orphanRemoveStack;
16
- private readonly changeSets;
17
- private readonly collectionUpdates;
18
- private readonly extraUpdates;
19
- private readonly metadata;
20
- private readonly platform;
21
- private readonly eventManager;
22
- private readonly comparator;
23
- private readonly changeSetComputer;
24
- private readonly changeSetPersister;
25
- private readonly queuedActions;
26
- private readonly loadedEntities;
27
- private readonly flushQueue;
28
- private working;
10
+ #private;
29
11
  constructor(em: EntityManager);
30
12
  merge<T extends object>(entity: T, visited?: Set<AnyEntity>): void;
13
+ /**
14
+ * Entity data can wary in its shape, e.g. we might get a deep relation graph with joined strategy, but for diffing,
15
+ * we need to normalize the shape, so relation values are only raw FKs. This method handles that.
16
+ * @internal
17
+ */
18
+ normalizeEntityData<T extends object>(meta: EntityMetadata<T>, data: EntityData<T>): void;
31
19
  /**
32
20
  * @internal
33
21
  */
@@ -36,11 +24,28 @@ export declare class UnitOfWork {
36
24
  * @internal
37
25
  */
38
26
  dispatchOnLoadEvent(): Promise<void>;
27
+ /**
28
+ * @internal
29
+ */
30
+ unmarkAsLoaded(entity: AnyEntity): void;
39
31
  /**
40
32
  * 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`.
41
33
  */
42
- getById<T extends object>(entityName: string, id: Primary<T> | Primary<T>[], schema?: string, convertCustomTypes?: boolean): T | undefined;
43
- tryGetById<T extends object>(entityName: string, where: FilterQuery<T>, schema?: string, strict?: boolean): T | null;
34
+ getById<T extends object>(entityName: EntityName<T>, id: Primary<T> | Primary<T>[], schema?: string, convertCustomTypes?: boolean): T | undefined;
35
+ /**
36
+ * Returns entity from the identity map by an alternate key (non-PK property).
37
+ * @param convertCustomTypes - If true, the value is in database format and will be converted to JS format for lookup.
38
+ * If false (default), the value is assumed to be in JS format already.
39
+ */
40
+ getByKey<T extends object>(entityName: EntityName<T>, key: string, value: unknown, schema?: string, convertCustomTypes?: boolean): T | undefined;
41
+ /**
42
+ * Stores an entity in the identity map under an alternate key (non-PK property).
43
+ * Also sets the property value on the entity.
44
+ * @param convertCustomTypes - If true, the value is in database format and will be converted to JS format.
45
+ * If false (default), the value is assumed to be in JS format already.
46
+ */
47
+ storeByKey<T extends object>(entity: T, key: string, value: unknown, schema?: string, convertCustomTypes?: boolean): void;
48
+ tryGetById<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, schema?: string, strict?: boolean): T | null;
44
49
  /**
45
50
  * Returns map of all managed entities.
46
51
  */
@@ -53,7 +58,13 @@ export declare class UnitOfWork {
53
58
  getRemoveStack(): Set<AnyEntity>;
54
59
  getChangeSets(): ChangeSet<AnyEntity>[];
55
60
  getCollectionUpdates(): Collection<AnyEntity>[];
56
- getExtraUpdates(): Set<[AnyEntity, string | string[], (AnyEntity | AnyEntity[] | Reference<any> | Collection<any>), ChangeSet<any> | undefined, ChangeSetType]>;
61
+ getExtraUpdates(): Set<[
62
+ AnyEntity,
63
+ string | string[],
64
+ AnyEntity | AnyEntity[] | Reference<any> | Collection<any>,
65
+ ChangeSet<any> | undefined,
66
+ ChangeSetType
67
+ ]>;
57
68
  shouldAutoFlush<T extends object>(meta: EntityMetadata<T>): boolean;
58
69
  clearActionsQueue(): void;
59
70
  computeChangeSet<T extends object>(entity: T, type?: ChangeSetType): void;
@@ -77,6 +88,11 @@ export declare class UnitOfWork {
77
88
  getOrphanRemoveStack(): Set<AnyEntity>;
78
89
  getChangeSetPersister(): ChangeSetPersister;
79
90
  private findNewEntities;
91
+ /**
92
+ * For TPT inheritance, creates separate changesets for each table in the hierarchy.
93
+ * Uses the same entity instance for all changesets - only the metadata and payload differ.
94
+ */
95
+ private createTPTChangeSets;
80
96
  /**
81
97
  * Returns `true` when the change set should be skipped as it will be empty after the extra update.
82
98
  */