@mikro-orm/decorators 7.2.0-dev.8 → 7.2.0

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/README.md CHANGED
@@ -24,6 +24,7 @@ npm install @mikro-orm/mysql # MySQL
24
24
  npm install @mikro-orm/mariadb # MariaDB
25
25
  npm install @mikro-orm/sqlite # SQLite
26
26
  npm install @mikro-orm/libsql # libSQL / Turso
27
+ npm install @mikro-orm/sql-js # sql.js (in-memory SQLite in WASM)
27
28
  npm install @mikro-orm/mongodb # MongoDB
28
29
  npm install @mikro-orm/mssql # MS SQL Server
29
30
  npm install @mikro-orm/oracledb # Oracle
package/es/Check.js CHANGED
@@ -1,11 +1,12 @@
1
+ import { ensureOwnMetadataArray } from '../utils.js';
1
2
  /** Defines a database check constraint on a property or entity class (TC39 decorator). */
2
3
  export function Check(options) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
- meta.checks ??= [];
6
+ const checks = ensureOwnMetadataArray(meta, 'checks');
6
7
  if (context.kind === 'field') {
7
8
  options.property ??= context.name;
8
9
  }
9
- meta.checks.push(options);
10
+ checks.push(options);
10
11
  };
11
12
  }
package/es/Indexed.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { ensureOwnMetadataArray } from '../utils.js';
1
2
  function createDecorator(options, unique) {
2
3
  return function (value, context) {
3
4
  const meta = context.metadata;
@@ -5,8 +6,7 @@ function createDecorator(options, unique) {
5
6
  options.properties ??= context.name;
6
7
  }
7
8
  const key = unique ? 'uniques' : 'indexes';
8
- meta[key] ??= [];
9
- meta[key].push(options);
9
+ ensureOwnMetadataArray(meta, key).push(options);
10
10
  };
11
11
  }
12
12
  /** Defines a database index on a property or entity class (TC39 decorator). */
package/es/ManyToOne.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { type ManyToOneOptions, type EntityName, type Primary, type Ref } from '@mikro-orm/core';
2
2
  /** Defines a many-to-one relationship (TC39 decorator). */
3
- export declare function ManyToOne<Target extends object, Owner extends object>(entity?: ManyToOneOptions<Owner, Target> | ((e?: Owner) => EntityName<Target> | EntityName[]), options?: Partial<ManyToOneOptions<Owner, Target>>): (_: unknown, context: ClassFieldDecoratorContext<Owner, Target | Primary<Target> | undefined | null | Ref<Target>>) => void;
3
+ export declare function ManyToOne<Target extends object, Owner extends object, Through extends object = Target>(entity?: ManyToOneOptions<Owner, Target, Through> | ((e?: Owner) => EntityName<Target> | EntityName[]), options?: Partial<ManyToOneOptions<Owner, Target, Through>>): (_: unknown, context: ClassFieldDecoratorContext<Owner, Target | Primary<Target> | undefined | null | Ref<Target>>) => void;
package/es/OneToOne.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { type EntityName, type OneToOneOptions, type Primary, type Ref } from '@mikro-orm/core';
2
2
  /** Defines a one-to-one relationship (TC39 decorator). */
3
- export declare function OneToOne<Target extends object, Owner extends object>(entity?: OneToOneOptions<Owner, Target> | string | ((e: Owner) => EntityName<Target> | EntityName[]), mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target>>, options?: Partial<OneToOneOptions<Owner, Target>>): (_: unknown, context: ClassFieldDecoratorContext<Owner, Target | Primary<Target> | Ref<Target> | null | undefined>) => void;
3
+ export declare function OneToOne<Target extends object, Owner extends object, Through extends object = Target>(entity?: OneToOneOptions<Owner, Target, Through> | string | ((e: Owner) => EntityName<Target> | EntityName[]), mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target, Through>>, options?: Partial<OneToOneOptions<Owner, Target, Through>>): (_: unknown, context: ClassFieldDecoratorContext<Owner, Target | Primary<Target> | Ref<Target> | null | undefined>) => void;
package/es/Property.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReferenceKind, Utils } from '@mikro-orm/core';
2
- import { prepareMetadataContext } from '../utils.js';
2
+ import { ensureOwnMetadataArray, prepareMetadataContext } from '../utils.js';
3
3
  /** Defines a scalar property on an entity (TC39 decorator). */
