@mikro-orm/decorators 7.0.0-dev.65 → 7.0.0-dev.67

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.
@@ -1,3 +1,3 @@
1
- import { type ContextProvider } from '../resolveContextProvider.js';
1
+ import { type ContextProvider } from '../utils.js';
2
2
  export declare function CreateRequestContext<T extends object>(contextProvider?: ContextProvider<T>, respectExistingContext?: boolean): (value: (this: T, ...args: any) => any, context: ClassMethodDecoratorContext<T>) => (this: T, ...args: any[]) => Promise<any>;
3
3
  export declare function EnsureRequestContext<T extends object>(context?: ContextProvider<T>): (value: (this: T, ...args: any) => any, context: ClassMethodDecoratorContext<T, (this: T, ...args: any) => any>) => (this: T, ...args: any[]) => Promise<any>;
@@ -1,5 +1,5 @@
1
1
  import { RequestContext, TransactionContext } from '@mikro-orm/core';
2
- import { resolveContextProvider } from '../resolveContextProvider.js';
2
+ import { resolveContextProvider } from '../utils.js';
3
3
  export function CreateRequestContext(contextProvider, respectExistingContext = false) {
4
4
  return function (value, context) {
5
5
  const name = respectExistingContext ? 'EnsureRequestContext' : 'CreateRequestContext';
package/es/Embeddable.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, Utils, } from '@mikro-orm/core';
1
+ import { Utils, } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  export function Embeddable(options = {}) {
3
4
  return function (target, context) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ const meta = getMetadataFromDecorator(target);
5
6
  Utils.mergeConfig(meta, context.metadata, options);
6
7
  meta.class = target;
7
8
  meta.name = meta.class.name;
package/es/Embedded.js CHANGED
@@ -1,9 +1,10 @@
1
- import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator } from '../utils.js';
2
3
  export function Embedded(type = {}, options = {}) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.EMBEDDED);
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.EMBEDDED);
7
8
  options = type instanceof Function ? { entity: type, ...options } : { ...type, ...options };
8
9
  Utils.defaultValue(options, 'prefix', true);
9
10
  meta.properties[context.name] = {
package/es/Entity.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, Utils, } from '@mikro-orm/core';
1
+ import { Utils, } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  export function Entity(options = {}) {
3
4
  return function (target, context) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ const meta = getMetadataFromDecorator(target);
5
6
  Utils.mergeConfig(meta, context.metadata, options);
6
7
  meta.class = target;
7
8
  if (!options.abstract || meta.discriminatorColumn) {
package/es/Filter.js CHANGED
@@ -1,7 +1,7 @@
1
- import { MetadataStorage } from '@mikro-orm/core';
1
+ import { getMetadataFromDecorator } from '../utils.js';
2
2
  export function Filter(options) {
3
3
  return function (target) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
4
+ const meta = getMetadataFromDecorator(target);
5
5
  meta.filters[options.name] = options;
6
6
  };
7
7
  }
package/es/ManyToMany.js CHANGED
@@ -1,10 +1,11 @@
1
- import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
3
  export function ManyToMany(entity, mappedBy, options = {}) {
3
4
  return function (_, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_MANY);
7
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_MANY);
8
+ options = processDecoratorParameters({ entity, mappedBy, options });
8
9
  const property = { name: context.name, kind: ReferenceKind.MANY_TO_MANY };
9
10
  meta.properties[context.name] ??= {};
10
11
  Utils.mergeConfig(meta.properties[context.name], property, options);
package/es/ManyToOne.js CHANGED
@@ -1,10 +1,11 @@
1
- import { ReferenceKind, MetadataValidator, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
3
  export function ManyToOne(entity = {}, options = {}) {
3
4
  return function (_, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_ONE);
7
- options = Utils.processDecoratorParameters({ entity, options });
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_ONE);
8
+ options = processDecoratorParameters({ entity, options });
8
9
  const property = { name: context.name, kind: ReferenceKind.MANY_TO_ONE };
9
10
  meta.properties[context.name] = Utils.mergeConfig(meta.properties[context.name] ?? {}, property, options);
10
11
  };
package/es/OneToMany.js CHANGED
@@ -1,10 +1,11 @@
1
- import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
3
  export function OneToMany(entity, mappedBy, options = {}) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_MANY);
