@mikro-orm/core 7.0.0-dev.9 → 7.0.0-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.
Files changed (191) hide show
  1. package/EntityManager.d.ts +77 -48
  2. package/EntityManager.js +288 -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 +25 -4
  17. package/entity/BaseEntity.d.ts +0 -1
  18. package/entity/BaseEntity.js +0 -3
  19. package/entity/Collection.d.ts +95 -30
  20. package/entity/Collection.js +432 -93
  21. package/entity/EntityAssigner.d.ts +1 -1
  22. package/entity/EntityAssigner.js +17 -9
  23. package/entity/EntityFactory.d.ts +7 -0
  24. package/entity/EntityFactory.js +63 -41
  25. package/entity/EntityHelper.js +26 -12
  26. package/entity/EntityLoader.d.ts +5 -4
  27. package/entity/EntityLoader.js +63 -38
  28. package/entity/EntityRepository.d.ts +1 -1
  29. package/entity/Reference.d.ts +6 -5
  30. package/entity/Reference.js +34 -9
  31. package/entity/WrappedEntity.d.ts +2 -7
  32. package/entity/WrappedEntity.js +2 -7
  33. package/entity/defineEntity.d.ts +568 -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 +10 -2
  44. package/errors.js +29 -10
  45. package/events/EventManager.d.ts +2 -1
  46. package/events/EventManager.js +19 -11
  47. package/events/EventSubscriber.d.ts +3 -1
  48. package/hydration/Hydrator.js +1 -2
  49. package/hydration/ObjectHydrator.d.ts +4 -4
  50. package/hydration/ObjectHydrator.js +35 -25
  51. package/index.d.ts +2 -2
  52. package/index.js +1 -2
  53. package/logging/DefaultLogger.d.ts +1 -1
  54. package/logging/DefaultLogger.js +1 -0
  55. package/logging/SimpleLogger.d.ts +1 -1
  56. package/logging/index.d.ts +1 -0
  57. package/logging/index.js +1 -0
  58. package/logging/inspect.d.ts +2 -0
  59. package/logging/inspect.js +16 -0
  60. package/metadata/EntitySchema.d.ts +9 -13
  61. package/metadata/EntitySchema.js +44 -26
  62. package/metadata/MetadataDiscovery.d.ts +6 -9
  63. package/metadata/MetadataDiscovery.js +165 -205
  64. package/metadata/MetadataProvider.d.ts +11 -2
  65. package/metadata/MetadataProvider.js +44 -2
  66. package/metadata/MetadataStorage.d.ts +1 -6
  67. package/metadata/MetadataStorage.js +6 -18
  68. package/metadata/MetadataValidator.d.ts +0 -7
  69. package/metadata/MetadataValidator.js +0 -10
  70. package/metadata/discover-entities.d.ts +5 -0
  71. package/metadata/discover-entities.js +40 -0
  72. package/metadata/index.d.ts +1 -1
  73. package/metadata/index.js +1 -1
  74. package/metadata/types.d.ts +480 -0
  75. package/metadata/types.js +1 -0
  76. package/naming-strategy/AbstractNamingStrategy.d.ts +5 -1
  77. package/naming-strategy/AbstractNamingStrategy.js +8 -2
  78. package/naming-strategy/NamingStrategy.d.ts +11 -1
  79. package/not-supported.d.ts +2 -0
  80. package/not-supported.js +4 -0
  81. package/package.json +18 -10
  82. package/platforms/ExceptionConverter.js +1 -1
  83. package/platforms/Platform.d.ts +6 -13
  84. package/platforms/Platform.js +15 -41
  85. package/serialization/EntitySerializer.d.ts +2 -0
  86. package/serialization/EntitySerializer.js +32 -14
  87. package/serialization/EntityTransformer.js +22 -12
  88. package/serialization/SerializationContext.js +16 -13
  89. package/types/ArrayType.d.ts +1 -1
  90. package/types/ArrayType.js +2 -3
  91. package/types/BigIntType.d.ts +8 -6
  92. package/types/BigIntType.js +1 -1
  93. package/types/BlobType.d.ts +0 -1
  94. package/types/BlobType.js +0 -3
  95. package/types/BooleanType.d.ts +2 -1
  96. package/types/BooleanType.js +3 -0
  97. package/types/DecimalType.d.ts +6 -4
  98. package/types/DecimalType.js +3 -3
  99. package/types/DoubleType.js +2 -2
  100. package/types/EnumArrayType.js +1 -2
  101. package/types/JsonType.d.ts +1 -1
  102. package/types/JsonType.js +7 -2
  103. package/types/TinyIntType.js +1 -1
  104. package/types/Type.d.ts +2 -4
  105. package/types/Type.js +3 -3
  106. package/types/Uint8ArrayType.d.ts +0 -1
  107. package/types/Uint8ArrayType.js +1 -4
  108. package/types/index.d.ts +1 -1
  109. package/typings.d.ts +109 -73
  110. package/typings.js +38 -35
  111. package/unit-of-work/ChangeSet.d.ts +0 -3
  112. package/unit-of-work/ChangeSet.js +2 -2
  113. package/unit-of-work/ChangeSetComputer.d.ts +1 -3
  114. package/unit-of-work/ChangeSetComputer.js +11 -9
  115. package/unit-of-work/ChangeSetPersister.d.ts +5 -4
  116. package/unit-of-work/ChangeSetPersister.js +51 -19
  117. package/unit-of-work/UnitOfWork.d.ts +8 -1
  118. package/unit-of-work/UnitOfWork.js +91 -49
  119. package/utils/AbstractSchemaGenerator.d.ts +5 -5
  120. package/utils/AbstractSchemaGenerator.js +11 -9
  121. package/utils/Configuration.d.ts +757 -206
  122. package/utils/Configuration.js +140 -188
  123. package/utils/ConfigurationLoader.d.ts +1 -54
  124. package/utils/ConfigurationLoader.js +1 -352
  125. package/utils/Cursor.d.ts +0 -3
  126. package/utils/Cursor.js +6 -3
  127. package/utils/DataloaderUtils.d.ts +15 -5
  128. package/utils/DataloaderUtils.js +54 -8
  129. package/utils/EntityComparator.d.ts +8 -4
  130. package/utils/EntityComparator.js +52 -17
  131. package/utils/QueryHelper.d.ts +9 -1
  132. package/utils/QueryHelper.js +70 -9
  133. package/utils/RawQueryFragment.d.ts +36 -13
  134. package/utils/RawQueryFragment.js +36 -16
  135. package/utils/TransactionManager.d.ts +65 -0
  136. package/utils/TransactionManager.js +223 -0
  137. package/utils/Utils.d.ts +8 -97
  138. package/utils/Utils.js +82 -302
  139. package/utils/clone.js +2 -3
  140. package/utils/env-vars.d.ts +3 -0
  141. package/utils/env-vars.js +87 -0
  142. package/utils/fs-utils.d.ts +12 -0
  143. package/utils/fs-utils.js +97 -0
  144. package/utils/index.d.ts +2 -1
  145. package/utils/index.js +2 -1
  146. package/utils/upsert-utils.d.ts +7 -2
  147. package/utils/upsert-utils.js +55 -4
  148. package/decorators/Check.d.ts +0 -3
  149. package/decorators/Check.js +0 -13
  150. package/decorators/CreateRequestContext.d.ts +0 -3
  151. package/decorators/CreateRequestContext.js +0 -32
  152. package/decorators/Embeddable.d.ts +0 -8
  153. package/decorators/Embeddable.js +0 -11
  154. package/decorators/Embedded.d.ts +0 -18
  155. package/decorators/Embedded.js +0 -18
  156. package/decorators/Entity.d.ts +0 -18
  157. package/decorators/Entity.js +0 -12
  158. package/decorators/Enum.d.ts +0 -9
  159. package/decorators/Enum.js +0 -16
  160. package/decorators/Filter.d.ts +0 -2
  161. package/decorators/Filter.js +0 -8
  162. package/decorators/Formula.d.ts +0 -4
  163. package/decorators/Formula.js +0 -15
  164. package/decorators/Indexed.d.ts +0 -19
  165. package/decorators/Indexed.js +0 -20
  166. package/decorators/ManyToMany.d.ts +0 -40
  167. package/decorators/ManyToMany.js +0 -14
  168. package/decorators/ManyToOne.d.ts +0 -30
  169. package/decorators/ManyToOne.js +0 -14
  170. package/decorators/OneToMany.d.ts +0 -28
  171. package/decorators/OneToMany.js +0 -17
  172. package/decorators/OneToOne.d.ts +0 -24
  173. package/decorators/OneToOne.js +0 -7
  174. package/decorators/PrimaryKey.d.ts +0 -8
  175. package/decorators/PrimaryKey.js +0 -20
  176. package/decorators/Property.d.ts +0 -250
  177. package/decorators/Property.js +0 -32
  178. package/decorators/Transactional.d.ts +0 -13
  179. package/decorators/Transactional.js +0 -28
  180. package/decorators/hooks.d.ts +0 -16
  181. package/decorators/hooks.js +0 -47
  182. package/decorators/index.d.ts +0 -17
  183. package/decorators/index.js +0 -17
  184. package/entity/ArrayCollection.d.ts +0 -116
  185. package/entity/ArrayCollection.js +0 -402
  186. package/entity/EntityValidator.d.ts +0 -19
  187. package/entity/EntityValidator.js +0 -150
  188. package/metadata/ReflectMetadataProvider.d.ts +0 -8
  189. package/metadata/ReflectMetadataProvider.js +0 -44
  190. package/utils/resolveContextProvider.d.ts +0 -10
  191. 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
+ /* v8 ignore next */
10
+ if (typeof prop.entity === 'string') {
11
+ prop.type = prop.entity;
12
+ }
13
+ else if (prop.entity) {
14
+ const tmp = prop.entity();
15
+ prop.type = Array.isArray(tmp) ? tmp.map(t => Utils.className(t)).sort().join(' | ') : Utils.className(tmp);
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) {
@@ -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';