4
4
  export function Property(options = {}) {
5
5
  return function (value, context) {
@@ -7,7 +7,6 @@ export function Property(options = {}) {
7
7
  const { check, ...opts } = options;
8
8
  const prop = { kind: ReferenceKind.SCALAR, ...opts };
9
9
  const name = options.name ?? context.name;
10
- meta.checks ??= [];
11
10
  if (context.name !== name) {
12
11
  Utils.renameKey(options, 'name', 'fieldName');
13
12
  }
@@ -39,7 +38,7 @@ export function Property(options = {}) {
39
38
  prop.name = name;
40
39
  }
41
40
  if (check) {
42
- meta.checks.push({ property: prop.name, expression: check });
41
+ ensureOwnMetadataArray(meta, 'checks').push({ property: prop.name, expression: check });
43
42
  }
44
43
  meta.properties[prop.name] = prop;
45
44
  };
package/es/Trigger.js CHANGED
@@ -1,8 +1,8 @@
1
+ import { ensureOwnMetadataArray } from '../utils.js';
1
2
  /** Defines a database trigger on an entity class (TC39 decorator). */
2
3
  export function Trigger(options) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
- meta.triggers ??= [];
6
- meta.triggers.push(options);
6
+ ensureOwnMetadataArray(meta, 'triggers').push(options);
7
7
  };
8
8
  }
package/es/hooks.js CHANGED
@@ -2,9 +2,9 @@ import { EventType } from '@mikro-orm/core';
2
2
  function hook(type) {
3
3
  return function (value, context) {
4
4
  const meta = context.metadata;
5
- meta.hooks ??= {};
6
- meta.hooks[type] ??= [];
7
- meta.hooks[type].push(value);
5
+ meta.hooks = Object.hasOwn(meta, 'hooks') ? meta.hooks : { ...meta.hooks };
6
+ // the array itself may still be inherited from a base class metadata, so never push into it in place
7
+ meta.hooks[type] = [...(meta.hooks[type] ?? []), value];
8
8
  };
9
9
  }
10
10
  /** Called before a new entity is persisted to the database (TC39 decorator). */
@@ -1,5 +1,5 @@
1
1
  import { type ManyToOneOptions, type EntityName } from '@mikro-orm/core';
2
2
  /** Defines a many-to-one relationship (legacy TypeScript decorator). */
3
- export declare function ManyToOne<Target extends object, Owner extends object>(entity: (e?: any) => EntityName<Target> | EntityName[], options?: Partial<ManyToOneOptions<Owner, Target>>): (target: Owner, propertyName: string) => void;
3
+ export declare function ManyToOne<Target extends object, Owner extends object, Through extends object = Target>(entity: (e?: any) => EntityName<Target> | EntityName[], options?: Partial<ManyToOneOptions<Owner, Target, Through>>): (target: Owner, propertyName: string) => void;
4
4
  export declare function ManyToOne<Target extends object, Owner extends object>(entity: string, options?: any): never;
5
- export declare function ManyToOne<Target extends object, Owner extends object>(options?: ManyToOneOptions<Owner, Target>): (target: Owner, propertyName: string) => void;
5
+ export declare function ManyToOne<Target extends object, Owner extends object, Through extends object = Target>(options?: ManyToOneOptions<Owner, Target, Through>): (target: Owner, propertyName: string) => void;
@@ -1,4 +1,4 @@
1
1
  import { type EntityName, type OneToOneOptions } from '@mikro-orm/core';
2
2
  /** Defines a one-to-one relationship (legacy TypeScript decorator). */
3
- export declare function OneToOne<Target, Owner>(entity: (e: Owner) => EntityName<Target> | EntityName[], mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target>>, options?: Partial<OneToOneOptions<Owner, Target>>): (target: Owner, propertyName: string) => void;
4
- export declare function OneToOne<Target, Owner>(entity?: OneToOneOptions<Owner, Target>): (target: Owner, propertyName: string) => void;
3
+ export declare function OneToOne<Target, Owner, Through = Target>(entity: (e: Owner) => EntityName<Target> | EntityName[], mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target, Through>>, options?: Partial<OneToOneOptions<Owner, Target, Through>>): (target: Owner, propertyName: string) => void;
4
+ export declare function OneToOne<Target, Owner, Through = Target>(entity?: OneToOneOptions<Owner, Target, Through>): (target: Owner, propertyName: string) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/decorators",
3
- "version": "7.2.0-dev.8",
3
+ "version": "7.2.0",
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",
@@ -49,10 +49,10 @@
49
49
  "copy": "node ../../scripts/copy.mjs"
