@mikro-orm/core 7.0.0-dev.23 → 7.0.0-dev.231
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 +91 -59
- package/EntityManager.js +303 -251
- package/MikroORM.d.ts +44 -35
- package/MikroORM.js +109 -143
- package/README.md +2 -0
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +17 -8
- package/cache/GeneratedCacheAdapter.d.ts +0 -1
- package/cache/GeneratedCacheAdapter.js +0 -2
- package/cache/index.d.ts +0 -1
- package/cache/index.js +0 -1
- package/connections/Connection.d.ts +12 -5
- package/connections/Connection.js +21 -12
- package/drivers/DatabaseDriver.d.ts +25 -16
- package/drivers/DatabaseDriver.js +118 -35
- package/drivers/IDatabaseDriver.d.ts +42 -19
- package/entity/BaseEntity.d.ts +61 -2
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +101 -29
- package/entity/Collection.js +436 -104
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +26 -18
- package/entity/EntityFactory.d.ts +7 -1
- package/entity/EntityFactory.js +83 -54
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +48 -15
- package/entity/EntityLoader.d.ts +7 -6
- package/entity/EntityLoader.js +221 -93
- package/entity/EntityRepository.d.ts +27 -7
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +1 -5
- package/entity/Reference.js +21 -12
- package/entity/WrappedEntity.d.ts +0 -5
- package/entity/WrappedEntity.js +2 -7
- package/entity/defineEntity.d.ts +380 -310
- package/entity/defineEntity.js +124 -273
- package/entity/index.d.ts +2 -2
- package/entity/index.js +2 -2
- package/entity/utils.js +1 -1
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +8 -6
- package/enums.js +2 -1
- package/errors.d.ts +20 -10
- package/errors.js +55 -23
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +87 -35
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +1 -0
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +7 -6
- package/logging/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +47 -23
- package/metadata/EntitySchema.js +92 -33
- package/metadata/MetadataDiscovery.d.ts +64 -9
- package/metadata/MetadataDiscovery.js +778 -325
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +46 -2
- package/metadata/MetadataStorage.d.ts +13 -11
- package/metadata/MetadataStorage.js +70 -37
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +196 -41
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +1 -1
- package/metadata/types.d.ts +526 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +20 -2
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +19 -11
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +7 -14
- package/platforms/Platform.js +20 -43
- package/serialization/EntitySerializer.d.ts +5 -0
- package/serialization/EntitySerializer.js +47 -27
- package/serialization/EntityTransformer.js +28 -18
- package/serialization/SerializationContext.d.ts +6 -6
- package/serialization/SerializationContext.js +3 -3
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.d.ts +8 -6
- package/types/BigIntType.js +1 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +1 -0
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +2 -2
- package/types/DoubleType.js +1 -1
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/index.d.ts +1 -1
- package/typings.d.ts +381 -171
- package/typings.js +97 -44
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +4 -5
- package/unit-of-work/ChangeSetComputer.d.ts +1 -3
- package/unit-of-work/ChangeSetComputer.js +35 -14
- package/unit-of-work/ChangeSetPersister.d.ts +7 -3
- package/unit-of-work/ChangeSetPersister.js +83 -25
- package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
- package/unit-of-work/CommitOrderCalculator.js +13 -13
- package/unit-of-work/IdentityMap.d.ts +12 -0
- package/unit-of-work/IdentityMap.js +39 -1
- package/unit-of-work/UnitOfWork.d.ts +27 -3
- package/unit-of-work/UnitOfWork.js +258 -92
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +28 -17
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +795 -209
- package/utils/Configuration.js +150 -192
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +0 -3
- package/utils/Cursor.js +24 -11
- package/utils/DataloaderUtils.d.ts +10 -5
- package/utils/DataloaderUtils.js +29 -12
- package/utils/EntityComparator.d.ts +16 -9
- package/utils/EntityComparator.js +158 -58
- package/utils/QueryHelper.d.ts +18 -6
- package/utils/QueryHelper.js +76 -23
- package/utils/RawQueryFragment.d.ts +28 -34
- package/utils/RawQueryFragment.js +35 -71
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.js +28 -4
- package/utils/Utils.d.ts +14 -127
- package/utils/Utils.js +85 -397
- package/utils/clone.js +8 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +97 -0
- package/utils/fs-utils.d.ts +33 -0
- package/utils/fs-utils.js +192 -0
- package/utils/index.d.ts +1 -1
- package/utils/index.js +1 -1
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +46 -3
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -12
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -33
- package/decorators/Entity.js +0 -12
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -4
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -19
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -42
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -34
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -28
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -8
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -14
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -118
- package/entity/ArrayCollection.js +0 -407
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { URL } from 'node:url';
|
|
2
1
|
import { Utils } from '../utils/Utils.js';
|
|
3
2
|
export class Connection {
|
|
4
3
|
config;
|
|
@@ -33,13 +32,32 @@ export class Connection {
|
|
|
33
32
|
.forEach(k => delete this.options[k]);
|
|
34
33
|
}
|
|
35
34
|
/**
|
|
36
|
-
* Ensure the connection exists, this is used to support lazy connect when using `MikroORM
|
|
35
|
+
* Ensure the connection exists, this is used to support lazy connect when using `new MikroORM()` instead of the async `init` method.
|
|
37
36
|
*/
|
|
38
37
|
async ensureConnection() {
|
|
39
38
|
if (!this.connected) {
|
|
40
39
|
await this.connect();
|
|
41
40
|
}
|
|
42
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Execute raw SQL queries, handy from running schema dump loaded from a file.
|
|
44
|
+
* This method doesn't support transactions, as opposed to `orm.schema.execute()`, which is used internally.
|
|
45
|
+
*/
|
|
46
|
+
async executeDump(dump) {
|
|
47
|
+
throw new Error(`Executing SQL dumps is not supported by current driver`);
|
|
48
|
+
}
|
|
49
|
+
async onConnect() {
|
|
50
|
+
const schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
|
|
51
|
+
if (this.type === 'write' && schemaGenerator) {
|
|
52
|
+
if (this.config.get('ensureDatabase')) {
|
|
53
|
+
const options = this.config.get('ensureDatabase');
|
|
54
|
+
await schemaGenerator.ensureDatabase(typeof options === 'boolean' ? {} : { ...options, forceCheck: true });
|
|
55
|
+
}
|
|
56
|
+
if (this.config.get('ensureIndexes')) {
|
|
57
|
+
await schemaGenerator.ensureIndexes();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
43
61
|
async transactional(cb, options) {
|
|
44
62
|
throw new Error(`Transactions are not supported by current driver`);
|
|
45
63
|
}
|
|
@@ -67,7 +85,7 @@ export class Connection {
|
|
|
67
85
|
}
|
|
68
86
|
}
|
|
69
87
|
else {
|
|
70
|
-
const url = new URL(this.config.
|
|
88
|
+
const url = new URL(this.config.get('clientUrl'));
|
|
71
89
|
this.options.host = ret.host = this.options.host ?? this.config.get('host', decodeURIComponent(url.hostname));
|
|
72
90
|
this.options.port = ret.port = this.options.port ?? this.config.get('port', +url.port);
|
|
73
91
|
this.options.user = ret.user = this.options.user ?? this.config.get('user', decodeURIComponent(url.username));
|
|
@@ -76,15 +94,6 @@ export class Connection {
|
|
|
76
94
|
}
|
|
77
95
|
return ret;
|
|
78
96
|
}
|
|
79
|
-
getClientUrl() {
|
|
80
|
-
const options = this.getConnectionOptions();
|
|
81
|
-
const url = new URL(this.config.getClientUrl(true));
|
|
82
|
-
const password = options.password ? ':*****' : '';
|
|
83
|
-
const schema = options.schema && options.schema !== this.platform.getDefaultSchemaName()
|
|
84
|
-
? `?schema=${options.schema}`
|
|
85
|
-
: '';
|
|
86
|
-
return `${url.protocol}//${options.user}${password}@${options.host}:${options.port}${schema}`;
|
|
87
|
-
}
|
|
88
97
|
setMetadata(metadata) {
|
|
89
98
|
this.metadata = metadata;
|
|
90
99
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type CountOptions, type DeleteOptions, type DriverMethodOptions, EntityManagerType, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type OrderDefinition } from './IDatabaseDriver.js';
|
|
2
|
-
import type { ConnectionType, Dictionary, EntityData, EntityDictionary, EntityMetadata, EntityProperty, FilterQuery, PopulateOptions, Primary } from '../typings.js';
|
|
1
|
+
import { type CountOptions, type DeleteOptions, type DriverMethodOptions, EntityManagerType, type FindOneOptions, type FindOptions, type IDatabaseDriver, type LockOptions, type NativeInsertUpdateManyOptions, type NativeInsertUpdateOptions, type OrderDefinition, type StreamOptions } from './IDatabaseDriver.js';
|
|
2
|
+
import type { ConnectionType, Constructor, Dictionary, EntityData, EntityDictionary, EntityMetadata, EntityName, EntityProperty, FilterQuery, PopulateOptions, Primary } from '../typings.js';
|
|
3
3
|
import type { MetadataStorage } from '../metadata/MetadataStorage.js';
|
|
4
4
|
import type { Connection, QueryResult, Transaction } from '../connections/Connection.js';
|
|
5
5
|
import { type Configuration, type ConnectionOptions } from '../utils/Configuration.js';
|
|
@@ -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 type { Logger } from '../logging/Logger.js';
|
|
13
|
+
import { MikroORM } from '../MikroORM.js';
|
|
13
14
|
export declare abstract class DatabaseDriver<C extends Connection> implements IDatabaseDriver<C> {
|
|
14
15
|
readonly config: Configuration;
|
|
15
16
|
protected readonly dependencies: string[];
|
|
@@ -21,29 +22,34 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
|
|
|
21
22
|
protected comparator: EntityComparator;
|
|
22
23
|
protected metadata: MetadataStorage;
|
|
23
24
|
protected constructor(config: Configuration, dependencies: string[]);
|
|
24
|
-
abstract find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName:
|
|
25
|
-
abstract findOne<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName:
|
|
26
|
-
abstract nativeInsert<T extends object>(entityName:
|
|
27
|
-
abstract nativeInsertMany<T extends object>(entityName:
|
|
28
|
-
abstract nativeUpdate<T extends object>(entityName:
|
|
29
|
-
nativeUpdateMany<T extends object>(entityName:
|
|
30
|
-
abstract nativeDelete<T extends object>(entityName:
|
|
31
|
-
abstract count<T extends object, P extends string = never>(entityName:
|
|
25
|
+
abstract find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
|
|
26
|
+
abstract 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>;
|
|
27
|
+
abstract nativeInsert<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
28
|
+
abstract nativeInsertMany<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
|
|
29
|
+
abstract nativeUpdate<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
30
|
+
nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
31
|
+
abstract nativeDelete<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options?: DeleteOptions<T>): Promise<QueryResult<T>>;
|
|
32
|
+
abstract count<T extends object, P extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
|
|
32
33
|
createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
|
|
33
|
-
findVirtual<T extends object>(entityName:
|
|
34
|
-
countVirtual<T extends object>(entityName:
|
|
35
|
-
aggregate(entityName:
|
|
34
|
+
findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
|
|
35
|
+
countVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: CountOptions<T, any>): Promise<number>;
|
|
36
|
+
aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
|
|
36
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[]>>;
|
|
37
38
|
syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
|
|
38
39
|
mapResult<T extends object>(result: EntityDictionary<T>, meta?: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
|
|
39
|
-
connect(
|
|
40
|
-
|
|
40
|
+
connect(options?: {
|
|
41
|
+
skipOnConnect?: boolean;
|
|
42
|
+
}): Promise<C>;
|
|
43
|
+
reconnect(options?: {
|
|
44
|
+
skipOnConnect?: boolean;
|
|
45
|
+
}): Promise<C>;
|
|
41
46
|
getConnection(type?: ConnectionType): C;
|
|
42
47
|
close(force?: boolean): Promise<void>;
|
|
43
48
|
getPlatform(): Platform;
|
|
44
49
|
setMetadata(metadata: MetadataStorage): void;
|
|
45
50
|
getMetadata(): MetadataStorage;
|
|
46
51
|
getDependencies(): string[];
|
|
52
|
+
protected isPopulated<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>, hint: PopulateOptions<T>, name?: string): boolean;
|
|
47
53
|
protected processCursorOptions<T extends object, P extends string>(meta: EntityMetadata<T>, options: FindOptions<T, P, any, any>, orderBy: OrderDefinition<T>): {
|
|
48
54
|
orderBy: OrderDefinition<T>[];
|
|
49
55
|
where: FilterQuery<T>;
|
|
@@ -52,9 +58,10 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
|
|
|
52
58
|
/** @internal */
|
|
53
59
|
mapDataToFieldNames(data: Dictionary, stringifyJsonArrays: boolean, properties?: Record<string, EntityProperty>, convertCustomTypes?: boolean, object?: boolean): Dictionary;
|
|
54
60
|
protected inlineEmbeddables<T extends object>(meta: EntityMetadata<T>, data: T, where?: boolean): void;
|
|
55
|
-
protected getPrimaryKeyFields(
|
|
61
|
+
protected getPrimaryKeyFields<T>(meta: EntityMetadata<T>): string[];
|
|
56
62
|
protected createReplicas(cb: (c: ConnectionOptions) => C): C[];
|
|
57
63
|
lockPessimistic<T extends object>(entity: T, options: LockOptions): Promise<void>;
|
|
64
|
+
abstract stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
|
|
58
65
|
/**
|
|
59
66
|
* @inheritDoc
|
|
60
67
|
*/
|
|
@@ -71,4 +78,6 @@ export declare abstract class DatabaseDriver<C extends Connection> implements ID
|
|
|
71
78
|
schema?: string;
|
|
72
79
|
parentSchema?: string;
|
|
73
80
|
}): string | undefined;
|
|
81
|
+
/** @internal */
|
|
82
|
+
getORMClass(): Constructor<MikroORM>;
|
|
74
83
|
}
|
|
@@ -8,7 +8,9 @@ import { EntityManager } from '../EntityManager.js';
|
|
|
8
8
|
import { CursorError, ValidationError } from '../errors.js';
|
|
9
9
|
import { DriverException } from '../exceptions.js';
|
|
10
10
|
import { helper } from '../entity/wrap.js';
|
|
11
|
+
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
11
12
|
import { JsonType } from '../types/JsonType.js';
|
|
13
|
+
import { MikroORM } from '../MikroORM.js';
|
|
12
14
|
export class DatabaseDriver {
|
|
13
15
|
config;
|
|
14
16
|
dependencies;
|
|
@@ -31,11 +33,11 @@ export class DatabaseDriver {
|
|
|
31
33
|
const EntityManagerClass = this.config.get('entityManager', EntityManager);
|
|
32
34
|
return new EntityManagerClass(this.config, this, this.metadata, useContext);
|
|
33
35
|
}
|
|
34
|
-
/* v8 ignore next
|
|
36
|
+
/* v8 ignore next */
|
|
35
37
|
async findVirtual(entityName, where, options) {
|
|
36
38
|
throw new Error(`Virtual entities are not supported by ${this.constructor.name} driver.`);
|
|
37
39
|
}
|
|
38
|
-
/* v8 ignore next
|
|
40
|
+
/* v8 ignore next */
|
|
39
41
|
async countVirtual(entityName, where, options) {
|
|
40
42
|
throw new Error(`Counting virtual entities is not supported by ${this.constructor.name} driver.`);
|
|
41
43
|
}
|
|
@@ -47,32 +49,35 @@ export class DatabaseDriver {
|
|
|
47
49
|
}
|
|
48
50
|
async syncCollections(collections, options) {
|
|
49
51
|
for (const coll of collections) {
|
|
52
|
+
/* v8 ignore else */
|
|
50
53
|
if (!coll.property.owner) {
|
|
51
54
|
if (coll.getSnapshot() === undefined) {
|
|
52
55
|
throw ValidationError.cannotModifyInverseCollection(coll.owner, coll.property);
|
|
53
56
|
}
|
|
54
57
|
continue;
|
|
55
58
|
}
|
|
56
|
-
/* v8 ignore next
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
/* v8 ignore next */
|
|
60
|
+
{
|
|
61
|
+
const pk = coll.property.targetMeta.primaryKeys[0];
|
|
62
|
+
const data = { [coll.property.name]: coll.getIdentifiers(pk) };
|
|
63
|
+
await this.nativeUpdate(coll.owner.constructor, helper(coll.owner).getPrimaryKey(), data, options);
|
|
64
|
+
}
|
|
60
65
|
}
|
|
61
66
|
}
|
|
62
67
|
mapResult(result, meta, populate = []) {
|
|
63
68
|
if (!result || !meta) {
|
|
64
69
|
return result ?? null;
|
|
65
70
|
}
|
|
66
|
-
return this.comparator.mapResult(meta
|
|
71
|
+
return this.comparator.mapResult(meta, result);
|
|
67
72
|
}
|
|
68
|
-
async connect() {
|
|
69
|
-
await this.connection.connect();
|
|
73
|
+
async connect(options) {
|
|
74
|
+
await this.connection.connect(options);
|
|
70
75
|
await Promise.all(this.replicas.map(replica => replica.connect()));
|
|
71
76
|
return this.connection;
|
|
72
77
|
}
|
|
73
|
-
async reconnect() {
|
|
78
|
+
async reconnect(options) {
|
|
74
79
|
await this.close(true);
|
|
75
|
-
await this.connect();
|
|
80
|
+
await this.connect(options);
|
|
76
81
|
return this.connection;
|
|
77
82
|
}
|
|
78
83
|
getConnection(type = 'write') {
|
|
@@ -105,6 +110,15 @@ export class DatabaseDriver {
|
|
|
105
110
|
getDependencies() {
|
|
106
111
|
return this.dependencies;
|
|
107
112
|
}
|
|
113
|
+
isPopulated(meta, prop, hint, name) {
|
|
114
|
+
if (hint.field === prop.name || hint.field === name || hint.all) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
if (prop.embedded && hint.children && meta.properties[prop.embedded[0]].name === hint.field) {
|
|
118
|
+
return hint.children.some(c => this.isPopulated(meta, prop, c, prop.embedded[1]));
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
108
122
|
processCursorOptions(meta, options, orderBy) {
|
|
109
123
|
const { first, last, before, after, overfetch } = options;
|
|
110
124
|
const limit = first ?? last;
|
|
@@ -139,7 +153,7 @@ export class DatabaseDriver {
|
|
|
139
153
|
}
|
|
140
154
|
const createOrderBy = (prop, direction) => {
|
|
141
155
|
if (Utils.isPlainObject(direction)) {
|
|
142
|
-
const value = Utils.
|
|
156
|
+
const value = Utils.getObjectQueryKeys(direction).reduce((o, key) => {
|
|
143
157
|
Object.assign(o, createOrderBy(key, direction[key]));
|
|
144
158
|
return o;
|
|
145
159
|
}, {});
|
|
@@ -155,22 +169,51 @@ export class DatabaseDriver {
|
|
|
155
169
|
};
|
|
156
170
|
}
|
|
157
171
|
createCursorCondition(definition, offsets, inverse, meta) {
|
|
158
|
-
const createCondition = (prop, direction, offset, eq = false) => {
|
|
159
|
-
if (offset === null) {
|
|
160
|
-
throw CursorError.missingValue(meta.className, prop);
|
|
161
|
-
}
|
|
172
|
+
const createCondition = (prop, direction, offset, eq = false, path = prop) => {
|
|
162
173
|
if (Utils.isPlainObject(direction)) {
|
|
174
|
+
if (offset === undefined) {
|
|
175
|
+
throw CursorError.missingValue(meta.className, path);
|
|
176
|
+
}
|
|
163
177
|
const value = Utils.keys(direction).reduce((o, key) => {
|
|
164
|
-
|
|
165
|
-
throw CursorError.missingValue(meta.className, `${prop}.${key}`);
|
|
166
|
-
}
|
|
167
|
-
Object.assign(o, createCondition(key, direction[key], offset[key], eq));
|
|
178
|
+
Object.assign(o, createCondition(key, direction[key], offset?.[key], eq, `${path}.${key}`));
|
|
168
179
|
return o;
|
|
169
180
|
}, {});
|
|
170
|
-
return
|
|
181
|
+
return { [prop]: value };
|
|
171
182
|
}
|
|
172
|
-
const
|
|
173
|
-
const
|
|
183
|
+
const isDesc = direction === QueryOrderNumeric.DESC || direction.toString().toLowerCase() === 'desc';
|
|
184
|
+
const dirStr = direction.toString().toLowerCase();
|
|
185
|
+
let nullsFirst;
|
|
186
|
+
if (dirStr.includes('nulls first')) {
|
|
187
|
+
nullsFirst = true;
|
|
188
|
+
}
|
|
189
|
+
else if (dirStr.includes('nulls last')) {
|
|
190
|
+
nullsFirst = false;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
// Default: NULLS LAST for ASC, NULLS FIRST for DESC (matches most databases)
|
|
194
|
+
nullsFirst = isDesc;
|
|
195
|
+
}
|
|
196
|
+
const operator = Utils.xor(isDesc, inverse) ? '$lt' : '$gt';
|
|
197
|
+
// For leaf-level properties, undefined means missing value
|
|
198
|
+
if (offset === undefined) {
|
|
199
|
+
throw CursorError.missingValue(meta.className, path);
|
|
200
|
+
}
|
|
201
|
+
// Handle null offset (intentional null cursor value)
|
|
202
|
+
if (offset === null) {
|
|
203
|
+
if (eq) {
|
|
204
|
+
// Equal to null
|
|
205
|
+
return { [prop]: null };
|
|
206
|
+
}
|
|
207
|
+
// Strict comparison with null cursor value
|
|
208
|
+
// hasItemsAfterNull: forward + nullsFirst, or backward + nullsLast
|
|
209
|
+
const hasItemsAfterNull = Utils.xor(nullsFirst, inverse);
|
|
210
|
+
if (hasItemsAfterNull) {
|
|
211
|
+
return { [prop]: { $ne: null } };
|
|
212
|
+
}
|
|
213
|
+
// No items after null in this direction, return impossible condition
|
|
214
|
+
return { [prop]: [] };
|
|
215
|
+
}
|
|
216
|
+
// Non-null offset
|
|
174
217
|
return { [prop]: { [operator + (eq ? 'e' : '')]: offset } };
|
|
175
218
|
};
|
|
176
219
|
const [order, ...otherOrders] = definition;
|
|
@@ -218,6 +261,43 @@ export class DatabaseDriver {
|
|
|
218
261
|
}
|
|
219
262
|
return;
|
|
220
263
|
}
|
|
264
|
+
// Handle polymorphic relations - convert tuple or PolymorphicRef to separate columns
|
|
265
|
+
// Tuple format: ['discriminator', id] or ['discriminator', id1, id2] for composite keys
|
|
266
|
+
// Must be checked BEFORE joinColumns array handling since polymorphic uses fieldNames (includes discriminator)
|
|
267
|
+
if (prop.polymorphic && prop.fieldNames && prop.fieldNames.length >= 2) {
|
|
268
|
+
let discriminator;
|
|
269
|
+
let ids;
|
|
270
|
+
if (Array.isArray(data[k]) && typeof data[k][0] === 'string' && prop.discriminatorMap?.[data[k][0]]) {
|
|
271
|
+
// Tuple format: ['discriminator', ...ids]
|
|
272
|
+
const [disc, ...rest] = data[k];
|
|
273
|
+
discriminator = disc;
|
|
274
|
+
ids = rest;
|
|
275
|
+
}
|
|
276
|
+
else if (data[k] instanceof PolymorphicRef) {
|
|
277
|
+
// PolymorphicRef wrapper (internal use)
|
|
278
|
+
discriminator = data[k].discriminator;
|
|
279
|
+
const polyId = data[k].id;
|
|
280
|
+
// Handle object-style composite key IDs like { tenantId: 1, orgId: 100 }
|
|
281
|
+
if (polyId && typeof polyId === 'object' && !Array.isArray(polyId)) {
|
|
282
|
+
const targetEntity = prop.discriminatorMap?.[discriminator];
|
|
283
|
+
const targetMeta = this.metadata.get(targetEntity);
|
|
284
|
+
ids = targetMeta.primaryKeys.map(pk => polyId[pk]);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
ids = Utils.asArray(polyId);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (discriminator) {
|
|
291
|
+
const discriminatorColumn = prop.fieldNames[0];
|
|
292
|
+
const idColumns = prop.fieldNames.slice(1);
|
|
293
|
+
delete data[k];
|
|
294
|
+
data[discriminatorColumn] = discriminator;
|
|
295
|
+
idColumns.forEach((col, idx) => {
|
|
296
|
+
data[col] = ids[idx];
|
|
297
|
+
});
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
221
301
|
if (prop.joinColumns && Array.isArray(data[k])) {
|
|
222
302
|
const copy = Utils.flatten(data[k]);
|
|
223
303
|
delete data[k];
|
|
@@ -244,7 +324,7 @@ export class DatabaseDriver {
|
|
|
244
324
|
return data;
|
|
245
325
|
}
|
|
246
326
|
inlineEmbeddables(meta, data, where) {
|
|
247
|
-
/* v8 ignore next
|
|
327
|
+
/* v8 ignore next */
|
|
248
328
|
if (data == null) {
|
|
249
329
|
return;
|
|
250
330
|
}
|
|
@@ -264,38 +344,38 @@ export class DatabaseDriver {
|
|
|
264
344
|
// explicitly allow `$exists`, `$eq` and `$ne` operators here as they can't be misused this way
|
|
265
345
|
const operator = Object.keys(data[prop.name]).some(f => Utils.isOperator(f) && !['$exists', '$ne', '$eq'].includes(f));
|
|
266
346
|
if (operator) {
|
|
267
|
-
throw ValidationError.cannotUseOperatorsInsideEmbeddables(meta.
|
|
347
|
+
throw ValidationError.cannotUseOperatorsInsideEmbeddables(meta.class, prop.name, data);
|
|
268
348
|
}
|
|
269
349
|
if (prop.object && where) {
|
|
270
350
|
const inline = (payload, sub, path) => {
|
|
271
351
|
if (sub.kind === ReferenceKind.EMBEDDED && Utils.isObject(payload[sub.embedded[1]])) {
|
|
272
352
|
return Object.keys(payload[sub.embedded[1]]).forEach(kkk => {
|
|
273
353
|
if (!sub.embeddedProps[kkk]) {
|
|
274
|
-
throw ValidationError.invalidEmbeddableQuery(meta.
|
|
354
|
+
throw ValidationError.invalidEmbeddableQuery(meta.class, kkk, sub.type);
|
|
275
355
|
}
|
|
276
|
-
inline(payload[sub.embedded[1]], sub.embeddedProps[kkk], [...path, sub.
|
|
356
|
+
inline(payload[sub.embedded[1]], sub.embeddedProps[kkk], [...path, sub.fieldNames[0]]);
|
|
277
357
|
});
|
|
278
358
|
}
|
|
279
|
-
data[`${path.join('.')}.${sub.
|
|
359
|
+
data[`${path.join('.')}.${sub.fieldNames[0]}`] = payload[sub.embedded[1]];
|
|
280
360
|
};
|
|
281
361
|
const parentPropName = kk.substring(0, kk.indexOf('.'));
|
|
282
362
|
// we might be using some native JSON operator, e.g. with mongodb's `$geoWithin` or `$exists`
|
|
283
363
|
if (props[kk]) {
|
|
284
364
|
/* v8 ignore next */
|
|
285
|
-
inline(data[prop.name], props[kk] || props[parentPropName], [prop.
|
|
365
|
+
inline(data[prop.name], props[kk] || props[parentPropName], [prop.fieldNames[0]]);
|
|
286
366
|
}
|
|
287
367
|
else if (props[parentPropName]) {
|
|
288
|
-
data[`${prop.
|
|
368
|
+
data[`${prop.fieldNames[0]}.${kk}`] = data[prop.name][kk];
|
|
289
369
|
}
|
|
290
370
|
else {
|
|
291
371
|
unknownProp = true;
|
|
292
372
|
}
|
|
293
373
|
}
|
|
294
374
|
else if (props[kk]) {
|
|
295
|
-
data[props[kk].
|
|
375
|
+
data[props[kk].fieldNames[0]] = data[prop.name][props[kk].embedded[1]];
|
|
296
376
|
}
|
|
297
377
|
else {
|
|
298
|
-
throw ValidationError.invalidEmbeddableQuery(meta.
|
|
378
|
+
throw ValidationError.invalidEmbeddableQuery(meta.class, kk, prop.type);
|
|
299
379
|
}
|
|
300
380
|
});
|
|
301
381
|
if (!unknownProp) {
|
|
@@ -304,9 +384,8 @@ export class DatabaseDriver {
|
|
|
304
384
|
}
|
|
305
385
|
});
|
|
306
386
|
}
|
|
307
|
-
getPrimaryKeyFields(
|
|
308
|
-
|
|
309
|
-
return meta ? Utils.flatten(meta.getPrimaryProps().map(pk => pk.fieldNames)) : [this.config.getNamingStrategy().referenceColumnName()];
|
|
387
|
+
getPrimaryKeyFields(meta) {
|
|
388
|
+
return meta.getPrimaryProps().flatMap(pk => pk.fieldNames);
|
|
310
389
|
}
|
|
311
390
|
createReplicas(cb) {
|
|
312
391
|
const replicas = this.config.get('replicas', []);
|
|
@@ -372,4 +451,8 @@ export class DatabaseDriver {
|
|
|
372
451
|
const schemaName = meta?.schema === '*' ? this.config.getSchema() : meta?.schema;
|
|
373
452
|
return options?.schema ?? options?.parentSchema ?? schemaName ?? this.config.getSchema();
|
|
374
453
|
}
|
|
454
|
+
/** @internal */
|
|
455
|
+
getORMClass() {
|
|
456
|
+
return MikroORM;
|
|
457
|
+
}
|
|
375
458
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ConnectionType, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary,
|
|
1
|
+
import type { ConnectionType, EntityData, EntityMetadata, EntityProperty, FilterQuery, Primary, Dictionary, IPrimaryKey, PopulateOptions, EntityDictionary, AutoPath, ObjectQuery, FilterObject, Populate, EntityName } from '../typings.js';
|
|
2
2
|
import type { Connection, QueryResult, Transaction } from '../connections/Connection.js';
|
|
3
3
|
import type { FlushMode, LockMode, QueryOrderMap, QueryFlag, LoadStrategy, PopulateHint, PopulatePath } from '../enums.js';
|
|
4
4
|
import type { Platform } from '../platforms/Platform.js';
|
|
@@ -8,33 +8,38 @@ import type { EntityManager } from '../EntityManager.js';
|
|
|
8
8
|
import type { DriverException } from '../exceptions.js';
|
|
9
9
|
import type { Configuration } from '../utils/Configuration.js';
|
|
10
10
|
import type { LoggingOptions, LogContext } from '../logging/Logger.js';
|
|
11
|
-
import type {
|
|
11
|
+
import type { Raw } from '../utils/RawQueryFragment.js';
|
|
12
12
|
export declare const EntityManagerType: unique symbol;
|
|
13
13
|
export interface IDatabaseDriver<C extends Connection = Connection> {
|
|
14
14
|
[EntityManagerType]: EntityManager<this>;
|
|
15
15
|
readonly config: Configuration;
|
|
16
16
|
createEntityManager(useContext?: boolean): this[typeof EntityManagerType];
|
|
17
|
-
connect(
|
|
17
|
+
connect(options?: {
|
|
18
|
+
skipOnConnect?: boolean;
|
|
19
|
+
}): Promise<C>;
|
|
18
20
|
close(force?: boolean): Promise<void>;
|
|
19
|
-
reconnect(
|
|
21
|
+
reconnect(options?: {
|
|
22
|
+
skipOnConnect?: boolean;
|
|
23
|
+
}): Promise<C>;
|
|
20
24
|
getConnection(type?: ConnectionType): C;
|
|
21
25
|
/**
|
|
22
26
|
* Finds selection of entities
|
|
23
27
|
*/
|
|
24
|
-
find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName:
|
|
28
|
+
find<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: FindOptions<T, P, F, E>): Promise<EntityData<T>[]>;
|
|
25
29
|
/**
|
|
26
30
|
* Finds single entity (table row, document)
|
|
27
31
|
*/
|
|
28
|
-
findOne<T extends object, P extends string = never, F extends string = '*', E extends string = never>(entityName:
|
|
29
|
-
findVirtual<T extends object>(entityName:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
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>;
|
|
33
|
+
findVirtual<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: FindOptions<T, any, any, any>): Promise<EntityData<T>[]>;
|
|
34
|
+
stream<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options: StreamOptions<T>): AsyncIterableIterator<T>;
|
|
35
|
+
nativeInsert<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
36
|
+
nativeInsertMany<T extends object>(entityName: EntityName<T>, data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>, transform?: (sql: string) => string): Promise<QueryResult<T>>;
|
|
37
|
+
nativeUpdate<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, data: EntityDictionary<T>, options?: NativeInsertUpdateOptions<T>): Promise<QueryResult<T>>;
|
|
38
|
+
nativeUpdateMany<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>[], data: EntityDictionary<T>[], options?: NativeInsertUpdateManyOptions<T>): Promise<QueryResult<T>>;
|
|
39
|
+
nativeDelete<T extends object>(entityName: EntityName<T>, where: FilterQuery<T>, options?: NativeDeleteOptions<T>): Promise<QueryResult<T>>;
|
|
35
40
|
syncCollections<T extends object, O extends object>(collections: Iterable<Collection<T, O>>, options?: DriverMethodOptions): Promise<void>;
|
|
36
|
-
count<T extends object, P extends string = never>(entityName:
|
|
37
|
-
aggregate(entityName:
|
|
41
|
+
count<T extends object, P extends string = never>(entityName: EntityName<T>, where: FilterQuery<T>, options?: CountOptions<T, P>): Promise<number>;
|
|
42
|
+
aggregate(entityName: EntityName, pipeline: any[]): Promise<any[]>;
|
|
38
43
|
mapResult<T extends object>(result: EntityDictionary<T>, meta: EntityMetadata<T>, populate?: PopulateOptions<T>[]): EntityData<T> | null;
|
|
39
44
|
/**
|
|
40
45
|
* When driver uses pivot tables for M:N, this method will load identifiers for given collections from them
|
|
@@ -68,6 +73,18 @@ export type OrderDefinition<T> = (QueryOrderMap<T> & {
|
|
|
68
73
|
export interface FindAllOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends FindOptions<T, P, F, E> {
|
|
69
74
|
where?: FilterQuery<T>;
|
|
70
75
|
}
|
|
76
|
+
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'> {
|
|
77
|
+
/**
|
|
78
|
+
* When populating to-many relations, the ORM streams fully merged entities instead of yielding every row.
|
|
79
|
+
* You can opt out of this behavior by specifying `mergeResults: false`. This will yield every row from
|
|
80
|
+
* the SQL result, but still mapped to entities, meaning that to-many collections will contain at most
|
|
81
|
+
* a single item, and you will get duplicate root entities when they have multiple items in the populated
|
|
82
|
+
* collection.
|
|
83
|
+
*
|
|
84
|
+
* @default true
|
|
85
|
+
*/
|
|
86
|
+
mergeResults?: boolean;
|
|
87
|
+
}
|
|
71
88
|
export type FilterOptions = Dictionary<boolean | Dictionary> | string[] | boolean;
|
|
72
89
|
export interface LoadHint<Entity, Hint extends string = never, Fields extends string = PopulatePath.ALL, Excludes extends string = never> {
|
|
73
90
|
populate?: Populate<Entity, Hint>;
|
|
@@ -127,7 +144,7 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
|
|
|
127
144
|
flags?: QueryFlag[];
|
|
128
145
|
/** sql only */
|
|
129
146
|
groupBy?: string | string[];
|
|
130
|
-
having?:
|
|
147
|
+
having?: FilterQuery<Entity>;
|
|
131
148
|
/** sql only */
|
|
132
149
|
strategy?: LoadStrategy | `${LoadStrategy}`;
|
|
133
150
|
flushMode?: FlushMode | `${FlushMode}`;
|
|
@@ -149,10 +166,10 @@ export interface FindOptions<Entity, Hint extends string = never, Fields extends
|
|
|
149
166
|
/** @internal used to apply filters to the auto-joined relations */
|
|
150
167
|
em?: EntityManager;
|
|
151
168
|
}
|
|
152
|
-
export interface FindByCursorOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never, I extends boolean = true> extends Omit<
|
|
169
|
+
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'> {
|
|
153
170
|
includeCount?: I;
|
|
154
171
|
}
|
|
155
|
-
export interface FindOneOptions<T
|
|
172
|
+
export interface FindOneOptions<T, P extends string = never, F extends string = '*', E extends string = never> extends Omit<FindOptions<T, P, F, E>, 'limit' | 'lockMode'> {
|
|
156
173
|
lockMode?: LockMode;
|
|
157
174
|
lockVersion?: number | Date;
|
|
158
175
|
}
|
|
@@ -172,10 +189,11 @@ export interface NativeInsertUpdateManyOptions<T> extends NativeInsertUpdateOpti
|
|
|
172
189
|
processCollections?: boolean;
|
|
173
190
|
}
|
|
174
191
|
export interface UpsertOptions<Entity, Fields extends string = never> extends Omit<NativeInsertUpdateOptions<Entity>, 'upsert'> {
|
|
175
|
-
onConflictFields?: (keyof Entity)[] |
|
|
192
|
+
onConflictFields?: (keyof Entity)[] | Raw;
|
|
176
193
|
onConflictAction?: 'ignore' | 'merge';
|
|
177
194
|
onConflictMergeFields?: AutoPath<Entity, Fields, `${PopulatePath.ALL}`>[];
|
|
178
195
|
onConflictExcludeFields?: AutoPath<Entity, Fields, `${PopulatePath.ALL}`>[];
|
|
196
|
+
onConflictWhere?: FilterQuery<Entity>;
|
|
179
197
|
disableIdentityMap?: boolean;
|
|
180
198
|
}
|
|
181
199
|
export interface UpsertManyOptions<Entity, Fields extends string = never> extends UpsertOptions<Entity, Fields> {
|
|
@@ -185,7 +203,7 @@ export interface CountOptions<T extends object, P extends string = never> {
|
|
|
185
203
|
filters?: FilterOptions;
|
|
186
204
|
schema?: string;
|
|
187
205
|
groupBy?: string | readonly string[];
|
|
188
|
-
having?:
|
|
206
|
+
having?: FilterQuery<T>;
|
|
189
207
|
cache?: boolean | number | [string, number];
|
|
190
208
|
populate?: Populate<T, P>;
|
|
191
209
|
populateWhere?: ObjectQuery<T> | PopulateHint | `${PopulateHint}`;
|
|
@@ -230,4 +248,9 @@ export interface GetReferenceOptions {
|
|
|
230
248
|
wrapped?: boolean;
|
|
231
249
|
convertCustomTypes?: boolean;
|
|
232
250
|
schema?: string;
|
|
251
|
+
/**
|
|
252
|
+
* Property name to use for identity map lookup instead of the primary key.
|
|
253
|
+
* This is useful for creating references by unique non-PK properties.
|
|
254
|
+
*/
|
|
255
|
+
key?: string;
|
|
233
256
|
}
|
package/entity/BaseEntity.d.ts
CHANGED
|
@@ -4,14 +4,73 @@ import { type AssignOptions } from './EntityAssigner.js';
|
|
|
4
4
|
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
|
+
import type { PopulatePath } from '../enums.js';
|
|
7
8
|
export declare abstract class BaseEntity {
|
|
8
9
|
isInitialized(): boolean;
|
|
9
|
-
isTouched(): boolean;
|
|
10
10
|
populated(populated?: boolean): void;
|
|
11
|
-
populate<Entity extends this = this, Hint extends string = never>(populate: AutoPath<Entity, Hint>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
|
|
11
|
+
populate<Entity extends this = this, Hint extends string = never>(populate: AutoPath<Entity, Hint, PopulatePath.ALL>[] | false, options?: EntityLoaderOptions<Entity>): Promise<Loaded<Entity, Hint>>;
|
|
12
12
|
toReference<Entity extends this = this>(): Ref<Entity> & LoadedReference<Loaded<Entity, AddEager<Entity>>>;
|
|
13
|
+
/**
|
|
14
|
+
* Converts the entity to a plain object representation.
|
|
15
|
+
*
|
|
16
|
+
* **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
|
|
17
|
+
* the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
|
|
18
|
+
* `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
|
|
19
|
+
* This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
|
|
20
|
+
*
|
|
21
|
+
* For correct typing that reflects loaded relations, use `wrap()`:
|
|
22
|
+
* ```ts
|
|
23
|
+
* const result = await em.find(User, {}, { populate: ['profile'] });
|
|
24
|
+
* // Type: EntityDTO<User> (profile is number)
|
|
25
|
+
* const obj1 = result[0].toObject();
|
|
26
|
+
* // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
|
|
27
|
+
* const obj2 = wrap(result[0]).toObject();
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Runtime values are correct in both cases - only the static types differ.
|
|
31
|
+
*/
|
|
13
32
|
toObject<Entity extends this = this>(): EntityDTO<Entity>;
|
|
33
|
+
/**
|
|
34
|
+
* Converts the entity to a plain object representation.
|
|
35
|
+
*
|
|
36
|
+
* **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
|
|
37
|
+
* the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
|
|
38
|
+
* `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
|
|
39
|
+
* This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
|
|
40
|
+
*
|
|
41
|
+
* For correct typing that reflects loaded relations, use `wrap()`:
|
|
42
|
+
* ```ts
|
|
43
|
+
* const result = await em.find(User, {}, { populate: ['profile'] });
|
|
44
|
+
* // Type: EntityDTO<User> (profile is number)
|
|
45
|
+
* const obj1 = result[0].toObject();
|
|
46
|
+
* // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
|
|
47
|
+
* const obj2 = wrap(result[0]).toObject();
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* Runtime values are correct in both cases - only the static types differ.
|
|
51
|
+
*/
|
|
14
52
|
toObject<Entity extends this = this>(ignoreFields: never[]): EntityDTO<Entity>;
|
|
53
|
+
/**
|
|
54
|
+
* Converts the entity to a plain object representation.
|
|
55
|
+
*
|
|
56
|
+
* **Note on typing with `Loaded` entities:** When called on a `Loaded<Entity, 'relation'>` type,
|
|
57
|
+
* the return type will be `EntityDTO<Entity>` (with relations as primary keys), not
|
|
58
|
+
* `EntityDTO<Loaded<Entity, 'relation'>>` (with loaded relations as nested objects).
|
|
59
|
+
* This is a TypeScript limitation - the `this` type resolves to the class, not the `Loaded` wrapper.
|
|
60
|
+
*
|
|
61
|
+
* For correct typing that reflects loaded relations, use `wrap()`:
|
|
62
|
+
* ```ts
|
|
63
|
+
* const result = await em.find(User, {}, { populate: ['profile'] });
|
|
64
|
+
* // Type: EntityDTO<User> (profile is number)
|
|
65
|
+
* const obj1 = result[0].toObject();
|
|
66
|
+
* // Type: EntityDTO<Loaded<User, 'profile'>> (profile is nested object)
|
|
67
|
+
* const obj2 = wrap(result[0]).toObject();
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* Runtime values are correct in both cases - only the static types differ.
|
|
71
|
+
*
|
|
72
|
+
* @param ignoreFields - Array of field names to omit from the result.
|
|
73
|
+
*/
|
|
15
74
|
toObject<Entity extends this = this, Ignored extends EntityKey<Entity> = never>(ignoreFields: Ignored[]): Omit<EntityDTO<Entity>, Ignored>;
|
|
16
75
|
toPOJO<Entity extends this = this>(): EntityDTO<Entity>;
|
|
17
76
|
serialize<Entity extends this = this, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Hint extends string = never, Exclude extends string = never>(options?: SerializeOptions<Naked, Hint, Exclude>): EntityDTO<Loaded<Naked, Hint>>;
|