@mikro-orm/core 7.1.14-dev.1 → 7.1.14-dev.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -77,6 +77,12 @@ export class EntityFactory {
77
77
  }
78
78
  }
79
79
  data = { ...data };
80
+ if (options.newEntity && meta2.root.inheritanceType === 'sti' && meta2.discriminatorValue != null) {
81
+ const prop = meta2.properties[meta2.root.discriminatorColumn];
82
+ if (prop && prop.userDefined !== false) {
83
+ data[prop.name] ??= meta2.discriminatorValue;
84
+ }
85
+ }
80
86
  const entity = exists ?? this.createEntity(data, meta2, options);
81
87
  wrapped = helper(entity);
82
88
  wrapped.__processing = true;
@@ -1,6 +1,6 @@
1
1
  import type { EntityManager } from '../EntityManager.js';
2
2
  import type { ColumnType, PropertyOptions, ReferenceOptions, EnumOptions, EmbeddedOptions, ManyToOneOptions, OneToManyOptions, OneToOneOptions, ManyToManyOptions, IndexColumnOptions } from '../metadata/types.js';
3
- import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, LazyRef, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints, ExtractDefineEntityProperties } from '../typings.js';
3
+ import type { AnyString, GeneratedColumnCallback, Constructor, CheckCallback, FilterQuery, EntityName, Dictionary, EntityMetadata, PrimaryKeyProp, EntityRepositoryType, Hidden, Opt, Primary, EntityClass, EntitySchemaWithMeta, InferEntity, MaybeReturnType, Ref, LazyRef, IndexCallback, TriggerCallback, FormulaCallback, EntityCtor, IsNever, IWrappedEntity, DefineConfig, Config, MaybePromise, IndexHints, OptionalProps, ExtractDefineEntityProperties } from '../typings.js';
4
4
  import type { Raw } from '../utils/RawQueryFragment.js';
5
5
  import type { ScalarReference } from './Reference.js';
6
6
  import type { SerializeOptions } from '../serialization/EntitySerializer.js';
@@ -618,7 +618,7 @@ type PartialWhere<TProperties, TBase> = string | FilterQuery<{
618
618
  [K in AllKeys<TProperties, TBase> & string]?: unknown;
619
619
  }>;
620
620
  /** Metadata descriptor for `defineEntity()`, combining entity options with property definitions. */
621
- 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' | 'discriminator' | 'discriminatorColumn' | 'discriminatorValue' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'triggers' | 'repository' | 'filters' | 'orderBy'> {
621
+ 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, TEmbeddable extends boolean = false> extends Omit<Partial<EntityMetadata<InferEntityFromProperties<TProperties, TPK, TBase, TRepository>>>, 'properties' | 'extends' | 'primaryKeys' | 'hooks' | 'discriminator' | 'discriminatorColumn' | 'discriminatorValue' | 'versionProperty' | 'concurrencyCheckKeys' | 'serializedPrimaryKey' | 'indexes' | 'uniques' | 'triggers' | 'repository' | 'filters' | 'orderBy'> {
622
622
  name: TName;
623
623
  tableName?: TTableName;
624
624
  extends?: {
@@ -640,6 +640,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
640
640
  strict?: boolean;
641
641
  }>;
642
642
  forceObject?: TForceObject;
643
+ embeddable?: TEmbeddable;
643
644
  inheritance?: 'tpt';
644
645
  orderBy?: {
645
646
  [K in Extract<AllKeys<TProperties, TBase>, string>]?: QueryOrderKeysFlat;
@@ -690,7 +691,7 @@ export interface EntityMetadataWithProperties<TName extends string, TTableName e
690
691
  }[];
691
692
  }
692
693
  /** Defines an entity schema using property builders, with full type inference from the property definitions. */
693
- 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>;
694
+ 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, const TEmbeddable extends boolean = false>(meta: EntityMetadataWithProperties<TName, TTableName, TProperties, TPK, TBase, TRepository, TForceObject, TDiscriminatorColumn, TDiscriminatorValue, TBaseDiscriminatorColumn, TEmbeddable>): EntitySchemaWithMeta<TName, TTableName, InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue, TEmbeddable>, TBase, TProperties, EntityCtor<InferEntityFromProperties<TProperties, TPK, TBase, TRepository, TForceObject, TBaseDiscriminatorColumn, TDiscriminatorValue, TEmbeddable>>, TDiscriminatorColumn>;
694
695
  export declare namespace defineEntity {
695
696
  export { propertyBuilders as properties };
696
697
  }
@@ -726,7 +727,7 @@ type InferJSType<T> = T extends typeof Type<infer TValue, any> ? NonNullable<TVa
726
727
  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;
