@mikro-orm/core 7.0.15-dev.1 → 7.0.15-dev.10
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.js +7 -4
- package/entity/defineEntity.d.ts +14 -7
- package/package.json +1 -1
- package/typings.d.ts +7 -4
- package/utils/AbstractMigrator.d.ts +2 -2
- package/utils/AbstractMigrator.js +15 -13
- package/utils/Utils.js +1 -1
package/EntityManager.js
CHANGED
|
@@ -355,7 +355,8 @@ export class EntityManager {
|
|
|
355
355
|
const field = hint.field.split(':')[0];
|
|
356
356
|
const prop = meta.properties[field];
|
|
357
357
|
const strategy = getLoadingStrategy(prop.strategy || hint.strategy || options.strategy || this.config.get('loadStrategy'), prop.kind);
|
|
358
|
-
const joined = strategy === LoadStrategy.JOINED
|
|
358
|
+
const joined = (strategy === LoadStrategy.JOINED || (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner)) &&
|
|
359
|
+
prop.kind !== ReferenceKind.SCALAR;
|
|
359
360
|
if (!joined && !hint.filter) {
|
|
360
361
|
continue;
|
|
361
362
|
}
|
|
@@ -424,11 +425,13 @@ export class EntityManager {
|
|
|
424
425
|
const populated = options.populate.filter(({ field }) => field.split(':')[0] === prop.name);
|
|
425
426
|
let found = false;
|
|
426
427
|
for (const hint of populated) {
|
|
427
|
-
|
|
428
|
+
const strategy = getLoadingStrategy(prop.strategy || hint.strategy || options.strategy || this.config.get('loadStrategy'), prop.kind);
|
|
429
|
+
// inverse 1:1 always produces a JOIN (forced by `joinedProps()`), regardless of strategy
|
|
430
|
+
const willJoin = strategy === LoadStrategy.JOINED || (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner);
|
|
431
|
+
if (!hint.all || willJoin) {
|
|
428
432
|
hint.filter = true;
|
|
429
433
|
}
|
|
430
|
-
|
|
431
|
-
if (hint.field === `${prop.name}:ref` || (hint.filter && strategy === LoadStrategy.JOINED)) {
|
|
434
|
+
if (hint.field === `${prop.name}:ref` || (hint.filter && willJoin)) {
|
|
432
435
|
found = true;
|
|
433
436
|
}
|
|
434
437
|
}
|
package/entity/defineEntity.d.ts
CHANGED
|
@@ -556,12 +556,15 @@ export type PropertyBuilders = {
|
|
|
556
556
|
/** Own keys + base entity keys (when TBase is not `never`). Guards against `keyof never = string | number | symbol`. */
|
|
557
557
|
type AllKeys<TProperties, TBase> = keyof TProperties | (IsNever<TBase> extends true ? never : keyof TBase);
|
|
558
558
|
/** Metadata descriptor for `defineEntity()`, combining entity options with property definitions. */
|
|
559
|
-
export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository' | 'filters' | 'orderBy'> {
|
|
559
|
+
export interface EntityMetadataWithProperties<TName extends string, TTableName extends string, TProperties extends Record<string, any>, TPK extends (keyof TProperties)[] | undefined = undefined, TBase = never, TRepository = never, TForceObject extends boolean = false, TDiscriminatorColumn extends string | undefined = undefined, TDiscriminatorValue extends string | number | undefined = undefined, TBaseDiscriminatorColumn extends string | undefined = undefined> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminatorColumn' | 'discriminatorValue' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'repository' | 'filters' | 'orderBy'> {
|
|
560
560
|
name: TName;
|
|
561
561
|
tableName?: TTableName;
|
|
562
562
|
extends?: {
|
|
563
563
|
'~entity': TBase;
|
|
564
|
-
|
|
564
|
+
'~discriminatorColumn'?: TBaseDiscriminatorColumn;
|
|
565
|
+
} | (EntityCtor<TBase> & {
|
|
566
|
+
'~discriminatorColumn'?: TBaseDiscriminatorColumn;
|
|
567
|
+
});
|
|
565
568
|
properties: TProperties | ((properties: PropertyBuilders) => TProperties);
|
|
566
569
|
primaryKeys?: TPK & InferPrimaryKeyConstraint<TProperties>[];
|
|
567
570
|
hooks?: DefineEntityHooks;
|
|
@@ -581,7 +584,8 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
|
|
|
581
584
|
} | {
|
|
582
585
|
[K in Extract<AllKeys<TProperties, TBase>, string>]?: QueryOrderKeysFlat;
|
|
583
586
|
}[];
|
|
584
|
-
discriminatorColumn?:
|
|
587
|
+
discriminatorColumn?: TDiscriminatorColumn;
|
|
588
|
+
discriminatorValue?: TDiscriminatorValue;
|
|
585
589
|
versionProperty?: AllKeys<TProperties, TBase>;
|
|
586
590
|
concurrencyCheckKeys?: Set<AllKeys<TProperties, TBase>>;
|
|
587
591
|
serializedPrimaryKey?: AllKeys<TProperties, TBase>;
|
|
@@ -611,7 +615,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
|
|
|
611
615
|
}[];
|
|
612
616
|
}
|
|
613
617
|
/** Defines an entity schema using property builders, with full type inference from the property definitions. */
|
|
614
|
-
export declare function defineEntity<const TName extends string, const TTableName extends string, const TProperties extends Record<string, any>, const TPK extends (keyof TProperties)[] | undefined = undefined, const TBase = never, const TRepository = never, const TForceObject extends boolean = false>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository, TForceObject>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject>, TBase, TProperties>;
|
|
618
|
+
export declare function defineEntity<const TName extends string, const TTableName extends string, const TProperties extends Record<string, any>, const TPK extends (keyof TProperties)[] | undefined = undefined, const TBase = never, const TRepository = never, const TForceObject extends boolean = false, const TDiscriminatorColumn extends string | undefined = undefined, const TDiscriminatorValue extends string | number | undefined = undefined, const TBaseDiscriminatorColumn extends string | undefined = undefined>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository, TForceObject, TDiscriminatorColumn, TDiscriminatorValue, TBaseDiscriminatorColumn>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue>, TBase, TProperties, EntityCtor<InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue>>, TDiscriminatorColumn>;
|
|
615
619
|
/** Defines an entity schema for an existing class, combining the class with property builders. */
|
|
616
620
|
export declare function defineEntity<const TEntity = any, const TProperties extends Record<string, any> = Record<string, any>, const TClassName extends string = string, const TTableName extends string = string, const TBase = never, const TClass extends EntityCtor = EntityCtor<TEntity>>(meta: Omit<Partial<EntityMetadata<TEntity>>, 'properties' | 'extends' | 'className' | 'tableName' | 'hooks'> & {
|
|
617
621
|
class: TClass;
|
|
@@ -647,7 +651,7 @@ type InferJSType<T> = T extends typeof Type<infer TValue, any> ? NonNullable<TVa
|
|
|
647
651
|
type InferColumnType<T extends string> = T extends 'int' | 'int4' | 'integer' | 'bigint' | 'int8' | 'int2' | 'tinyint' | 'smallint' | 'mediumint' ? number : T extends 'double' | 'double precision' | 'real' | 'float8' | 'decimal' | 'numeric' | 'float' | 'float4' ? number : T extends 'datetime' | 'time' | 'time with time zone' | 'timestamp' | 'timestamp with time zone' | 'timetz' | 'timestamptz' | 'date' | 'interval' ? Date : T extends 'ObjectId' | 'objectId' | 'character varying' | 'varchar' | 'char' | 'character' | 'uuid' | 'text' | 'tinytext' | 'mediumtext' | 'longtext' | 'enum' ? string : T extends 'boolean' | 'bool' | 'bit' ? boolean : T extends 'blob' | 'tinyblob' | 'mediumblob' | 'longblob' | 'bytea' ? Buffer : T extends 'point' | 'line' | 'lseg' | 'box' | 'circle' | 'path' | 'polygon' | 'geometry' ? number[] : T extends 'tsvector' | 'tsquery' ? string[] : T extends 'json' | 'jsonb' ? any : any;
|
|
648
652
|
type BaseEntityMethodKeys = 'toObject' | 'toPOJO' | 'serialize' | 'assign' | 'populate' | 'init' | 'toReference';
|
|
649
653
|
/** Infers the entity type from a `defineEntity()` properties map, resolving builders, base classes, and primary keys. */
|
|
650
|
-
export type InferEntityFromProperties<Properties extends Record<string, any>, PK extends (keyof Properties)[] | undefined = undefined, Base = never, Repository = never, ForceObject extends boolean = false> = (IsNever<Base> extends true ? {} : Base extends {
|
|
654
|
+
export type InferEntityFromProperties<Properties extends Record<string, any>, PK extends (keyof Properties)[] | undefined = undefined, Base = never, Repository = never, ForceObject extends boolean = false, BaseDiscriminatorColumn extends string | undefined = undefined, DiscriminatorValue extends string | number | undefined = undefined> = (IsNever<Base> extends true ? {} : Base extends {
|
|
651
655
|
toObject(...args: any[]): any;
|
|
652
656
|
} ? Pick<IWrappedEntity<{
|
|
653
657
|
-readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
|
|
@@ -655,17 +659,20 @@ export type InferEntityFromProperties<Properties extends Record<string, any>, PK
|
|
|
655
659
|
[PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
|
|
656
660
|
} & (IsNever<Repository> extends true ? {} : {
|
|
657
661
|
[EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
|
|
658
|
-
}) & Omit<Base, typeof PrimaryKeyProp>>, BaseEntityMethodKeys> : {}) & {
|
|
662
|
+
}) & NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>>, BaseEntityMethodKeys> : {}) & {
|
|
659
663
|
-readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
|
|
660
664
|
} & {
|
|
661
665
|
[PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
|
|
662
666
|
} & (IsNever<Repository> extends true ? {} : {
|
|
663
667
|
[EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
|
|
664
|
-
}) & (IsNever<Base> extends true ? {} : Omit<Base, typeof PrimaryKeyProp>) & (ForceObject extends true ? {
|
|
668
|
+
}) & (IsNever<Base> extends true ? {} : NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>) & (ForceObject extends true ? {
|
|
665
669
|
[Config]?: DefineConfig<{
|
|
666
670
|
forceObject: true;
|
|
667
671
|
}>;
|
|
668
672
|
} : {});
|
|
673
|
+
type NarrowDiscriminator<Base, DiscColumn extends string | undefined, DiscValue> = DiscColumn extends string ? DiscColumn extends keyof Base ? DiscValue extends string | number ? Omit<Base, DiscColumn> & {
|
|
674
|
+
[K in DiscColumn]: DiscValue;
|
|
675
|
+
} : Base : Base : Base;
|
|
669
676
|
type InferCombinedPrimaryKey<Properties extends Record<string, any>, PK, Base> = PK extends undefined ? CombinePrimaryKeys<InferPrimaryKey<Properties>, ExtractBasePrimaryKey<Base>> : PK;
|
|
670
677
|
type ExtractBasePrimaryKey<Base> = Base extends {
|
|
671
678
|
[PrimaryKeyProp]?: infer BasePK;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/core",
|
|
3
|
-
"version": "7.0.15-dev.
|
|
3
|
+
"version": "7.0.15-dev.10",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
package/typings.d.ts
CHANGED
|
@@ -990,7 +990,7 @@ export type MigrationRow = {
|
|
|
990
990
|
* @internal
|
|
991
991
|
*/
|
|
992
992
|
export interface IMigrationRunner {
|
|
993
|
-
run(migration: Migration, method: 'up' | 'down'): Promise<void>;
|
|
993
|
+
run(migration: Migration, method: 'up' | 'down', afterRun?: (tx?: Transaction) => Promise<void>): Promise<void>;
|
|
994
994
|
setMasterMigration(trx: Transaction): void;
|
|
995
995
|
unsetMasterMigration(): void;
|
|
996
996
|
}
|
|
@@ -999,8 +999,8 @@ export interface IMigrationRunner {
|
|
|
999
999
|
*/
|
|
1000
1000
|
export interface IMigratorStorage {
|
|
1001
1001
|
executed(): Promise<string[]>;
|
|
1002
|
-
logMigration(params: Dictionary): Promise<void>;
|
|
1003
|
-
unlogMigration(params: Dictionary): Promise<void>;
|
|
1002
|
+
logMigration(params: Dictionary, tx?: Transaction): Promise<void>;
|
|
1003
|
+
unlogMigration(params: Dictionary, tx?: Transaction): Promise<void>;
|
|
1004
1004
|
getExecutedMigrations(): Promise<MigrationRow[]>;
|
|
1005
1005
|
ensureTable?(): Promise<void>;
|
|
1006
1006
|
setMasterMigration(trx: Transaction): void;
|
|
@@ -1298,15 +1298,18 @@ export type MaybeReturnType<T> = T extends (...args: any[]) => infer R ? R : T;
|
|
|
1298
1298
|
* Extended `EntitySchema` interface that carries additional type-level metadata (entity name, properties, table name).
|
|
1299
1299
|
* Returned by `defineEntity()` to provide strong type inference without explicit generics.
|
|
1300
1300
|
*/
|
|
1301
|
-
export interface EntitySchemaWithMeta<TName extends string = string, TTableName extends string = string, TEntity = any, TBase = never, TProperties extends Record<string, any> = Record<string, any>, TClass extends EntityCtor = EntityCtor<TEntity
|
|
1301
|
+
export interface EntitySchemaWithMeta<TName extends string = string, TTableName extends string = string, TEntity = any, TBase = never, TProperties extends Record<string, any> = Record<string, any>, TClass extends EntityCtor = EntityCtor<TEntity>, TDiscriminatorColumn extends string | undefined = undefined> extends EntitySchema<TEntity, TBase, TClass> {
|
|
1302
1302
|
readonly name: TName;
|
|
1303
1303
|
readonly properties: TProperties;
|
|
1304
1304
|
readonly tableName: TTableName;
|
|
1305
1305
|
/** @internal Direct entity type access - avoids expensive pattern matching */
|
|
1306
1306
|
readonly '~entity': TEntity;
|
|
1307
|
+
/** @internal Type-level marker carrying the discriminator column name, used by `defineEntity()` to narrow inherited discriminator properties to the literal `discriminatorValue` of each child schema. */
|
|
1308
|
+
readonly '~discriminatorColumn'?: TDiscriminatorColumn;
|
|
1307
1309
|
/** @internal */
|
|
1308
1310
|
readonly class: TClass & {
|
|
1309
1311
|
'~entityName'?: TName;
|
|
1312
|
+
'~discriminatorColumn'?: TDiscriminatorColumn;
|
|
1310
1313
|
};
|
|
1311
1314
|
}
|
|
1312
1315
|
/**
|
|
@@ -5,8 +5,8 @@ import type { EntityManagerType, IDatabaseDriver } from '../drivers/IDatabaseDri
|
|
|
5
5
|
interface RunnableMigration {
|
|
6
6
|
name: string;
|
|
7
7
|
path?: string;
|
|
8
|
-
up: () => MaybePromise<void>;
|
|
9
|
-
down: () => MaybePromise<void>;
|
|
8
|
+
up: (afterRun?: (tx?: Transaction) => Promise<void>) => MaybePromise<void>;
|
|
9
|
+
down: (afterRun?: (tx?: Transaction) => Promise<void>) => MaybePromise<void>;
|
|
10
10
|
}
|
|
11
11
|
type MigrateOptions = {
|
|
12
12
|
from?: string | number;
|
|
@@ -98,26 +98,26 @@ export class AbstractMigrator {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
resolve(params) {
|
|
101
|
-
const createMigrationHandler = async (method) => {
|
|
101
|
+
const createMigrationHandler = async (method, afterRun) => {
|
|
102
102
|
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
103
103
|
const migration = await fs.dynamicImport(params.path);
|
|
104
104
|
const MigrationClass = Object.values(migration).find(cls => typeof cls === 'function' && typeof cls.constructor === 'function');
|
|
105
105
|
const instance = new MigrationClass(this.driver, this.config);
|
|
106
|
-
await this.runner.run(instance, method);
|
|
106
|
+
await this.runner.run(instance, method, afterRun);
|
|
107
107
|
};
|
|
108
108
|
return {
|
|
109
109
|
name: this.storage.getMigrationName(params.name),
|
|
110
110
|
path: params.path,
|
|
111
|
-
up:
|
|
112
|
-
down:
|
|
111
|
+
up: afterRun => createMigrationHandler('up', afterRun),
|
|
112
|
+
down: afterRun => createMigrationHandler('down', afterRun),
|
|
113
113
|
};
|
|
114
114
|
}
|
|
115
115
|
initialize(MigrationClass, name) {
|
|
116
116
|
const instance = new MigrationClass(this.driver, this.config);
|
|
117
117
|
return {
|
|
118
118
|
name: this.storage.getMigrationName(name),
|
|
119
|
-
up:
|
|
120
|
-
down:
|
|
119
|
+
up: afterRun => this.runner.run(instance, 'up', afterRun),
|
|
120
|
+
down: afterRun => this.runner.run(instance, 'down', afterRun),
|
|
121
121
|
};
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
@@ -196,13 +196,15 @@ export class AbstractMigrator {
|
|
|
196
196
|
for (const migration of toRun) {
|
|
197
197
|
const event = { name: migration.name, path: migration.path };
|
|
198
198
|
await this.emit(eventBefore, event);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
199
|
+
// log inside the runner's tx (if any) so a logMigration failure rolls the migration back
|
|
200
|
+
await migration[method](async (tx) => {
|
|
201
|
+
if (method === 'up') {
|
|
202
|
+
await this.storage.logMigration({ name: migration.name }, tx);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
await this.storage.unlogMigration({ name: migration.name }, tx);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
206
208
|
await this.emit(eventAfter, event);
|
|
207
209
|
result.push(event);
|
|
208
210
|
}
|
package/utils/Utils.js
CHANGED
|
@@ -141,7 +141,7 @@ export function parseJsonSafe(value) {
|
|
|
141
141
|
/** Collection of general-purpose utility methods used throughout the ORM. */
|
|
142
142
|
export class Utils {
|
|
143
143
|
static PK_SEPARATOR = '~~~';
|
|
144
|
-
static #ORM_VERSION = '7.0.15-dev.
|
|
144
|
+
static #ORM_VERSION = '7.0.15-dev.10';
|
|
145
145
|
/**
|
|
146
146
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
147
147
|
*/
|