50
50
  },
51
51
  "devDependencies": {
52
- "@mikro-orm/core": "^7.1.11"
52
+ "@mikro-orm/core": "^7.2.0"
53
53
  },
54
54
  "peerDependencies": {
55
- "@mikro-orm/core": "7.2.0-dev.8",
55
+ "@mikro-orm/core": "7.2.0",
56
56
  "reflect-metadata": "^0.1.0 || ^0.2.0"
57
57
  },
58
58
  "peerDependenciesMeta": {
package/utils.d.ts CHANGED
@@ -34,6 +34,13 @@ export declare function validateSingleDecorator(meta: EntityMetadata, propertyNa
34
34
  * exist for some base entity.
35
35
  */
36
36
  export declare function prepareMetadataContext<T>(context: ClassFieldDecoratorContext<T> | ClassGetterDecoratorContext<T> | ClassSetterDecoratorContext<T> | ClassAccessorDecoratorContext<T> | ClassMethodDecoratorContext<T>, kind?: ReferenceKind): EntityMetadata<T>;
37
+ /**
38
+ * Ensures the metadata object has its own array under the given key, copying any items inherited from a base class.
39
+ * The decorator metadata object respects inheritance (a subclass metadata object has the parent metadata object as
40
+ * its prototype), and the `@Entity()` decorator transfers only own properties of the metadata object, so pushing to
41
+ * an inherited array would both leak the item to the base class metadata and lose it for the subclass.
42
+ */
43
+ export declare function ensureOwnMetadataArray<T, K extends 'checks' | 'indexes' | 'uniques' | 'triggers'>(meta: Partial<EntityMetadata<T>>, key: K): NonNullable<Partial<EntityMetadata<T>>[K]>;
37
44
  /**
38
45
  * Uses some dark magic to get source path to caller where decorator is used.
39
46
  * Analyzes stack trace of error created inside the function call.
package/utils.js CHANGED
@@ -75,6 +75,18 @@ export function prepareMetadataContext(context, kind) {
75
75
  }
76
76
  return meta;
77
77
  }
78
+ /**
79
+ * Ensures the metadata object has its own array under the given key, copying any items inherited from a base class.
80
+ * The decorator metadata object respects inheritance (a subclass metadata object has the parent metadata object as
81
+ * its prototype), and the `@Entity()` decorator transfers only own properties of the metadata object, so pushing to
82
+ * an inherited array would both leak the item to the base class metadata and lose it for the subclass.
83
+ */
84
+ export function ensureOwnMetadataArray(meta, key) {
85
+ if (!Object.hasOwn(meta, key)) {
86
+ meta[key] = [...(meta[key] ?? [])];
87
+ }
88
+ return meta[key];
89
+ }
78
90
  /**
79
91
  * Uses some dark magic to get source path to caller where decorator is used.
80
92
  * Analyzes stack trace of error created inside the function call.
@@ -106,10 +118,11 @@ export function lookupPathFromDecorator(name, stack) {
106
118
  if (stack[line].includes('Reflect.decorate')) {
107
119
  line++;
108
120
  }
109
- // Skip decorator runtime helpers (tslib, @oxc-project/runtime, etc.)
121
+ // Skip decorator runtime helpers (tslib, @oxc-project/runtime, @oxc-node/core, etc.)
110
122
  // The @oxc-project/runtime check covers both node_modules installs and Rolldown-bundled
111
123
  // virtual modules (e.g. \0@oxc-project+runtime@0.120.0/helpers/decorate.js).
112
- while (line < stack.length && /node_modules\/tslib\/|@oxc-project[/+]runtime/.test(stack[line].replace(/\\/g, '/'))) {
124
+ while (line < stack.length &&
125
+ /node_modules\/tslib\/|@oxc-project[/+]runtime|@oxc-node\/core\//.test(stack[line].replace(/\\/g, '/'))) {
113
126
  line++;
114
127
  }
115
128
  try {