@mikro-orm/core 7.2.0-dev.10 → 7.2.0-dev.12

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.
@@ -375,12 +375,10 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
375
375
  */
376
376
  nativeDelete<Entity extends object>(entityName: EntityName<Entity>, where: FilterQuery<NoInfer<Entity>>, options?: DeleteOptions<Entity>): Promise<number>;
377
377
  /**
378
- * Maps raw database result to an entity and merges it to this EntityManager.
378
+ * Maps raw database result to an entity and merges it to this EntityManager by default.
379
+ * Use `disableIdentityMap` to return an isolated entity without affecting the current context.
379
380
  */
380
- map<Entity extends object>(entityName: EntityName<Entity>, result: EntityDictionary<Entity>, options?: {
381
- schema?: string;
382
- mapped?: boolean;
383
- }): Entity;
381
+ map<Entity extends object>(entityName: EntityName<Entity>, result: EntityDictionary<Entity>, options?: MapOptions): Entity;
384
382
  /**
385
383
  * Merges given entity to this EntityManager so it becomes managed. You can force refreshing of existing entities
386
384
  * via second parameter. By default, it will return already loaded entities without modifying them.
@@ -667,6 +665,14 @@ export interface CreateOptions<Convert extends boolean> {
667
665
  */
668
666
  processOnCreateHooksEarly?: boolean;
669
667
  }