7
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_MANY);
8
+ options = processDecoratorParameters({ entity, mappedBy, options });
8
9
  const property = { name: context.name, kind: ReferenceKind.ONE_TO_MANY };
9
10
  meta.properties[context.name] ??= {};
10
11
  Utils.mergeConfig(meta.properties[context.name], property, options);
package/es/OneToOne.js CHANGED
@@ -1,12 +1,13 @@
1
- import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
3
  export function OneToOne(entity, mappedByOrOptions, options = {}) {
3
4
  const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
4
5
  options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
5
6
  return function (_, context) {
6
7
  const meta = context.metadata;
7
8
  meta.properties ??= {};
8
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_ONE);
9
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
9
+ validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_ONE);
10
+ options = processDecoratorParameters({ entity, mappedBy, options });
10
11
  const property = { name: context.name, kind: ReferenceKind.ONE_TO_ONE };
11
12
  meta.properties[context.name] = Object.assign(meta.properties[context.name] ?? {}, property, options);
12
13
  };
package/es/PrimaryKey.js CHANGED
@@ -1,9 +1,10 @@
1
- import { ReferenceKind, MetadataValidator, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator } from '../utils.js';
2
3
  function createDecorator(options, serialized) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
8
  const key = serialized ? 'serializedPrimaryKey' : 'primary';
8
9
  options[key] = true;
9
10
  meta.properties[context.name] = { name: context.name, kind: ReferenceKind.SCALAR, ...options };
