@mikro-orm/core 7.0.2-dev.8 → 7.0.2

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 (207) hide show
  1. package/EntityManager.d.ts +883 -579
  2. package/EntityManager.js +1897 -1865
  3. package/MikroORM.d.ts +103 -72
  4. package/MikroORM.js +178 -177
  5. package/README.md +128 -294
  6. package/cache/CacheAdapter.d.ts +38 -36
  7. package/cache/FileCacheAdapter.d.ts +30 -24
  8. package/cache/FileCacheAdapter.js +80 -78
  9. package/cache/GeneratedCacheAdapter.d.ts +19 -20
  10. package/cache/GeneratedCacheAdapter.js +31 -30
  11. package/cache/MemoryCacheAdapter.d.ts +19 -20
  12. package/cache/MemoryCacheAdapter.js +36 -36
  13. package/cache/NullCacheAdapter.d.ts +17 -16
  14. package/cache/NullCacheAdapter.js +25 -24
  15. package/connections/Connection.d.ts +99 -75
  16. package/connections/Connection.js +166 -160
  17. package/drivers/DatabaseDriver.d.ts +187 -69
  18. package/drivers/DatabaseDriver.js +451 -432
  19. package/drivers/IDatabaseDriver.d.ts +464 -281
  20. package/drivers/IDatabaseDriver.js +1 -0
  21. package/entity/BaseEntity.d.ts +121 -73
  22. package/entity/BaseEntity.js +44 -33
  23. package/entity/Collection.d.ts +216 -157
  24. package/entity/Collection.js +728 -707
  25. package/entity/EntityAssigner.d.ts +90 -76
  26. package/entity/EntityAssigner.js +232 -229
  27. package/entity/EntityFactory.d.ts +68 -40
  28. package/entity/EntityFactory.js +427 -366
  29. package/entity/EntityHelper.d.ts +34 -22
  30. package/entity/EntityHelper.js +280 -267
  31. package/entity/EntityIdentifier.d.ts +4 -4
  32. package/entity/EntityIdentifier.js +10 -10
  33. package/entity/EntityLoader.d.ts +105 -56
  34. package/entity/EntityLoader.js +754 -722
  35. package/entity/EntityRepository.d.ts +317 -200
  36. package/entity/EntityRepository.js +214 -212
  37. package/entity/PolymorphicRef.d.ts +5 -5
  38. package/entity/PolymorphicRef.js +10 -10
  39. package/entity/Reference.d.ts +130 -66
  40. package/entity/Reference.js +280 -260
  41. package/entity/WrappedEntity.d.ts +116 -53
  42. package/entity/WrappedEntity.js +169 -147
  43. package/entity/defineEntity.d.ts +1290 -614
  44. package/entity/defineEntity.js +521 -511
  45. package/entity/utils.d.ts +13 -3
  46. package/entity/utils.js +71 -73
  47. package/entity/validators.js +43 -43
  48. package/entity/wrap.js +8 -8
  49. package/enums.d.ts +275 -138
  50. package/enums.js +268 -137
  51. package/errors.d.ts +120 -72
  52. package/errors.js +356 -253
  53. package/events/EventManager.d.ts +27 -10
  54. package/events/EventManager.js +80 -73
  55. package/events/EventSubscriber.d.ts +33 -29
  56. package/events/TransactionEventBroadcaster.d.ts +16 -7
  57. package/events/TransactionEventBroadcaster.js +15 -13
  58. package/exceptions.d.ts +23 -40
  59. package/exceptions.js +35 -52
  60. package/hydration/Hydrator.d.ts +43 -16
  61. package/hydration/Hydrator.js +44 -42
  62. package/hydration/ObjectHydrator.d.ts +51 -17
  63. package/hydration/ObjectHydrator.js +480 -416
  64. package/index.d.ts +116 -2
  65. package/index.js +10 -1
  66. package/logging/DefaultLogger.d.ts +35 -30
  67. package/logging/DefaultLogger.js +87 -84
  68. package/logging/Logger.d.ts +45 -40
  69. package/logging/SimpleLogger.d.ts +13 -11
  70. package/logging/SimpleLogger.js +22 -22
  71. package/logging/colors.d.ts +6 -6
  72. package/logging/colors.js +11 -10
  73. package/logging/inspect.js +7 -7
  74. package/metadata/EntitySchema.d.ts +214 -108
  75. package/metadata/EntitySchema.js +398 -379
  76. package/metadata/MetadataDiscovery.d.ts +115 -111
  77. package/metadata/MetadataDiscovery.js +1948 -1857
  78. package/metadata/MetadataProvider.d.ts +25 -14
  79. package/metadata/MetadataProvider.js +83 -77
  80. package/metadata/MetadataStorage.d.ts +39 -19
  81. package/metadata/MetadataStorage.js +119 -106
  82. package/metadata/MetadataValidator.d.ts +39 -39
  83. package/metadata/MetadataValidator.js +381 -338
  84. package/metadata/discover-entities.d.ts +5 -2
  85. package/metadata/discover-entities.js +27 -27
  86. package/metadata/types.d.ts +615 -531
  87. package/naming-strategy/AbstractNamingStrategy.d.ts +55 -39
  88. package/naming-strategy/AbstractNamingStrategy.js +91 -85
  89. package/naming-strategy/EntityCaseNamingStrategy.d.ts +6 -6
  90. package/naming-strategy/EntityCaseNamingStrategy.js +22 -22
  91. package/naming-strategy/MongoNamingStrategy.d.ts +7 -6
  92. package/naming-strategy/MongoNamingStrategy.js +19 -18
  93. package/naming-strategy/NamingStrategy.d.ts +109 -99
  94. package/naming-strategy/UnderscoreNamingStrategy.d.ts +8 -7
  95. package/naming-strategy/UnderscoreNamingStrategy.js +22 -21
  96. package/not-supported.js +7 -4
  97. package/package.json +1 -1
  98. package/platforms/ExceptionConverter.d.ts +2 -1
  99. package/platforms/ExceptionConverter.js +5 -4
  100. package/platforms/Platform.d.ts +310 -236
  101. package/platforms/Platform.js +661 -573
  102. package/serialization/EntitySerializer.d.ts +49 -25
  103. package/serialization/EntitySerializer.js +224 -216
  104. package/serialization/EntityTransformer.d.ts +11 -5
  105. package/serialization/EntityTransformer.js +220 -216
  106. package/serialization/SerializationContext.d.ts +27 -18
  107. package/serialization/SerializationContext.js +105 -100
  108. package/types/ArrayType.d.ts +9 -8
  109. package/types/ArrayType.js +34 -33
  110. package/types/BigIntType.d.ts +17 -10
  111. package/types/BigIntType.js +37 -37
  112. package/types/BlobType.d.ts +4 -3
  113. package/types/BlobType.js +14 -13
  114. package/types/BooleanType.d.ts +5 -4
  115. package/types/BooleanType.js +13 -12
  116. package/types/CharacterType.d.ts +3 -2
  117. package/types/CharacterType.js +7 -6
  118. package/types/DateTimeType.d.ts +6 -5
  119. package/types/DateTimeType.js +16 -15
  120. package/types/DateType.d.ts +6 -5
  121. package/types/DateType.js +16 -15
  122. package/types/DecimalType.d.ts +7 -7
  123. package/types/DecimalType.js +26 -26
  124. package/types/DoubleType.d.ts +3 -3
  125. package/types/DoubleType.js +12 -12
  126. package/types/EnumArrayType.d.ts +6 -5
  127. package/types/EnumArrayType.js +25 -24
  128. package/types/EnumType.d.ts +4 -3
  129. package/types/EnumType.js +12 -11
  130. package/types/FloatType.d.ts +4 -3
  131. package/types/FloatType.js +10 -9
  132. package/types/IntegerType.d.ts +4 -3
  133. package/types/IntegerType.js +10 -9
  134. package/types/IntervalType.d.ts +5 -4
  135. package/types/IntervalType.js +13 -12
  136. package/types/JsonType.d.ts +9 -8
  137. package/types/JsonType.js +33 -32
  138. package/types/MediumIntType.d.ts +2 -1
  139. package/types/MediumIntType.js +4 -3
  140. package/types/SmallIntType.d.ts +4 -3
  141. package/types/SmallIntType.js +10 -9
  142. package/types/StringType.d.ts +5 -4
  143. package/types/StringType.js +13 -12
  144. package/types/TextType.d.ts +4 -3
  145. package/types/TextType.js +10 -9
  146. package/types/TimeType.d.ts +6 -5
  147. package/types/TimeType.js +18 -17
  148. package/types/TinyIntType.d.ts +4 -3
  149. package/types/TinyIntType.js +11 -10
  150. package/types/Type.d.ts +88 -73
  151. package/types/Type.js +85 -74
  152. package/types/Uint8ArrayType.d.ts +5 -4
  153. package/types/Uint8ArrayType.js +22 -21
  154. package/types/UnknownType.d.ts +5 -4
  155. package/types/UnknownType.js +13 -12
  156. package/types/UuidType.d.ts +6 -5
  157. package/types/UuidType.js +20 -19
  158. package/types/index.d.ts +77 -49
  159. package/types/index.js +64 -26
  160. package/typings.d.ts +1388 -729
  161. package/typings.js +255 -231
  162. package/unit-of-work/ChangeSet.d.ts +28 -24
  163. package/unit-of-work/ChangeSet.js +58 -54
  164. package/unit-of-work/ChangeSetComputer.d.ts +13 -11
  165. package/unit-of-work/ChangeSetComputer.js +180 -159
  166. package/unit-of-work/ChangeSetPersister.d.ts +64 -41
  167. package/unit-of-work/ChangeSetPersister.js +443 -418
  168. package/unit-of-work/CommitOrderCalculator.d.ts +40 -40
  169. package/unit-of-work/CommitOrderCalculator.js +89 -88
  170. package/unit-of-work/IdentityMap.d.ts +32 -25
  171. package/unit-of-work/IdentityMap.js +106 -99
  172. package/unit-of-work/UnitOfWork.d.ts +182 -127
  173. package/unit-of-work/UnitOfWork.js +1201 -1169
  174. package/utils/AbstractMigrator.d.ts +111 -91
  175. package/utils/AbstractMigrator.js +275 -275
  176. package/utils/AbstractSchemaGenerator.d.ts +43 -34
  177. package/utils/AbstractSchemaGenerator.js +121 -122
  178. package/utils/AsyncContext.d.ts +3 -3
  179. package/utils/AsyncContext.js +34 -35
  180. package/utils/Configuration.d.ts +853 -801
  181. package/utils/Configuration.js +360 -337
  182. package/utils/Cursor.d.ts +40 -22
  183. package/utils/Cursor.js +135 -127
  184. package/utils/DataloaderUtils.d.ts +58 -43
  185. package/utils/DataloaderUtils.js +203 -198
  186. package/utils/EntityComparator.d.ts +99 -80
  187. package/utils/EntityComparator.js +825 -727
  188. package/utils/NullHighlighter.d.ts +2 -1
  189. package/utils/NullHighlighter.js +4 -3
  190. package/utils/QueryHelper.d.ts +79 -51
  191. package/utils/QueryHelper.js +372 -361
  192. package/utils/RawQueryFragment.d.ts +54 -28
  193. package/utils/RawQueryFragment.js +110 -99
  194. package/utils/RequestContext.d.ts +33 -32
  195. package/utils/RequestContext.js +52 -53
  196. package/utils/TransactionContext.d.ts +17 -16
  197. package/utils/TransactionContext.js +28 -27
  198. package/utils/TransactionManager.d.ts +58 -58
  199. package/utils/TransactionManager.js +199 -197
  200. package/utils/Utils.d.ts +210 -145
  201. package/utils/Utils.js +820 -813
  202. package/utils/clone.js +104 -113
  203. package/utils/env-vars.js +90 -88
  204. package/utils/fs-utils.d.ts +15 -15
  205. package/utils/fs-utils.js +180 -181
  206. package/utils/upsert-utils.d.ts +20 -5
  207. package/utils/upsert-utils.js +114 -116
