@mikro-orm/core 7.2.0-dev.11 → 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.
@@ -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.11",
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.11';
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
  */