@mikro-orm/core 7.1.0-dev.10 → 7.1.0-dev.11
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.
- package/entity/BaseEntity.d.ts +68 -1
- package/entity/BaseEntity.js +18 -0
- package/entity/Reference.d.ts +42 -1
- package/entity/Reference.js +9 -0
- package/entity/defineEntity.d.ts +42 -6
- package/entity/defineEntity.js +10 -0
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/typings.d.ts +44 -8
- package/utils/Utils.js +1 -1
package/entity/BaseEntity.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Ref } from './Reference.js';
|
|
1
|
+
import { type LoadReferenceOptions, type LoadReferenceOrFailOptions, type Ref } from './Reference.js';
|
|
2
2
|
import type { AutoPath, EntityData, EntityDTO, ExtractFieldsHint, Loaded, LoadedReference, AddEager, EntityKey, FromEntityType, IsSubset, MergeSelected, ResolveSerializeFields, SerializeDTO, SerializeFieldsKeepPK } from '../typings.js';
|
|
3
3
|
import { type AssignOptions } from './EntityAssigner.js';
|
|
4
4
|
import type { EntityLoaderOptions } from './EntityLoader.js';
|
|
@@ -90,3 +90,70 @@ export declare abstract class BaseEntity {
|
|
|
90
90
|
/** Sets the database schema for this entity. */
|
|
91
91
|
setSchema(schema?: string): void;
|
|
92
92
|
}
|
|
93
|
+
type EntityConstructor<T extends object = object> = abstract new (...args: any[]) => T;
|
|
94
|
+
/**
|
|
95
|
+
* The `load()` / `loadOrFail()` methods added by the {@link Loadable} mixin. Declared as an interface so the
|
|
96
|
+
* mixin function can have an explicit return type (required by JSR fast-check).
|
|
97
|
+
*/
|
|
98
|
+
export interface LoadableEntity {
|
|
99
|
+
/**
|
|
100
|
+
* Ensures this entity is loaded (without reloading it if it already is). Returns the entity, or `null` if it
|
|
101
|
+
* was not found in the database (e.g. it was deleted in the meantime, or active filters disallow loading it).
|
|
102
|
+
* Use `loadOrFail()` if you want an error to be thrown in such a case.
|
|
103
|
+
*/
|
|
104
|
+
load<Entity extends this = this, Hint extends string = never, Fields extends string = never, Excludes extends string = never>(options?: LoadReferenceOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
|
|
105
|
+
/**
|
|
106
|
+
* Ensures this entity is loaded (without reloading it if it already is). Returns the entity, or throws an error
|
|
107
|
+
* just like `em.findOneOrFail()` (and respects the same config options) if it was not found.
|
|
108
|
+
*/
|
|
109
|
+
loadOrFail<Entity extends this = this, Hint extends string = never, Fields extends string = never, Excludes extends string = never>(options?: LoadReferenceOrFailOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes>>;
|
|
110
|
+
}
|
|
111
|
+
/** Return-type shape of {@link Loadable} — a constructor that produces instances of `TBase` enriched with {@link LoadableEntity}. */
|
|
112
|
+
export type LoadableConstructor<TBase extends EntityConstructor> = abstract new (...args: any[]) => InstanceType<TBase> & LoadableEntity;
|
|
113
|
+
/** Internal: rejects a base class that already defines `load` or `loadOrFail` to prevent silent override. */
|
|
114
|
+
type EnsureNoLoadConflict<TBase extends EntityConstructor> = InstanceType<TBase> extends {
|
|
115
|
+
load: any;
|
|
116
|
+
} ? 'Loadable: base class already defines `load` — remove it or do not apply the mixin' : InstanceType<TBase> extends {
|
|
117
|
+
loadOrFail: any;
|
|
118
|
+
} ? 'Loadable: base class already defines `loadOrFail` — remove it or do not apply the mixin' : TBase;
|
|
119
|
+
/** Empty base for {@link Loadable} when called without arguments — standalone mixin, no inherited base. */
|
|
120
|
+
declare abstract class EmptyBase {
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Mixin that adds `load()` / `loadOrFail()` methods to an entity class. These methods ensure the entity is loaded
|
|
124
|
+
* from the database without reloading it if it already is — unlike `init()`, which always refreshes.
|
|
125
|
+
*
|
|
126
|
+
* Useful when migrating from a non-`Ref`-based codebase where lazy loading support is desired without the
|
|
127
|
+
* `.$` / `.get()` indirection that the `Reference` wrapper requires. Opt-in so it does not conflict with entities
|
|
128
|
+
* that already define a `load` or `loadOrFail` property — applying the mixin to a base class that already has
|
|
129
|
+
* either method is a compile error to prevent silent override.
|
|
130
|
+
*
|
|
131
|
+
* Call without arguments (`Loadable()`) for a standalone base with no other inheritance, or pass a base class
|
|
132
|
+
* (`Loadable(BaseEntity)`) to compose. The convenience alias {@link LoadableBaseEntity} is shorthand for the
|
|
133
|
+
* latter.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* // compose with BaseEntity
|
|
138
|
+
* class User extends Loadable(BaseEntity) {
|
|
139
|
+
* @PrimaryKey()
|
|
140
|
+
* id!: number;
|
|
141
|
+
* }
|
|
142
|
+
*
|
|
143
|
+
* // standalone — no inherited base
|
|
144
|
+
* class Product extends Loadable() {
|
|
145
|
+
* @PrimaryKey()
|
|
146
|
+
* id!: number;
|
|
147
|
+
* }
|
|
148
|
+
*
|
|
149
|
+
* const user = orm.em.getReference(User, 1);
|
|
150
|
+
* await user.load();
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
export declare function Loadable(): LoadableConstructor<typeof EmptyBase> & typeof EmptyBase;
|
|
154
|
+
export declare function Loadable<TBase extends EntityConstructor>(Base: EnsureNoLoadConflict<TBase> extends TBase ? TBase : never): LoadableConstructor<TBase> & TBase;
|
|
155
|
+
declare const LoadableBaseEntityBase: LoadableConstructor<typeof BaseEntity> & typeof BaseEntity;
|
|
156
|
+
/** Convenience: `BaseEntity` pre-composed with the `Loadable` mixin. */
|
|
157
|
+
export declare abstract class LoadableBaseEntity extends LoadableBaseEntityBase {
|
|
158
|
+
}
|
|
159
|
+
export {};
|
package/entity/BaseEntity.js
CHANGED
|
@@ -49,3 +49,21 @@ export class BaseEntity {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
Object.defineProperty(BaseEntity.prototype, '__baseEntity', { value: true, writable: false, enumerable: false });
|
|
52
|
+
/** Empty base for {@link Loadable} when called without arguments — standalone mixin, no inherited base. */
|
|
53
|
+
class EmptyBase {
|
|
54
|
+
}
|
|
55
|
+
export function Loadable(Base = EmptyBase) {
|
|
56
|
+
class LoadableMixin extends Base {
|
|
57
|
+
async load(options) {
|
|
58
|
+
return Reference.create(this).load(options);
|
|
59
|
+
}
|
|
60
|
+
async loadOrFail(options) {
|
|
61
|
+
return Reference.create(this).loadOrFail(options);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return LoadableMixin;
|
|
65
|
+
}
|
|
66
|
+
const LoadableBaseEntityBase = Loadable(BaseEntity);
|
|
67
|
+
/** Convenience: `BaseEntity` pre-composed with the `Loadable` mixin. */
|
|
68
|
+
export class LoadableBaseEntity extends LoadableBaseEntityBase {
|
|
69
|
+
}
|
package/entity/Reference.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AddEager, AddOptional, Dictionary, EntityClass, EntityKey, EntityProperty, Loaded, LoadedReference, Primary, Ref } from '../typings.js';
|
|
1
|
+
import type { AddEager, AddOptional, Dictionary, EntityClass, EntityKey, EntityProperty, LazyRef, Loaded, LoadedReference, Primary, Ref } from '../typings.js';
|
|
2
2
|
import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver.js';
|
|
3
3
|
/** Wrapper around an entity that provides lazy loading capabilities and identity-preserving reference semantics. */
|
|
4
4
|
export declare class Reference<T extends object> {
|
|
@@ -98,6 +98,47 @@ export declare function ref<I extends unknown | Ref<unknown> | undefined | null,
|
|
|
98
98
|
* shortcut for `Reference.createFromPK(entityType, pk)`
|
|
99
99
|
*/
|
|
100
100
|
export declare function ref<I extends unknown | undefined | null, T, PKV extends Primary<T> = Primary<T>>(entityType: EntityClass<T>, pk: I): Ref<T> | AddOptional<typeof pk>;
|
|
101
|
+
/**
|
|
102
|
+
* Unwraps a reference to its underlying value, returning the entity or scalar. Works on any of:
|
|
103
|
+
*
|
|
104
|
+
* - `Ref<T>` / `Reference<T>` (entity) — calls `.unwrap()` to extract the wrapped entity, returns `T`.
|
|
105
|
+
* - `LazyRef<T>` — type-only marker; runtime value is already `T`, so returned as-is.
|
|
106
|
+
* - plain entity `T` — returned as-is.
|
|
107
|
+
* - `ScalarReference<V>` / `ScalarRef<V>` — calls `.unwrap()` to extract the scalar value, returns `V | undefined`
|
|
108
|
+
* (a scalar reference may be bound without an initial value).
|
|
109
|
+
*
|
|
110
|
+
* Inverse of the {@link ref} helper. Use as a typed escape hatch when you know a relation is populated
|
|
111
|
+
* but don't want to (or can't) thread `Loaded<T, 'relation'>` through a function signature.
|
|
112
|
+
*
|
|
113
|
+
* ```ts
|
|
114
|
+
* function logAuthor(book: Book) {
|
|
115
|
+
* // `book.author` is typed as `LazyRef<Author>`, so `.name` is not directly accessible
|
|
116
|
+
* console.log(unref(book.author).name);
|
|
117
|
+
* }
|
|
118
|
+
*
|
|
119
|
+
* // Scalar reference:
|
|
120
|
+
* const scalar = ref('hello');
|
|
121
|
+
* unref(scalar); // 'hello' (type: string | undefined)
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* Note: `unref` is a compile-time narrowing only for entity refs. On a `LazyRef<T>` or plain entity
|
|
125
|
+
* relation that is a stub (PK only, never populated), reads of non-PK properties will still return
|
|
126
|
+
* `undefined` at runtime — same footgun as a plain non-`Ref` relation.
|
|
127
|
+
*/
|
|
128
|
+
export declare function unref<V>(value: ScalarReference<V>): V | undefined;
|
|
129
|
+
export declare function unref<T extends object>(value: Ref<T> | LazyRef<T> | T): T;
|
|
130
|
+
/**
|
|
131
|
+
* Unwraps a reference to its underlying value. Nullable variant — returns `null` unchanged.
|
|
132
|
+
*/
|
|
133
|
+
export declare function unref<T extends object>(value: Ref<T> | LazyRef<T> | T | null): T | null;
|
|
134
|
+
/**
|
|
135
|
+
* Unwraps a reference to its underlying value. Optional variant — returns `undefined` unchanged.
|
|
136
|
+
*/
|
|
137
|
+
export declare function unref<T extends object>(value: Ref<T> | LazyRef<T> | T | undefined): T | undefined;
|
|
138
|
+
/**
|
|
139
|
+
* Unwraps a reference to its underlying value. Nullable variant — returns `null`/`undefined` unchanged.
|
|
140
|
+
*/
|
|
141
|
+
export declare function unref<T extends object>(value: Ref<T> | LazyRef<T> | T | null | undefined): T | null | undefined;
|
|
101
142
|
/**
|
|
102
143
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
103
144
|
*/
|
package/entity/Reference.js
CHANGED
|
@@ -303,6 +303,15 @@ export function ref(entityOrType, pk) {
|
|
|
303
303
|
}
|
|
304
304
|
return Reference.createFromPK(entityOrType, pk);
|
|
305
305
|
}
|
|
306
|
+
export function unref(value) {
|
|
307
|
+
if (value == null) {
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
if (ScalarReference.isScalarReference(value)) {
|
|
311
|
+
return value.unwrap();
|
|
312
|
+
}
|
|
313
|
+
return Reference.unwrapReference(value);
|
|
314
|
+
}
|
|
306
315
|
/**
|
|
307
316
|
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
308
317
|
*/
|
package/entity/defineEntity.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { EntityManager } from '../EntityManager.js';
|
|
2
2
|
import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions } from '../metadata/types.js';
|
|
3
|
-
import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints } from '../typings.js';
|
|
3
|
+
import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, LazyRef, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints } from '../typings.js';
|
|
4
4
|
import type { Raw } from '../utils/RawQueryFragment.js';
|
|
5
5
|
import type { ScalarReference } from './Reference.js';
|
|
6
6
|
import type { SerializeOptions } from '../serialization/EntitySerializer.js';
|
|
@@ -12,7 +12,7 @@ import type { Collection } from './Collection.js';
|
|
|
12
12
|
import type { FilterOptions, FindOptions, FindOneOptions } from '../drivers/IDatabaseDriver.js';
|
|
13
13
|
/** Union of all option keys supported across all property definition types (scalar, enum, embedded, relations). */
|
|
14
14
|
export type UniversalPropertyKeys = keyof PropertyOptions<any> | keyof EnumOptions<any> | keyof EmbeddedOptions<any, any> | keyof ReferenceOptions<any, any> | keyof ManyToOneOptions<any, any> | keyof OneToManyOptions<any, any> | keyof OneToOneOptions<any, any> | keyof ManyToManyOptions<any, any>;
|
|
15
|
-
type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable';
|
|
15
|
+
type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable' | 'lazyRef';
|
|
16
16
|
type ExcludeKeys = 'entity' | 'items';
|
|
17
17
|
type BuilderKeys = Exclude<UniversalPropertyKeys, ExcludeKeys> | BuilderExtraKeys;
|
|
18
18
|
type IncludeKeysForProperty = Exclude<keyof PropertyOptions<any>, ExcludeKeys> | BuilderExtraKeys;
|
|
@@ -36,9 +36,23 @@ export interface PropertyChain<Value, Options> {
|
|
|
36
36
|
nullable: true;
|
|
37
37
|
strictNullable: true;
|
|
38
38
|
}>;
|
|
39
|
-
ref():
|
|
39
|
+
ref(): Options extends {
|
|
40
|
+
lazyRef: true;
|
|
41
|
+
} ? never : PropertyChain<Value, Omit<Options, 'ref'> & {
|
|
40
42
|
ref: true;
|
|
41
43
|
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — direct entity at runtime, type-restricted
|
|
46
|
+
* until `Loaded<>` narrows it. Type-level only, no `ref: true` wrapping. Not compatible with `.ref()` or
|
|
47
|
+
* `.mapToPk()` — those chains are rejected in both directions at compile time.
|
|
48
|
+
*/
|
|
49
|
+
lazyRef(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
|
|
50
|
+
ref: true;
|
|
51
|
+
} | {
|
|
52
|
+
mapToPk: true;
|
|
53
|
+
} ? never : PropertyChain<Value, Omit<Options, 'lazyRef'> & {
|
|
54
|
+
lazyRef: true;
|
|
55
|
+
}> : never;
|
|
42
56
|
primary(): PropertyChain<Value, Omit<Options, 'primary'> & {
|
|
43
57
|
primary: true;
|
|
44
58
|
}>;
|
|
@@ -125,7 +139,9 @@ export interface PropertyChain<Value, Options> {
|
|
|
125
139
|
owner(): HasKind<Options, '1:1' | 'm:n'> extends true ? PropertyChain<Value, Omit<Options, 'owner'> & {
|
|
126
140
|
owner: true;
|
|
127
141
|
}> : never;
|
|
128
|
-
mapToPk(): HasKind<Options, 'm:1' | '1:1'> extends true ?
|
|
142
|
+
mapToPk(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
|
|
143
|
+
lazyRef: true;
|
|
144
|
+
} ? never : PropertyChain<Value, Omit<Options, 'mapToPk'> & {
|
|
129
145
|
mapToPk: true;
|
|
130
146
|
}> : never;
|
|
131
147
|
orphanRemoval(orphanRemoval?: boolean): HasKind<Options, '1:m' | '1:1'> extends true ? PropertyChain<Value, Options> : never;
|
|
@@ -325,9 +341,24 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
|
|
|
325
341
|
/**
|
|
326
342
|
* Enable `ScalarReference` wrapper for lazy values. Use this in combination with `lazy: true` to have a type-safe accessor object in place of the value.
|
|
327
343
|
*/
|
|
328
|
-
ref():
|
|
344
|
+
ref(): Options extends {
|
|
345
|
+
lazyRef: true;
|
|
346
|
+
} ? never : UniversalPropertyOptionsBuilder<Value, Omit<Options, 'ref'> & {
|
|
329
347
|
ref: true;
|
|
330
348
|
}, IncludeKeys>;
|
|
349
|
+
/**
|
|
350
|
+
* Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — the runtime value stays a plain entity
|
|
351
|
+
* (no `Reference` wrapper) but the TypeScript type restricts access to the primary key until `Loaded<>`
|
|
352
|
+
* narrows it. Type-level only; does not set `ref: true`. Not compatible with `.ref()` or `.mapToPk()` —
|
|
353
|
+
* those chains are rejected in both directions at compile time.
|
|
354
|
+
*/
|
|
355
|
+
lazyRef(): HasKind<Options, 'm:1' | '1:1'> extends true ? Options extends {
|
|
356
|
+
ref: true;
|
|
357
|
+
} | {
|
|
358
|
+
mapToPk: true;
|
|
359
|
+
} ? never : UniversalPropertyOptionsBuilder<Value, Omit<Options, 'lazyRef'> & {
|
|
360
|
+
lazyRef: true;
|
|
361
|
+
}, IncludeKeys> : never;
|
|
331
362
|
/**
|
|
332
363
|
* Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
|
|
333
364
|
*/
|
|
@@ -512,7 +543,9 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
|
|
|
512
543
|
/** What to do when the reference to the target entity gets updated. */
|
|
513
544
|
updateRule(updateRule: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString): Pick<UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys>, IncludeKeys>;
|
|
514
545
|
/** Map this relation to the primary key value instead of an entity. */
|
|
515
|
-
mapToPk():
|
|
546
|
+
mapToPk(): Options extends {
|
|
547
|
+
lazyRef: true;
|
|
548
|
+
} ? never : Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'mapToPk'> & {
|
|
516
549
|
mapToPk: true;
|
|
517
550
|
}, IncludeKeys>, IncludeKeys>;
|
|
518
551
|
/** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */
|
|
@@ -738,6 +771,9 @@ type MaybeNullable<Value, Options> = Options extends {
|
|
|
738
771
|
type MaybeRelationRef<Value, Options> = Options extends {
|
|
739
772
|
mapToPk: true;
|
|
740
773
|
} ? Value : Options extends {
|
|
774
|
+
lazyRef: true;
|
|
775
|
+
kind: '1:1' | 'm:1';
|
|
776
|
+
} ? Value extends object ? LazyRef<Value> : never : Options extends {
|
|
741
777
|
ref: true;
|
|
742
778
|
kind: '1:1';
|
|
743
779
|
} ? Value extends object ? Ref<Value> : never : Options extends {
|
package/entity/defineEntity.js
CHANGED
|
@@ -173,6 +173,16 @@ export class UniversalPropertyOptionsBuilder {
|
|
|
173
173
|
ref() {
|
|
174
174
|
return this.assignOptions({ ref: true });
|
|
175
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Mark a to-one relation (`m:1`/`1:1`) as a {@apilink LazyRef} — the runtime value stays a plain entity
|
|
178
|
+
* (no `Reference` wrapper) but the TypeScript type restricts access to the primary key until `Loaded<>`
|
|
179
|
+
* narrows it. Type-level only; does not set `ref: true`. Not compatible with `.ref()` or `.mapToPk()` —
|
|
180
|
+
* those chains are rejected in both directions at compile time.
|
|
181
|
+
*/
|
|
182
|
+
lazyRef() {
|
|
183
|
+
// purely type-level — no runtime metadata change
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
176
186
|
/**
|
|
177
187
|
* Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}.
|
|
178
188
|
*/
|
package/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @module core
|
|
4
4
|
*/
|
|
5
5
|
export { EntityMetadata, PrimaryKeyProp, EntityRepositoryType, OptionalProps, EagerProps, HiddenProps, Config, EntityName, IndexHints, } from './typings.js';
|
|
6
|
-
export type { CompiledFunctions, Constructor, ConnectionType, Dictionary, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, InferEntityName, EntityData, Highlighter, MaybePromise, AnyEntity, EntityClass, EntityProperty, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator, MigratorEvent, GetRepository, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, EntityDTOFlat, EntityDTOProp, SerializeDTO, MigrationDiff, GenerateOptions, FilterObject, IndexFilterQuery, ExtractIndexHints, IndexName, IndexColumns, WithUsingOptions, IMigrationRunner, IEntityGenerator, ISeedManager, SeederObject, IMigratorStorage, RequiredEntityData, CheckCallback, TriggerCallback, IndexCallback, FormulaCallback, FormulaTable, SchemaTable, SchemaColumns, SimpleColumnMeta, Rel, Ref, ScalarRef, EntityRef, ISchemaGenerator, MigrationInfo, MigrateOptions, MigrationResult, MigrationRow, EntityKey, EntityValue, EntityDataValue, FilterKey, EntityType, FromEntityType, Selected, IsSubset, EntityProps, ExpandProperty, ExpandScalar, FilterItemValue, ExpandQuery, Scalar, ExpandHint, FilterValue, MergeLoaded, MergeSelected, TypeConfig, AnyString, ClearDatabaseOptions, CreateSchemaOptions, EnsureDatabaseOptions, UpdateSchemaOptions, DropSchemaOptions, RefreshDatabaseOptions, AutoPath, UnboxArray, MetadataProcessor, ImportsResolver, RequiredNullable, DefineConfig, Opt, Hidden, EntitySchemaWithMeta, InferEntity, CheckConstraint, TriggerDef, GeneratedColumnCallback, FilterDef, EntityCtor, Subquery, PopulateHintOptions, Prefixes, } from './typings.js';
|
|
6
|
+
export type { CompiledFunctions, Constructor, ConnectionType, Dictionary, Primary, IPrimaryKey, ObjectQuery, FilterQuery, IWrappedEntity, InferEntityName, EntityData, Highlighter, MaybePromise, AnyEntity, EntityClass, EntityProperty, PopulateOptions, Populate, Loaded, New, LoadedReference, LoadedCollection, IMigrator, IMigrationGenerator, MigratorEvent, GetRepository, MigrationObject, DeepPartial, PrimaryProperty, Cast, IsUnknown, EntityDictionary, EntityDTO, EntityDTOFlat, EntityDTOProp, SerializeDTO, MigrationDiff, GenerateOptions, FilterObject, IndexFilterQuery, ExtractIndexHints, IndexName, IndexColumns, WithUsingOptions, IMigrationRunner, IEntityGenerator, ISeedManager, SeederObject, IMigratorStorage, RequiredEntityData, CheckCallback, TriggerCallback, IndexCallback, FormulaCallback, FormulaTable, SchemaTable, SchemaColumns, SimpleColumnMeta, Rel, Ref, LazyRef, ScalarRef, EntityRef, ISchemaGenerator, MigrationInfo, MigrateOptions, MigrationResult, MigrationRow, EntityKey, EntityValue, EntityDataValue, FilterKey, EntityType, FromEntityType, Selected, IsSubset, EntityProps, ExpandProperty, ExpandScalar, FilterItemValue, ExpandQuery, Scalar, ExpandHint, FilterValue, MergeLoaded, MergeSelected, TypeConfig, AnyString, ClearDatabaseOptions, CreateSchemaOptions, EnsureDatabaseOptions, UpdateSchemaOptions, DropSchemaOptions, RefreshDatabaseOptions, AutoPath, UnboxArray, MetadataProcessor, ImportsResolver, RequiredNullable, DefineConfig, Opt, Hidden, EntitySchemaWithMeta, InferEntity, CheckConstraint, TriggerDef, GeneratedColumnCallback, FilterDef, EntityCtor, Subquery, PopulateHintOptions, Prefixes, } from './typings.js';
|
|
7
7
|
export * from './enums.js';
|
|
8
8
|
export * from './errors.js';
|
|
9
9
|
export * from './exceptions.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.1.0-dev.
|
|
3
|
+
"version": "7.1.0-dev.11",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
package/typings.d.ts
CHANGED
|
@@ -113,7 +113,7 @@ type LoadedReferenceShape<T = any> = ReferenceShape & {
|
|
|
113
113
|
* Using this instead of `Loadable<any>` in conditional type checks prevents
|
|
114
114
|
* TypeScript from evaluating the full Collection/Reference interfaces.
|
|
115
115
|
*/
|
|
116
|
-
type LoadableShape = CollectionShape | ReferenceShape | readonly any[];
|
|
116
|
+
type LoadableShape = CollectionShape | ReferenceShape | LazyRef.Brand<any> | readonly any[];
|
|
117
117
|
/** Gets all keys from all members of a union type (distributes over the union). */
|
|
118
118
|
export type UnionKeys<T> = T extends any ? keyof T : never;
|
|
119
119
|
/** Gets the type of a property from all union members that have it (distributes over the union). */
|
|
@@ -467,12 +467,12 @@ type NonArrayObject = object & {
|
|
|
467
467
|
export type EntityDataProp<T, C extends boolean> = T extends Date ? string | Date : T extends Scalar ? T : T extends ScalarReference<infer U> ? EntityDataProp<U, C> : T extends {
|
|
468
468
|
__runtime?: infer Runtime;
|
|
469
469
|
__raw?: infer Raw;
|
|
470
|
-
} ? C extends true ? Raw : Runtime : T extends ReferenceShape<infer U> ? EntityDataNested<U, C> : T extends CollectionShape<infer U> ? U | U[] | EntityDataNested<U & object, C> | EntityDataNested<U & object, C>[] : T extends readonly (infer U)[] ? U extends NonArrayObject ? U | U[] | EntityDataNested<U, C> | EntityDataNested<U, C>[] : U[] | EntityDataNested<U, C>[] : EntityDataNested<T, C>;
|
|
470
|
+
} ? C extends true ? Raw : Runtime : T extends LazyRef.Brand<infer U> ? EntityDataNested<U, C> : T extends ReferenceShape<infer U> ? EntityDataNested<U, C> : T extends CollectionShape<infer U> ? U | U[] | EntityDataNested<U & object, C> | EntityDataNested<U & object, C>[] : T extends readonly (infer U)[] ? U extends NonArrayObject ? U | U[] | EntityDataNested<U, C> | EntityDataNested<U, C>[] : U[] | EntityDataNested<U, C>[] : EntityDataNested<T, C>;
|
|
471
471
|
/** Like `EntityDataProp` but used in `RequiredEntityData` context with required/optional key distinction. */
|
|
472
472
|
export type RequiredEntityDataProp<T, O, C extends boolean> = T extends Date ? string | Date : Exclude<T, null> extends RequiredNullable.Brand ? T | null : T extends Scalar ? T : T extends ScalarReference<infer U> ? RequiredEntityDataProp<U, O, C> : T extends {
|
|
473
473
|
__runtime?: infer Runtime;
|
|
474
474
|
__raw?: infer Raw;
|
|
475
|
-
} ? C extends true ? Raw : Runtime : T extends ReferenceShape<infer U> ? RequiredEntityDataNested<U, O, C> : T extends CollectionShape<infer U> ? U | U[] | RequiredEntityDataNested<U & object, O, C> | RequiredEntityDataNested<U & object, O, C>[] : T extends readonly (infer U)[] ? U extends NonArrayObject ? U | U[] | RequiredEntityDataNested<U, O, C> | RequiredEntityDataNested<U, O, C>[] : U[] | RequiredEntityDataNested<U, O, C>[] : RequiredEntityDataNested<T, O, C>;
|
|
475
|
+
} ? C extends true ? Raw : Runtime : T extends LazyRef.Brand<infer U> ? RequiredEntityDataNested<U, O, C> : T extends ReferenceShape<infer U> ? RequiredEntityDataNested<U, O, C> : T extends CollectionShape<infer U> ? U | U[] | RequiredEntityDataNested<U & object, O, C> | RequiredEntityDataNested<U & object, O, C>[] : T extends readonly (infer U)[] ? U extends NonArrayObject ? U | U[] | RequiredEntityDataNested<U, O, C> | RequiredEntityDataNested<U, O, C>[] : U[] | RequiredEntityDataNested<U, O, C>[] : RequiredEntityDataNested<T, O, C>;
|
|
476
476
|
/** Nested entity data shape for embedded or related entities within `EntityData`. */
|
|
477
477
|
export type EntityDataNested<T, C extends boolean = false> = T extends undefined ? never : T extends any[] ? Readonly<T> : EntityData<T, C> | ExpandEntityProp<T, C>;
|
|
478
478
|
type UnwrapScalarRef<T> = T extends ScalarReference<infer U> ? U : T;
|
|
@@ -521,6 +521,35 @@ type Relation<T> = {
|
|
|
521
521
|
export type Rel<T> = T;
|
|
522
522
|
/** Alias for `ScalarReference` (see {@apilink Ref}). */
|
|
523
523
|
export type ScalarRef<T> = ScalarReference<T>;
|
|
524
|
+
/**
|
|
525
|
+
* Type-level marker for a to-one relation that is **direct at runtime** (no `Reference` wrapper) but
|
|
526
|
+
* **restricted at compile time** until narrowed via `Loaded<>`.
|
|
527
|
+
*
|
|
528
|
+
* Use as the property type on a `@ManyToOne`/`@OneToOne` relation that does **not** have `ref: true`:
|
|
529
|
+
*
|
|
530
|
+
* ```ts
|
|
531
|
+
* @ManyToOne(() => User)
|
|
532
|
+
* author!: LazyRef<User>;
|
|
533
|
+
* ```
|
|
534
|
+
*
|
|
535
|
+
* Semantics:
|
|
536
|
+
* - **Runtime**: identical to a plain direct relation — `article.author` is a `User` instance (stub or hydrated),
|
|
537
|
+
* `article.author instanceof User` is `true`, no `Reference` object is created.
|
|
538
|
+
* - **Type (unloaded view)**: only the primary key is accessible. Accessing other properties is a compile error.
|
|
539
|
+
* - **Type (loaded view)**: once the relation is in the populate hint of a `Loaded<Entity, 'author'>`, it narrows
|
|
540
|
+
* back to the full entity — no `.$` or `.get()` indirection needed.
|
|
541
|
+
*
|
|
542
|
+
* Note: the safety is purely compile-time. JS code or `as any` casts bypass it.
|
|
543
|
+
*/
|
|
544
|
+
export type LazyRef<T extends object> = IsAny<T> extends true ? LazyRef.Brand<T> : {
|
|
545
|
+
[K in PrimaryProperty<T> & keyof T]: T[K];
|
|
546
|
+
} & LazyRef.Brand<T>;
|
|
547
|
+
export declare namespace LazyRef {
|
|
548
|
+
const __lazyRef: unique symbol;
|
|
549
|
+
interface Brand<T> {
|
|
550
|
+
[__lazyRef]?: (arg: T) => T;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
524
553
|
/** Alias for `Reference<T> & { id: number }` (see {@apilink Ref}). */
|
|
525
554
|
export type EntityRef<T extends object> = true extends IsUnknown<PrimaryProperty<T>> ? Reference<T> : IsAny<T> extends true ? Reference<T> : {
|
|
526
555
|
[K in PrimaryProperty<T> & keyof T]: T[K];
|
|
@@ -1236,6 +1265,13 @@ export type PopulateHintOptions = {
|
|
|
1236
1265
|
orderBy?: QueryOrderMap<any>;
|
|
1237
1266
|
};
|
|
1238
1267
|
type ExtractType<T> = T extends CollectionShape<infer U> ? U : T extends ReferenceShape<infer U> ? U : T extends Ref<infer U> ? U : T extends readonly (infer U)[] ? U : T;
|
|
1268
|
+
/**
|
|
1269
|
+
* Like {@link ExtractType} but also unwraps {@link LazyRef}. Kept separate so {@link ExtractType}'s
|
|
1270
|
+
* hot path (used by `IsOptional`, `EntityData`, `RequiredEntityData`) doesn't pay the cost of the
|
|
1271
|
+
* extra branch — those contexts handle `LazyRef` in their own earlier branches (see
|
|
1272
|
+
* `EntityDataProp` / `RequiredEntityDataProp`) before delegating to `ExtractType`.
|
|
1273
|
+
*/
|
|
1274
|
+
type ExtractTypeWithLazyRef<T> = T extends LazyRef.Brand<infer U> ? U : ExtractType<T>;
|
|
1239
1275
|
type ExtractStringKeys<T> = {
|
|
1240
1276
|
[K in keyof T]-?: CleanKeys<T, K>;
|
|
1241
1277
|
}[keyof T] & {};
|
|
@@ -1243,8 +1279,8 @@ type ExtractStringKeys<T> = {
|
|
|
1243
1279
|
* Extracts string keys from an entity type, handling Collection/Reference wrappers.
|
|
1244
1280
|
* Simplified to just check `T extends object` since ExtractType handles the unwrapping.
|
|
1245
1281
|
*/
|
|
1246
|
-
type StringKeys<T, E extends string = never> = T extends object ? ExtractStringKeys<
|
|
1247
|
-
type GetStringKey<T, K extends StringKeys<T, string>, E extends string> = K extends keyof T ?
|
|
1282
|
+
type StringKeys<T, E extends string = never> = T extends object ? ExtractStringKeys<ExtractTypeWithLazyRef<T>> | E : never;
|
|
1283
|
+
type GetStringKey<T, K extends StringKeys<T, string>, E extends string> = K extends keyof T ? ExtractTypeWithLazyRef<T[K]> : K extends E ? keyof T : never;
|
|
1248
1284
|
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
1249
1285
|
type RelationKeys<T> = T extends object ? {
|
|
1250
1286
|
[K in keyof T]-?: CleanKeys<T, K, true>;
|
|
@@ -1259,8 +1295,8 @@ export type UnboxArray<T> = T extends any[] ? ArrayElement<T> : T;
|
|
|
1259
1295
|
/** Extracts the element type from an array type. */
|
|
1260
1296
|
export type ArrayElement<ArrayType extends unknown[]> = ArrayType extends (infer ElementType)[] ? ElementType : never;
|
|
1261
1297
|
/** Unwraps a property type from its wrapper (Reference, Collection, or array) to the inner entity type. */
|
|
1262
|
-
export type ExpandProperty<T> = T extends ReferenceShape<infer U> ? NonNullable<U> : T extends CollectionShape<infer U> ? NonNullable<U> : T extends (infer U)[] ? NonNullable<U> : NonNullable<T>;
|
|
1263
|
-
type LoadedLoadable<T, E extends object> = T extends CollectionShape ? LoadedCollection<E> : T extends ScalarReference<infer U> ? LoadedScalarReference<U> : T extends ReferenceShape ? T & LoadedReference<E> : T extends Scalar ? T : T extends (infer U)[] ? U extends Scalar ? T : E[] : E;
|
|
1298
|
+
export type ExpandProperty<T> = T extends LazyRef.Brand<infer U> ? NonNullable<U> : T extends ReferenceShape<infer U> ? NonNullable<U> : T extends CollectionShape<infer U> ? NonNullable<U> : T extends (infer U)[] ? NonNullable<U> : NonNullable<T>;
|
|
1299
|
+
type LoadedLoadable<T, E extends object> = T extends CollectionShape ? LoadedCollection<E> : T extends ScalarReference<infer U> ? LoadedScalarReference<U> : T extends LazyRef.Brand<any> ? E : T extends ReferenceShape ? T & LoadedReference<E> : T extends Scalar ? T : T extends (infer U)[] ? U extends Scalar ? T : E[] : E;
|
|
1264
1300
|
type IsTrue<T> = IsNever<T> extends true ? false : T extends boolean ? (T extends true ? true : false) : false;
|
|
1265
1301
|
type StringLiteral<T> = T extends string ? (string extends T ? never : T) : never;
|
|
1266
1302
|
type Prefix<T, K> = K extends `${infer S}.${string}` ? S : K extends '*' ? keyof T : K;
|
|
@@ -1294,7 +1330,7 @@ export type MergeSelected<T, U, F extends string> = IsLoadedType<T> extends true
|
|
|
1294
1330
|
export type MergeLoaded<T, U, P extends string, F extends string, E extends string, R extends boolean = false> = IsLoadedType<T> extends true ? T & Loaded<U, P, F, E> : Loaded<T, P, F, E>;
|
|
1295
1331
|
/** Extracts the nullability modifiers (`null`, `undefined`, or both) from a type `T`. */
|
|
1296
1332
|
export type AddOptional<T> = undefined | null extends T ? null | undefined : null extends T ? null : undefined extends T ? undefined : never;
|
|
1297
|
-
type LoadedProp<T, L extends string = never, F extends string = '*', E extends string = never> = LoadedLoadable<T, Loaded<
|
|
1333
|
+
type LoadedProp<T, L extends string = never, F extends string = '*', E extends string = never> = LoadedLoadable<T, Loaded<ExtractTypeWithLazyRef<T>, L, F, E>>;
|
|
1298
1334
|
/** Extracts the eager-loaded property names declared via `[EagerProps]` as a string union. */
|
|
1299
1335
|
export type AddEager<T> = ExtractEagerProps<T> & string;
|
|
1300
1336
|
/** Combines an explicit populate hint `L` with the entity's eagerly loaded properties. */
|
package/utils/Utils.js
CHANGED
|
@@ -141,7 +141,7 @@ export function parseJsonSafe(value) {
|
|
|
141
141
|
/** Collection of general-purpose utility methods used throughout the ORM. */
|
|
142
142
|
export class Utils {
|
|
143
143
|
static PK_SEPARATOR = '~~~';
|
|
144
|
-
static #ORM_VERSION = '7.1.0-dev.
|
|
144
|
+
static #ORM_VERSION = '7.1.0-dev.11';
|
|
145
145
|
/**
|
|
146
146
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
147
147
|
*/
|