package/utils/fs-utils.js CHANGED
@@ -5,189 +5,188 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
5
5
  import { Utils } from './Utils.js';
6
6
  import { colors } from '../logging/colors.js';
7
7
  let globSync = (patterns, options) => {
8
- const files = nodeGlobSync(patterns, { ...options, withFileTypes: true });
9
- return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
8
+ const files = nodeGlobSync(patterns, { ...options, withFileTypes: true });
9
+ return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
10
10
  };
11
11
  export const fs = {
12
- async init() {
13
- const tinyGlobby = await import('tinyglobby').catch(() => null);
14
- if (tinyGlobby) {
15
- globSync = (patterns, options) => {
16
- patterns = Utils.asArray(patterns).map(p => p.replace(/\\/g, '/'));
17
- if (options?.cwd) {
18
- options = { ...options, cwd: options.cwd.replace(/\\/g, '/') };
19
- }
20
- return tinyGlobby.globSync(patterns, { ...options, expandDirectories: false });
21
- };
22
- }
23
- },
24
- pathExists(path) {
25
- if (/[*?[\]]/.test(path)) {
26
- return globSync(path).length > 0;
27
- }
28
- return existsSync(path);
29
- },
30
- ensureDir(path) {
31
- if (!existsSync(path)) {
32
- mkdirSync(path, { recursive: true });
33
- }
34
- },
35
- readJSONSync(path) {
36
- const file = readFileSync(path);
37
- return JSON.parse(file.toString());
38
- },
39
- glob(input, cwd) {
40
- const patterns = Array.isArray(input) ? input : [input];
41
- const positive = [];
42
- const negative = [];
43
- for (const p of patterns) {
44
- if (p.startsWith('!')) {
45
- negative.push(p.slice(1));
46
- }
47
- else {
48
- positive.push(p);
49
- }
50
- }
51
- const included = new Set(this.resolveGlob(positive, cwd));
52
- if (included.size > 0 && negative.length > 0) {
53
- const excluded = this.resolveGlob(negative, cwd);
54
- for (const file of excluded) {
55
- included.delete(file);
56
- }
57
- }
58
- return [...included];
59
- },
60
- resolveGlob(input, cwd) {
61
- if (Array.isArray(input)) {
62
- return input.flatMap(paths => this.resolveGlob(paths, cwd));
63
- }
64
- const hasGlobChars = /[*?[\]]/.test(input);
65
- if (!hasGlobChars) {
66
- try {
67
- const s = statSync(cwd ? this.normalizePath(cwd, input) : input);
68
- if (s.isDirectory()) {
69
- return globSync(join(input, '**'), { cwd });
70
- }
71
- }
72
- catch {
73
- // ignore
74
- }
75
- }
76
- return globSync(input, { cwd });
77
- },
78
- getPackageConfig(basePath = process.cwd()) {
79
- if (this.pathExists(`${basePath}/package.json`)) {
80
- try {
81
- const path = this.normalizePath(import.meta.resolve(`${basePath}/package.json`));
82
- return this.readJSONSync(path);
83
- }
84
- catch (e) {
85
- /* v8 ignore next */
86
- return {};
87
- }
88
- }
89
- const parentFolder = realpathSync(`${basePath}/..`);
90
- // we reached the root folder
91
- if (basePath === parentFolder) {
92
- return {};
93
- }
94
- return this.getPackageConfig(parentFolder);
95
- },
96
- getORMPackages() {
97
- const pkg = this.getPackageConfig();
98
- return new Set([...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {})]);
99
- },
100
- getORMPackageVersion(name) {
101
- try {
102
- const path = import.meta.resolve(`${name}/package.json`);
103
- const pkg = this.readJSONSync(fileURLToPath(path));
104
- return pkg?.version;
105
- }
106
- catch (e) {
107
- return undefined;
108
- }
109
- },
110
- // inspired by https://github.com/facebook/docusaurus/pull/3386
111
- checkPackageVersion() {
112
- const coreVersion = Utils.getORMVersion();
113
- if (process.env.MIKRO_ORM_ALLOW_VERSION_MISMATCH || coreVersion === '[[MIKRO_ORM_VERSION]]') {
114
- return;
115
- }
116
- const deps = this.getORMPackages();
117
- const exceptions = new Set(['nestjs', 'sql-highlighter', 'mongo-highlighter']);
118
- const ormPackages = [...deps].filter(d => d.startsWith('@mikro-orm/') && d !== '@mikro-orm/core' && !exceptions.has(d.substring('@mikro-orm/'.length)));
119
- for (const ormPackage of ormPackages) {
120
- const version = this.getORMPackageVersion(ormPackage);
121
- if (version != null && version !== coreVersion) {
122
- throw new Error(`Bad ${colors.cyan(ormPackage)} version ${colors.yellow('' + version)}.\n` +
123
- `All official @mikro-orm/* packages need to have the exact same version as @mikro-orm/core (${colors.green(coreVersion)}).\n` +
124
- `Only exceptions are packages that don't live in the 'mikro-orm' repository: ${[...exceptions].join(', ')}.\n` +
125
- `Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?`);
126
- }
127
- }
128
- },
129
- /**
130
- * Resolves and normalizes a series of path parts relative to each preceding part.
131
- * If any part is a `file:` URL, it is converted to a local path. If any part is an
132
- * absolute path, it replaces preceding paths (similar to `path.resolve` in NodeJS).
133
- * Trailing directory separators are removed, and all directory separators are converted
134
- * to POSIX-style separators (`/`).
135
- */
136
- normalizePath(...parts) {
137
- let start = 0;
138
- for (let i = 0; i < parts.length; i++) {
139
- const part = parts[i];
140
- if (isAbsolute(part)) {
141
- start = i;
142
- }
143
- else if (part.startsWith('file:')) {
144
- start = i;
145
- parts[i] = fileURLToPath(part);
146
- }
147
- }
148
- if (start > 0) {
149
- parts = parts.slice(start);
150
- }
151
- let path = parts.join('/').replace(/\\/g, '/').replace(/\/$/, '');
152
- path = normalize(path).replace(/\\/g, '/');
153
- return /^[/.]|[a-zA-Z]:/.exec(path) || path.startsWith('!') ? path : './' + path;
154
- },
155
- /**
156
- * Determines the relative path between two paths. If either path is a `file:` URL,
157
- * it is converted to a local path.
158
- */
159
- relativePath(path, relativeTo) {
160
- if (!path) {
161
- return path;
162
- }
163
- path = this.normalizePath(path);
164
- if (path.startsWith('.')) {
165
- return path;
166
- }
167
- path = relative(this.normalizePath(relativeTo), path);
168
- return this.normalizePath(path);
169
- },
170
- /**
171
- * Computes the absolute path to for the given path relative to the provided base directory.
172
- * If either `path` or `baseDir` are `file:` URLs, they are converted to local paths.
173
- */
174
- absolutePath(path, baseDir = process.cwd()) {
175
- if (!path) {
176
- return this.normalizePath(baseDir);
177
- }
178
- if (!isAbsolute(path) && !path.startsWith('file://')) {
179
- path = baseDir + '/' + path;
180
- }
181
- return this.normalizePath(path);
182
- },
183
- async writeFile(path, data, options) {
184
- await nodeWriteFile(path, data, options);
185
- },
186
- async dynamicImport(id) {
12
+ async init() {
13
+ const tinyGlobby = await import('tinyglobby').catch(() => null);
14
+ if (tinyGlobby) {
15
+ globSync = (patterns, options) => {
16
+ patterns = Utils.asArray(patterns).map(p => p.replace(/\\/g, '/'));
17
+ if (options?.cwd) {
18
+ options = { ...options, cwd: options.cwd.replace(/\\/g, '/') };
19
+ }
20
+ return tinyGlobby.globSync(patterns, { ...options, expandDirectories: false });
21
+ };
22
+ }
23
+ },
24
+ pathExists(path) {
25
+ if (/[*?[\]]/.test(path)) {
26
+ return globSync(path).length > 0;
27
+ }
28
+ return existsSync(path);
29
+ },
30
+ ensureDir(path) {
31
+ if (!existsSync(path)) {
32
+ mkdirSync(path, { recursive: true });
33
+ }
34
+ },
35
+ readJSONSync(path) {
36
+ const file = readFileSync(path);
37
+ return JSON.parse(file.toString());
38
+ },
39
+ glob(input, cwd) {
40
+ const patterns = Array.isArray(input) ? input : [input];
41
+ const positive = [];
42
+ const negative = [];
43
+ for (const p of patterns) {
44
+ if (p.startsWith('!')) {
45
+ negative.push(p.slice(1));
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) {
60
+ if (Array.isArray(input)) {
61
+ return input.flatMap(paths => this.resolveGlob(paths, cwd));
62
+ }
63
+ const hasGlobChars = /[*?[\]]/.test(input);
64
+ if (!hasGlobChars) {
65
+ try {
66
+ const s = statSync(cwd ? this.normalizePath(cwd, input) : input);
67
+ if (s.isDirectory()) {
68
+ return globSync(join(input, '**'), { cwd });
69
+ }
70
+ } catch {
71
+ // ignore
72
+ }
73
+ }
74
+ return globSync(input, { cwd });
75
+ },
76
+ getPackageConfig(basePath = process.cwd()) {
77
+ if (this.pathExists(`${basePath}/package.json`)) {
78
+ try {
79
+ const path = this.normalizePath(import.meta.resolve(`${basePath}/package.json`));
80
+ return this.readJSONSync(path);
81
+ } catch (e) {
187
82
  /* v8 ignore next */
188
- const specifier = id.startsWith('file://') ? id : pathToFileURL(id).href;
189
- const dynamicImportProvider = globalThis.dynamicImportProvider ?? ((id) => import(id));
190
- return dynamicImportProvider(specifier);
191
- },
83
+ return {};
84
+ }
85
+ }
86
+ const parentFolder = realpathSync(`${basePath}/..`);
87
+ // we reached the root folder
88
+ if (basePath === parentFolder) {
89
+ return {};
90
+ }
91
+ return this.getPackageConfig(parentFolder);
92
+ },
93
+ getORMPackages() {
94
+ const pkg = this.getPackageConfig();
95
+ return new Set([...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {})]);
96
+ },
97
+ getORMPackageVersion(name) {
98
+ try {
99
+ const path = import.meta.resolve(`${name}/package.json`);
100
+ const pkg = this.readJSONSync(fileURLToPath(path));
101
+ return pkg?.version;
102
+ } catch (e) {
103
+ return undefined;
104
+ }
105
+ },
106
+ // inspired by https://github.com/facebook/docusaurus/pull/3386
107
+ checkPackageVersion() {
108
+ const coreVersion = Utils.getORMVersion();
109
+ if (process.env.MIKRO_ORM_ALLOW_VERSION_MISMATCH || coreVersion === '[[MIKRO_ORM_VERSION]]') {
110
+ return;
111
+ }
112
+ const deps = this.getORMPackages();
113
+ const exceptions = new Set(['nestjs', 'sql-highlighter', 'mongo-highlighter']);
114
+ const ormPackages = [...deps].filter(
115
+ d => d.startsWith('@mikro-orm/') && d !== '@mikro-orm/core' && !exceptions.has(d.substring('@mikro-orm/'.length)),
116
+ );
117
+ for (const ormPackage of ormPackages) {
118
+ const version = this.getORMPackageVersion(ormPackage);
119
+ if (version != null && version !== coreVersion) {
120
+ throw new Error(
121
+ `Bad ${colors.cyan(ormPackage)} version ${colors.yellow('' + version)}.\n` +
122
+ `All official @mikro-orm/* packages need to have the exact same version as @mikro-orm/core (${colors.green(coreVersion)}).\n` +
123
+ `Only exceptions are packages that don't live in the 'mikro-orm' repository: ${[...exceptions].join(', ')}.\n` +
124
+ `Maybe you want to check, or regenerate your yarn.lock or package-lock.json file?`,
125
+ );
126
+ }
127
+ }
128
+ },
129
+ /**
130
+ * Resolves and normalizes a series of path parts relative to each preceding part.
131
+ * If any part is a `file:` URL, it is converted to a local path. If any part is an
132
+ * absolute path, it replaces preceding paths (similar to `path.resolve` in NodeJS).
133
+ * Trailing directory separators are removed, and all directory separators are converted
134
+ * to POSIX-style separators (`/`).
135
+ */
136
+ normalizePath(...parts) {
137
+ let start = 0;
138
+ for (let i = 0; i < parts.length; i++) {
139
+ const part = parts[i];
140
+ if (isAbsolute(part)) {
141
+ start = i;
142
+ } else if (part.startsWith('file:')) {
143
+ start = i;
144
+ parts[i] = fileURLToPath(part);
145
+ }
146
+ }
147
+ if (start > 0) {
148
+ parts = parts.slice(start);
149
+ }
150
+ let path = parts.join('/').replace(/\\/g, '/').replace(/\/$/, '');
151
+ path = normalize(path).replace(/\\/g, '/');
152
+ return /^[/.]|[a-zA-Z]:/.exec(path) || path.startsWith('!') ? path : './' + path;
153
+ },
154
+ /**
155
+ * Determines the relative path between two paths. If either path is a `file:` URL,
156
+ * it is converted to a local path.
157
+ */
158
+ relativePath(path, relativeTo) {
159
+ if (!path) {
160
+ return path;
161
+ }
162
+ path = this.normalizePath(path);
163
+ if (path.startsWith('.')) {
164
+ return path;
165
+ }
166
+ path = relative(this.normalizePath(relativeTo), path);
167
+ return this.normalizePath(path);
168
+ },
169
+ /**
170
+ * Computes the absolute path to for the given path relative to the provided base directory.
171
+ * If either `path` or `baseDir` are `file:` URLs, they are converted to local paths.
172
+ */
173
+ absolutePath(path, baseDir = process.cwd()) {
174
+ if (!path) {
175
+ return this.normalizePath(baseDir);
176
+ }
177
+ if (!isAbsolute(path) && !path.startsWith('file://')) {
178
+ path = baseDir + '/' + path;
179
+ }
180
+ return this.normalizePath(path);
181
+ },
182
+ async writeFile(path, data, options) {
183
+ await nodeWriteFile(path, data, options);
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
+ },
192
191
  };
