@mikro-orm/core 7.0.0-dev.99 → 7.0.0-rc.1

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 (107) hide show
  1. package/EntityManager.d.ts +34 -17
  2. package/EntityManager.js +95 -103
  3. package/MikroORM.d.ts +5 -5
  4. package/MikroORM.js +25 -20
  5. package/cache/FileCacheAdapter.js +11 -3
  6. package/connections/Connection.d.ts +3 -2
  7. package/connections/Connection.js +4 -3
  8. package/drivers/DatabaseDriver.d.ts +11 -11
  9. package/drivers/DatabaseDriver.js +91 -25
  10. package/drivers/IDatabaseDriver.d.ts +50 -20
  11. package/entity/BaseEntity.d.ts +61 -1
  12. package/entity/Collection.d.ts +8 -1
  13. package/entity/Collection.js +12 -13
  14. package/entity/EntityAssigner.js +9 -9
  15. package/entity/EntityFactory.d.ts +6 -1
  16. package/entity/EntityFactory.js +40 -22
  17. package/entity/EntityHelper.d.ts +2 -2
  18. package/entity/EntityHelper.js +27 -4
  19. package/entity/EntityLoader.d.ts +5 -4
  20. package/entity/EntityLoader.js +193 -80
  21. package/entity/EntityRepository.d.ts +27 -7
  22. package/entity/EntityRepository.js +8 -2
  23. package/entity/PolymorphicRef.d.ts +12 -0
  24. package/entity/PolymorphicRef.js +18 -0
  25. package/entity/WrappedEntity.d.ts +2 -2
  26. package/entity/WrappedEntity.js +1 -1
  27. package/entity/defineEntity.d.ts +89 -50
  28. package/entity/defineEntity.js +12 -0
  29. package/entity/index.d.ts +1 -0
  30. package/entity/index.js +1 -0
  31. package/entity/utils.d.ts +6 -1
  32. package/entity/utils.js +33 -0
  33. package/entity/validators.js +2 -2
  34. package/enums.d.ts +2 -2
  35. package/enums.js +1 -0
  36. package/errors.d.ts +16 -8
  37. package/errors.js +40 -13
  38. package/hydration/ObjectHydrator.js +63 -21
  39. package/index.d.ts +1 -1
  40. package/logging/colors.d.ts +1 -1
  41. package/logging/colors.js +7 -6
  42. package/logging/inspect.js +1 -6
  43. package/metadata/EntitySchema.d.ts +43 -13
  44. package/metadata/EntitySchema.js +82 -27
  45. package/metadata/MetadataDiscovery.d.ts +60 -3
  46. package/metadata/MetadataDiscovery.js +665 -154
  47. package/metadata/MetadataProvider.js +3 -1
  48. package/metadata/MetadataStorage.d.ts +13 -6
  49. package/metadata/MetadataStorage.js +64 -19
  50. package/metadata/MetadataValidator.d.ts +32 -2
  51. package/metadata/MetadataValidator.js +196 -31
  52. package/metadata/discover-entities.js +5 -5
  53. package/metadata/types.d.ts +111 -14
  54. package/naming-strategy/AbstractNamingStrategy.d.ts +11 -3
  55. package/naming-strategy/AbstractNamingStrategy.js +12 -0
  56. package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
  57. package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
  58. package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
  59. package/naming-strategy/MongoNamingStrategy.js +6 -6
  60. package/naming-strategy/NamingStrategy.d.ts +17 -3
  61. package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
  62. package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
  63. package/package.json +2 -2
  64. package/platforms/Platform.d.ts +4 -2
  65. package/platforms/Platform.js +5 -2
  66. package/serialization/EntitySerializer.d.ts +3 -0
  67. package/serialization/EntitySerializer.js +15 -13
  68. package/serialization/EntityTransformer.js +6 -6
  69. package/serialization/SerializationContext.d.ts +6 -6
  70. package/typings.d.ts +325 -110
  71. package/typings.js +84 -17
  72. package/unit-of-work/ChangeSet.d.ts +4 -3
  73. package/unit-of-work/ChangeSet.js +2 -3
  74. package/unit-of-work/ChangeSetComputer.d.ts +3 -6
  75. package/unit-of-work/ChangeSetComputer.js +34 -13
  76. package/unit-of-work/ChangeSetPersister.d.ts +12 -10
  77. package/unit-of-work/ChangeSetPersister.js +55 -25
  78. package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
  79. package/unit-of-work/CommitOrderCalculator.js +13 -13
  80. package/unit-of-work/IdentityMap.d.ts +12 -0
  81. package/unit-of-work/IdentityMap.js +39 -1
  82. package/unit-of-work/UnitOfWork.d.ts +21 -3
  83. package/unit-of-work/UnitOfWork.js +203 -56
  84. package/utils/AbstractSchemaGenerator.js +17 -8
  85. package/utils/AsyncContext.d.ts +6 -0
  86. package/utils/AsyncContext.js +42 -0
  87. package/utils/Configuration.d.ts +52 -11
  88. package/utils/Configuration.js +12 -8
  89. package/utils/Cursor.js +21 -8
  90. package/utils/DataloaderUtils.js +13 -11
  91. package/utils/EntityComparator.d.ts +14 -7
  92. package/utils/EntityComparator.js +132 -46
  93. package/utils/QueryHelper.d.ts +16 -6
  94. package/utils/QueryHelper.js +53 -18
  95. package/utils/RawQueryFragment.d.ts +28 -23
  96. package/utils/RawQueryFragment.js +34 -56
  97. package/utils/RequestContext.js +2 -2
  98. package/utils/TransactionContext.js +2 -2
  99. package/utils/TransactionManager.js +1 -1
  100. package/utils/Utils.d.ts +7 -26
  101. package/utils/Utils.js +25 -79
  102. package/utils/clone.js +7 -21
  103. package/utils/env-vars.d.ts +4 -0
  104. package/utils/env-vars.js +13 -3
  105. package/utils/fs-utils.d.ts +21 -0
  106. package/utils/fs-utils.js +106 -11
  107. package/utils/upsert-utils.d.ts +4 -4