727
728
  type BaseEntityMethodKeys = 'toObject' | 'toPOJO' | 'serialize' | 'assign' | 'populate' | 'init' | 'toReference';
728
729
  /** Infers the entity type from a `defineEntity()` properties map, resolving builders, base classes, and primary keys. */
729
- 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 {
730
+ 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, Embeddable extends boolean = false> = (IsNever<Base> extends true ? {} : Base extends {
730
731
  toObject(...args: any[]): any;
731
732
  } ? Pick<IWrappedEntity<{
732
733
  -readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
@@ -734,13 +735,13 @@ export type InferEntityFromProperties<Properties extends Record<string, any>, PK
734
735
  [PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
735
736
  } & (IsNever<Repository> extends true ? {} : {
736
737
  [EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
737
- }) & NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>>, BaseEntityMethodKeys> : {}) & {
738
+ }) & NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue, Embeddable>>, BaseEntityMethodKeys> : {}) & {
738
739
  -readonly [K in keyof Properties]: InferBuilderValue<MaybeReturnType<Properties[K]>>;
739
740
  } & {
740
741
  [PrimaryKeyProp]?: InferCombinedPrimaryKey<Properties, PK, Base>;
741
742
  } & (IsNever<Repository> extends true ? {} : {
742
743
  [EntityRepositoryType]?: Repository extends Constructor<infer R> ? R : Repository;
743
- }) & (IsNever<Base> extends true ? {} : NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue>) & (ForceObject extends true ? {
744
+ }) & (IsNever<Base> extends true ? {} : NarrowDiscriminator<Omit<Base, typeof PrimaryKeyProp>, BaseDiscriminatorColumn, DiscriminatorValue, Embeddable>) & (ForceObject extends true ? {
744
745
  [Config]?: DefineConfig<{
745
746
  forceObject: true;
746
747
  }>;
@@ -748,13 +749,26 @@ export type InferEntityFromProperties<Properties extends Record<string, any>, PK
748
749
  [IndexHints]?: [Omit<ExtractBaseProperties<Base>, keyof Properties> & Properties];
749
750
  };
750
751
  type ExtractBaseProperties<Base> = [ExtractDefineEntityProperties<Base>] extends [infer P extends Record<string, any>] ? [P] extends [never] ? {} : P : {};
751
- type NarrowDiscriminator<Base, DiscColumn extends string | undefined, DiscValue> = DiscColumn extends string ? DiscColumn extends keyof Base ? DiscValue extends string | number ? Omit<Base, DiscColumn> & {
752
+ type ExtractOptionalProps<Base> = Base extends {
753
+ [OptionalProps]?: infer K;
754
+ } ? (K extends string ? K : never) : never;
755
+ type NarrowDiscriminator<Base, DiscColumn extends string | undefined, DiscValue, Embeddable extends boolean = false> = DiscColumn extends string ? DiscColumn extends keyof Base ? DiscValue extends string | number ? Embeddable extends true ? Omit<Base, DiscColumn> & {
752
756
  [K in DiscColumn]: DiscValue;
757
+ } : Omit<Base, DiscColumn | typeof OptionalProps> & {
758
+ [K in DiscColumn]: DiscValue;
759
+ } & {
760
+ [OptionalProps]?: DiscColumn | ExtractOptionalProps<Base>;
753
761
  } : Base : Base : Base;
754
762
  type InferCombinedPrimaryKey<Properties extends Record<string, any>, PK, Base> = PK extends undefined ? CombinePrimaryKeys<InferPrimaryKey<Properties>, ExtractBasePrimaryKey<Base>> : PK;
755
- type ExtractBasePrimaryKey<Base> = Base extends {
763
+ type ExtractBasePrimaryKey<Base> = typeof PrimaryKeyProp extends keyof Base ? Base extends {
756
764
  [PrimaryKeyProp]?: infer BasePK;
757
- } ? BasePK : never;
765
+ } ? BasePK : never : [keyof Base] extends [never] ? never : Base extends {
766
+ _id?: any;
767
+ } ? '_id' : Base extends {
768
+ id?: any;
769
+ } ? 'id' : Base extends {
770
+ uuid?: any;
771
+ } ? 'uuid' : never;
758
772
  type CombinePrimaryKeys<ChildPK, BasePK> = [ChildPK] extends [never] ? BasePK : [BasePK] extends [never] ? IsUnion<ChildPK> extends true ? ChildPK[] : ChildPK : ChildPK | BasePK;
759
773
  /** Extracts the primary key property names from a properties map by finding builders with `primary: true`. */
760
774
  export type InferPrimaryKey<Properties extends Record<string, any>> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.1.14-dev.1",
3
+ "version": "7.1.14-dev.11",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -211,8 +211,9 @@ export declare abstract class Platform {
211
211
  getBlobDeclarationSQL(): string;
212
212
  getJsonDeclarationSQL(): string;
213
213
  getSearchJsonPropertySQL(path: string, type: string, aliased: boolean): string | Raw;
214
- getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean, value?: unknown): string | Raw;
215
- processJsonCondition<T extends object>(o: FilterQuery<T>, value: EntityValue<T>, path: EntityKey<T>[], alias: boolean): FilterQuery<T>;
214
+ /** When `aliased` is a string, it holds an explicit alias the key was prefixed with. */
215
+ getSearchJsonPropertyKey(path: string[], type: string, aliased: boolean | string, value?: unknown): string | Raw;
216
+ processJsonCondition<T extends object>(o: FilterQuery<T>, value: EntityValue<T>, path: EntityKey<T>[], alias: boolean | string): FilterQuery<T>;
216
217
  protected getJsonValueType(value: unknown): string;
217
218
  getJsonIndexDefinition(index: {
218
219
  columnNames: string[];
@@ -413,6 +413,7 @@ export class Platform {
413
413
  getSearchJsonPropertySQL(path, type, aliased) {
414
414
  return path;
415
415
  }
416
+ /** When `aliased` is a string, it holds an explicit alias the key was prefixed with. */
416
417
  getSearchJsonPropertyKey(path, type, aliased, value) {
417
418
  return path.join('.');
418
419
  }
@@ -424,7 +425,8 @@ export class Platform {
424
425
  return o;
425
426
  }
426
427
  if (path.length === 1) {
427
- o[path[0]] = value;
428
+ const key = typeof alias === 'string' ? `${alias}.${path[0]}` : path[0];
429
+ o[key] = value;
428
430
  return o;
429
431
  }
430
432
  const type = this.getJsonValueType(value);
@@ -526,6 +526,10 @@ export class UnitOfWork {
526
526
  if (prop.formula) {
527
527
  delete referrer[prop.name];
528
528
  }
529
+ else if (prop.primary) {
530
+ // the referrer's primary key contains the removed entity, so its identity cannot survive the removal
531
+ this.unsetIdentity(referrer);
532
+ }
529
533
  else {
530
534
  delete helper(referrer).__data[prop.name];
531
535
  }
@@ -197,7 +197,8 @@ export class DataloaderUtils {
197
197
  const prop = group[0][0].property;
198
198
  const options = {};
199
199
  const wrap = (cond) => ({ [prop.name]: cond });
200
- const orderBy = Utils.asArray(group[0][1]?.orderBy).map(o => wrap(o));
200
+ // `findChildrenFromPivotTable` expects the `orderBy` relative to the target entity, so no wrapping here
201
+ const orderBy = Utils.asArray(group[0][1]?.orderBy);
201
202
  const populate = wrap(group[0][1]?.populate);
202
203
  const owners = group.map(c => c[0].owner);
203
204
  const $or = [];
@@ -237,7 +237,11 @@ export class QueryHelper {
237
237
  // oxfmt-ignore
238
238
  const isJsonProperty = prop?.customType instanceof JsonType && !isRaw(value) && (Utils.isPlainObject(value) ? !['$eq', '$elemMatch'].includes(Object.keys(value)[0]) : !Array.isArray(value));
239
239
  if (isJsonProperty && prop?.kind !== ReferenceKind.EMBEDDED) {
240
- return this.processJsonCondition(o, value, [prop.fieldNames[0]], platform, aliased);
240
+ // an explicit alias prefix (e.g. `a.meta`) has to survive, otherwise the condition falls back to the root alias
241
+ const explicitAlias = key.includes('.')
242
+ ? key.split('.').slice(0, -1).join('.')
243
+ : undefined;
244
+ return this.processJsonCondition(o, value, [prop.fieldNames[0]], platform, aliased && explicitAlias != null ? explicitAlias : aliased);
241
245
  }
242
246
  // oxfmt-ignore
243
247
  if (Array.isArray(value) && !Utils.isOperator(key) && !QueryHelper.isSupportedOperator(key) && !(customExpression && Raw.getKnownFragment(key).params.length > 0) && options.type !== 'orderBy') {
package/utils/Utils.js CHANGED
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
153
153
  /** Collection of general-purpose utility methods used throughout the ORM. */
154
154
  export class Utils {
155
155
  static PK_SEPARATOR = '~~~';
156
- static #ORM_VERSION = '7.1.14-dev.1';
156
+ static #ORM_VERSION = '7.1.14-dev.11';
157
157
  /**
158
158
  * Checks if the argument is instance of `Object`. Returns false for arrays.
159
159
  */