@mikro-orm/core 7.3.0-dev.2 → 7.3.0-dev.21

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 (57) hide show
  1. package/EntityManager.js +158 -42
  2. package/cache/cache-key.d.ts +5 -0
  3. package/cache/cache-key.js +30 -0
  4. package/drivers/DatabaseDriver.d.ts +2 -0
  5. package/drivers/DatabaseDriver.js +16 -3
  6. package/drivers/IDatabaseDriver.d.ts +2 -0
  7. package/entity/EntityAssigner.js +15 -3
  8. package/entity/EntityFactory.d.ts +8 -0
  9. package/entity/EntityFactory.js +69 -3
  10. package/entity/EntityLoader.d.ts +1 -0
  11. package/entity/EntityLoader.js +66 -23
  12. package/entity/WrappedEntity.js +6 -1
  13. package/entity/defineEntity.d.ts +32 -19
  14. package/entity/defineEntity.js +9 -0
  15. package/entity/validators.js +4 -1
  16. package/errors.d.ts +7 -0
  17. package/errors.js +15 -0
  18. package/hydration/ObjectHydrator.js +6 -2
  19. package/metadata/MetadataDiscovery.d.ts +10 -0
  20. package/metadata/MetadataDiscovery.js +178 -30
  21. package/metadata/MetadataStorage.d.ts +8 -0
  22. package/metadata/MetadataStorage.js +47 -3
  23. package/metadata/MetadataValidator.js +2 -1
  24. package/metadata/types.d.ts +12 -0
  25. package/package.json +1 -1
  26. package/platforms/Platform.d.ts +6 -0
  27. package/platforms/Platform.js +26 -0
  28. package/serialization/EntitySerializer.js +56 -16
  29. package/serialization/EntityTransformer.js +4 -4
  30. package/serialization/SerializationContext.d.ts +2 -1
  31. package/serialization/SerializationContext.js +4 -3
  32. package/types/DecimalType.js +34 -1
  33. package/types/DoubleType.d.ts +7 -3
  34. package/types/DoubleType.js +8 -3
  35. package/typings.d.ts +26 -6
  36. package/typings.js +5 -2
  37. package/unit-of-work/ChangeSetComputer.js +22 -7
  38. package/unit-of-work/ChangeSetPersister.js +24 -5
  39. package/unit-of-work/CommitOrderCalculator.js +2 -1
  40. package/unit-of-work/IdentityMap.d.ts +3 -2
  41. package/unit-of-work/IdentityMap.js +30 -7
  42. package/unit-of-work/UnitOfWork.js +49 -9
  43. package/utils/Configuration.d.ts +12 -1
  44. package/utils/DataloaderUtils.js +19 -8
  45. package/utils/EntityComparator.d.ts +5 -0
  46. package/utils/EntityComparator.js +87 -19
  47. package/utils/QueryHelper.d.ts +14 -0
  48. package/utils/QueryHelper.js +90 -13
  49. package/utils/RawQueryFragment.d.ts +5 -1
  50. package/utils/RawQueryFragment.js +27 -19
  51. package/utils/TransactionManager.js +4 -0
  52. package/utils/Utils.d.ts +2 -0
  53. package/utils/Utils.js +24 -8
  54. package/utils/clone.js +5 -7
  55. package/utils/env-vars.js +2 -0
  56. package/utils/fs-utils.js +19 -15
  57. package/utils/upsert-utils.js +2 -1
@@ -3,8 +3,8 @@ import { DANGEROUS_PROPERTY_NAMES, Utils } from './Utils.js';
3
3
  import { ARRAY_OPERATORS, GroupOperator, JSON_KEY_OPERATORS, ReferenceKind } from '../enums.js';
4
4
  import { JsonType } from '../types/JsonType.js';
5
5
  import { helper } from '../entity/wrap.js';