package/es/Property.js CHANGED
@@ -1,9 +1,10 @@
1
- import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator } from '../utils.js';
2
3
  export function Property(options = {}) {
3
4
  return function (value, context) {
4
5
  const meta = context.metadata;
5
6
  meta.properties ??= {};
6
- MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
+ validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
8
  const { check, ...opts } = options;
8
9
  const prop = { kind: ReferenceKind.SCALAR, ...opts };
9
10
  const name = options.name ?? context.name;
@@ -1,5 +1,5 @@
1
1
  import { type TransactionOptions } from '@mikro-orm/core';
2
- import { type ContextProvider } from '../resolveContextProvider.js';
2
+ import { type ContextProvider } from '../utils.js';
3
3
  type TransactionalOptions<T> = TransactionOptions & {
4
4
  context?: ContextProvider<T>;
5
5
  contextName?: string;
@@ -1,5 +1,5 @@
1
1
  import { RequestContext, TransactionContext, TransactionPropagation } from '@mikro-orm/core';
2
- import { resolveContextProvider } from '../resolveContextProvider.js';
2
+ import { resolveContextProvider } from '../utils.js';
3
3
  /**
4
4
  * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
5
5
  * The difference is that you can specify the context in which the transaction begins by providing `context` option,
package/legacy/Check.js CHANGED
@@ -1,7 +1,7 @@
1
- import { MetadataStorage } from '@mikro-orm/core';
1
+ import { getMetadataFromDecorator } from '../utils.js';
2
2
  export function Check(options) {
3
3
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator((propertyName ? target.constructor : target));
4
+ const meta = getMetadataFromDecorator((propertyName ? target.constructor : target));
5
5
  options.property ??= propertyName;
6
6
  meta.checks.push(options);
7
7
  if (!propertyName) {
@@ -1,3 +1,3 @@
1
- import { type ContextProvider } from '../resolveContextProvider.js';
1
+ import { type ContextProvider } from '../utils.js';
2
2
  export declare function CreateRequestContext<T extends object>(context?: ContextProvider<T>, respectExistingContext?: boolean): MethodDecorator;
3
3
  export declare function EnsureRequestContext<T extends object>(context?: ContextProvider<T>): MethodDecorator;
@@ -1,5 +1,5 @@
1
1
  import { RequestContext, TransactionContext } from '@mikro-orm/core';
2
- import { resolveContextProvider } from '../resolveContextProvider.js';
2
+ import { resolveContextProvider } from '../utils.js';
3
3
  export function CreateRequestContext(context, respectExistingContext = false) {
4
4
  return function (target, propertyKey, descriptor) {
5
5
  const originalMethod = descriptor.value;
@@ -1,7 +1,7 @@
1
- import { MetadataStorage } from '@mikro-orm/core';
1
+ import { getMetadataFromDecorator } from '../utils.js';
2
2
  export function Embeddable(options = {}) {
3
3
  return function (target) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
4
+ const meta = getMetadataFromDecorator(target);
5
5
  meta.class = target;
6
6
  meta.name = meta.class.name;
7
7
  meta.embeddable = true;
@@ -1,8 +1,9 @@
1
- import { MetadataValidator, MetadataStorage, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function Embedded(type = {}, options = {}) {
3
4
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.EMBEDDED);
5
+ const meta = getMetadataFromDecorator(target.constructor);
6
+ validateSingleDecorator(meta, propertyName, ReferenceKind.EMBEDDED);
6
7
  options = type instanceof Function ? { entity: type, ...options } : { ...type, ...options };
7
8
  Utils.defaultValue(options, 'prefix', true);
8
9
  meta.properties[propertyName] = {
package/legacy/Entity.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, Utils } from '@mikro-orm/core';
1
+ import { Utils } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  export function Entity(options = {}) {
3
4
  return function (target) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ const meta = getMetadataFromDecorator(target);
5
6
  Utils.mergeConfig(meta, options);
6
7
  meta.class = target;
7
8
  if (!options.abstract || meta.discriminatorColumn) {
package/legacy/Enum.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, ReferenceKind, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  export function Enum(options = {}) {
3
4
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ const meta = getMetadataFromDecorator(target.constructor);
5
6
  options = options instanceof Function ? { items: options } : options;
6
7
  meta.properties[propertyName] = {
7
8
  name: propertyName,
package/legacy/Filter.js CHANGED
@@ -1,7 +1,7 @@
1
- import { MetadataStorage } from '@mikro-orm/core';
1
+ import { getMetadataFromDecorator } from '../utils.js';
2
2
  export function Filter(options) {
3
3
  return function (target) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target);
4
+ const meta = getMetadataFromDecorator(target);
5
5
  meta.filters[options.name] = options;
6
6
  };
7
7
  }
package/legacy/Formula.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, ReferenceKind, } from '@mikro-orm/core';
1
+ import { ReferenceKind } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  export function Formula(formula, options = {}) {
3
4
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ const meta = getMetadataFromDecorator(target.constructor);
5
6
  meta.properties[propertyName] = {
6
7
  name: propertyName,
7
8
  kind: ReferenceKind.SCALAR,
package/legacy/Indexed.js CHANGED
@@ -1,7 +1,7 @@
1
- import { MetadataStorage } from '@mikro-orm/core';
1
+ import { getMetadataFromDecorator } from '../utils.js';
2
2
  function createDecorator(options, unique) {
3
3
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(propertyName ? target.constructor : target);
4
+ const meta = getMetadataFromDecorator(propertyName ? target.constructor : target);
5
5
  options.properties ??= propertyName;
6
6
  const key = unique ? 'uniques' : 'indexes';
7
7
  meta[key].push(options);
@@ -1,9 +1,10 @@
1
- import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function ManyToMany(entity, mappedBy, options = {}) {
3
4
  return function (target, propertyName) {
4
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
5
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
6
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.MANY_TO_MANY);
5
+ options = processDecoratorParameters({ entity, mappedBy, options });
6
+ const meta = getMetadataFromDecorator(target.constructor);
7
+ validateSingleDecorator(meta, propertyName, ReferenceKind.MANY_TO_MANY);
7
8
  const property = { name: propertyName, kind: ReferenceKind.MANY_TO_MANY };
8
9
  meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
10
  };
@@ -1,9 +1,10 @@
1
- import { ReferenceKind, MetadataStorage, MetadataValidator, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function ManyToOne(entity = {}, options = {}) {
3
4
  return function (target, propertyName) {
4
- options = Utils.processDecoratorParameters({ entity, options });
5
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
6
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.MANY_TO_ONE);
5
+ options = processDecoratorParameters({ entity, options });
6
+ const meta = getMetadataFromDecorator(target.constructor);
7
+ validateSingleDecorator(meta, propertyName, ReferenceKind.MANY_TO_ONE);
7
8
  const property = { name: propertyName, kind: ReferenceKind.MANY_TO_ONE };
8
9
  meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
10
  };
@@ -1,9 +1,10 @@
1
- import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function OneToMany(entity, mappedBy, options = {}) {
3
4
  return function (target, propertyName) {
4
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
5
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
6
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_MANY);
5
+ options = processDecoratorParameters({ entity, mappedBy, options });
6
+ const meta = getMetadataFromDecorator(target.constructor);
7
+ validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_MANY);
7
8
  const property = { name: propertyName, kind: ReferenceKind.ONE_TO_MANY };
8
9
  meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
10
  };
@@ -1,11 +1,12 @@
1
- import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { processDecoratorParameters, validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function OneToOne(entity, mappedByOrOptions, options = {}) {
3
4
  const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
4
5
  options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
5
6
  return function (target, propertyName) {
6
- options = Utils.processDecoratorParameters({ entity, mappedBy, options });
7
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
8
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_ONE);
7
+ options = processDecoratorParameters({ entity, mappedBy, options });
8
+ const meta = getMetadataFromDecorator(target.constructor);
9
+ validateSingleDecorator(meta, propertyName, ReferenceKind.ONE_TO_ONE);
9
10
  const property = { name: propertyName, kind: ReferenceKind.ONE_TO_ONE };
10
11
  meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
11
12
  };
@@ -1,8 +1,9 @@
1
- import { MetadataStorage, MetadataValidator, ReferenceKind, } from '@mikro-orm/core';
1
+ import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  function createDecorator(options, serialized) {
3
4
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
5
+ const meta = getMetadataFromDecorator(target.constructor);
6
+ validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
6
7
  const k = serialized ? 'serializedPrimaryKey' : 'primary';
7
8
  options[k] = true;
8
9
  meta.properties[propertyName] = { name: propertyName, kind: ReferenceKind.SCALAR, ...options };
@@ -1,9 +1,10 @@
1
- import { MetadataStorage, MetadataValidator, Utils, ReferenceKind, } from '@mikro-orm/core';
1
+ import { Utils, ReferenceKind, } from '@mikro-orm/core';
2
+ import { validateSingleDecorator, getMetadataFromDecorator } from '../utils.js';
2
3
  export function Property(options = {}) {
3
4
  return function (target, propertyName) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ const meta = getMetadataFromDecorator(target.constructor);
5
6
  const desc = Object.getOwnPropertyDescriptor(target, propertyName) || {};
6
- MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
7
+ validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
7
8
  const name = options.name || propertyName;
8
9
  if (propertyName !== name && !(desc.value instanceof Function)) {
9
10
  Utils.renameKey(options, 'name', 'fieldName');
@@ -1,5 +1,5 @@
1
1
  import { type TransactionOptions } from '@mikro-orm/core';
2
- import { type ContextProvider } from '../resolveContextProvider.js';
2
+ import { type ContextProvider } from '../utils.js';
3
3
  type TransactionalOptions<T> = TransactionOptions & {
4
4
  context?: ContextProvider<T>;
5
5
  contextName?: string;
@@ -1,5 +1,5 @@
1
1
  import { TransactionPropagation, RequestContext, TransactionContext } from '@mikro-orm/core';
2
- import { resolveContextProvider } from '../resolveContextProvider.js';
2
+ import { resolveContextProvider } from '../utils.js';
3
3
  /**
4
4
  * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
5
5
  * The difference is that you can specify the context in which the transaction begins by providing `context` option,
package/legacy/hooks.js CHANGED
@@ -1,7 +1,8 @@
1
- import { MetadataStorage, EventType } from '@mikro-orm/core';
1
+ import { EventType } from '@mikro-orm/core';
2
+ import { getMetadataFromDecorator } from '../utils.js';
2
3
  function hook(type) {
3
4
  return function (target, method) {
4
- const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ const meta = getMetadataFromDecorator(target.constructor);
5
6
  meta.hooks[type] ??= [];
6
7
  meta.hooks[type].push(method);
7
8
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/decorators",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.65",
4
+ "version": "7.0.0-dev.67",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -55,7 +55,7 @@
55
55
  "@mikro-orm/core": "^6.6.1"
56
56
  },
57
57
  "peerDependencies": {
58
- "@mikro-orm/core": "7.0.0-dev.65",
58
+ "@mikro-orm/core": "7.0.0-dev.67",
59
59
  "reflect-metadata": "^0.1.0 || ^0.2.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
package/utils.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { type ReferenceKind, type EntityMetadata, type Dictionary, type MaybePromise, EntityManager, EntityRepository, MikroORM, MetadataStorage } from '@mikro-orm/core';
2
+ /**
3
+ * The type of context that the user intends to inject.
4
+ */
5
+ export type ContextProvider<T> = MaybePromise<MikroORM> | ((type: T) => MaybePromise<MikroORM | EntityManager | EntityRepository<any> | {
6
+ getEntityManager(): EntityManager;
7
+ }>);
8
+ /**
9
+ * Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
10
+ */
11
+ export declare function resolveContextProvider<T>(caller: T & {
12
+ orm?: MaybePromise<MikroORM>;
13
+ em?: MaybePromise<EntityManager>;
14
+ }, provider?: ContextProvider<T>): Promise<EntityManager | undefined>;
15
+ /**
16
+ * Relation decorators allow using two signatures
17
+ * - using first parameter as options object
18
+ * - using all parameters
19
+ *
20
+ * This function validates those two ways are not mixed and returns the final options object.
21
+ * If the second way is used, we always consider the last parameter as options object.
22
+ * @internal
23
+ */
24
+ export declare function processDecoratorParameters<T>(params: Dictionary): T;
25
+ /**
26
+ * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
27
+ * on the same property. One should use only `@ManyToOne()` in such case.
28
+ * We allow the existence of the property in metadata if the reference type is the same, this should allow things like HMR to work.
29
+ */
30
+ export declare function validateSingleDecorator(meta: EntityMetadata, propertyName: string, reference: ReferenceKind): void;
31
+ /**
32
+ * Uses some dark magic to get source path to caller where decorator is used.
33
+ * Analyzes stack trace of error created inside the function call.
34
+ */
35
+ export declare function lookupPathFromDecorator(name: string, stack?: string[]): string;
36
+ export declare function getMetadataFromDecorator<T = any>(target: T & Dictionary & {
37
+ [MetadataStorage.PATH_SYMBOL]?: string;
38
+ }): EntityMetadata<T>;
package/utils.js ADDED
@@ -0,0 +1,105 @@
1
+ import { Utils, EntityManager, EntityRepository, MikroORM, MetadataError, MetadataStorage, } from '@mikro-orm/core';
2
+ function getEntityManager(caller, context) {
3
+ if (context instanceof EntityManager) {
4
+ return context;
5
+ }
6
+ if (context instanceof EntityRepository) {
7
+ return context.getEntityManager();
8
+ }
9
+ if (context instanceof MikroORM) {
10
+ return context.em;
11
+ }
12
+ if (caller.em instanceof EntityManager) {
13
+ return caller.em;
14
+ }
15
+ if (caller.orm instanceof MikroORM) {
16
+ return caller.orm.em;
17
+ }
18
+ return undefined;
19
+ }
20
+ /**
21
+ * Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
22
+ */
23
+ export async function resolveContextProvider(caller, provider) {
24
+ const context = typeof provider === 'function' ? await provider(caller) : await provider;
25
+ return getEntityManager({ orm: await caller.orm, em: await caller.em }, context);
26
+ }
27
+ /**
28
+ * Relation decorators allow using two signatures
29
+ * - using first parameter as options object
30
+ * - using all parameters
31
+ *
32
+ * This function validates those two ways are not mixed and returns the final options object.
33
+ * If the second way is used, we always consider the last parameter as options object.
34
+ * @internal
35
+ */
36
+ export function processDecoratorParameters(params) {
37
+ const keys = Object.keys(params);
38
+ const values = Object.values(params);
39
+ if (!Utils.isPlainObject(values[0])) {
40
+ const lastKey = keys[keys.length - 1];
41
+ const last = params[lastKey];
42
+ delete params[lastKey];
43
+ return { ...last, ...params };
44
+ }
45
+ // validate only first parameter is used if its an option object
46
+ const empty = (v) => v == null || (Utils.isPlainObject(v) && !Utils.hasObjectKeys(v));
47
+ if (values.slice(1).some(v => !empty(v))) {
48
+ throw new Error('Mixing first decorator parameter as options object with other parameters is forbidden. ' +
49
+ 'If you want to use the options parameter at first position, provide all options inside it.');
50
+ }
51
+ return values[0];
52
+ }
53
+ /**
54
+ * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
55
+ * on the same property. One should use only `@ManyToOne()` in such case.
56
+ * We allow the existence of the property in metadata if the reference type is the same, this should allow things like HMR to work.
57
+ */
58
+ export function validateSingleDecorator(meta, propertyName, reference) {
59
+ if (meta.properties[propertyName] && meta.properties[propertyName].kind !== reference) {
60
+ throw MetadataError.multipleDecorators(meta.className, propertyName);
61
+ }
62
+ }
63
+ /**
64
+ * Uses some dark magic to get source path to caller where decorator is used.
65
+ * Analyzes stack trace of error created inside the function call.
66
+ */
67
+ export function lookupPathFromDecorator(name, stack) {
68
+ // use some dark magic to get source path to caller
69
+ stack = stack || new Error().stack.split('\n');
70
+ // In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
71
+ // __decorate(), replacing it with the constructor name. To support these cases we look for
72
+ // Reflect.decorate() as well. Also when babel is used, we need to check
73
+ // the `_applyDecoratedDescriptor` method instead.
74
+ let line = stack.findIndex(line => line.match(/__decorate|Reflect\.decorate|_applyDecoratedDescriptor/));
75
+ // bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
76
+ // but those are also present in node, so we need to check this only if they weren't found.
77
+ if (line === -1) {
78
+ // here we handle bun which stack is different from nodejs so we search for reflect-metadata
79
+ // Different bun versions might have different stack traces. The "last index" works for both 1.2.6 and 1.2.7.
80
+ const reflectLine = stack.findLastIndex(line => Utils.normalizePath(line).includes('node_modules/reflect-metadata/Reflect.js'));
81
+ if (reflectLine === -1 || reflectLine + 2 >= stack.length || !stack[reflectLine + 1].includes('bun:wrap')) {
82
+ return name;
83
+ }
84
+ line = reflectLine + 2;
85
+ }
86
+ if (stack[line].includes('Reflect.decorate')) {
87
+ line++;
88
+ }
89
+ if (Utils.normalizePath(stack[line]).includes('node_modules/tslib/tslib')) {
90
+ line++;
91
+ }
92
+ try {
93
+ const re = stack[line].match(/\(.+\)/i) ? /\((.*):\d+:\d+\)/ : /at\s*(.*):\d+:\d+$/;
94
+ return Utils.normalizePath(stack[line].match(re)[1]);
95
+ }
96
+ catch {
97
+ return name;
98
+ }
99
+ }
100
+ export function getMetadataFromDecorator(target) {
101
+ if (!Object.hasOwn(target, MetadataStorage.PATH_SYMBOL)) {
102
+ Object.defineProperty(target, MetadataStorage.PATH_SYMBOL, { value: lookupPathFromDecorator(target.name), writable: true });
103
+ }
104
+ return MetadataStorage.getMetadata(target.name, target[MetadataStorage.PATH_SYMBOL]);
105
+ }
@@ -1,14 +0,0 @@
1
- import { type MaybePromise, EntityManager, EntityRepository, MikroORM } from '@mikro-orm/core';
2
- /**
3
- * The type of context that the user intends to inject.
4
- */
5
- export type ContextProvider<T> = MaybePromise<MikroORM> | ((type: T) => MaybePromise<MikroORM | EntityManager | EntityRepository<any> | {
6
- getEntityManager(): EntityManager;
7
- }>);
8
- /**
9
- * Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
10
- */
11
- export declare function resolveContextProvider<T>(caller: T & {
12
- orm?: MaybePromise<MikroORM>;
13
- em?: MaybePromise<EntityManager>;
14
- }, provider?: ContextProvider<T>): Promise<EntityManager | undefined>;
@@ -1,26 +0,0 @@
1
- import { EntityManager, EntityRepository, MikroORM } from '@mikro-orm/core';
2
- function getEntityManager(caller, context) {
3
- if (context instanceof EntityManager) {
4
- return context;
5
- }
6
- if (context instanceof EntityRepository) {
7
- return context.getEntityManager();
8
- }
9
- if (context instanceof MikroORM) {
10
- return context.em;
11
- }
12
- if (caller.em instanceof EntityManager) {
13
- return caller.em;
14
- }
15
- if (caller.orm instanceof MikroORM) {
16
- return caller.orm.em;
17
- }
18
- return undefined;
19
- }
20
- /**
21
- * Find `EntityManager` in provided context, or else in instance's `orm` or `em` properties.
22
- */
23
- export async function resolveContextProvider(caller, provider) {
24
- const context = typeof provider === 'function' ? await provider(caller) : await provider;
25
- return getEntityManager({ orm: await caller.orm, em: await caller.em }, context);
26
- }