@mikro-orm/core 7.0.2-dev.9 → 7.0.3-dev.0
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/EntityManager.d.ts +4 -0
- package/EntityManager.js +4 -0
- package/MikroORM.d.ts +2 -0
- package/MikroORM.js +2 -0
- package/README.md +128 -294
- package/cache/CacheAdapter.d.ts +2 -0
- package/cache/GeneratedCacheAdapter.d.ts +1 -0
- package/cache/GeneratedCacheAdapter.js +1 -0
- package/cache/MemoryCacheAdapter.d.ts +1 -0
- package/cache/MemoryCacheAdapter.js +1 -0
- package/cache/NullCacheAdapter.d.ts +1 -0
- package/cache/NullCacheAdapter.js +1 -0
- package/connections/Connection.d.ts +13 -0
- package/connections/Connection.js +9 -0
- package/drivers/DatabaseDriver.d.ts +12 -0
- package/drivers/DatabaseDriver.js +12 -0
- package/drivers/IDatabaseDriver.d.ts +44 -0
- package/drivers/IDatabaseDriver.js +1 -0
- package/entity/BaseEntity.d.ts +11 -0
- package/entity/BaseEntity.js +11 -0
- package/entity/Collection.d.ts +26 -0
- package/entity/Collection.js +15 -0
- package/entity/EntityAssigner.d.ts +3 -0
- package/entity/EntityAssigner.js +2 -0
- package/entity/EntityFactory.d.ts +16 -0
- package/entity/EntityFactory.js +19 -0
- package/entity/EntityLoader.d.ts +19 -0
- package/entity/EntityLoader.js +2 -0
- package/entity/EntityRepository.d.ts +2 -0
- package/entity/EntityRepository.js +2 -0
- package/entity/Reference.d.ts +20 -0
- package/entity/Reference.js +16 -0
- package/entity/WrappedEntity.d.ts +20 -0
- package/entity/WrappedEntity.js +21 -1
- package/entity/defineEntity.d.ts +12 -2
- package/entity/defineEntity.js +1 -0
- package/enums.d.ts +132 -0
- package/enums.js +132 -0
- package/errors.d.ts +6 -0
- package/errors.js +6 -0
- package/events/EventManager.d.ts +5 -0
- package/events/EventManager.js +5 -0
- package/events/EventSubscriber.d.ts +4 -0
- package/events/TransactionEventBroadcaster.d.ts +2 -0
- package/events/TransactionEventBroadcaster.js +2 -0
- package/hydration/Hydrator.d.ts +2 -0
- package/hydration/Hydrator.js +2 -0
- package/hydration/ObjectHydrator.d.ts +1 -0
- package/hydration/ObjectHydrator.js +1 -0
- package/logging/DefaultLogger.d.ts +3 -0
- package/logging/DefaultLogger.js +3 -0
- package/logging/Logger.d.ts +5 -0
- package/metadata/EntitySchema.d.ts +22 -0
- package/metadata/EntitySchema.js +22 -2
- package/metadata/MetadataDiscovery.d.ts +4 -0
- package/metadata/MetadataDiscovery.js +18 -11
- package/metadata/MetadataProvider.d.ts +8 -0
- package/metadata/MetadataProvider.js +9 -1
- package/metadata/MetadataStorage.d.ts +14 -0
- package/metadata/MetadataStorage.js +14 -1
- package/naming-strategy/AbstractNamingStrategy.d.ts +1 -0
- package/naming-strategy/AbstractNamingStrategy.js +1 -0
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -0
- package/naming-strategy/MongoNamingStrategy.js +1 -0
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -0
- package/naming-strategy/UnderscoreNamingStrategy.js +1 -0
- package/package.json +1 -1
- package/platforms/ExceptionConverter.d.ts +1 -0
- package/platforms/ExceptionConverter.js +1 -0
- package/platforms/Platform.d.ts +65 -0
- package/platforms/Platform.js +65 -0
- package/serialization/EntitySerializer.d.ts +2 -0
- package/serialization/EntitySerializer.js +2 -0
- package/serialization/EntityTransformer.d.ts +2 -0
- package/serialization/EntityTransformer.js +2 -0
- package/serialization/SerializationContext.d.ts +5 -0
- package/serialization/SerializationContext.js +5 -0
- package/types/ArrayType.d.ts +1 -0
- package/types/ArrayType.js +1 -0
- package/types/BlobType.d.ts +1 -0
- package/types/BlobType.js +1 -0
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +1 -0
- package/types/CharacterType.d.ts +1 -0
- package/types/CharacterType.js +1 -0
- package/types/DateTimeType.d.ts +1 -0
- package/types/DateTimeType.js +1 -0
- package/types/DateType.d.ts +1 -0
- package/types/DateType.js +1 -0
- package/types/EnumArrayType.d.ts +1 -0
- package/types/EnumArrayType.js +1 -0
- package/types/EnumType.d.ts +1 -0
- package/types/EnumType.js +1 -0
- package/types/FloatType.d.ts +1 -0
- package/types/FloatType.js +1 -0
- package/types/IntegerType.d.ts +1 -0
- package/types/IntegerType.js +1 -0
- package/types/IntervalType.d.ts +1 -0
- package/types/IntervalType.js +1 -0
- package/types/JsonType.d.ts +1 -0
- package/types/JsonType.js +1 -0
- package/types/MediumIntType.d.ts +1 -0
- package/types/MediumIntType.js +1 -0
- package/types/SmallIntType.d.ts +1 -0
- package/types/SmallIntType.js +1 -0
- package/types/StringType.d.ts +1 -0
- package/types/StringType.js +1 -0
- package/types/TextType.d.ts +1 -0
- package/types/TextType.js +1 -0
- package/types/TimeType.d.ts +1 -0
- package/types/TimeType.js +1 -0
- package/types/TinyIntType.d.ts +1 -0
- package/types/TinyIntType.js +1 -0
- package/types/Type.d.ts +11 -0
- package/types/Type.js +11 -0
- package/types/Uint8ArrayType.d.ts +1 -0
- package/types/Uint8ArrayType.js +1 -0
- package/types/UnknownType.d.ts +1 -0
- package/types/UnknownType.js +1 -0
- package/types/UuidType.d.ts +1 -0
- package/types/UuidType.js +1 -0
- package/types/index.d.ts +2 -0
- package/types/index.js +13 -1
- package/typings.d.ts +177 -0
- package/typings.js +11 -0
- package/unit-of-work/ChangeSet.d.ts +4 -0
- package/unit-of-work/ChangeSet.js +4 -0
- package/unit-of-work/ChangeSetComputer.d.ts +2 -0
- package/unit-of-work/ChangeSetComputer.js +15 -2
- package/unit-of-work/ChangeSetPersister.d.ts +4 -0
- package/unit-of-work/ChangeSetPersister.js +4 -0
- package/unit-of-work/IdentityMap.d.ts +7 -0
- package/unit-of-work/IdentityMap.js +7 -0
- package/unit-of-work/UnitOfWork.d.ts +15 -0
- package/unit-of-work/UnitOfWork.js +15 -0
- package/utils/Configuration.d.ts +8 -0
- package/utils/Configuration.js +8 -0
- package/utils/EntityComparator.d.ts +2 -0
- package/utils/EntityComparator.js +2 -0
- package/utils/NullHighlighter.d.ts +1 -0
- package/utils/NullHighlighter.js +1 -0
- package/utils/RawQueryFragment.d.ts +10 -0
- package/utils/RawQueryFragment.js +9 -0
- package/utils/RequestContext.d.ts +1 -0
- package/utils/TransactionContext.d.ts +1 -0
- package/utils/TransactionContext.js +1 -0
- package/utils/Utils.d.ts +6 -0
- package/utils/Utils.js +7 -1
|
@@ -5,6 +5,7 @@ import type { ConnectionType, Dictionary, MaybePromise, Primary } from '../typin
|
|
|
5
5
|
import type { Platform } from '../platforms/Platform.js';
|
|
6
6
|
import type { TransactionEventBroadcaster } from '../events/TransactionEventBroadcaster.js';
|
|
7
7
|
import type { IsolationLevel } from '../enums.js';
|
|
8
|
+
/** Abstract base class for database connections, providing transaction and query execution support. */
|
|
8
9
|
export declare abstract class Connection {
|
|
9
10
|
#private;
|
|
10
11
|
protected readonly config: Configuration;
|
|
@@ -49,6 +50,7 @@ export declare abstract class Connection {
|
|
|
49
50
|
*/
|
|
50
51
|
executeDump(dump: string): Promise<void>;
|
|
51
52
|
protected onConnect(): Promise<void>;
|
|
53
|
+
/** Executes a callback inside a transaction, committing on success and rolling back on failure. */
|
|
52
54
|
transactional<T>(cb: (trx: Transaction) => Promise<T>, options?: {
|
|
53
55
|
isolationLevel?: IsolationLevel | `${IsolationLevel}`;
|
|
54
56
|
readOnly?: boolean;
|
|
@@ -56,6 +58,7 @@ export declare abstract class Connection {
|
|
|
56
58
|
eventBroadcaster?: TransactionEventBroadcaster;
|
|
57
59
|
loggerContext?: LogContext;
|
|
58
60
|
}): Promise<T>;
|
|
61
|
+
/** Begins a new database transaction and returns the transaction context. */
|
|
59
62
|
begin(options?: {
|
|
60
63
|
isolationLevel?: IsolationLevel | `${IsolationLevel}`;
|
|
61
64
|
readOnly?: boolean;
|
|
@@ -63,16 +66,24 @@ export declare abstract class Connection {
|
|
|
63
66
|
eventBroadcaster?: TransactionEventBroadcaster;
|
|
64
67
|
loggerContext?: LogContext;
|
|
65
68
|
}): Promise<Transaction>;
|
|
69
|
+
/** Commits the given transaction. */
|
|
66
70
|
commit(ctx: Transaction, eventBroadcaster?: TransactionEventBroadcaster, loggerContext?: LogContext): Promise<void>;
|
|
71
|
+
/** Rolls back the given transaction. */
|
|
67
72
|
rollback(ctx: Transaction, eventBroadcaster?: TransactionEventBroadcaster, loggerContext?: LogContext): Promise<void>;
|
|
73
|
+
/** Executes a raw query and returns the result. */
|
|
68
74
|
abstract execute<T>(query: string, params?: any[], method?: 'all' | 'get' | 'run', ctx?: Transaction): Promise<QueryResult<T> | any | any[]>;
|
|
75
|
+
/** Parses and returns the resolved connection configuration (host, port, user, etc.). */
|
|
69
76
|
getConnectionOptions(): ConnectionConfig;
|
|
77
|
+
/** Sets the metadata storage on this connection. */
|
|
70
78
|
setMetadata(metadata: MetadataStorage): void;
|
|
79
|
+
/** Sets the platform abstraction on this connection. */
|
|
71
80
|
setPlatform(platform: Platform): void;
|
|
81
|
+
/** Returns the platform abstraction for this connection. */
|
|
72
82
|
getPlatform(): Platform;
|
|
73
83
|
protected executeQuery<T>(query: string, cb: () => Promise<T>, context?: LogContext): Promise<T>;
|
|
74
84
|
protected logQuery(query: string, context?: LogContext): void;
|
|
75
85
|
}
|
|
86
|
+
/** Result of a native database query (insert, update, delete). */
|
|
76
87
|
export interface QueryResult<T = {
|
|
77
88
|
id: number;
|
|
78
89
|
}> {
|
|
@@ -82,6 +93,7 @@ export interface QueryResult<T = {
|
|
|
82
93
|
rows?: Dictionary[];
|
|
83
94
|
insertedIds?: Primary<T>[];
|
|
84
95
|
}
|
|
96
|
+
/** Resolved database connection parameters. */
|
|
85
97
|
export interface ConnectionConfig {
|
|
86
98
|
host?: string;
|
|
87
99
|
port?: number;
|
|
@@ -90,4 +102,5 @@ export interface ConnectionConfig {
|
|
|
90
102
|
database?: string;
|
|
91
103
|
schema?: string;
|
|
92
104
|
}
|
|
105
|
+
/** Opaque transaction context type, wrapping the driver-specific transaction object. */
|
|
93
106
|
export type Transaction<T = any> = T & {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Utils } from '../utils/Utils.js';
|
|
2
|
+
/** Abstract base class for database connections, providing transaction and query execution support. */
|
|
2
3
|
export class Connection {
|
|
3
4
|
config;
|
|
4
5
|
type;
|
|
@@ -75,18 +76,23 @@ export class Connection {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
}
|
|
79
|
+
/** Executes a callback inside a transaction, committing on success and rolling back on failure. */
|
|
78
80
|
async transactional(cb, options) {
|
|
79
81
|
throw new Error(`Transactions are not supported by current driver`);
|
|
80
82
|
}
|
|
83
|
+
/** Begins a new database transaction and returns the transaction context. */
|
|
81
84
|
async begin(options) {
|
|
82
85
|
throw new Error(`Transactions are not supported by current driver`);
|
|
83
86
|
}
|
|
87
|
+
/** Commits the given transaction. */
|
|
84
88
|
async commit(ctx, eventBroadcaster, loggerContext) {
|
|
85
89
|
throw new Error(`Transactions are not supported by current driver`);
|
|
86
90
|
}
|
|
91
|
+
/** Rolls back the given transaction. */
|
|
87
92
|
async rollback(ctx, eventBroadcaster, loggerContext) {
|
|
88
93
|
throw new Error(`Transactions are not supported by current driver`);
|
|
89
94
|
}
|
|
95
|
+
/** Parses and returns the resolved connection configuration (host, port, user, etc.). */
|
|
90
96
|
getConnectionOptions() {
|
|
91
97
|
const ret = {};
|
|
92
98
|
if (this.options.clientUrl) {
|
|
@@ -113,12 +119,15 @@ export class Connection {
|
|
|
113
119
|
}
|
|
114
120
|
return ret;
|
|
115
121
|
}
|
|
122
|
+
/** Sets the metadata storage on this connection. */
|
|
116
123
|
setMetadata(metadata) {
|
|
117
124
|
this.metadata = metadata;
|
|
118
125
|
}
|
|
126
|
+
/** Sets the platform abstraction on this connection. */
|
|
119
127
|
setPlatform(platform) {
|
|
120
128
|
this.platform = platform;
|
|
121
129
|
}
|
|
130
|
+
/** Returns the platform abstraction for this connection. */
|
|
122
131
|
getPlatform() {
|
|
123
132
|
return this.platform;
|
|
124
133
|
}
|
|
@@ -10,6 +10,7 @@ import type { Collection } from '../entity/Collection.js';
|
|
|
10
10
|
import { EntityManager } from '../EntityManager.js';
|
|
11
11
|
import { DriverException } from '../exceptions.js';
|
|
12
12
|
import { MikroORM } from '../MikroORM.js';
|
|
13
|
+
/** Abstract base class for all database drivers, implementing common driver logic. */
|
|
13
14
|
export declare abstract class DatabaseDriver<C extends Connection> implements IDatabaseDriver<C> {
|
|
14
15
|
readonly config: Configuration;
|
|
15
16
|
protected readonly dependencies: string[];
|
|
@@ -28,24 +29,34 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
|
|
|
28
29
|
nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
29
30
|
abstract nativeDelete<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options?: DeleteOptions<T>): Promise<QueryResult<T>>;
|
|
30
31
|
abstract count<T extends object, P extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
|
|
32
|
+
/** Creates a new EntityManager instance bound to this driver. */
|
|
31
33
|
createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
|
|
32
34
|
findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
|
|
33
35
|
countVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: CountOptions<T, any>): Promise<number>;
|
|
34
36
|
aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
|
|
35
37
|
loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<any>, orderBy?: OrderDefinition<T>, ctx?: Transaction, options?: FindOptions<T, any, any, any>, pivotJoin?: boolean): Promise<Dictionary<T[]>>;
|
|
36
38
|
syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
|
|
39
|
+
/** Maps raw database result to entity data, converting column names to property names. */
|
|
37
40
|
mapResult<T extends object>(result: EntityDictionary<T>, meta?: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
|
|
41
|
+
/** Opens the primary connection and all read replicas. */
|
|
38
42
|
connect(options?: {
|
|
39
43
|
skipOnConnect?: boolean;
|
|
40
44
|
}): Promise<C>;
|
|
45
|
+
/** Closes all connections and re-establishes them. */
|
|
41
46
|
reconnect(options?: {
|
|
42
47
|
skipOnConnect?: boolean;
|
|
43
48
|
}): Promise<C>;
|
|
49
|
+
/** Returns the write connection or a random read replica. */
|
|
44
50
|
getConnection(type?: ConnectionType): C;
|
|
51
|
+
/** Closes the primary connection and all read replicas. */
|
|
45
52
|
close(force?: boolean): Promise<void>;
|
|
53
|
+
/** Returns the database platform abstraction for this driver. */
|
|
46
54
|
getPlatform(): Platform;
|
|
55
|
+
/** Sets the metadata storage and initializes the comparator for all connections. */
|
|
47
56
|
setMetadata(metadata: MetadataStorage): void;
|
|
57
|
+
/** Returns the metadata storage used by this driver. */
|
|
48
58
|
getMetadata(): MetadataStorage;
|
|
59
|
+
/** Returns the names of native database dependencies required by this driver. */
|
|
49
60
|
getDependencies(): string[];
|
|
50
61
|
protected isPopulated<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>, hint: PopulateOptions<T>, name?: string): boolean;
|
|
51
62
|
protected processCursorOptions<T extends object, P extends string>(meta: EntityMetadata<T>, options: FindOptions<T, P, any, any>, orderBy: OrderDefinition<T>): {
|
|
@@ -58,6 +69,7 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
|
|
|
58
69
|
protected inlineEmbeddables<T extends object>(meta: EntityMetadata<T>, data: T, where?: boolean): void;
|
|
59
70
|
protected getPrimaryKeyFields<T>(meta: EntityMetadata<T>): string[];
|
|
60
71
|
protected createReplicas(cb: (c: ConnectionOptions) => C): C[];
|
|
72
|
+
/** Acquires a pessimistic lock on the given entity. */
|
|
61
73
|
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
|
|
62
74
|
abstract stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
|
|
63
75
|
/**
|
|
@@ -11,6 +11,7 @@ import { helper } from '../entity/wrap.js';
|
|
|
11
11
|
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
12
12
|
import { JsonType } from '../types/JsonType.js';
|
|
13
13
|
import { MikroORM } from '../MikroORM.js';
|
|
14
|
+
/** Abstract base class for all database drivers, implementing common driver logic. */
|
|
14
15
|
export class DatabaseDriver {
|
|
15
16
|
config;
|
|
16
17
|
dependencies;
|
|
@@ -27,6 +28,7 @@ export class DatabaseDriver {
|
|
|
27
28
|
async nativeUpdateMany(entityName, where, data, options) {
|
|
28
29
|
throw new Error(`Batch updates are not supported by ${this.constructor.name} driver`);
|
|
29
30
|
}
|
|
31
|
+
/** Creates a new EntityManager instance bound to this driver. */
|
|
30
32
|
createEntityManager(useContext) {
|
|
31
33
|
const EntityManagerClass = this.config.get('entityManager', EntityManager);
|
|
32
34
|
return new EntityManagerClass(this.config, this, this.metadata, useContext);
|
|
@@ -62,22 +64,26 @@ export class DatabaseDriver {
|
|
|
62
64
|
}
|
|
63
65
|
}
|
|
64
66
|
}
|
|
67
|
+
/** Maps raw database result to entity data, converting column names to property names. */
|
|
65
68
|
mapResult(result, meta, populate = []) {
|
|
66
69
|
if (!result || !meta) {
|
|
67
70
|
return result ?? null;
|
|
68
71
|
}
|
|
69
72
|
return this.comparator.mapResult(meta, result);
|
|
70
73
|
}
|
|
74
|
+
/** Opens the primary connection and all read replicas. */
|
|
71
75
|
async connect(options) {
|
|
72
76
|
await this.connection.connect(options);
|
|
73
77
|
await Promise.all(this.replicas.map(replica => replica.connect()));
|
|
74
78
|
return this.connection;
|
|
75
79
|
}
|
|
80
|
+
/** Closes all connections and re-establishes them. */
|
|
76
81
|
async reconnect(options) {
|
|
77
82
|
await this.close(true);
|
|
78
83
|
await this.connect(options);
|
|
79
84
|
return this.connection;
|
|
80
85
|
}
|
|
86
|
+
/** Returns the write connection or a random read replica. */
|
|
81
87
|
getConnection(type = 'write') {
|
|
82
88
|
if (type === 'write' || this.replicas.length === 0) {
|
|
83
89
|
return this.connection;
|
|
@@ -85,13 +91,16 @@ export class DatabaseDriver {
|
|
|
85
91
|
const rand = Utils.randomInt(0, this.replicas.length - 1);
|
|
86
92
|
return this.replicas[rand];
|
|
87
93
|
}
|
|
94
|
+
/** Closes the primary connection and all read replicas. */
|
|
88
95
|
async close(force) {
|
|
89
96
|
await Promise.all(this.replicas.map(replica => replica.close(force)));
|
|
90
97
|
await this.connection.close(force);
|
|
91
98
|
}
|
|
99
|
+
/** Returns the database platform abstraction for this driver. */
|
|
92
100
|
getPlatform() {
|
|
93
101
|
return this.platform;
|
|
94
102
|
}
|
|
103
|
+
/** Sets the metadata storage and initializes the comparator for all connections. */
|
|
95
104
|
setMetadata(metadata) {
|
|
96
105
|
this.metadata = metadata;
|
|
97
106
|
this.comparator = new EntityComparator(this.metadata, this.platform);
|
|
@@ -102,9 +111,11 @@ export class DatabaseDriver {
|
|
|
102
111
|
replica.setPlatform(this.platform);
|
|
103
112
|
});
|
|
104
113
|
}
|
|
114
|
+
/** Returns the metadata storage used by this driver. */
|
|
105
115
|
getMetadata() {
|
|
106
116
|
return this.metadata;
|
|
107
117
|
}
|
|
118
|
+
/** Returns the names of native database dependencies required by this driver. */
|
|
108
119
|
getDependencies() {
|
|
109
120
|
return this.dependencies;
|
|
110
121
|
}
|
|
@@ -426,6 +437,7 @@ export class DatabaseDriver {
|
|
|
426
437
|
}
|
|
427
438
|
return ret;
|
|
428
439
|
}
|
|
440
|
+
/** Acquires a pessimistic lock on the given entity. */
|
|
429
441
|
async lockPessimistic(entity, options) {
|
|
430
442
|
throw new Error(`Pessimistic locks are not supported by ${this.constructor.name} driver`);
|
|
431
443
|
}
|
|
@@ -10,18 +10,25 @@ import type { Configuration } from '../utils/Configuration.js';
|
|
|
10
10
|
import type { MikroORM } from '../MikroORM.js';
|
|
11
11
|
import type { LoggingOptions, LogContext } from '../logging/Logger.js';
|
|
12
12
|
import type { Raw } from '../utils/RawQueryFragment.js';
|
|
13
|
+
/** Symbol used to extract the EntityManager type from a driver instance. */
|
|
13
14
|
export declare const EntityManagerType: unique symbol;
|
|
15
|
+
/** Interface defining the contract for all database drivers. */
|
|
14
16
|
export interface IDatabaseDriver<C extends Connection = Connection> {
|
|
15
17
|
[EntityManagerType]: EntityManager<this>;
|
|
16
18
|
readonly config: Configuration;
|
|
19
|
+
/** Creates a new EntityManager instance for this driver. */
|
|
17
20
|
createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
|
|
21
|
+
/** Opens a connection to the database. */
|
|
18
22
|
connect(options?: {
|
|
19
23
|
skipOnConnect?: boolean;
|
|
20
24
|
}): Promise<C>;
|
|
25
|
+
/** Closes the database connection. */
|
|
21
26
|
close(force?: boolean): Promise<void>;
|
|
27
|
+
/** Closes and re-establishes the database connection. */
|
|
22
28
|
reconnect(options?: {
|
|
23
29
|
skipOnConnect?: boolean;
|
|
24
30
|
}): Promise<C>;
|
|
31
|
+
/** Returns the underlying database connection (write or read replica). */
|
|
25
32
|
getConnection(type?: ConnectionType): C;
|
|
26
33
|
/**
|
|
27
34
|
* Finds selection of entities
|
|
@@ -31,29 +38,44 @@ export interface IDatabaseDriver<C extends Connection = Connection> {
|
|
|
31
38
|
* Finds single entity (table row, document)
|
|
32
39
|
*/
|
|
33
40
|
findOne<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOneOptions<T, P, F, E>): Promise<EntityData<T> | null>;
|
|
41
|
+
/** Finds entities backed by a virtual (expression-based) definition. */
|
|
34
42
|
findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
|
|
43
|
+
/** Returns an async iterator that streams query results one entity at a time. */
|
|
35
44
|
stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
|
|
45
|
+
/** Inserts a single row into the database. */
|
|
36
46
|
nativeInsert<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
47
|
+
/** Inserts multiple rows into the database in a single batch operation. */
|
|
37
48
|
nativeInsertMany<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
|
|
49
|
+
/** Updates rows matching the given condition. */
|
|
38
50
|
nativeUpdate<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
51
|
+
/** Updates multiple rows with different payloads in a single batch operation. */
|
|
39
52
|
nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
53
|
+
/** Deletes rows matching the given condition. */
|
|
40
54
|
nativeDelete<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options?: NativeDeleteOptions<T>): Promise<QueryResult<T>>;
|
|
55
|
+
/** Persists changes to M:N collections (inserts/deletes pivot table rows). */
|
|
41
56
|
syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
|
|
57
|
+
/** Counts entities matching the given condition. */
|
|
42
58
|
count<T extends object, P extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
|
|
59
|
+
/** Executes a MongoDB aggregation pipeline (MongoDB driver only). */
|
|
43
60
|
aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
|
|
61
|
+
/** Maps raw database result to entity data, converting column names to property names. */
|
|
44
62
|
mapResult<T extends object>(result: EntityDictionary<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
|
|
45
63
|
/**
|
|
46
64
|
* When driver uses pivot tables for M:N, this method will load identifiers for given collections from them
|
|
47
65
|
*/
|
|
48
66
|
loadFromPivotTable<T extends object, O extends object>(prop: EntityProperty, owners: Primary<O>[][], where?: FilterQuery<T>, orderBy?: OrderDefinition<T>, ctx?: Transaction, options?: FindOptions<T, any, any, any>, pivotJoin?: boolean): Promise<Dictionary<T[]>>;
|
|
67
|
+
/** Returns the database platform abstraction for this driver. */
|
|
49
68
|
getPlatform(): Platform;
|
|
69
|
+
/** Sets the metadata storage used by this driver. */
|
|
50
70
|
setMetadata(metadata: MetadataStorage): void;
|
|
71
|
+
/** Returns the metadata storage used by this driver. */
|
|
51
72
|
getMetadata(): MetadataStorage;
|
|
52
73
|
/**
|
|
53
74
|
* Returns name of the underlying database dependencies (e.g. `mongodb` or `mysql2`)
|
|
54
75
|
* for SQL drivers it also returns `knex` in the array as connectors are not used directly there
|
|
55
76
|
*/
|
|
56
77
|
getDependencies(): string[];
|
|
78
|
+
/** Acquires a pessimistic lock on the given entity. */
|
|
57
79
|
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
|
|
58
80
|
/**
|
|
59
81
|
* Converts native db errors to standardized driver exceptions
|
|
@@ -71,13 +93,17 @@ export interface IDatabaseDriver<C extends Connection = Connection> {
|
|
|
71
93
|
*/
|
|
72
94
|
getORMClass(): Constructor<MikroORM>;
|
|
73
95
|
}
|
|
96
|
+
/** Represents a field selector for entity queries (property name or wildcard). */
|
|
74
97
|
export type EntityField<T, P extends string = PopulatePath.ALL> = keyof T | PopulatePath.ALL | AutoPath<T, P, `${PopulatePath.ALL}`>;
|
|
98
|
+
/** Defines the ordering for query results, either a single order map or an array of them. */
|
|
75
99
|
export type OrderDefinition<T> = (QueryOrderMap<T> & {
|
|
76
100
|
0?: never;
|
|
77
101
|
}) | QueryOrderMap<T>[];
|
|
102
|
+
/** Options for `em.findAll()`, extends FindOptions with an optional `where` clause. */
|
|
78
103
|
export interface FindAllOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends FindOptions<T, P, F, E> {
|
|
79
104
|
where?: FilterQuery<T>;
|
|
80
105
|
}
|
|
106
|
+
/** Options for streaming query results via `em.stream()`. */
|
|
81
107
|
export interface StreamOptions<Entity, Populate extends string = never, Fields extends string = '*', Exclude extends string = never> extends Omit<FindAllOptions<Entity, Populate, Fields, Exclude>, 'cache' | 'before' | 'after' | 'first' | 'last' | 'overfetch' | 'strategy'> {
|
|
82
108
|
/**
|
|
83
109
|
* When populating to-many relations, the ORM streams fully merged entities instead of yielding every row.
|
|
@@ -90,12 +116,15 @@ export interface StreamOptions<Entity, Populate extends string = never, Fields e
|
|
|
90
116
|
*/
|
|
91
117
|
mergeResults?: boolean;
|
|
92
118
|
}
|
|
119
|
+
/** Configuration for enabling/disabling named filters on a query. */
|
|
93
120
|
export type FilterOptions = Dictionary<boolean | Dictionary> | string[] | boolean;
|
|
121
|
+
/** Specifies which relations to populate and which fields to select or exclude. */
|
|
94
122
|
export interface LoadHint<Entity, Hint extends string = never, Fields extends string = PopulatePath.ALL, Excludes extends string = never> {
|
|
95
123
|
populate?: Populate<Entity, Hint>;
|
|
96
124
|
fields?: readonly AutoPath<Entity, Fields, `${PopulatePath.ALL}`>[];
|
|
97
125
|
exclude?: readonly AutoPath<Entity, Excludes>[];
|
|
98
126
|
}
|
|
127
|
+
/** Options for `em.find()` queries, including population, ordering, pagination, and locking. */
|
|
99
128
|
export interface FindOptions<Entity, Hint extends string = never, Fields extends string = PopulatePath.ALL, Excludes extends string = never> extends LoadHint<Entity, Hint, Fields, Excludes> {
|
|
100
129
|
/**
|
|
101
130
|
* Where condition for populated relations. This will have no effect on the root entity.
|
|
@@ -197,17 +226,21 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
|
|
|
197
226
|
/** @internal used to apply filters to the auto-joined relations */
|
|
198
227
|
em?: EntityManager;
|
|
199
228
|
}
|
|
229
|
+
/** Options for cursor-based pagination via `em.findByCursor()`. */
|
|
200
230
|
export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<FindAllOptions<T, P, F, E>, 'limit' | 'offset'> {
|
|
201
231
|
includeCount?: I;
|
|
202
232
|
}
|
|
233
|
+
/** Options for `em.findOne()`, extends FindOptions with optimistic lock version support. */
|
|
203
234
|
export interface FindOneOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'lockMode'> {
|
|
204
235
|
lockMode?: LockMode;
|
|
205
236
|
lockVersion?: number | Date;
|
|
206
237
|
}
|
|
238
|
+
/** Options for `em.findOneOrFail()`, adds a custom error handler for missing entities. */
|
|
207
239
|
export interface FindOneOrFailOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends FindOneOptions<T, P, F, E> {
|
|
208
240
|
failHandler?: (entityName: string, where: Dictionary | IPrimaryKey | any) => Error;
|
|
209
241
|
strict?: boolean;
|
|
210
242
|
}
|
|
243
|
+
/** Options for native insert and update operations. */
|
|
211
244
|
export interface NativeInsertUpdateOptions<T> {
|
|
212
245
|
convertCustomTypes?: boolean;
|
|
213
246
|
ctx?: Transaction;
|
|
@@ -223,9 +256,11 @@ export interface NativeInsertUpdateOptions<T> {
|
|
|
223
256
|
/** @internal */
|
|
224
257
|
em?: EntityManager;
|
|
225
258
|
}
|
|
259
|
+
/** Options for batch native insert and update operations. */
|
|
226
260
|
export interface NativeInsertUpdateManyOptions<T> extends NativeInsertUpdateOptions<T> {
|
|
227
261
|
processCollections?: boolean;
|
|
228
262
|
}
|
|
263
|
+
/** Options for `em.upsert()`, controlling conflict resolution behavior. */
|
|
229
264
|
export interface UpsertOptions<Entity, Fields extends string = never> extends Omit<NativeInsertUpdateOptions<Entity>, 'upsert'> {
|
|
230
265
|
onConflictFields?: (keyof Entity)[] | Raw;
|
|
231
266
|
onConflictAction?: 'ignore' | 'merge';
|
|
@@ -234,9 +269,11 @@ export interface UpsertOptions<Entity, Fields extends string = never> extends Om
|
|
|
234
269
|
onConflictWhere?: FilterQuery<Entity>;
|
|
235
270
|
disableIdentityMap?: boolean;
|
|
236
271
|
}
|
|
272
|
+
/** Options for `em.upsertMany()`, adds batch size control. */
|
|
237
273
|
export interface UpsertManyOptions<Entity, Fields extends string = never> extends UpsertOptions<Entity, Fields> {
|
|
238
274
|
batchSize?: number;
|
|
239
275
|
}
|
|
276
|
+
/** Options for `em.count()` queries. */
|
|
240
277
|
export interface CountOptions<T extends object, P extends string = never> {
|
|
241
278
|
filters?: FilterOptions;
|
|
242
279
|
schema?: string;
|
|
@@ -268,6 +305,7 @@ export interface CountOptions<T extends object, P extends string = never> {
|
|
|
268
305
|
/** @internal used to apply filters to the auto-joined relations */
|
|
269
306
|
em?: EntityManager;
|
|
270
307
|
}
|
|
308
|
+
/** Options for `em.qb().update()` operations. */
|
|
271
309
|
export interface UpdateOptions<T> {
|
|
272
310
|
filters?: FilterOptions;
|
|
273
311
|
schema?: string;
|
|
@@ -277,6 +315,7 @@ export interface UpdateOptions<T> {
|
|
|
277
315
|
/** sql only */
|
|
278
316
|
unionWhereStrategy?: 'union-all' | 'union';
|
|
279
317
|
}
|
|
318
|
+
/** Options for `em.qb().delete()` operations. */
|
|
280
319
|
export interface DeleteOptions<T> extends DriverMethodOptions {
|
|
281
320
|
filters?: FilterOptions;
|
|
282
321
|
/** sql only */
|
|
@@ -286,6 +325,7 @@ export interface DeleteOptions<T> extends DriverMethodOptions {
|
|
|
286
325
|
/** @internal */
|
|
287
326
|
em?: EntityManager;
|
|
288
327
|
}
|
|
328
|
+
/** Options for `em.nativeDelete()` operations. */
|
|
289
329
|
export interface NativeDeleteOptions<T> extends DriverMethodOptions {
|
|
290
330
|
filters?: FilterOptions;
|
|
291
331
|
/** sql only */
|
|
@@ -295,17 +335,20 @@ export interface NativeDeleteOptions<T> extends DriverMethodOptions {
|
|
|
295
335
|
/** @internal */
|
|
296
336
|
em?: EntityManager;
|
|
297
337
|
}
|
|
338
|
+
/** Options for pessimistic and optimistic lock operations. */
|
|
298
339
|
export interface LockOptions extends DriverMethodOptions {
|
|
299
340
|
lockMode?: LockMode;
|
|
300
341
|
lockVersion?: number | Date;
|
|
301
342
|
lockTableAliases?: string[];
|
|
302
343
|
logging?: LoggingOptions;
|
|
303
344
|
}
|
|
345
|
+
/** Base options shared by all driver methods (transaction context, schema, logging). */
|
|
304
346
|
export interface DriverMethodOptions {
|
|
305
347
|
ctx?: Transaction;
|
|
306
348
|
schema?: string;
|
|
307
349
|
loggerContext?: LogContext;
|
|
308
350
|
}
|
|
351
|
+
/** MongoDB-style collation options for locale-aware string comparison. */
|
|
309
352
|
export interface CollationOptions {
|
|
310
353
|
locale: string;
|
|
311
354
|
caseLevel?: boolean;
|
|
@@ -316,6 +359,7 @@ export interface CollationOptions {
|
|
|
316
359
|
maxVariable?: string;
|
|
317
360
|
backwards?: boolean;
|
|
318
361
|
}
|
|
362
|
+
/** Options for `em.getReference()`, controlling wrapping and type conversion. */
|
|
319
363
|
export interface GetReferenceOptions {
|
|
320
364
|
wrapped?: boolean;
|
|
321
365
|
convertCustomTypes?: boolean;
|
package/entity/BaseEntity.d.ts
CHANGED
|
@@ -5,10 +5,15 @@ import type { EntityLoaderOptions } from './EntityLoader.js';
|
|
|
5
5
|
import { type SerializeOptions } from '../serialization/EntitySerializer.js';
|
|
6
6
|
import type { FindOneOptions } from '../drivers/IDatabaseDriver.js';
|
|
7
7
|
import type { PopulatePath } from '../enums.js';
|
|
8
|
+
/** Base class for entities providing convenience methods like `assign()`, `toObject()`, and `populate()`. */
|
|
8
9
|
export declare abstract class BaseEntity {
|
|
10
|
+
/** Returns whether the entity has been fully loaded from the database. */
|
|
9
11
|
isInitialized(): boolean;
|
|
12
|
+
/** Marks the entity as populated or not for serialization purposes. */
|
|
10
13
|
populated(populated?: boolean): void;
|
|
14
|
+
/** Loads the specified relations on this entity. */
|
|
11
15
|
populate<Entity extends this = this, Hint extends string = never, Fields extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity, Fields>): Promise<Loaded<Entity, Hint>>;
|
|
16
|
+
/** Returns a Reference wrapper for this entity. */
|
|
12
17
|
toReference<Entity extends this = this>(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
|
|
13
18
|
/**
|
|
14
19
|
* Converts the entity to a plain object representation.
|
|
@@ -72,10 +77,16 @@ export declare abstract class BaseEntity {
|
|
|
72
77
|
* @param ignoreFields - Array of field names to omit from the result.
|
|
73
78
|
*/
|
|
74
79
|
toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
|
|
80
|
+
/** Converts the entity to a plain object, including all properties regardless of serialization rules. */
|
|
75
81
|
toPOJO<Entity extends this = this>(): EntityDTO<Entity>;
|
|
82
|
+
/** Serializes the entity with control over which relations and fields to include or exclude. */
|
|
76
83
|
serialize<Entity extends this = this, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Naked, Hint, Exclude>): SerializeDTO<Naked, Hint, Exclude>;
|
|
84
|
+
/** Assigns the given data to this entity, updating its properties and relations. */
|
|
77
85
|
assign<Entity extends this, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Convert extends boolean = false, Data extends EntityData<Naked, Convert> | Partial<EntityDTO<Naked>> = EntityData<Naked, Convert> | Partial<EntityDTO<Naked>>>(data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions<Convert>): MergeSelected<Entity, Naked, keyof Data & string>;
|
|
86
|
+
/** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
|
|
78
87
|
init<Entity extends this = this, Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
|
|
88
|
+
/** Returns the database schema this entity belongs to. */
|
|
79
89
|
getSchema(): string | undefined;
|
|
90
|
+
/** Sets the database schema for this entity. */
|
|
80
91
|
setSchema(schema?: string): void;
|
|
81
92
|
}
|
package/entity/BaseEntity.js
CHANGED
|
@@ -2,37 +2,48 @@ import { Reference } from './Reference.js';
|
|
|
2
2
|
import { EntityAssigner } from './EntityAssigner.js';
|
|
3
3
|
import { EntitySerializer } from '../serialization/EntitySerializer.js';
|
|
4
4
|
import { helper } from './wrap.js';
|
|
5
|
+
/** Base class for entities providing convenience methods like `assign()`, `toObject()`, and `populate()`. */
|
|
5
6
|
export class BaseEntity {
|
|
7
|
+
/** Returns whether the entity has been fully loaded from the database. */
|
|
6
8
|
isInitialized() {
|
|
7
9
|
return helper(this).__initialized;
|
|
8
10
|
}
|
|
11
|
+
/** Marks the entity as populated or not for serialization purposes. */
|
|
9
12
|
populated(populated = true) {
|
|
10
13
|
helper(this).populated(populated);
|
|
11
14
|
}
|
|
15
|
+
/** Loads the specified relations on this entity. */
|
|
12
16
|
async populate(populate, options = {}) {
|
|
13
17
|
return helper(this).populate(populate, options);
|
|
14
18
|
}
|
|
19
|
+
/** Returns a Reference wrapper for this entity. */
|
|
15
20
|
toReference() {
|
|
16
21
|
return Reference.create(this);
|
|
17
22
|
}
|
|
18
23
|
toObject(ignoreFields) {
|
|
19
24
|
return helper(this).toObject(ignoreFields);
|
|
20
25
|
}
|
|
26
|
+
/** Converts the entity to a plain object, including all properties regardless of serialization rules. */
|
|
21
27
|
toPOJO() {
|
|
22
28
|
return helper(this).toPOJO();
|
|
23
29
|
}
|
|
30
|
+
/** Serializes the entity with control over which relations and fields to include or exclude. */
|
|
24
31
|
serialize(options) {
|
|
25
32
|
return EntitySerializer.serialize(this, options);
|
|
26
33
|
}
|
|
34
|
+
/** Assigns the given data to this entity, updating its properties and relations. */
|
|
27
35
|
assign(data, options = {}) {
|
|
28
36
|
return EntityAssigner.assign(this, data, options);
|
|
29
37
|
}
|
|
38
|
+
/** Initializes (refreshes) the entity by reloading it from the database. Returns null if not found. */
|
|
30
39
|
init(options) {
|
|
31
40
|
return helper(this).init(options);
|
|
32
41
|
}
|
|
42
|
+
/** Returns the database schema this entity belongs to. */
|
|
33
43
|
getSchema() {
|
|
34
44
|
return helper(this).getSchema();
|
|
35
45
|
}
|
|
46
|
+
/** Sets the database schema for this entity. */
|
|
36
47
|
setSchema(schema) {
|
|
37
48
|
helper(this).setSchema(schema);
|
|
38
49
|
}
|
package/entity/Collection.d.ts
CHANGED
|
@@ -3,11 +3,16 @@ import { Reference } from './Reference.js';
|
|
|
3
3
|
import type { Transaction } from '../connections/Connection.js';
|
|
4
4
|
import type { CountOptions, FindOptions } from '../drivers/IDatabaseDriver.js';
|
|
5
5
|
import type { EntityLoaderOptions } from './EntityLoader.js';
|
|
6
|
+
/** Options for the `Collection.matching()` method to query a subset of collection items from the database. */
|
|
6
7
|
export interface MatchingOptions<T extends object, P extends string = never> extends FindOptions<T, P> {
|
|
8
|
+
/** Additional filtering conditions for the query. */
|
|
7
9
|
where?: FilterQuery<T>;
|
|
10
|
+
/** Whether to store the matched items in the collection (makes it read-only). */
|
|
8
11
|
store?: boolean;
|
|
12
|
+
/** Transaction context for the query. */
|
|
9
13
|
ctx?: Transaction;
|
|
10
14
|
}
|
|
15
|
+
/** Represents a to-many relation (1:m or m:n) as an iterable, managed collection of entities. */
|
|
11
16
|
export declare class Collection<T extends object, O extends object = object> {
|
|
12
17
|
#private;
|
|
13
18
|
readonly owner: O;
|
|
@@ -32,12 +37,15 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
32
37
|
* The value is cached (unless you use the `where` option), use `refresh: true` to force reload it.
|
|
33
38
|
*/
|
|
34
39
|
loadCount(options?: LoadCountOptions<T> | boolean): Promise<number>;
|
|
40
|
+
/** Queries a subset of the collection items from the database with custom filtering, ordering, and pagination. */
|
|
35
41
|
matching<TT extends T, P extends string = never>(options: MatchingOptions<T, P>): Promise<Loaded<TT, P>[]>;
|
|
36
42
|
/**
|
|
37
43
|
* Returns the items (the collection must be initialized)
|
|
38
44
|
*/
|
|
39
45
|
getItems(check?: boolean): T[];
|
|
46
|
+
/** Serializes the collection items to plain JSON objects. Returns an empty array if not initialized. */
|
|
40
47
|
toJSON<TT extends T>(): EntityDTO<TT>[];
|
|
48
|
+
/** Adds one or more items to the collection, propagating the change to the inverse side. Returns the number of items added. */
|
|
41
49
|
add<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>>, ...entities: (TT | Reference<TT>)[]): number;
|
|
42
50
|
/**
|
|
43
51
|
* Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,
|
|
@@ -46,11 +54,17 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
46
54
|
* which tells the ORM we don't want orphaned entities to exist, so we know those should be removed.
|
|
47
55
|
*/
|
|
48
56
|
remove<TT extends T>(entity: TT | Reference<TT> | Iterable<TT | Reference<TT>> | ((item: TT) => boolean), ...entities: (TT | Reference<TT>)[]): number;
|
|
57
|
+
/** Checks whether the collection contains the given item. */
|
|
49
58
|
contains<TT extends T>(item: TT | Reference<TT>, check?: boolean): boolean;
|
|
59
|
+
/** Returns the number of items in the collection. Throws if the collection is not initialized. */
|
|
50
60
|
count(): number;
|
|
61
|
+
/** Returns true if the collection has no items. Throws if the collection is not initialized. */
|
|
51
62
|
isEmpty(): boolean;
|
|
63
|
+
/** Returns whether this collection should be included in serialization based on its populated state. */
|
|
52
64
|
shouldPopulate(populated?: boolean): boolean;
|
|
65
|
+
/** Marks the collection as populated or not for serialization purposes. */
|
|
53
66
|
populated(populated?: boolean | undefined): void;
|
|
67
|
+
/** Initializes the collection by loading its items from the database. */
|
|
54
68
|
init<TT extends T, P extends string = never>(options?: InitCollectionOptions<TT, P>): Promise<LoadedCollection<Loaded<TT, P>>>;
|
|
55
69
|
private getEntityManager;
|
|
56
70
|
private createCondition;
|
|
@@ -63,12 +77,15 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
63
77
|
private reorderItems;
|
|
64
78
|
private cancelOrphanRemoval;
|
|
65
79
|
private validateModification;
|
|
80
|
+
/** Converts all items in the collection to plain DTO objects. */
|
|
66
81
|
toArray<TT extends T>(): EntityDTO<TT>[];
|
|
82
|
+
/** Returns the primary key values (or a specific field) of all items in the collection. */
|
|
67
83
|
getIdentifiers<U extends IPrimaryKey = Primary<T> & IPrimaryKey>(field?: string | string[]): U[];
|
|
68
84
|
/**
|
|
69
85
|
* @internal
|
|
70
86
|
*/
|
|
71
87
|
addWithoutPropagation(entity: T): void;
|
|
88
|
+
/** Replaces all items in the collection with the given items. */
|
|
72
89
|
set(items: Iterable<T | Reference<T>>): void;
|
|
73
90
|
private compare;
|
|
74
91
|
/**
|
|
@@ -129,8 +146,10 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
129
146
|
* If there are more items with the same key, only the first one will be present.
|
|
130
147
|
*/
|
|
131
148
|
indexBy<K1 extends keyof T, K2 extends keyof T = never>(key: K1, valueKey: K2): Record<T[K1] & PropertyKey, T[K2]>;
|
|
149
|
+
/** Returns whether the collection has been initialized. Pass `fully = true` to also check that all items are initialized. */
|
|
132
150
|
isInitialized(fully?: boolean): boolean;
|
|
133
151
|
isDirty(): boolean;
|
|
152
|
+
/** Returns whether the collection was partially loaded (propagation is disabled for partial collections). */
|
|
134
153
|
isPartial(): boolean;
|
|
135
154
|
setDirty(dirty?: boolean): void;
|
|
136
155
|
get length(): number;
|
|
@@ -157,12 +176,19 @@ export declare class Collection<T extends object, O extends object = object> {
|
|
|
157
176
|
protected shouldPropagateToCollection(collection: Collection<O, T>, method: 'add' | 'remove' | 'takeSnapshot'): boolean;
|
|
158
177
|
protected incrementCount(value: number): void;
|
|
159
178
|
}
|
|
179
|
+
/** Options for initializing a collection via `init()` or `load()`. */
|
|
160
180
|
export interface InitCollectionOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends EntityLoaderOptions<T, F, E> {
|
|
181
|
+
/** Whether to use the dataloader for batching collection loads. */
|
|
161
182
|
dataloader?: boolean;
|
|
183
|
+
/** Relations to populate on the loaded items. */
|
|
162
184
|
populate?: Populate<T, P>;
|
|
185
|
+
/** Populate only references (without loading full entities). Works only with M:N collections that use pivot table. */
|
|
163
186
|
ref?: boolean;
|
|
164
187
|
}
|
|
188
|
+
/** Options for the `Collection.loadCount()` method. */
|
|
165
189
|
export interface LoadCountOptions<T extends object> extends CountOptions<T, '*'> {
|
|
190
|
+
/** Whether to reload the count from the database even if it is already cached. */
|
|
166
191
|
refresh?: boolean;
|
|
192
|
+
/** Additional filtering conditions for the count query. */
|
|
167
193
|
where?: FilterQuery<T>;
|
|
168
194
|
}
|