6
- import { isRaw, Raw } from './RawQueryFragment.js';
7
- import { MetadataError } from '../errors.js';
6
+ import { isRaw, isSubquery, Raw } from './RawQueryFragment.js';
7
+ import { MetadataError, ValidationError } from '../errors.js';
8
8
  /** @internal */
9
9
  export class QueryHelper {
10
10
  static SUPPORTED_OPERATORS = ['>', '<', '<=', '>=', '!', '!='];
@@ -102,10 +102,7 @@ export class QueryHelper {
102
102
  for (const k of keys) {
103
103
  const value = where[k];
104
104
  const prop = meta.properties[k];
105
- // Polymorphic relations use multiple columns (discriminator + FK), so they cannot
106
- // participate in the standard single-column FK expansion. Query by discriminator
107
- // column directly instead, e.g. { likeableType: 'post', likeableId: 1 }.
108
- if (!prop || ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) || prop.polymorphic) {
105
+ if (!prop || ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
109
106
  continue;
110
107
  }
111
108
  const op = this.liftGroupOperators(value, prop.targetMeta, metadata, k);
@@ -130,7 +127,7 @@ export class QueryHelper {
130
127
  if (Array.isArray(where)) {
131
128
  where.forEach((item, i) => {
132
129
  if (this.inlinePrimaryKeyObjects(item, meta, metadata, key)) {
133
- where[i] = Utils.getPrimaryKeyValues(item, meta, false);
130
+ where[i] = Utils.getPrimaryKeyValues(item, meta, true);
134
131
  }
135
132
  });
136
133
  }
@@ -160,6 +157,9 @@ export class QueryHelper {
160
157
  Object.keys(where).forEach(k => {
161
158
  const prop = meta.properties[QueryHelper.splitPolymorphicKey(k)[0]];
162
159
  const meta2 = metadata.find(QueryHelper.findTargetMeta(prop, k)?.class) || meta;
160
+ if (prop?.polymorphic) {
161
+ where[k] = this.inlinePolymorphicPrimaryKeys(where[k], meta2, prop);
162
+ }
163
163
  if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
164
164
  // Skip the PK collapse when an owning M:1/1:1 relation's FK column count does not match
165
165
  // the target's PK column count (e.g. FK references target PK + an extra unique column).
@@ -177,6 +177,37 @@ export class QueryHelper {
177
177
  });
178
178
  return false;
179
179
  }
180
+ /**
181
+ * A bare value on a polymorphic relation would be compared to the discriminator column, so PK values are turned
182
+ * into a PK condition instead, e.g. `1` becomes `{ id: 1 }` and `[{ id: 1 }, { id: 2 }]` becomes `{ id: { $in: [1, 2] } }`.
183
+ * Null checks and `[type, id]` tuples keep their meaning.
184
+ */
185
+ static inlinePolymorphicPrimaryKeys(value, meta, prop) {
186
+ const pk = meta.primaryKeys[0];
187
+ const isScalar = (v) => v != null && !Array.isArray(v) && Utils.isPrimaryKey(v);
188
+ const isPrimaryKeyObject = (v) => Utils.isPlainObject(v) && Utils.getObjectKeysSize(v) === 1 && pk in v;
189
+ // lists of plain values are how `[type, id]` tuples get normalized (`{ $in: [type, id] }`), so only PK objects are inlined
190
+ const isList = (v) => Array.isArray(v) && v.length > 0 && v.every(isPrimaryKeyObject);
191
+ let cond;
192
+ if (isScalar(value)) {
193
+ cond = value;
194
+ }
195
+ else if (isList(value)) {
196
+ cond = { $in: value.map(v => v[pk]) };
197
+ }
198
+ else if (Utils.isPlainObject(value) &&
199
+ Utils.hasObjectKeys(value) &&
200
+ Object.entries(value).every(([op, v]) => Utils.isOperator(op) && !(op in GroupOperator) && op !== '$not' && (isScalar(v) || isList(v)))) {
201
+ cond = Object.fromEntries(Object.entries(value).map(([op, v]) => [op, isList(v) ? v.map(i => i[pk]) : v]));
202
+ }
203
+ else {
204
+ return value;
205
+ }
206
+ if (meta.compositePK) {
207
+ throw new ValidationError(`Polymorphic relation ${prop.name} targets entities with a composite primary key, use PK objects or entity references in the condition instead.`);
208
+ }
209
+ return { [pk]: cond };
210
+ }
180
211
  /** Copies the plain object and array structure of the condition, keeping the leaf values (e.g. entities) by reference. */
181
212
  static cloneWhere(where) {
182
213
  if (Array.isArray(where)) {
@@ -231,8 +262,10 @@ export class QueryHelper {
231
262
  if (!Utils.isPlainObject(where)) {
232
263
  return where;
233
264
  }
234
- if (meta && options.type !== 'orderBy') {
235
- where = QueryHelper.splitPolymorphicConditions(meta, where);
265
+ if (meta) {
266
+ where = (options.type === 'orderBy'
267
+ ? QueryHelper.splitPolymorphicOrderBy(meta, where)
268
+ : QueryHelper.splitPolymorphicConditions(meta, where));
236
269
  }
237
270
  return Utils.getObjectQueryKeys(where).reduce((o, key) => {
238
271
  let value = where[key];
@@ -280,6 +313,13 @@ export class QueryHelper {
280
313
  o[key] = { [op]: value };
281
314
  return o;
282
315
  }
316
+ // `$all` items are matched like separate `$some` payloads, so they need the same processing
317
+ if (key === '$all' && Array.isArray(value)) {
318
+ o[key] = value.map(item => Utils.isPlainObject(item)
319
+ ? QueryHelper.processWhere({ ...options, where: item, entityName, root: false })
320
+ : item);
321
+ return o;
322
+ }
283
323
  if (Utils.isPlainObject(value)) {
284
324
  const targetMeta = customExpression ? undefined : QueryHelper.findTargetMeta(prop, key);
285
325
  o[key] = QueryHelper.processWhere({
@@ -297,13 +337,18 @@ export class QueryHelper {
297
337
  }
298
338
  /** @internal Splits a polymorphic target key like `imageable[Article]` into the property and target class names. */
299
339
  static splitPolymorphicKey(key) {
300
- const match = /^(\w+)\[(\w+)]$/.exec(key);
340
+ const match = /^(\w+)\[([\w,]+)]$/.exec(key);
301
341
  return match ? [match[1], match[2]] : [key];
302
342
  }
343
+ /** @internal Resolves the targets of a polymorphic target key like `imageable[Article]` or `imageable[Product,Article]`. */
344
+ static findTargetMetas(prop, key) {
345
+ const [, targetNames] = QueryHelper.splitPolymorphicKey(key);
346
+ const targets = targetNames?.split(',').map(name => prop?.polymorphTargets?.find(t => t.className === name));
347
+ return targets?.every(Boolean) ? targets : [];
348
+ }
303
349
  /** @internal Resolves the target entity of a condition key, including polymorphic target keys like `imageable[Article]`. */
304
350
  static findTargetMeta(prop, key) {
305
- const [, targetName] = QueryHelper.splitPolymorphicKey(key);
306
- return targetName ? prop?.polymorphTargets?.find(t => t.className === targetName) : prop?.targetMeta;
351
+ return QueryHelper.splitPolymorphicKey(key)[1] ? QueryHelper.findTargetMetas(prop, key)[0] : prop?.targetMeta;
307
352
  }
308
353
  /**
309
354
  * Routes conditions on polymorphic to-one relations to the targets that define all the queried properties,
@@ -342,6 +387,34 @@ export class QueryHelper {
342
387
  }
343
388
  return ret;
344
389
  }
390
+ /**
391
+ * Routes ordering by properties of polymorphic to-one relations to the targets that define them, e.g.
392
+ * `{ imageable: { name: 'asc' } }` becomes `{ 'imageable[Product,Article]': { name: 'asc' } }`, ordered by
393
+ * the first non-null value. Nested relations and formulas are ordered via the first target defining them.
394
+ */
395
+ static splitPolymorphicOrderBy(meta, orderBy) {
396
+ const ret = {};
397
+ for (const key of Utils.getObjectQueryKeys(orderBy)) {
398
+ const prop = meta.properties[key];
399
+ const value = orderBy[key];
400
+ if (!prop?.polymorphTargets ||
401
+ ![ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) ||
402
+ !Utils.isPlainObject(value)) {
403
+ ret[key] = value;
404
+ continue;
405
+ }
406
+ for (const field of Utils.getObjectQueryKeys(value)) {
407
+ const found = prop.polymorphTargets.filter(target => field in target.properties);
408
+ const sharedPK = found.length === prop.polymorphTargets.length && found.every(target => target.primaryKeys.includes(field));
409
+ const single = Utils.isPlainObject(value[field]) || found.some(target => target.properties[field].formula);
410
+ const targets = found.length === 0 ? [prop.targetMeta] : single ? [found[0]] : found;
411
+ // shared PKs are ordered by the FK column without a join
412
+ const targetKey = sharedPK ? key : `${key}[${targets.map(target => target.className).join(',')}]`;
413
+ ret[targetKey] = { ...ret[targetKey], [field]: value[field] };
414
+ }
415
+ }
416
+ return ret;
417
+ }
345
418
  /** Property names used in a condition, including those nested in group operators. */
346
419
  static collectConditionKeys(payload) {
347
420
  return Object.keys(payload).flatMap(k => {
@@ -480,7 +553,8 @@ export class QueryHelper {
480
553
  if (Array.isArray(cond) && !(key && ARRAY_OPERATORS.includes(key))) {
481
554
  return cond.map(v => QueryHelper.processCustomType(prop, v, platform, key, fromQuery));
482
555
  }
483
- if (isRaw(cond)) {
556
+ // a query builder renders as a sub-query, not as a value of the property type
557
+ if (isRaw(cond) || isSubquery(cond)) {
484
558
  return cond;
485
559
  }
486
560
  return prop.customType.convertToDatabaseValue(cond, platform, { fromQuery, key, mode: 'query' });
@@ -555,6 +629,9 @@ export class QueryHelper {
555
629
  if (expanded !== w) {
556
630
  where[k] = expanded;
557
631
  }
632
+ else if (Array.isArray(w)) {
633
+ where[k] = w.map(expandItem);
634
+ }
558
635
  else if (Utils.isPlainObject(w)) {
559
636
  for (const op of Object.keys(w)) {
560
637
  if (!Utils.isOperator(op, false)) {
@@ -1,11 +1,15 @@
1
- import type { AnyString, Dictionary, EntityKey } from '../typings.js';
1
+ import type { AnyString, Dictionary, EntityKey, Subquery } from '../typings.js';
2
2
  declare const rawFragmentSymbolBrand: unique symbol;
3
3
  /** Branded symbol type used as a unique key for tracking raw SQL fragments in object properties. */
4
4
  export type RawQueryFragmentSymbol = symbol & {
5
5
  readonly [rawFragmentSymbolBrand]: true;
6
6
  };
7
+ /** Prototype brand of query builders usable as sub-queries; a symbol so JSON payloads cannot carry it, `Symbol.for` so package copies agree. */
8
+ export declare const SUBQUERY_BRAND: unique symbol;
7
9
  /** Checks whether the given value is a `RawQueryFragment` instance. */
8
10
  export declare function isRaw(value: unknown): value is RawQueryFragment;
11
+ /** Checks whether the given value is a query builder usable as a sub-query (its prototype carries the `SUBQUERY_BRAND`). */
12
+ export declare function isSubquery(value: unknown): value is Subquery;
9
13
  /** Represents a raw SQL fragment with optional parameters, usable as both a value and an object key via Symbol coercion. */
10
14
  export declare class RawQueryFragment<Alias extends string = string> {
11
15
  #private;
@@ -1,11 +1,6 @@
1
1
  import { Utils } from './Utils.js';
2
- // Brand lives on the prototype so JSON payloads — whose proto is
3
- // `Object.prototype` — cannot forge it. String key, not a `Symbol.for(...)`,
4
- // so each CJS/ESM copy of this module independently installs it on its own
5
- // prototype without publishing a global key for the marker that controls raw
6
- // SQL assembly. The string is namespaced so it does not collide with property
7
- // names users might independently install on their own prototypes.
8
- const RAW_FRAGMENT_BRAND = '__mikroOrmRawFragment';
2
+ // Prototype brand of raw fragments; `Symbol.for` so package copies agree.
3
+ const RAW_FRAGMENT_BRAND = Symbol.for('mikro-orm.raw-fragment');
9
4
  // Back-references from a fragment's symbol key to the fragment itself, shared
10
5
  // across CJS/ESM module copies via globalThis: when one copy creates a fragment
11
6
  // via `raw('…')` and stores its symbol in a where-clause object key, the other
@@ -13,6 +8,20 @@ const RAW_FRAGMENT_BRAND = '__mikroOrmRawFragment';
13
8
  const REGISTRY_KEY = Symbol.for('@mikro-orm/core/RawQueryFragment.references');
14
9
  const rawQueryReferences = (globalThis[REGISTRY_KEY] ??=
15
10
  new WeakMap());
11
+ /** Prototype brand of query builders usable as sub-queries; a symbol so JSON payloads cannot carry it, `Symbol.for` so package copies agree. */
12
+ export const SUBQUERY_BRAND = Symbol.for('mikro-orm.subquery');
13
+ function hasPrototypeBrand(value, brand) {
14
+ // Walk the prototype chain starting from the *prototype* (not the value) so
15
+ // own-property spoofing from JSON payloads cannot forge the brand. Stop at
16
+ // `Object.prototype`, which is never branded — that bails plain objects,
17
+ // JSON payloads, and built-ins like `Date`/`Array`/`Map` at depth 1.
18
+ for (let p = Object.getPrototypeOf(value); p != null && p !== Object.prototype; p = Object.getPrototypeOf(p)) {
19
+ if (Object.hasOwn(p, brand)) {
20
+ return true;
21
+ }
22
+ }
23
+ return false;
24
+ }
16
25
  /** Checks whether the given value is a `RawQueryFragment` instance. */
17
26
  export function isRaw(value) {
18
27
  if (value == null || typeof value !== 'object') {
@@ -23,16 +32,11 @@ export function isRaw(value) {
23
32
  if (value instanceof RawQueryFragment) {
24
33
  return true;
25
34
  }
26
- // Walk the prototype chain starting from the *prototype* (not the value) so
27
- // own-property spoofing from JSON payloads cannot forge the brand. Stop at
28
- // `Object.prototype`, which is never branded — that bails plain objects,
29
- // JSON payloads, and built-ins like `Date`/`Array`/`Map` at depth 1.
30
- for (let p = Object.getPrototypeOf(value); p != null && p !== Object.prototype; p = Object.getPrototypeOf(p)) {
31
- if (Object.hasOwn(p, RAW_FRAGMENT_BRAND)) {
32
- return true;
33
- }
34
- }
35
- return false;
35
+ return hasPrototypeBrand(value, RAW_FRAGMENT_BRAND);
36
+ }
37
+ /** Checks whether the given value is a query builder usable as a sub-query (its prototype carries the `SUBQUERY_BRAND`). */
38
+ export function isSubquery(value) {
39
+ return value != null && typeof value === 'object' && hasPrototypeBrand(value, SUBQUERY_BRAND);
36
40
  }
37
41
  /** Represents a raw SQL fragment with optional parameters, usable as both a value and an object key via Symbol coercion. */
38
42
  export class RawQueryFragment {
@@ -121,6 +125,10 @@ export { RawQueryFragment as Raw };
121
125
  export const ALIAS_REPLACEMENT = '[::alias::]';
122
126
  /** @internal */
123
127
  export const ALIAS_REPLACEMENT_RE = '\\[::alias::\\]';
128
+ // a query builder param is embedded as a sub-query instead of being bound as a value
129
+ function toRawParam(param) {
130
+ return isSubquery(param) ? param.toRaw() : param;
131
+ }
124
132
  /**
125
133
  * Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented
126
134
  * by `RawQueryFragment` class instance that can be serialized to a string, so it can be used both as an object value
@@ -204,12 +212,12 @@ export function raw(sql, params) {
204
212
  if (!Object.hasOwn(dict, key)) {
205
213
  return match;
206
214
  }
207
- objectParams.push(dict[key]);
215
+ objectParams.push(toRawParam(dict[key]));
208
216
  return identifier ? '??' : '?';
209
217
  });
210
218
  return new RawQueryFragment(sql, objectParams);
211
219
  }
212
- return new RawQueryFragment(sql, params);
220
+ return new RawQueryFragment(sql, params?.map(toRawParam));
213
221
  }
214
222
  /**
215
223
  * Alternative to the `raw()` helper allowing to use it as a tagged template function for the simple cases.
@@ -189,6 +189,10 @@ export class TransactionManager {
189
189
  if (wrapped.__originalEntityData) {
190
190
  parentWrapped.__originalEntityData = originalEntityData;
191
191
  }
192
+ // index a merged `targetKey` value, `getByKey` ignores the old one as stale
193
+ if (meta.root.targetKeys) {
194
+ parentUoW.getIdentityMap().store(parentEntity);
195
+ }
192
196
  }
193
197
  else {
194
198
  parentUoW.merge(entity, new Set([entity]));
package/utils/Utils.d.ts CHANGED
@@ -175,6 +175,8 @@ export declare class Utils {
175
175
  static getORMVersion(): string;
176
176
  static createFunction(context: Map<string, any>, code: string, compiledFunctions?: CompiledFunctions, key?: string): any;
177
177
  static callCompiledFunction<T extends unknown[], R>(fn: (...args: T) => R, ...args: T): R;
178
+ /** @internal Same-named properties of polymorphic embeddable variants conflict when their kinds differ, to-one kinds share the column. */
179
+ static isSameKind(a: EntityProperty, b: EntityProperty): boolean;
178
180
  static unwrapProperty<T>(entity: T, meta: EntityMetadata<T>, prop: EntityProperty<T>, payload?: boolean): [unknown, number[]][];
179
181
  static setPayloadProperty<T>(entity: EntityDictionary<T>, meta: EntityMetadata<T>, prop: EntityProperty<T>, value: unknown, idx: number[]): void;
180
182
  static tryImport<T extends Dictionary = any>({ module, warning, }: {
package/utils/Utils.js CHANGED
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
153
153
  /** Collection of general-purpose utility methods used throughout the ORM. */
154
154
  export class Utils {
155
155
  static PK_SEPARATOR = '~~~';
156
- static #ORM_VERSION = '7.3.0-dev.2';
156
+ static #ORM_VERSION = '7.3.0-dev.21';
157
157
  /** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
158
158
  static getRlsSettingName(filterName, argName) {
159
159
  return `mikro.${filterName}.${argName}`;
@@ -244,7 +244,7 @@ export class Utils {
244
244
  /**
245
245
  * Merges all sources into the target recursively.
246
246
  */
247
- static _merge(target, sources, ignoreUndefined) {
247
+ static _merge(target, sources, ignoreUndefined, copies) {
248
248
  if (!sources.length) {
249
249
  return target;
250
250
  }
@@ -259,7 +259,13 @@ export class Utils {
259
259
  }
260
260
  if (Utils.isPlainObject(value)) {
261
261
  if (!Utils.isObject(target[key])) {
262
- target[key] = Utils.copy(value);
262
+ // copy plain objects but keep class instances by reference, tracking copies so cycles terminate
263
+ const seen = copies ?? new WeakMap();
264
+ if (!seen.has(value)) {
265
+ seen.set(value, {});
266
+ Utils._merge(seen.get(value), [value], ignoreUndefined, seen);
267
+ }
268
+ target[key] = seen.get(value);
263
269
  continue;
264
270
  }
265
271
  /* v8 ignore next */
@@ -651,14 +657,19 @@ export class Utils {
651
657
  static isCollection(item) {
652
658
  return Collection.isCollection(item);
653
659
  }
654
- // FNV-1a 64-bit
660
+ // FNV-1a 64-bit, computed on two uint32 halves instead of a BigInt. The prime is 2^40 + 0x1b3, so
661
+ // `h * prime mod 2^64` is `h * 0x1b3` plus the low half shifted up by 40 bits. Every partial product
662
+ // stays below 2^41, so the double arithmetic is exact.
655
663
  static hash(data, length) {
656
- let h1 = 0xcbf29ce484222325n;
664
+ let hi = 0xcbf29ce4;
665
+ let lo = 0x84222325;
657
666
  for (let i = 0; i < data.length; i++) {
658
- h1 ^= BigInt(data.charCodeAt(i));
659
- h1 = (h1 * 0x100000001b3n) & 0xffffffffffffffffn;
667
+ lo = (lo ^ data.charCodeAt(i)) >>> 0;
668
+ const loProduct = lo * 0x1b3;
669
+ hi = (((hi * 0x1b3) >>> 0) + Math.floor(loProduct / 0x100000000) + ((lo << 8) >>> 0)) >>> 0;
670
+ lo = loProduct >>> 0;
660
671
  }
661
- const hash = h1.toString(16).padStart(16, '0');
672
+ const hash = hi.toString(16).padStart(8, '0') + lo.toString(16).padStart(8, '0');
662
673
  if (length) {
663
674
  return hash.substring(0, length);
664
675
  }
@@ -775,6 +786,11 @@ export class Utils {
775
786
  throw e;
776
787
  }
777
788
  }
789
+ /** @internal Same-named properties of polymorphic embeddable variants conflict when their kinds differ, to-one kinds share the column. */
790
+ static isSameKind(a, b) {
791
+ const toOne = (p) => p.kind === ReferenceKind.MANY_TO_ONE || p.kind === ReferenceKind.ONE_TO_ONE;
792
+ return a.kind === b.kind || (toOne(a) && toOne(b));
793
+ }
778
794
  static unwrapProperty(entity, meta, prop, payload = false) {
779
795
  let p = prop;
780
796
  const path = [];
package/utils/clone.js CHANGED
@@ -22,8 +22,8 @@ function getPropertyDescriptor(obj, prop) {
22
22
  }
23
23
  const TypedArray = Object.getPrototypeOf(Uint8Array);
24
24
  export function clone(parent, respectCustomCloneMethod = true) {
25
- const allParents = [];
26
- const allChildren = [];
25
+ // already cloned objects, so shared references and cycles map to a single clone
26
+ const clones = new Map();
27
27
  function _clone(parent) {
28
28
  // cloning null always returns null
29
29
  if (parent === null) {
@@ -92,12 +92,10 @@ export function clone(parent, respectCustomCloneMethod = true) {
92
92
  proto = Object.getPrototypeOf(parent);
93
93
  child = Object.create(proto);
94
94
  }
95
- const index = allParents.indexOf(parent);
96
- if (index !== -1) {
97
- return allChildren[index];
95
+ if (clones.has(parent)) {
96
+ return clones.get(parent);
98
97
  }
99
- allParents.push(parent);
100
- allChildren.push(child);
98
+ clones.set(parent, child);
101
99
  if (parent instanceof Map) {
102
100
  parent.forEach((value, key) => {
103
101
  const keyChild = _clone(key);
package/utils/env-vars.js CHANGED
@@ -74,6 +74,8 @@ export function loadEnvironmentVars() {
74
74
  read2('transactional', bool);
75
75
  read2('disableForeignKeys', bool);
76
76
  read2('allOrNothing', bool);
77
+ read2('lock', bool);
78
+ read2('lockTimeout', num);
77
79
  read2('dropTables', bool);
78
80
  read2('safe', bool);
79
81
  read2('silent', bool);
package/utils/fs-utils.js CHANGED
@@ -5,7 +5,8 @@ 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 });
8
+ const cwd = options?.dir ? join(options.cwd ?? '', options.dir) : options?.cwd;
9
+ const files = nodeGlobSync(patterns, { cwd, withFileTypes: true });
9
10
  return files.filter(f => f.isFile()).map(f => join(f.parentPath, f.name));
10
11
  };
11
12
  export const fs = {
@@ -13,7 +14,8 @@ export const fs = {
13
14
  const tinyGlobby = await import('tinyglobby').catch(() => null);
14
15
  if (tinyGlobby) {
15
16
  globSync = (patterns, options) => {
16
- patterns = Utils.asArray(patterns).map(p => p.replace(/\\/g, '/'));
17
+ const dir = options?.dir ? tinyGlobby.escapePath(options.dir.replace(/\\/g, '/')) + '/' : '';
18
+ patterns = Utils.asArray(patterns).map(p => dir + p.replace(/\\/g, '/'));
17
19
  // never forward `cwd: undefined` — tinyglobby >= 0.2.16 calls `path.resolve(undefined)` and throws
18
20
  return tinyGlobby.globSync(patterns, {
19
21
  expandDirectories: false,
@@ -23,10 +25,10 @@ export const fs = {
23
25
  }
24
26
  },
25
27
  pathExists(path) {
26
- if (/[*?[\]]/.test(path)) {
27
- return globSync(path).length > 0;
28
+ if (existsSync(path)) {
29
+ return true;
28
30
  }
29
- return existsSync(path);
31
+ return /[*?[\]]/.test(path) && this.resolveGlob(path).length > 0;
30
32
  },
31
33
  ensureDir(path) {
32
34
  if (!existsSync(path)) {
@@ -62,18 +64,20 @@ export const fs = {
62
64
  if (Array.isArray(input)) {
63
65
  return input.flatMap(paths => this.resolveGlob(paths, cwd));
64
66
  }
65
- const hasGlobChars = /[*?[\]]/.test(input);
66
- if (!hasGlobChars) {
67
- try {
68
- const s = statSync(cwd ? this.normalizePath(cwd, input) : input);
69
- if (s.isDirectory()) {
70
- return globSync(join(input, '**'), { cwd });
71
- }
72
- }
73
- catch {
74
- // ignore
67
+ // an absolute path repeats the cwd, which may contain glob characters (e.g. `~/Dropbox (Team)/app`)
68
+ if (isAbsolute(input)) {
69
+ cwd ??= process.cwd();
70
+ input = relative(cwd, input).replace(/\\/g, '/') || '.';
71
+ }
72
+ try {
73
+ const s = statSync(cwd ? this.normalizePath(cwd, input) : input);
74
+ if (s.isDirectory()) {
75
+ return globSync('**', { cwd, dir: input });
75
76
  }
76
77
  }
78
+ catch {
79
+ // ignore
80
+ }
77
81
  return globSync(input, { cwd });
78
82
  },
79
83
  getPackageConfig(basePath = process.cwd()) {
@@ -103,7 +103,8 @@ export function getOnConflictReturningFields(meta, data, uniqueFields, options,
103
103
  if (p.lazy || p.embeddable) {
104
104
  return false;
105
105
  }
106
- if (p.autoincrement) {
106
+ // a PK missing in the data is generated by the database
107
+ if (p.autoincrement || (p.primary && data[p.name] == null)) {
107
108
  return true;
108
109
  }
109
110
  // for a raw `onConflictFields` fragment we can't introspect which columns form the conflict