193
192
  export * from '../cache/FileCacheAdapter.js';
@@ -2,11 +2,26 @@ import type { EntityData, EntityMetadata, FilterQuery } from '../typings.js';
2
2
  import type { UpsertOptions } from '../drivers/IDatabaseDriver.js';
3
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)[] | Raw, options: UpsertOptions<T>): (keyof T)[];
5
+ export declare function getOnConflictFields<T>(
6
+ meta: EntityMetadata<T> | undefined,
7
+ data: EntityData<T>,
8
+ uniqueFields: (keyof T)[] | Raw,
9
+ options: UpsertOptions<T>,
10
+ ): (keyof T)[];
6
11
  /** @internal */
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)[] | '*';
12
+ export declare function getOnConflictReturningFields<T, P extends string>(
13
+ meta: EntityMetadata<T> | undefined,
14
+ data: EntityData<T>,
15
+ uniqueFields: (keyof T)[] | Raw,
16
+ options: UpsertOptions<T, P>,
17
+ ): (keyof T)[] | '*';
8
18
  /** @internal */
9
- export declare function getWhereCondition<T extends object>(meta: EntityMetadata<T>, onConflictFields: (keyof T)[] | Raw | undefined, data: EntityData<T>, where: FilterQuery<T>): {
10
- where: FilterQuery<T>;
11
- propIndex: number | false;
19
+ export declare function getWhereCondition<T extends object>(
20
+ meta: EntityMetadata<T>,
21
+ onConflictFields: (keyof T)[] | Raw | undefined,
22
+ data: EntityData<T>,
23
+ where: FilterQuery<T>,
24
+ ): {
25
+ where: FilterQuery<T>;
26
+ propIndex: number | false;
12
27
  };
@@ -1,144 +1,142 @@
1
1
  import { isRaw } from '../utils/RawQueryFragment.js';
2
2
  import { Utils } from './Utils.js';
3
3
  function expandEmbeddedProperties(prop, key) {
4
- if (prop.object) {
5
- return [prop.name];
4
+ if (prop.object) {
5
+ return [prop.name];
6
+ }
7
+ return Object.values(prop.embeddedProps).flatMap(p => {
8
+ /* v8 ignore next */
9
+ if (p.embeddable && !p.object) {
10
+ return expandEmbeddedProperties(p);
6
11
  }
7
- return Object.values(prop.embeddedProps).flatMap(p => {
8
- /* v8 ignore next */
9
- if (p.embeddable && !p.object) {
10
- return expandEmbeddedProperties(p);
11
- }
12
- return [p.name];
13
- });
12
+ return [p.name];
13
+ });
14
14
  }
15
15
  /**
16
16
  * Expands dot paths and stars
17
17
  */
18
18
  function expandFields(meta, fields) {
19
- return fields.flatMap(f => {
20
- if (f === '*' && meta) {
21
- return meta.comparableProps.filter(p => !p.lazy && !p.embeddable).map(p => p.name);
22
- }
23
- if (f.includes('.')) {
24
- const [k, ...tmp] = f.split('.');
25
- const rest = tmp.join('.');
26
- const prop = meta?.properties[k];
27
- if (prop?.embeddable) {
28
- if (rest === '*') {
29
- return expandEmbeddedProperties(prop);
30
- }
31
- return expandEmbeddedProperties(prop, rest);
32
- }
33
- }
34
- const prop = meta?.properties[f];
35
- if (prop?.embeddable) {
36
- return expandEmbeddedProperties(prop);
19
+ return fields.flatMap(f => {
20
+ if (f === '*' && meta) {
21
+ return meta.comparableProps.filter(p => !p.lazy && !p.embeddable).map(p => p.name);
22
+ }
23
+ if (f.includes('.')) {
24
+ const [k, ...tmp] = f.split('.');
25
+ const rest = tmp.join('.');
26
+ const prop = meta?.properties[k];
27
+ if (prop?.embeddable) {
28
+ if (rest === '*') {
29
+ return expandEmbeddedProperties(prop);
37
30
  }
38
- return [f];
39
- });
31
+ return expandEmbeddedProperties(prop, rest);
32
+ }
33
+ }
34
+ const prop = meta?.properties[f];
35
+ if (prop?.embeddable) {
36
+ return expandEmbeddedProperties(prop);
37
+ }
38
+ return [f];
39
+ });
40
40
  }
41
41
  /** @internal */
42
42
  export function getOnConflictFields(meta, data, uniqueFields, options) {
43
- if (options.onConflictMergeFields) {
44
- const onConflictMergeFields = expandFields(meta, options.onConflictMergeFields);
45
- return onConflictMergeFields.flatMap(f => {
46
- const prop = meta?.properties[f];
47
- /* v8 ignore next */
48
- if (prop?.embeddable && !prop.object) {
49
- return Object.values(prop.embeddedProps).map(p => p.name);
50
- }
51
- return f;
52
- });
53
- }
54
- const keys = Object.keys(data).flatMap(f => {
55
- if (!(Array.isArray(uniqueFields) && !uniqueFields.includes(f))) {
56
- return [];
57
- }
58
- const prop = meta?.properties[f];
59
- if (prop?.embeddable && !prop.object) {
60
- return expandEmbeddedProperties(prop);
61
- }
62
- return [f];
43
+ if (options.onConflictMergeFields) {
44
+ const onConflictMergeFields = expandFields(meta, options.onConflictMergeFields);
45
+ return onConflictMergeFields.flatMap(f => {
46
+ const prop = meta?.properties[f];
47
+ /* v8 ignore next */
48
+ if (prop?.embeddable && !prop.object) {
49
+ return Object.values(prop.embeddedProps).map(p => p.name);
50
+ }
51
+ return f;
63
52
  });
64
- if (options.onConflictExcludeFields) {
65
- const onConflictExcludeFields = expandFields(meta, options.onConflictExcludeFields);
66
- return keys.filter(f => !onConflictExcludeFields.includes(f));
53
+ }
54
+ const keys = Object.keys(data).flatMap(f => {
55
+ if (!(Array.isArray(uniqueFields) && !uniqueFields.includes(f))) {
56
+ return [];
67
57
  }
68
- return keys;
58
+ const prop = meta?.properties[f];
59
+ if (prop?.embeddable && !prop.object) {
60
+ return expandEmbeddedProperties(prop);
61
+ }
62
+ return [f];
63
+ });
64
+ if (options.onConflictExcludeFields) {
65
+ const onConflictExcludeFields = expandFields(meta, options.onConflictExcludeFields);
66
+ return keys.filter(f => !onConflictExcludeFields.includes(f));
67
+ }
68
+ return keys;
69
69
  }
70
70
  /** @internal */
71
71
  export function getOnConflictReturningFields(meta, data, uniqueFields, options) {
72
- /* v8 ignore next */
73
- if (!meta) {
74
- return '*';
75
- }
76
- const keys = meta.comparableProps
77
- .filter(p => {
78
- if (p.lazy || p.embeddable) {
79
- return false;
80
- }
81
- if (p.autoincrement) {
82
- return true;
83
- }
84
- return Array.isArray(uniqueFields) && !uniqueFields.includes(p.name);
72
+ /* v8 ignore next */
73
+ if (!meta) {
74
+ return '*';
75
+ }
76
+ const keys = meta.comparableProps
77
+ .filter(p => {
78
+ if (p.lazy || p.embeddable) {
79
+ return false;
80
+ }
81
+ if (p.autoincrement) {
82
+ return true;
83
+ }
84
+ return Array.isArray(uniqueFields) && !uniqueFields.includes(p.name);
85
85
  })
86
- .map(p => p.name);
87
- if (meta.versionProperty) {
88
- keys.push(meta.versionProperty);
89
- }
90
- if (options.onConflictAction === 'ignore') {
91
- return keys;
92
- }
93
- if (options.onConflictMergeFields) {
94
- const onConflictMergeFields = expandFields(meta, options.onConflictMergeFields);
95
- return keys.filter(key => !onConflictMergeFields.includes(key));
96
- }
97
- if (options.onConflictExcludeFields) {
98
- const onConflictExcludeFields = expandFields(meta, options.onConflictExcludeFields);
99
- return [...new Set(keys.concat(...onConflictExcludeFields))];
100
- }
101
- return keys.filter(key => !(key in data));
86
+ .map(p => p.name);
87
+ if (meta.versionProperty) {
88
+ keys.push(meta.versionProperty);
89
+ }
90
+ if (options.onConflictAction === 'ignore') {
91
+ return keys;
92
+ }
93
+ if (options.onConflictMergeFields) {
94
+ const onConflictMergeFields = expandFields(meta, options.onConflictMergeFields);
95
+ return keys.filter(key => !onConflictMergeFields.includes(key));
96
+ }
97
+ if (options.onConflictExcludeFields) {
98
+ const onConflictExcludeFields = expandFields(meta, options.onConflictExcludeFields);
99
+ return [...new Set(keys.concat(...onConflictExcludeFields))];
100
+ }
101
+ return keys.filter(key => !(key in data));
102
102
  }
103
103
  function getPropertyValue(obj, key) {
104
- if (!key.includes('.')) {
105
- return obj[key];
106
- }
107
- const parts = key.split('.');
108
- let curr = obj;
109
- for (let i = 0; i < parts.length - 1; i++) {
110
- curr[parts[i]] ??= {};
111
- curr = curr[parts[i]];
112
- }
113
- return curr[parts[parts.length - 1]];
104
+ if (!key.includes('.')) {
105
+ return obj[key];
106
+ }
107
+ const parts = key.split('.');
108
+ let curr = obj;
109
+ for (let i = 0; i < parts.length - 1; i++) {
110
+ curr[parts[i]] ??= {};
111
+ curr = curr[parts[i]];
112
+ }
113
+ return curr[parts[parts.length - 1]];
114
114
  }
115
115
  /** @internal */
116
116
  export function getWhereCondition(meta, onConflictFields, data, where) {
117
- const unique = onConflictFields ?? meta.props.filter(p => p.unique).map(p => p.name);
118
- const propIndex = !isRaw(unique) &&
119
- unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
120
- if (onConflictFields || where == null) {
121
- if (propIndex !== false && propIndex >= 0) {
122
- let key = unique[propIndex];
123
- if (key.includes('.')) {
124
- const prop = meta.properties[key.substring(0, key.indexOf('.'))];
125
- if (prop) {
126
- key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`;
127
- }
128
- }
129
- where = { [key]: getPropertyValue(data, unique[propIndex]) };
117
+ const unique = onConflictFields ?? meta.props.filter(p => p.unique).map(p => p.name);
118
+ const propIndex = !isRaw(unique) && unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
119
+ if (onConflictFields || where == null) {
120
+ if (propIndex !== false && propIndex >= 0) {
121
+ let key = unique[propIndex];
122
+ if (key.includes('.')) {
123
+ const prop = meta.properties[key.substring(0, key.indexOf('.'))];
124
+ if (prop) {
125
+ key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`;
130
126
  }
131
- else if (meta.uniques.length > 0) {
132
- for (const u of meta.uniques) {
133
- if (Utils.asArray(u.properties).every(p => data[p] != null)) {
134
- where = Utils.asArray(u.properties).reduce((o, key) => {
135
- o[key] = data[key];
136
- return o;
137
- }, {});
138
- break;
139
- }
140
- }
127
+ }
128
+ where = { [key]: getPropertyValue(data, unique[propIndex]) };
129
+ } else if (meta.uniques.length > 0) {
130
+ for (const u of meta.uniques) {
131
+ if (Utils.asArray(u.properties).every(p => data[p] != null)) {
132
+ where = Utils.asArray(u.properties).reduce((o, key) => {
133
+ o[key] = data[key];
134
+ return o;
135
+ }, {});
136
+ break;
141
137
  }
138
+ }
142
139
  }
143
- return { where, propIndex };
140
+ }
141
+ return { where, propIndex };
144
142
  }