@mikro-orm/core 7.0.0-dev.8 → 7.0.0-dev.81

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 (181) hide show
  1. package/EntityManager.d.ts +85 -48
  2. package/EntityManager.js +300 -225
  3. package/MikroORM.d.ts +40 -31
  4. package/MikroORM.js +98 -137
  5. package/README.md +3 -2
  6. package/cache/FileCacheAdapter.d.ts +1 -1
  7. package/cache/FileCacheAdapter.js +6 -5
  8. package/cache/GeneratedCacheAdapter.d.ts +0 -1
  9. package/cache/GeneratedCacheAdapter.js +0 -2
  10. package/cache/index.d.ts +0 -1
  11. package/cache/index.js +0 -1
  12. package/connections/Connection.d.ts +11 -7
  13. package/connections/Connection.js +16 -14
  14. package/drivers/DatabaseDriver.d.ts +11 -5
  15. package/drivers/DatabaseDriver.js +23 -11
  16. package/drivers/IDatabaseDriver.d.ts +27 -5
  17. package/entity/BaseEntity.d.ts +0 -1
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +98 -30
  20. package/entity/Collection.js +432 -93
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +15 -7
  23. package/entity/EntityFactory.d.ts +7 -0
  24. package/entity/EntityFactory.js +64 -41
  25. package/entity/EntityHelper.js +26 -9
  26. package/entity/EntityLoader.d.ts +5 -4
  27. package/entity/EntityLoader.js +73 -40
  28. package/entity/EntityRepository.d.ts +1 -1
  29. package/entity/Reference.d.ts +9 -7
  30. package/entity/Reference.js +33 -6
  31. package/entity/WrappedEntity.d.ts +2 -4
  32. package/entity/WrappedEntity.js +1 -5
  33. package/entity/defineEntity.d.ts +549 -0
  34. package/entity/defineEntity.js +529 -0
  35. package/entity/index.d.ts +3 -2
  36. package/entity/index.js +3 -2
  37. package/entity/utils.d.ts +7 -0
  38. package/entity/utils.js +16 -4
  39. package/entity/validators.d.ts +11 -0
  40. package/entity/validators.js +65 -0
  41. package/enums.d.ts +21 -6
  42. package/enums.js +14 -1
  43. package/errors.d.ts +6 -2
  44. package/errors.js +14 -9
  45. package/events/EventSubscriber.d.ts +3 -1
  46. package/hydration/Hydrator.js +1 -2
  47. package/hydration/ObjectHydrator.d.ts +4 -4
  48. package/hydration/ObjectHydrator.js +36 -25
  49. package/index.d.ts +2 -2
  50. package/index.js +1 -2
  51. package/logging/DefaultLogger.d.ts +1 -1
  52. package/logging/SimpleLogger.d.ts +1 -1
  53. package/metadata/EntitySchema.d.ts +9 -13
  54. package/metadata/EntitySchema.js +44 -26
  55. package/metadata/MetadataDiscovery.d.ts +6 -9
  56. package/metadata/MetadataDiscovery.js +167 -206
  57. package/metadata/MetadataProvider.d.ts +11 -2
  58. package/metadata/MetadataProvider.js +44 -2
  59. package/metadata/MetadataStorage.d.ts +1 -6
  60. package/metadata/MetadataStorage.js +6 -18
  61. package/metadata/MetadataValidator.d.ts +0 -7
  62. package/metadata/MetadataValidator.js +4 -13
  63. package/metadata/discover-entities.d.ts +5 -0
  64. package/metadata/discover-entities.js +40 -0
  65. package/metadata/index.d.ts +1 -1
  66. package/metadata/index.js +1 -1
  67. package/metadata/types.d.ts +480 -0
  68. package/metadata/types.js +1 -0
  69. package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
  70. package/naming-strategy/AbstractNamingStrategy.js +8 -2
  71. package/naming-strategy/NamingStrategy.d.ts +11 -1
  72. package/not-supported.d.ts +2 -0
  73. package/not-supported.js +4 -0
  74. package/package.json +18 -10
  75. package/platforms/ExceptionConverter.js +1 -1
  76. package/platforms/Platform.d.ts +6 -10
  77. package/platforms/Platform.js +14 -39
  78. package/serialization/EntitySerializer.d.ts +2 -0
  79. package/serialization/EntitySerializer.js +32 -14
  80. package/serialization/EntityTransformer.js +22 -12
  81. package/serialization/SerializationContext.js +16 -13
  82. package/types/ArrayType.d.ts +1 -1
  83. package/types/ArrayType.js +2 -3
  84. package/types/BigIntType.d.ts +8 -6
  85. package/types/BigIntType.js +1 -1
  86. package/types/BlobType.d.ts +0 -1
  87. package/types/BlobType.js +0 -3
  88. package/types/BooleanType.d.ts +2 -1
  89. package/types/BooleanType.js +3 -0
  90. package/types/DecimalType.d.ts +6 -4
  91. package/types/DecimalType.js +3 -3
  92. package/types/DoubleType.js +2 -2
  93. package/types/JsonType.d.ts +1 -1
  94. package/types/JsonType.js +7 -2
  95. package/types/TinyIntType.js +1 -1
  96. package/types/Type.d.ts +2 -1
  97. package/types/Type.js +1 -1
  98. package/types/Uint8ArrayType.d.ts +0 -1
  99. package/types/Uint8ArrayType.js +1 -4
  100. package/types/index.d.ts +1 -1
  101. package/typings.d.ts +113 -77
  102. package/typings.js +41 -35
  103. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  104. package/unit-of-work/ChangeSetComputer.js +11 -9
  105. package/unit-of-work/ChangeSetPersister.d.ts +5 -4
  106. package/unit-of-work/ChangeSetPersister.js +58 -20
  107. package/unit-of-work/UnitOfWork.d.ts +8 -1
  108. package/unit-of-work/UnitOfWork.js +115 -57
  109. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  110. package/utils/AbstractSchemaGenerator.js +11 -9
  111. package/utils/Configuration.d.ts +757 -206
  112. package/utils/Configuration.js +139 -187
  113. package/utils/ConfigurationLoader.d.ts +1 -54
  114. package/utils/ConfigurationLoader.js +1 -352
  115. package/utils/Cursor.d.ts +3 -3
  116. package/utils/Cursor.js +4 -1
  117. package/utils/DataloaderUtils.d.ts +15 -5
  118. package/utils/DataloaderUtils.js +54 -8
  119. package/utils/EntityComparator.d.ts +8 -4
  120. package/utils/EntityComparator.js +111 -64
  121. package/utils/QueryHelper.d.ts +9 -1
  122. package/utils/QueryHelper.js +70 -9
  123. package/utils/RawQueryFragment.d.ts +36 -4
  124. package/utils/RawQueryFragment.js +35 -14
  125. package/utils/TransactionManager.d.ts +65 -0
  126. package/utils/TransactionManager.js +223 -0
  127. package/utils/Utils.d.ts +8 -97
  128. package/utils/Utils.js +88 -303
  129. package/utils/clone.js +2 -3
  130. package/utils/env-vars.d.ts +3 -0
  131. package/utils/env-vars.js +87 -0
  132. package/utils/fs-utils.d.ts +12 -0
  133. package/utils/fs-utils.js +96 -0
  134. package/utils/index.d.ts +2 -1
  135. package/utils/index.js +2 -1
  136. package/utils/upsert-utils.d.ts +7 -2
  137. package/utils/upsert-utils.js +55 -4
  138. package/decorators/Check.d.ts +0 -3
  139. package/decorators/Check.js +0 -13
  140. package/decorators/CreateRequestContext.d.ts +0 -3
  141. package/decorators/CreateRequestContext.js +0 -32
  142. package/decorators/Embeddable.d.ts +0 -8
  143. package/decorators/Embeddable.js +0 -11
  144. package/decorators/Embedded.d.ts +0 -18
  145. package/decorators/Embedded.js +0 -18
  146. package/decorators/Entity.d.ts +0 -18
  147. package/decorators/Entity.js +0 -12
  148. package/decorators/Enum.d.ts +0 -9
  149. package/decorators/Enum.js +0 -16
  150. package/decorators/Filter.d.ts +0 -2
  151. package/decorators/Filter.js +0 -8
  152. package/decorators/Formula.d.ts +0 -4
  153. package/decorators/Formula.js +0 -15
  154. package/decorators/Indexed.d.ts +0 -19
  155. package/decorators/Indexed.js +0 -20
  156. package/decorators/ManyToMany.d.ts +0 -40
  157. package/decorators/ManyToMany.js +0 -14
  158. package/decorators/ManyToOne.d.ts +0 -30
  159. package/decorators/ManyToOne.js +0 -14
  160. package/decorators/OneToMany.d.ts +0 -28
  161. package/decorators/OneToMany.js +0 -17
  162. package/decorators/OneToOne.d.ts +0 -24
  163. package/decorators/OneToOne.js +0 -7
  164. package/decorators/PrimaryKey.d.ts +0 -8
  165. package/decorators/PrimaryKey.js +0 -20
  166. package/decorators/Property.d.ts +0 -250
  167. package/decorators/Property.js +0 -32
  168. package/decorators/Transactional.d.ts +0 -13
  169. package/decorators/Transactional.js +0 -28
  170. package/decorators/hooks.d.ts +0 -16
  171. package/decorators/hooks.js +0 -47
  172. package/decorators/index.d.ts +0 -17
  173. package/decorators/index.js +0 -17
  174. package/entity/ArrayCollection.d.ts +0 -116
  175. package/entity/ArrayCollection.js +0 -402
  176. package/entity/EntityValidator.d.ts +0 -19
  177. package/entity/EntityValidator.js +0 -150
  178. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  179. package/metadata/ReflectMetadataProvider.js +0 -44
  180. package/utils/resolveContextProvider.d.ts +0 -10
  181. package/utils/resolveContextProvider.js +0 -28