668
+ export interface MapOptions {
669
+ /** schema to use when mapping the entity */
670
+ schema?: string;
671
+ /** set to true when the result is already mapped to entity property names */
672
+ mapped?: boolean;
673
+ /** map the entity in an isolated context without adding it to the identity map */
674
+ disableIdentityMap?: boolean;
675
+ }
670
676
  export interface MergeOptions {
671
677
  refresh?: boolean;
672
678
  convertCustomTypes?: boolean;
package/EntityManager.js CHANGED
@@ -1399,10 +1399,18 @@ export class EntityManager {
1399
1399
  return res.affectedRows;
1400
1400
  }
1401
1401
  /**
1402
- * Maps raw database result to an entity and merges it to this EntityManager.
1402
+ * Maps raw database result to an entity and merges it to this EntityManager by default.
1403
+ * Use `disableIdentityMap` to return an isolated entity without affecting the current context.
1403
1404
  */
1404
1405
  map(entityName, result, options = {}) {
1405
- const { mapped, ...rest } = options;
1406
+ if (options.disableIdentityMap ?? this.config.get('disableIdentityMap')) {
1407
+ const em = this.getContext(false);
1408
+ const fork = em.fork({ keepTransactionContext: true });
1409
+ const ret = fork.map(entityName, result, { ...options, disableIdentityMap: false });
1410
+ fork.clear();
1411
+ return ret;
1412
+ }
1413
+ const { mapped, disableIdentityMap, ...rest } = options;
1406
1414
  const meta = this.metadata.get(entityName);
1407
1415
  const data = (mapped ? result : this.driver.mapResult(result, meta));
1408
1416
  for (const k of Object.keys(data)) {
@@ -1,5 +1,5 @@
1
1
  import type { PopulatePath } from '../enums.js';
2
- import type { CreateOptions, EntityManager, MergeOptions } from '../EntityManager.js';
2
+ import type { CreateOptions, EntityManager, MapOptions, MergeOptions } from '../EntityManager.js';
3
3
  import type { AssignOptions } from './EntityAssigner.js';
4
4
  import type { Dictionary, EntityData, EntityDictionary, EntityKey, EntityName, FilterQuery, Loaded, Primary, AutoPath, RequiredEntityData, Ref, EntityType, EntityDTO, MergeSelected, FromEntityType, IsSubset, MergeLoaded, ArrayElement, IndexFilterQuery, WithUsingOptions } from '../typings.js';
5
5
  import type { CountByOptions, CountOptions, DeleteOptions, FindAllOptions, FindByCursorOptions, FindOneOptions, FindOneOrFailOptions, FindOptions, GetReferenceOptions, NativeInsertUpdateOptions, StreamOptions, UpdateOptions, UpsertManyOptions, UpsertOptions } from '../drivers/IDatabaseDriver.js';
@@ -115,11 +115,10 @@ export declare class EntityRepository<Entity extends object> {
115
115
  */
116
116
  nativeDelete(where: FilterQuery<Entity>, options?: DeleteOptions<Entity>): Promise<number>;
117
117
  /**
118
- * Maps raw database result to an entity and merges it to this EntityManager.
118
+ * Maps raw database result to an entity and merges it to this EntityManager by default.
119
+ * Use `disableIdentityMap` to return an isolated entity without affecting the current context.
119
120
  */
120
- map(result: EntityDictionary<Entity>, options?: {
121
- schema?: string;
122
- }): Entity;
121
+ map(result: EntityDictionary<Entity>, options?: Omit<MapOptions, 'mapped'>): Entity;
123
122
  /**
124
123
  * Gets a reference to the entity identified by the given type and alternate key property without actually loading it.
125
124
  * The key option specifies which property to use for identity map lookup instead of the primary key.
@@ -131,7 +131,8 @@ export class EntityRepository {
131
131
  return this.getEntityManager().nativeDelete(this.entityName, where, options);
132
132
  }
133
133
  /**
134
- * Maps raw database result to an entity and merges it to this EntityManager.
134
+ * Maps raw database result to an entity and merges it to this EntityManager by default.
135
+ * Use `disableIdentityMap` to return an isolated entity without affecting the current context.
135
136
  */
136
137
  map(result, options) {
137
138
  return this.getEntityManager().map(this.entityName, result, options);
@@ -570,6 +570,13 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
570
570
  export interface EmptyOptions extends Partial<Record<UniversalPropertyKeys, unknown>> {
571
571
  }
572
572
  /** @internal */
573
+ export declare class StringPropertyOptionsBuilder<Value, Options> extends UniversalPropertyOptionsBuilder<Value, Options, IncludeKeysForProperty> {
574
+ trim(): StringPropertyOptionsBuilder<Value, Options>;
575
+ lowercase(): StringPropertyOptionsBuilder<Value, Options>;
576
+ uppercase(): StringPropertyOptionsBuilder<Value, Options>;
577
+ private withOptions;
578
+ }
579
+ /** @internal */
573
580
  export declare class OneToManyOptionsBuilderOnlyMappedBy<Value extends object> extends UniversalPropertyOptionsBuilder<Value, EmptyOptions & {
574
581
  kind: '1:m';
575
582
  }, IncludeKeysForOneToManyOptions> {
@@ -582,7 +589,7 @@ type EntityTarget = {
582
589
  '~entity': any;
583
590
  } | EntityClass;
584
591
  declare const propertyBuilders: PropertyBuilders;
585
- type PropertyBuildersOverrideKeys = 'bigint' | 'array' | 'decimal' | 'json' | 'datetime' | 'time' | 'enum';
592
+ type PropertyBuildersOverrideKeys = 'bigint' | 'array' | 'decimal' | 'json' | 'string' | 'text' | 'datetime' | 'time' | 'enum';
586
593
  /** Map of factory functions for creating type-safe property builders (scalars, enums, embeddables, and relations). */
587
594
  export type PropertyBuilders = {
588
595
  [K in Exclude<keyof typeof types, PropertyBuildersOverrideKeys>]: () => UniversalPropertyOptionsBuilder<InferPropertyValueType<(typeof types)[K]>, EmptyOptions, IncludeKeysForProperty>;
@@ -591,6 +598,8 @@ export type PropertyBuilders = {
591
598
  array: <T = string>(toJsValue?: (i: string) => T, toDbValue?: (i: T) => string) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.array<T>>, EmptyOptions, IncludeKeysForProperty>;
592
599
  decimal: <Mode extends 'number' | 'string' = 'string'>(mode?: Mode) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.decimal<Mode>>, EmptyOptions, IncludeKeysForProperty>;
593
600
  json: <T>() => UniversalPropertyOptionsBuilder<T, EmptyOptions, IncludeKeysForProperty>;
601
+ string: () => StringPropertyOptionsBuilder<InferPropertyValueType<typeof types.string>, EmptyOptions>;
602
+ text: () => StringPropertyOptionsBuilder<InferPropertyValueType<typeof types.text>, EmptyOptions>;
594
603
  formula: <T>(formula: string | FormulaCallback<any>) => UniversalPropertyOptionsBuilder<T, EmptyOptions, IncludeKeysForProperty>;
595
604
  datetime: (length?: number) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.datetime>, EmptyOptions, IncludeKeysForProperty>;
596
605
  time: (length?: number) => UniversalPropertyOptionsBuilder<InferPropertyValueType<typeof types.time>, EmptyOptions, IncludeKeysForProperty>;
@@ -472,6 +472,27 @@ export class UniversalPropertyOptionsBuilder {
472
472
  }
473
473
  }
474
474
  /** @internal */
475
+ export class StringPropertyOptionsBuilder extends UniversalPropertyOptionsBuilder {
476
+ trim() {
477
+ return this.withOptions({ trim: true });
478
+ }
479
+ lowercase() {
480
+ return this.withOptions({ case: 'lower' });
481
+ }
482
+ uppercase() {
483
+ return this.withOptions({ case: 'upper' });
484
+ }
485
+ withOptions(options) {
486
+ const type = this['~options'].type;
487
+ const TypeClass = typeof type === 'function' ? type : type.constructor;
488
+ const currentOptions = typeof type === 'function' ? {} : type.options;
489
+ return new StringPropertyOptionsBuilder({
490
+ ...this['~options'],
491
+ type: new TypeClass({ ...currentOptions, ...options }),
492
+ });
493
+ }
494
+ }
495
+ /** @internal */
475
496
  export class OneToManyOptionsBuilderOnlyMappedBy extends UniversalPropertyOptionsBuilder {
476
497
  /** Point to the owning side property name. */
477
498
  mappedBy(mappedBy) {
@@ -487,6 +508,12 @@ const propertyBuilders = {
487
508
  array: (toJsValue = i => i, toDbValue = i => i) => new UniversalPropertyOptionsBuilder({ type: new types.array(toJsValue, toDbValue) }),
488
509
  decimal: (mode) => new UniversalPropertyOptionsBuilder({ type: new types.decimal(mode) }),
489
510
  json: () => new UniversalPropertyOptionsBuilder({ type: types.json }),
511
+ string: () => new StringPropertyOptionsBuilder({
512
+ type: types.string,
513
+ }),
514
+ text: () => new StringPropertyOptionsBuilder({
515
+ type: types.text,
516
+ }),
490
517
  formula: (formula) => new UniversalPropertyOptionsBuilder({ formula }),
491
518
  datetime: (length) => new UniversalPropertyOptionsBuilder({ type: types.datetime, length }),
492
519
  time: (length) => new UniversalPropertyOptionsBuilder({ type: types.time, length }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.0-dev.10",
3
+ "version": "7.2.0-dev.12",
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",
@@ -1,10 +1,21 @@
1
1
  import { Type } from './Type.js';
2
2
  import type { Platform } from '../platforms/Platform.js';
3
3
  import type { EntityProperty } from '../typings.js';
4
- /** Maps a database VARCHAR column to a JS `string`. */
5
- export declare class StringType extends Type<string | null | undefined, string | null | undefined> {
6
- getColumnType(prop: EntityProperty, platform: Platform): string;
4
+ export interface StringTypeOptions {
5
+ trim?: boolean;
6
+ case?: 'upper' | 'lower';
7
+ }
8
+ /** @internal */
9
+ export declare abstract class BaseStringType extends Type<string | null | undefined, string | null | undefined> {
10
+ readonly options: StringTypeOptions;
11
+ constructor(options?: StringTypeOptions);
12
+ convertToDatabaseValue(value: string | null | undefined): string | null | undefined;
7
13
  compareAsType(): string;
8
14
  ensureComparable(): boolean;
15
+ private normalize;
16
+ }
17
+ /** Maps a database VARCHAR column to a JS `string`. */
18
+ export declare class StringType extends BaseStringType {
19
+ getColumnType(prop: EntityProperty, platform: Platform): string;
9
20
  getDefaultLength(platform: Platform): number;
10
21
  }
@@ -1,8 +1,17 @@
1
1
  import { Type } from './Type.js';
2
- /** Maps a database VARCHAR column to a JS `string`. */
3
- export class StringType extends Type {
4
- getColumnType(prop, platform) {
5
- return platform.getVarcharTypeDeclarationSQL(prop);
2
+ /** @internal */
3
+ export class BaseStringType extends Type {
4
+ options;
5
+ constructor(options = {}) {
6
+ super();
7
+ this.options = options;
8
+ // a defined `compareValues` replaces the inline `!==` comparator, so only provide it when normalization is configured
9
+ if (options.trim || options.case) {
10
+ this.compareValues = (a, b) => this.normalize(a) === this.normalize(b);
11
+ }
12
+ }
13
+ convertToDatabaseValue(value) {
14
+ return this.normalize(value);
6
15
  }
7
16
  compareAsType() {
8
17
  return 'string';
@@ -10,6 +19,27 @@ export class StringType extends Type {
10
19
  ensureComparable() {
11
20
  return false;
12
21
  }
22
+ normalize(value) {
23
+ if (value == null) {
24
+ return value;
25
+ }
26
+ if (this.options.trim) {
27
+ value = value.trim();
28
+ }
29
+ if (this.options.case === 'upper') {
30
+ return value.toUpperCase();
31
+ }
32
+ if (this.options.case === 'lower') {
33
+ return value.toLowerCase();
34
+ }
35
+ return value;
36
+ }
37
+ }
38
+ /** Maps a database VARCHAR column to a JS `string`. */
39
+ export class StringType extends BaseStringType {
40
+ getColumnType(prop, platform) {
41
+ return platform.getVarcharTypeDeclarationSQL(prop);
42
+ }
13
43
  getDefaultLength(platform) {
14
44
  return platform.getDefaultVarcharLength();
15
45
  }
@@ -1,9 +1,7 @@
1
- import { Type } from './Type.js';
1
+ import { BaseStringType } from './StringType.js';
2
2
  import type { Platform } from '../platforms/Platform.js';
3
3
  import type { EntityProperty } from '../typings.js';
4
4
  /** Maps a database TEXT column (unbounded length) to a JS `string`. */
5
- export declare class TextType extends Type<string | null | undefined, string | null | undefined> {
5
+ export declare class TextType extends BaseStringType {
6
6
  getColumnType(prop: EntityProperty, platform: Platform): string;
7
- compareAsType(): string;
8
- ensureComparable(): boolean;
9
7
  }
package/types/TextType.js CHANGED
@@ -1,13 +1,7 @@
1
- import { Type } from './Type.js';
1
+ import { BaseStringType } from './StringType.js';
2
2
  /** Maps a database TEXT column (unbounded length) to a JS `string`. */
3
- export class TextType extends Type {
3
+ export class TextType extends BaseStringType {
4
4
  getColumnType(prop, platform) {
5
5
  return platform.getTextTypeDeclarationSQL(prop);
6
6
  }
7
- compareAsType() {
8
- return 'string';
9
- }
10
- ensureComparable() {
11
- return false;
12
- }
13
7
  }
package/types/index.d.ts CHANGED
@@ -15,7 +15,7 @@ import { IntervalType } from './IntervalType.js';
15
15
  import { JsonType } from './JsonType.js';
16
16
  import { MediumIntType } from './MediumIntType.js';
17
17
  import { SmallIntType } from './SmallIntType.js';
18
- import { StringType } from './StringType.js';
18
+ import { type StringTypeOptions, StringType } from './StringType.js';
19
19
  import { TextType } from './TextType.js';
20
20
  import { TimeType } from './TimeType.js';
21
21
  import { TinyIntType } from './TinyIntType.js';
@@ -23,7 +23,7 @@ import { type IType, type TransformContext, Type } from './Type.js';
23
23
  import { Uint8ArrayType } from './Uint8ArrayType.js';
24
24
  import { UnknownType } from './UnknownType.js';
25
25
  import { UuidType } from './UuidType.js';
26
- export type { TransformContext, IType };
26
+ export type { TransformContext, IType, StringTypeOptions };
27
27
  export { Type, DateType, TimeType, DateTimeType, BigIntType, BlobType, Uint8ArrayType, ArrayType, EnumArrayType, EnumType, JsonType, IntegerType, SmallIntType, TinyIntType, MediumIntType, FloatType, DoubleType, BooleanType, DecimalType, StringType, UuidType, TextType, UnknownType, IntervalType, CharacterType, };
28
28
  /** Registry of all built-in type constructors, keyed by their short name (e.g., `types.integer`, `types.uuid`). */
29
29
  export declare const types: {
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.2.0-dev.10';
156
+ static #ORM_VERSION = '7.2.0-dev.12';
157
157
  /**
158
158
  * Checks if the argument is instance of `Object`. Returns false for arrays.
159
159
  */