package/utils/fs-utils.js CHANGED
@@ -1,9 +1,25 @@
1
- import { existsSync, globSync, mkdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
1
+ import { existsSync, globSync as nodeGlobSync, mkdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
2
+ import { isAbsolute, join, normalize, relative } from 'node:path';
3
+ import { fileURLToPath, pathToFileURL } from 'node:url';
4
4
  import { Utils } from './Utils.js';
5
5
  import { colors } from '../logging/colors.js';
6
+ let globSync = (patterns, options) => {
7
+ const files = nodeGlobSync(patterns, { ...options, withFileTypes: true });
8
+ return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
9
+ };
6
10
  export const fs = {
11
+ async init() {
12
+ const tinyGlobby = await import('tinyglobby').catch(() => null);
13
+ if (tinyGlobby) {
14
+ globSync = (patterns, options) => {
15
+ patterns = Utils.asArray(patterns).map(p => p.replace(/\\/g, '/'));
16
+ if (options?.cwd) {
17
+ options = { ...options, cwd: options.cwd.replace(/\\/g, '/') };
18
+ }
19
+ return tinyGlobby.globSync(patterns, { ...options, expandDirectories: false });
20
+ };
21
+ }
22
+ },
7
23
  pathExists(path) {
8
24
  if (/[*?[\]]/.test(path)) {
9
25
  return globSync(path).length > 0;
@@ -20,30 +36,49 @@ export const fs = {
20
36
  return JSON.parse(file.toString());
21
37
  },
22
38
  glob(input, cwd) {
39
+ const patterns = Array.isArray(input) ? input : [input];
40
+ const positive = [];
41
+ const negative = [];
42
+ for (const p of patterns) {
43
+ if (p.startsWith('!')) {
44
+ negative.push(p.slice(1));
45
+ }
46
+ else {
47
+ positive.push(p);
48
+ }
49
+ }
50
+ const included = new Set(this.resolveGlob(positive, cwd));
51
+ if (included.size > 0 && negative.length > 0) {
52
+ const excluded = this.resolveGlob(negative, cwd);
53
+ for (const file of excluded) {
54
+ included.delete(file);
55
+ }
56
+ }
57
+ return [...included];
58
+ },
59
+ resolveGlob(input, cwd) {
23
60
  if (Array.isArray(input)) {
24
- return input.flatMap(paths => this.glob(paths, cwd));
61
+ return input.flatMap(paths => this.resolveGlob(paths, cwd));
25
62
  }
26
63
  const hasGlobChars = /[*?[\]]/.test(input);
27
64
  if (!hasGlobChars) {
28
65
  try {
29
- const s = statSync(cwd ? Utils.normalizePath(cwd, input) : input);
66
+ const s = statSync(cwd ? this.normalizePath(cwd, input) : input);
30
67
  if (s.isDirectory()) {
31
- const files = globSync(join(input, '**'), { cwd, withFileTypes: true });
32
- return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
68
+ return globSync(join(input, '**'), { cwd });
33
69
  }
34
70
  }
35
71
  catch {
36
72
  // ignore
37
73
  }
38
74
  }
39
- const files = globSync(input, { cwd, withFileTypes: true });
40
- return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
75
+ return globSync(input, { cwd });
41
76
  },
42
77
  getPackageConfig(basePath = process.cwd()) {
43
78
  if (this.pathExists(`${basePath}/package.json`)) {
44
79
  try {
45
- const path = import.meta.resolve(`${basePath}/package.json`);
46
- return this.readJSONSync(fileURLToPath(path));
80
+ const path = this.normalizePath(import.meta.resolve(`${basePath}/package.json`));
81
+ return this.readJSONSync(path);
47
82
  }
48
83
  catch (e) {
49
84
  /* v8 ignore next */
@@ -93,5 +128,65 @@ export const fs = {
93
128
  }
94
129
  }
95
130
  },
131
+ /**
132
+ * Resolves and normalizes a series of path parts relative to each preceding part.
133
+ * If any part is a `file:` URL, it is converted to a local path. If any part is an
134
+ * absolute path, it replaces preceding paths (similar to `path.resolve` in NodeJS).
135
+ * Trailing directory separators are removed, and all directory separators are converted
136
+ * to POSIX-style separators (`/`).
137
+ */
138
+ normalizePath(...parts) {
139
+ let start = 0;
140
+ for (let i = 0; i < parts.length; i++) {
141
+ const part = parts[i];
142
+ if (isAbsolute(part)) {
143
+ start = i;
144
+ }
145
+ else if (part.startsWith('file:')) {
146
+ start = i;
147
+ parts[i] = fileURLToPath(part);
148
+ }
149
+ }
150
+ if (start > 0) {
151
+ parts = parts.slice(start);
152
+ }
153
+ let path = parts.join('/').replace(/\\/g, '/').replace(/\/$/, '');
154
+ path = normalize(path).replace(/\\/g, '/');
155
+ return (path.match(/^[/.]|[a-zA-Z]:/) || path.startsWith('!')) ? path : './' + path;
156
+ },
157
+ /**
158
+ * Determines the relative path between two paths. If either path is a `file:` URL,
159
+ * it is converted to a local path.
160
+ */
161
+ relativePath(path, relativeTo) {
162
+ if (!path) {
163
+ return path;
164
+ }
165
+ path = this.normalizePath(path);
166
+ if (path.startsWith('.')) {
167
+ return path;
168
+ }
169
+ path = relative(this.normalizePath(relativeTo), path);
170
+ return this.normalizePath(path);
171
+ },
172
+ /**
173
+ * Computes the absolute path to for the given path relative to the provided base directory.
174
+ * If either `path` or `baseDir` are `file:` URLs, they are converted to local paths.
175
+ */
176
+ absolutePath(path, baseDir = process.cwd()) {
177
+ if (!path) {
178
+ return this.normalizePath(baseDir);
179
+ }
180
+ if (!isAbsolute(path) && !path.startsWith('file://')) {
181
+ path = baseDir + '/' + path;
182
+ }
183
+ return this.normalizePath(path);
184
+ },
185
+ async dynamicImport(id) {
186
+ /* v8 ignore next */
187
+ const specifier = id.startsWith('file://') ? id : pathToFileURL(id).href;
188
+ const dynamicImportProvider = globalThis.dynamicImportProvider ?? ((id) => import(id));
189
+ return dynamicImportProvider(specifier);
190
+ },
96
191
  };
97
192
  export * from '../cache/FileCacheAdapter.js';
@@ -1,12 +1,12 @@
1
1
  import type { EntityData, EntityMetadata, FilterQuery } from '../typings.js';
2
2
  import type { UpsertOptions } from '../drivers/IDatabaseDriver.js';
3
- import { type RawQueryFragment } from '../utils/RawQueryFragment.js';
3
+ import { type Raw } from '../utils/RawQueryFragment.js';
4
4
  /** @internal */
5
- export declare function getOnConflictFields<T>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T>): (keyof T)[];
5
+ export declare function getOnConflictFields<T>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | Raw, options: UpsertOptions<T>): (keyof T)[];
6
6
  /** @internal */
7
- export declare function getOnConflictReturningFields<T, P extends string>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | RawQueryFragment, options: UpsertOptions<T, P>): (keyof T)[] | '*';
7
+ export declare function getOnConflictReturningFields<T, P extends string>(meta: EntityMetadata<T> | undefined, data: EntityData<T>, uniqueFields: (keyof T)[] | Raw, options: UpsertOptions<T, P>): (keyof T)[] | '*';
8
8
  /** @internal */
9
- export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | RawQueryFragment | undefined, data: EntityData<T>, where: FilterQuery<T>): {
9
+ export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | Raw | undefined, data: EntityData<T>, where: FilterQuery<T>): {
10
10
  where: FilterQuery<T>;
11
11
  propIndex: number | false;
12
12
  };