@@ -1,13 +1,22 @@
1
1
  import type { EntityMetadata } from '../typings.js';
2
2
  import type { Logger } from '../logging/Logger.js';
3
+ import type { SyncCacheAdapter } from '../cache/CacheAdapter.js';
4
+ import type { Platform } from '../platforms/Platform.js';
3
5
  export interface IConfiguration {
4
6
  get(key: string, defaultValue?: any): any;
5
7
  getLogger(): Logger;
8
+ getMetadataCacheAdapter(): SyncCacheAdapter;
9
+ getPlatform(): Platform;
6
10
  }
7
- export declare abstract class MetadataProvider {
11
+ export declare class MetadataProvider {
8
12
  protected readonly config: IConfiguration;
9
13
  constructor(config: IConfiguration);
10
- abstract loadEntityMetadata(meta: EntityMetadata, name: string): void;
14
+ loadEntityMetadata(meta: EntityMetadata): void;
11
15
  loadFromCache(meta: EntityMetadata, cache: EntityMetadata): void;
16
+ static useCache(): boolean;
12
17
  useCache(): boolean;
18
+ saveToCache(meta: EntityMetadata): void;
19
+ getCachedMetadata<T>(meta: Pick<EntityMetadata<T>, 'className' | 'path' | 'root'>, root: EntityMetadata<T>): EntityMetadata<T> | undefined;
20
+ combineCache(): void;
21
+ getCacheKey(meta: Pick<EntityMetadata, 'className' | 'path'>): string;
13
22
  }
@@ -4,17 +4,59 @@ export class MetadataProvider {
4
4
  constructor(config) {
5
5
  this.config = config;
6
6
  }
7
+ loadEntityMetadata(meta) {
8
+ for (const prop of meta.props) {
9
+ if (typeof prop.entity === 'string') {
10
+ prop.type = prop.entity;
11
+ }
12
+ else if (prop.entity) {
13
+ const tmp = prop.entity();
14
+ prop.type = Array.isArray(tmp) ? tmp.map(t => Utils.className(t)).sort().join(' | ') : Utils.className(tmp);
15
+ /* v8 ignore next */
16
+ }
17
+ else if (!prop.type && !(prop.enum && (prop.items?.length ?? 0) > 0)) {
18
+ throw new Error(`Please provide either 'type' or 'entity' attribute in ${meta.className}.${prop.name}.`);
19
+ }
20
+ }
21
+ }
7
22
  loadFromCache(meta, cache) {
8
23
  Object.values(cache.properties).forEach(prop => {
9
24
  const metaProp = meta.properties[prop.name];
10
- /* v8 ignore next 3 */
25
+ /* v8 ignore next */
11
26
  if (metaProp?.enum && Array.isArray(metaProp.items)) {
12
27
  delete prop.items;
13
28
  }
14
29
  });
15
30
  Utils.mergeConfig(meta, cache);
16
31
  }
32
+ static useCache() {
33
+ return false;
34
+ }
17
35
  useCache() {
18
- return this.config.get('metadataCache').enabled ?? false;
36
+ return this.config.get('metadataCache').enabled ?? MetadataProvider.useCache();
37
+ }
38
+ saveToCache(meta) {
39
+ //
40
+ }
41
+ getCachedMetadata(meta, root) {
42
+ if (!this.useCache()) {
43
+ return undefined;
44
+ }
45
+ const cache = meta.path && this.config.getMetadataCacheAdapter().get(this.getCacheKey(meta));
46
+ if (cache) {
47
+ this.loadFromCache(meta, cache);
48
+ meta.root = root;
49
+ }
50
+ return cache;
51
+ }
52
+ combineCache() {
53
+ const path = this.config.getMetadataCacheAdapter().combine?.();
54
+ // override the path in the options, so we can log it from the CLI in `cache:generate` command
55
+ if (path) {
56
+ this.config.get('metadataCache').combined = path;
57
+ }
58
+ }
59
+ getCacheKey(meta) {
60
+ return meta.className;
19
61
  }
20
62
  }
@@ -1,4 +1,4 @@
1
- import { EntityMetadata, type Dictionary, type EntityData, type EntityName } from '../typings.js';
1
+ import { EntityMetadata, type Dictionary, type EntityName } from '../typings.js';
2
2
  import type { EntityManager } from '../EntityManager.js';
3
3
  export declare class MetadataStorage {
4
4
  static readonly PATH_SYMBOL: unique symbol;
@@ -8,13 +8,8 @@ export declare class MetadataStorage {
8
8
  static getMetadata(): Dictionary<EntityMetadata>;
9
9
  static getMetadata<T = any>(entity: string, path: string): EntityMetadata<T>;
10
10
  static isKnownEntity(name: string): boolean;
11
- static getMetadataFromDecorator<T = any>(target: T & Dictionary & {
12
- [MetadataStorage.PATH_SYMBOL]?: string;
13
- }): EntityMetadata<T>;
14
- static init(): MetadataStorage;
15
11
  static clear(): void;
16
12
  getAll(): Dictionary<EntityMetadata>;
17
- getByDiscriminatorColumn<T>(meta: EntityMetadata<T>, data: EntityData<T>): EntityMetadata<T> | undefined;
18
13
  get<T = any>(entityName: EntityName<T>, init?: boolean, validate?: boolean): EntityMetadata<T>;
19
14
  find<T = any>(entityName: EntityName<T>): EntityMetadata<T> | undefined;
20
15
  has(entity: string): boolean;
@@ -2,9 +2,14 @@ import { EntityMetadata } from '../typings.js';
2
2
  import { Utils } from '../utils/Utils.js';
3
3
  import { MetadataError } from '../errors.js';
4
4
  import { EntityHelper } from '../entity/EntityHelper.js';
5
+ function getGlobalStorage(namespace) {
6
+ const key = `mikro-orm-${namespace}`;
7
+ globalThis[key] = globalThis[key] || {};
8
+ return globalThis[key];
9
+ }
5
10
  export class MetadataStorage {
6
11
  static PATH_SYMBOL = Symbol('MetadataStorage.PATH_SYMBOL');
7
- static metadata = Utils.getGlobalStorage('metadata');
12
+ static metadata = getGlobalStorage('metadata');
8
13
  metadata;
9
14
  constructor(metadata = {}) {
10
15
  this.metadata = Utils.copy(metadata, false);
@@ -22,29 +27,12 @@ export class MetadataStorage {
22
27
  static isKnownEntity(name) {
23
28
  return !!Object.values(this.metadata).find(meta => meta.className === name);
24
29
  }
25
- static getMetadataFromDecorator(target) {
26
- if (!Object.hasOwn(target, MetadataStorage.PATH_SYMBOL)) {
27
- Object.defineProperty(target, MetadataStorage.PATH_SYMBOL, { value: Utils.lookupPathFromDecorator(target.name), writable: true });
28
- }
29
- return MetadataStorage.getMetadata(target.name, target[MetadataStorage.PATH_SYMBOL]);
30
- }
31
- static init() {
32
- return new MetadataStorage(MetadataStorage.metadata);
33
- }
34
30
  static clear() {
35
31
  Object.keys(this.metadata).forEach(k => delete this.metadata[k]);
36
32
  }
37
33
  getAll() {
38
34
  return this.metadata;
39
35
  }
40
- getByDiscriminatorColumn(meta, data) {
41
- const value = data[meta.root.discriminatorColumn];
42
- if (!value) {
43
- return undefined;
44
- }
45
- const type = meta.root.discriminatorMap[value];
46
- return this.metadata[type];
47
- }
48
36
  get(entityName, init = false, validate = true) {
49
37
  entityName = Utils.className(entityName);
50
38
  if (validate && !init && !this.has(entityName)) {
@@ -1,17 +1,10 @@
1
1
  import type { EntityMetadata } from '../typings.js';
2
2
  import { type MetadataDiscoveryOptions } from '../utils/Configuration.js';
3
- import { ReferenceKind } from '../enums.js';
4
3
  import type { MetadataStorage } from './MetadataStorage.js';
5
4
  /**
6
5
  * @internal
7
6
  */
8
7
  export declare class MetadataValidator {
9
- /**
10
- * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
11
- * on the same property. One should use only `@ManyToOne()` in such case.
12
- * We allow the existence of the property in metadata if the reference type is the same, this should allow things like HMR to work.
13
- */
14
- static validateSingleDecorator(meta: EntityMetadata, propertyName: string, reference: ReferenceKind): void;
15
8
  validateEntityDefinition<T>(metadata: MetadataStorage, name: string, options: MetadataDiscoveryOptions): void;
16
9
  validateDiscovered(discovered: EntityMetadata[], options: MetadataDiscoveryOptions): void;
17
10
  private validateReference;
@@ -5,16 +5,6 @@ import { ReferenceKind } from '../enums.js';
5
5
  * @internal
6
6
  */
7
7
  export class MetadataValidator {
8
- /**
9
- * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
10
- * on the same property. One should use only `@ManyToOne()` in such case.
11
- * We allow the existence of the property in metadata if the reference type is the same, this should allow things like HMR to work.
12
- */
13
- static validateSingleDecorator(meta, propertyName, reference) {
14
- if (meta.properties[propertyName] && meta.properties[propertyName].kind !== reference) {
15
- throw MetadataError.multipleDecorators(meta.className, propertyName);
16
- }
17
- }
18
8
  validateEntityDefinition(metadata, name, options) {
19
9
  const meta = metadata.get(name);
20
10
  if (meta.virtual || meta.expression) {
@@ -100,14 +90,15 @@ export class MetadataValidator {
100
90
  if (!prop.type) {
101
91
  throw MetadataError.fromWrongTypeDefinition(meta, prop);
102
92
  }
93
+ const targetMeta = metadata.find(prop.type);
103
94
  // references do have type of known entity
104
- if (!metadata.find(prop.type)) {
95
+ if (!targetMeta) {
105
96
  throw MetadataError.fromWrongTypeDefinition(meta, prop);
106
97
  }
107
- if (metadata.find(prop.type).abstract && !metadata.find(prop.type).discriminatorColumn) {
98
+ if (targetMeta.abstract && !targetMeta.discriminatorColumn && !targetMeta.embeddable) {
108
99
  throw MetadataError.targetIsAbstract(meta, prop);
109
100
  }
110
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && prop.persist === false && metadata.find(prop.type).compositePK && options.checkNonPersistentCompositeProps) {
101
+ if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && prop.persist === false && targetMeta.compositePK && options.checkNonPersistentCompositeProps) {
111
102
  throw MetadataError.nonPersistentCompositeProp(meta, prop);
112
103
  }
113
104
  }
@@ -0,0 +1,5 @@
1
+ import { type Constructor } from '../typings.js';
2
+ import { EntitySchema } from './EntitySchema.js';
3
+ export declare function discoverEntities(paths: string | string[], options?: {
4
+ baseDir?: string;
5
+ }): Promise<Iterable<EntitySchema | Constructor>>;
@@ -0,0 +1,40 @@
1
+ import { basename } from 'node:path';
2
+ import { fs } from '../utils/fs-utils.js';
3
+ import { Utils } from '../utils/Utils.js';
4
+ import { MetadataStorage } from './MetadataStorage.js';
5
+ import { EntitySchema } from './EntitySchema.js';
6
+ async function getEntityClassOrSchema(filepath, allTargets, baseDir) {
7
+ const path = Utils.normalizePath(baseDir, filepath);
8
+ const exports = await Utils.dynamicImport(path);
9
+ const targets = Object.values(exports);
10
+ // ignore class implementations that are linked from an EntitySchema
11
+ for (const item of targets) {
12
+ if (item instanceof EntitySchema) {
13
+ for (const item2 of targets) {
14
+ if (item.meta.class === item2) {
15
+ targets.splice(targets.indexOf(item2), 1);
16
+ }
17
+ }
18
+ }
19
+ }
20
+ for (const item of targets) {
21
+ const validTarget = item instanceof EntitySchema || (item instanceof Function && MetadataStorage.isKnownEntity(item.name));
22
+ if (validTarget && !allTargets.has(item)) {
23
+ allTargets.set(item, path);
24
+ }
25
+ }
26
+ }
27
+ export async function discoverEntities(paths, options) {
28
+ paths = Utils.asArray(paths).map(path => Utils.normalizePath(path));
29
+ const baseDir = options?.baseDir ?? process.cwd();
30
+ const files = fs.glob(paths, Utils.normalizePath(baseDir));
31
+ const found = new Map();
32
+ for (const filepath of files) {
33
+ const filename = basename(filepath);
34
+ if (!filename.match(/\.[cm]?[jt]s$/) || filename.match(/\.d\.[cm]?ts/)) {
35
+ continue;
36
+ }
37
+ await getEntityClassOrSchema(filepath, found, baseDir);
38
+ }
39
+ return found.keys();
40
+ }
@@ -1,6 +1,6 @@
1
+ export * from './types.js';
1
2
  export * from './EntitySchema.js';
2
3
  export * from './MetadataDiscovery.js';
3
4
  export * from './MetadataStorage.js';
4
5
  export * from './MetadataProvider.js';
5
6
  export * from './MetadataValidator.js';
6
- export * from './ReflectMetadataProvider.js';
package/metadata/index.js CHANGED
@@ -1,6 +1,6 @@
1
+ export * from './types.js';
1
2
  export * from './EntitySchema.js';
2
3
  export * from './MetadataDiscovery.js';
3
4
  export * from './MetadataStorage.js';
4
5
  export * from './MetadataProvider.js';
5
6
  export * from './MetadataValidator.js';
6
- export * from './ReflectMetadataProvider.js';