@mikro-orm/decorators 7.0.0-dev.64

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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +390 -0
  3. package/es/Check.d.ts +2 -0
  4. package/es/Check.js +8 -0
  5. package/es/CreateRequestContext.d.ts +3 -0
  6. package/es/CreateRequestContext.js +29 -0
  7. package/es/Embeddable.d.ts +2 -0
  8. package/es/Embeddable.js +11 -0
  9. package/es/Embedded.d.ts +2 -0
  10. package/es/Embedded.js +15 -0
  11. package/es/Entity.d.ts +2 -0
  12. package/es/Entity.js +11 -0
  13. package/es/Enum.d.ts +2 -0
  14. package/es/Enum.js +14 -0
  15. package/es/Filter.d.ts +2 -0
  16. package/es/Filter.js +7 -0
  17. package/es/Formula.d.ts +2 -0
  18. package/es/Formula.js +13 -0
  19. package/es/Indexed.d.ts +3 -0
  20. package/es/Indexed.js +17 -0
  21. package/es/ManyToMany.d.ts +2 -0
  22. package/es/ManyToMany.js +12 -0
  23. package/es/ManyToOne.d.ts +2 -0
  24. package/es/ManyToOne.js +11 -0
  25. package/es/OneToMany.d.ts +3 -0
  26. package/es/OneToMany.js +12 -0
  27. package/es/OneToOne.d.ts +2 -0
  28. package/es/OneToOne.js +13 -0
  29. package/es/PrimaryKey.d.ts +3 -0
  30. package/es/PrimaryKey.js +17 -0
  31. package/es/Property.d.ts +2 -0
  32. package/es/Property.js +31 -0
  33. package/es/Transactional.d.ts +15 -0
  34. package/es/Transactional.js +27 -0
  35. package/es/hooks.d.ts +16 -0
  36. package/es/hooks.js +45 -0
  37. package/es/index.d.ts +17 -0
  38. package/es/index.js +17 -0
  39. package/legacy/Check.d.ts +2 -0
  40. package/legacy/Check.js +12 -0
  41. package/legacy/CreateRequestContext.d.ts +3 -0
  42. package/legacy/CreateRequestContext.js +31 -0
  43. package/legacy/Embeddable.d.ts +2 -0
  44. package/legacy/Embeddable.js +11 -0
  45. package/legacy/Embedded.d.ts +2 -0
  46. package/legacy/Embedded.js +14 -0
  47. package/legacy/Entity.d.ts +2 -0
  48. package/legacy/Entity.js +11 -0
  49. package/legacy/Enum.d.ts +2 -0
  50. package/legacy/Enum.js +13 -0
  51. package/legacy/Filter.d.ts +2 -0
  52. package/legacy/Filter.js +7 -0
  53. package/legacy/Formula.d.ts +2 -0
  54. package/legacy/Formula.js +12 -0
  55. package/legacy/Indexed.d.ts +3 -0
  56. package/legacy/Indexed.js +19 -0
  57. package/legacy/ManyToMany.d.ts +2 -0
  58. package/legacy/ManyToMany.js +10 -0
  59. package/legacy/ManyToOne.d.ts +2 -0
  60. package/legacy/ManyToOne.js +10 -0
  61. package/legacy/OneToMany.d.ts +3 -0
  62. package/legacy/OneToMany.js +10 -0
  63. package/legacy/OneToOne.d.ts +2 -0
  64. package/legacy/OneToOne.js +12 -0
  65. package/legacy/PrimaryKey.d.ts +3 -0
  66. package/legacy/PrimaryKey.js +16 -0
  67. package/legacy/Property.d.ts +2 -0
  68. package/legacy/Property.js +28 -0
  69. package/legacy/ReflectMetadataProvider.d.ts +6 -0
  70. package/legacy/ReflectMetadataProvider.js +39 -0
  71. package/legacy/Transactional.d.ts +15 -0
  72. package/legacy/Transactional.js +29 -0
  73. package/legacy/hooks.d.ts +16 -0
  74. package/legacy/hooks.js +44 -0
  75. package/legacy/index.d.ts +18 -0
  76. package/legacy/index.js +18 -0
  77. package/package.json +66 -0
  78. package/resolveContextProvider.d.ts +14 -0
  79. package/resolveContextProvider.js +26 -0
package/es/OneToOne.js ADDED
@@ -0,0 +1,13 @@
1
+ import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function OneToOne(entity, mappedByOrOptions, options = {}) {
3
+ const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
4
+ options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
5
+ return function (_, context) {
6
+ const meta = context.metadata;
7
+ meta.properties ??= {};
8
+ MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_ONE);
9
+ options = Utils.processDecoratorParameters({ entity, mappedBy, options });
10
+ const property = { name: context.name, kind: ReferenceKind.ONE_TO_ONE };
11
+ meta.properties[context.name] = Object.assign(meta.properties[context.name] ?? {}, property, options);
12
+ };
13
+ }
@@ -0,0 +1,3 @@
1
+ import { type PrimaryKeyOptions, type SerializedPrimaryKeyOptions } from '@mikro-orm/core';
2
+ export declare function PrimaryKey<T extends object>(options?: PrimaryKeyOptions<T>): (value: unknown, context: ClassFieldDecoratorContext<T, unknown>) => void;
3
+ export declare function SerializedPrimaryKey<T extends object>(options?: SerializedPrimaryKeyOptions<T>): (value: unknown, context: ClassFieldDecoratorContext<T, unknown>) => void;
@@ -0,0 +1,17 @@
1
+ import { ReferenceKind, MetadataValidator, } from '@mikro-orm/core';
2
+ function createDecorator(options, serialized) {
3
+ return function (value, context) {
4
+ const meta = context.metadata;
5
+ meta.properties ??= {};
6
+ MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
+ const key = serialized ? 'serializedPrimaryKey' : 'primary';
8
+ options[key] = true;
9
+ meta.properties[context.name] = { name: context.name, kind: ReferenceKind.SCALAR, ...options };
10
+ };
11
+ }
12
+ export function PrimaryKey(options = {}) {
13
+ return createDecorator(options, false);
14
+ }
15
+ export function SerializedPrimaryKey(options = {}) {
16
+ return createDecorator(options, true);
17
+ }
@@ -0,0 +1,2 @@
1
+ import { type PropertyOptions } from '@mikro-orm/core';
2
+ export declare function Property<T extends object>(options?: PropertyOptions<T>): (value: unknown, context: ClassFieldDecoratorContext<T> | ClassGetterDecoratorContext<T> | ClassMethodDecoratorContext<T>) => void;
package/es/Property.js ADDED
@@ -0,0 +1,31 @@
1
+ import { MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function Property(options = {}) {
3
+ return function (value, context) {
4
+ const meta = context.metadata;
5
+ meta.properties ??= {};
6
+ MetadataValidator.validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
7
+ const { check, ...opts } = options;
8
+ const prop = { kind: ReferenceKind.SCALAR, ...opts };
9
+ const name = options.name ?? context.name;
10
+ meta.checks ??= [];
11
+ if (context.name !== name) {
12
+ Utils.renameKey(options, 'name', 'fieldName');
13
+ }
14
+ if (context.kind === 'field') {
15
+ prop.name = context.name;
16
+ prop.getter = false;
17
+ prop.setter = false;
18
+ }
19
+ else if (context.kind === 'method') {
20
+ prop.getter = true;
21
+ prop.persist = false;
22
+ prop.type = 'method';
23
+ prop.getterName = context.name;
24
+ prop.name = name;
25
+ }
26
+ if (check) {
27
+ meta.checks.push({ property: prop.name, expression: check });
28
+ }
29
+ meta.properties[prop.name] = prop;
30
+ };
31
+ }
@@ -0,0 +1,15 @@
1
+ import { type TransactionOptions } from '@mikro-orm/core';
2
+ import { type ContextProvider } from '../resolveContextProvider.js';
3
+ type TransactionalOptions<T> = TransactionOptions & {
4
+ context?: ContextProvider<T>;
5
+ contextName?: string;
6
+ };
7
+ /**
8
+ * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
9
+ * The difference is that you can specify the context in which the transaction begins by providing `context` option,
10
+ * and if omitted, the transaction will begin in the current context implicitly.
11
+ * It works on async functions and can be nested with `em.transactional()`.
12
+ * Unlike `em.transactional()`, this decorator uses `REQUIRED` propagation by default, which means it will join existing transactions.
13
+ */
14
+ export declare function Transactional<Owner extends object, Value extends (this: Owner, ...args: any) => any = (this: Owner, ...args: any) => any>(options?: TransactionalOptions<Owner>): (value: Value, context: ClassMethodDecoratorContext<Owner, Value>) => (this: Owner, ...args: any) => Promise<any>;
15
+ export {};
@@ -0,0 +1,27 @@
1
+ import { RequestContext, TransactionContext, TransactionPropagation } from '@mikro-orm/core';
2
+ import { resolveContextProvider } from '../resolveContextProvider.js';
3
+ /**
4
+ * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
5
+ * The difference is that you can specify the context in which the transaction begins by providing `context` option,
6
+ * and if omitted, the transaction will begin in the current context implicitly.
7
+ * It works on async functions and can be nested with `em.transactional()`.
8
+ * Unlike `em.transactional()`, this decorator uses `REQUIRED` propagation by default, which means it will join existing transactions.
9
+ */
10
+ export function Transactional(options = {}) {
11
+ return function (value, context) {
12
+ if (value.constructor.name !== 'AsyncFunction') {
13
+ throw new Error('@Transactional() should be use with async functions');
14
+ }
15
+ return async function (...args) {
16
+ const { context, contextName, ...txOptions } = options;
17
+ txOptions.propagation ??= TransactionPropagation.REQUIRED;
18
+ const em = (await resolveContextProvider(this, context))
19
+ || TransactionContext.getEntityManager(contextName)
20
+ || RequestContext.getEntityManager(contextName);
21
+ if (!em) {
22
+ throw new Error(`@Transactional() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@Transactional(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
23
+ }
24
+ return em.transactional(() => value.apply(this, args), txOptions);
25
+ };
26
+ };
27
+ }
package/es/hooks.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export declare function BeforeCreate(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
2
+ export declare function AfterCreate(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
3
+ export declare function BeforeUpdate(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
4
+ export declare function AfterUpdate(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
5
+ export declare function BeforeUpsert(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
6
+ export declare function AfterUpsert(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
7
+ export declare function OnInit(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
8
+ export declare function OnLoad(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
9
+ /**
10
+ * Called before deleting entity, but only when providing initialized entity to EM#remove()
11
+ */
12
+ export declare function BeforeDelete(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
13
+ /**
14
+ * Called after deleting entity, but only when providing initialized entity to EM#remove()
15
+ */
16
+ export declare function AfterDelete(): (value: (...args: any[]) => unknown, context: ClassMethodDecoratorContext) => void;
package/es/hooks.js ADDED
@@ -0,0 +1,45 @@
1
+ import { EventType } from '@mikro-orm/core';
2
+ function hook(type) {
3
+ return function (value, context) {
4
+ const meta = context.metadata;
5
+ meta.hooks ??= {};
6
+ meta.hooks[type] ??= [];
7
+ meta.hooks[type].push(value);
8
+ };
9
+ }
10
+ export function BeforeCreate() {
11
+ return hook(EventType.beforeCreate);
12
+ }
13
+ export function AfterCreate() {
14
+ return hook(EventType.afterCreate);
15
+ }
16
+ export function BeforeUpdate() {
17
+ return hook(EventType.beforeUpdate);
18
+ }
19
+ export function AfterUpdate() {
20
+ return hook(EventType.afterUpdate);
21
+ }
22
+ export function BeforeUpsert() {
23
+ return hook(EventType.beforeUpsert);
24
+ }
25
+ export function AfterUpsert() {
26
+ return hook(EventType.afterUpsert);
27
+ }
28
+ export function OnInit() {
29
+ return hook(EventType.onInit);
30
+ }
31
+ export function OnLoad() {
32
+ return hook(EventType.onLoad);
33
+ }
34
+ /**
35
+ * Called before deleting entity, but only when providing initialized entity to EM#remove()
36
+ */
37
+ export function BeforeDelete() {
38
+ return hook(EventType.beforeDelete);
39
+ }
40
+ /**
41
+ * Called after deleting entity, but only when providing initialized entity to EM#remove()
42
+ */
43
+ export function AfterDelete() {
44
+ return hook(EventType.afterDelete);
45
+ }
package/es/index.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ export * from './PrimaryKey.js';
2
+ export * from './Entity.js';
3
+ export * from './OneToOne.js';
4
+ export * from './ManyToOne.js';
5
+ export * from './ManyToMany.js';
6
+ export * from './OneToMany.js';
7
+ export * from './Property.js';
8
+ export * from './Check.js';
9
+ export * from './Enum.js';
10
+ export * from './Formula.js';
11
+ export * from './Indexed.js';
12
+ export * from './Embeddable.js';
13
+ export * from './Embedded.js';
14
+ export * from './Filter.js';
15
+ export * from './CreateRequestContext.js';
16
+ export * from './hooks.js';
17
+ export * from './Transactional.js';
package/es/index.js ADDED
@@ -0,0 +1,17 @@
1
+ export * from './PrimaryKey.js';
2
+ export * from './Entity.js';
3
+ export * from './OneToOne.js';
4
+ export * from './ManyToOne.js';
5
+ export * from './ManyToMany.js';
6
+ export * from './OneToMany.js';
7
+ export * from './Property.js';
8
+ export * from './Check.js';
9
+ export * from './Enum.js';
10
+ export * from './Formula.js';
11
+ export * from './Indexed.js';
12
+ export * from './Embeddable.js';
13
+ export * from './Embedded.js';
14
+ export * from './Filter.js';
15
+ export * from './CreateRequestContext.js';
16
+ export * from './hooks.js';
17
+ export * from './Transactional.js';
@@ -0,0 +1,2 @@
1
+ import { type CheckConstraint, type EntityClass } from '@mikro-orm/core';
2
+ export declare function Check<T>(options: CheckConstraint<T>): (target: T, propertyName?: T extends EntityClass<unknown> ? undefined : keyof T) => any;
@@ -0,0 +1,12 @@
1
+ import { MetadataStorage } from '@mikro-orm/core';
2
+ export function Check(options) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator((propertyName ? target.constructor : target));
5
+ options.property ??= propertyName;
6
+ meta.checks.push(options);
7
+ if (!propertyName) {
8
+ return target;
9
+ }
10
+ return undefined;
11
+ };
12
+ }
@@ -0,0 +1,3 @@
1
+ import { type ContextProvider } from '../resolveContextProvider.js';
2
+ export declare function CreateRequestContext<T extends object>(context?: ContextProvider<T>, respectExistingContext?: boolean): MethodDecorator;
3
+ export declare function EnsureRequestContext<T extends object>(context?: ContextProvider<T>): MethodDecorator;
@@ -0,0 +1,31 @@
1
+ import { RequestContext, TransactionContext } from '@mikro-orm/core';
2
+ import { resolveContextProvider } from '../resolveContextProvider.js';
3
+ export function CreateRequestContext(context, respectExistingContext = false) {
4
+ return function (target, propertyKey, descriptor) {
5
+ const originalMethod = descriptor.value;
6
+ const name = respectExistingContext ? 'EnsureRequestContext' : 'CreateRequestContext';
7
+ if (originalMethod.constructor.name !== 'AsyncFunction') {
8
+ throw new Error(`@${name}() should be use with async functions`);
9
+ }
10
+ descriptor.value = async function (...args) {
11
+ const em = await resolveContextProvider(this, context);
12
+ if (!em) {
13
+ throw new Error(`@${name}() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@${name}(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
14
+ }
15
+ // reuse existing context if available for given respect `contextName`
16
+ if (respectExistingContext && RequestContext.getEntityManager(em.name)) {
17
+ return originalMethod.apply(this, args);
18
+ }
19
+ // Otherwise, the outer tx context would be preferred.
20
+ const txContext = TransactionContext.currentTransactionContext();
21
+ const provider = txContext ? TransactionContext : RequestContext;
22
+ return txContext
23
+ ? provider.create(em.fork({ useContext: true }), () => originalMethod.apply(this, args))
24
+ : provider.create(em, () => originalMethod.apply(this, args));
25
+ };
26
+ return descriptor;
27
+ };
28
+ }
29
+ export function EnsureRequestContext(context) {
30
+ return CreateRequestContext(context, true);
31
+ }
@@ -0,0 +1,2 @@
1
+ import { type EmbeddableOptions } from '@mikro-orm/core';
2
+ export declare function Embeddable<T>(options?: EmbeddableOptions<T>): (target: T) => T;
@@ -0,0 +1,11 @@
1
+ import { MetadataStorage } from '@mikro-orm/core';
2
+ export function Embeddable(options = {}) {
3
+ return function (target) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ meta.class = target;
6
+ meta.name = meta.class.name;
7
+ meta.embeddable = true;
8
+ Object.assign(meta, options);
9
+ return target;
10
+ };
11
+ }
@@ -0,0 +1,2 @@
1
+ import { type AnyEntity, type EntityName, type EmbeddedOptions } from '@mikro-orm/core';
2
+ export declare function Embedded<Owner extends object, Target>(type?: EmbeddedOptions<Owner, Target> | (() => EntityName<Target> | EntityName<Target>[]), options?: EmbeddedOptions<Owner, Target>): (target: AnyEntity, propertyName: string) => void;
@@ -0,0 +1,14 @@
1
+ import { MetadataValidator, MetadataStorage, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function Embedded(type = {}, options = {}) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.EMBEDDED);
6
+ options = type instanceof Function ? { entity: type, ...options } : { ...type, ...options };
7
+ Utils.defaultValue(options, 'prefix', true);
8
+ meta.properties[propertyName] = {
9
+ name: propertyName,
10
+ kind: ReferenceKind.EMBEDDED,
11
+ ...options,
12
+ };
13
+ };
14
+ }
@@ -0,0 +1,2 @@
1
+ import { type EntityClass, type EntityOptions } from '@mikro-orm/core';
2
+ export declare function Entity<T extends EntityClass<unknown>>(options?: EntityOptions<T>): (target: T) => void;
@@ -0,0 +1,11 @@
1
+ import { MetadataStorage, Utils } from '@mikro-orm/core';
2
+ export function Entity(options = {}) {
3
+ return function (target) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ Utils.mergeConfig(meta, options);
6
+ meta.class = target;
7
+ if (!options.abstract || meta.discriminatorColumn) {
8
+ meta.name = target.name;
9
+ }
10
+ };
11
+ }
@@ -0,0 +1,2 @@
1
+ import { type EnumOptions, type AnyEntity, type Dictionary } from '@mikro-orm/core';
2
+ export declare function Enum<T extends object>(options?: EnumOptions<AnyEntity> | (() => Dictionary)): (target: T, propertyName: string) => void;
package/legacy/Enum.js ADDED
@@ -0,0 +1,13 @@
1
+ import { MetadataStorage, ReferenceKind, } from '@mikro-orm/core';
2
+ export function Enum(options = {}) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ options = options instanceof Function ? { items: options } : options;
6
+ meta.properties[propertyName] = {
7
+ name: propertyName,
8
+ kind: ReferenceKind.SCALAR,
9
+ enum: true,
10
+ ...options,
11
+ };
12
+ };
13
+ }
@@ -0,0 +1,2 @@
1
+ import { type FilterDef, type EntityClass } from '@mikro-orm/core';
2
+ export declare function Filter<T extends EntityClass<unknown>>(options: FilterDef<T>): (target: T) => void;
@@ -0,0 +1,7 @@
1
+ import { MetadataStorage } from '@mikro-orm/core';
2
+ export function Filter(options) {
3
+ return function (target) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target);
5
+ meta.filters[options.name] = options;
6
+ };
7
+ }
@@ -0,0 +1,2 @@
1
+ import { type PropertyOptions } from '@mikro-orm/core';
2
+ export declare function Formula<T extends object>(formula: string | ((alias: string) => string), options?: PropertyOptions<T>): (target: T, propertyName: string) => void;
@@ -0,0 +1,12 @@
1
+ import { MetadataStorage, ReferenceKind, } from '@mikro-orm/core';
2
+ export function Formula(formula, options = {}) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ meta.properties[propertyName] = {
6
+ name: propertyName,
7
+ kind: ReferenceKind.SCALAR,
8
+ formula,
9
+ ...options,
10
+ };
11
+ };
12
+ }
@@ -0,0 +1,3 @@
1
+ import { type EntityClass, type IndexOptions, type UniqueOptions } from '@mikro-orm/core';
2
+ export declare function Index<T extends object, H extends string>(options?: IndexOptions<T, H>): (target: T, propertyName?: (T extends EntityClass<unknown> ? undefined : keyof T) | undefined) => any;
3
+ export declare function Unique<T extends object, H extends string>(options?: UniqueOptions<T, H>): (target: T, propertyName?: (T extends EntityClass<unknown> ? undefined : keyof T) | undefined) => any;
@@ -0,0 +1,19 @@
1
+ import { MetadataStorage } from '@mikro-orm/core';
2
+ function createDecorator(options, unique) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(propertyName ? target.constructor : target);
5
+ options.properties ??= propertyName;
6
+ const key = unique ? 'uniques' : 'indexes';
7
+ meta[key].push(options);
8
+ if (!propertyName) {
9
+ return target;
10
+ }
11
+ return undefined;
12
+ };
13
+ }
14
+ export function Index(options = {}) {
15
+ return createDecorator(options, false);
16
+ }
17
+ export function Unique(options = {}) {
18
+ return createDecorator(options, true);
19
+ }
@@ -0,0 +1,2 @@
1
+ import { type EntityName, type ManyToManyOptions } from '@mikro-orm/core';
2
+ export declare function ManyToMany<Target extends object, Owner extends object>(entity?: ManyToManyOptions<Owner, Target> | string | (() => EntityName<Target>), mappedBy?: (string & keyof Target) | ((e: Target) => any), options?: Partial<ManyToManyOptions<Owner, Target>>): (target: Owner, propertyName: keyof Owner) => void;
@@ -0,0 +1,10 @@
1
+ import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function ManyToMany(entity, mappedBy, options = {}) {
3
+ 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);
7
+ const property = { name: propertyName, kind: ReferenceKind.MANY_TO_MANY };
8
+ meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ import { type ManyToOneOptions, type EntityName } from '@mikro-orm/core';
2
+ export declare function ManyToOne<Target extends object, Owner extends object>(entity?: ManyToOneOptions<Owner, Target> | string | ((e?: any) => EntityName<Target>), options?: Partial<ManyToOneOptions<Owner, Target>>): (target: Owner, propertyName: keyof Owner) => void;
@@ -0,0 +1,10 @@
1
+ import { ReferenceKind, MetadataStorage, MetadataValidator, Utils, } from '@mikro-orm/core';
2
+ export function ManyToOne(entity = {}, options = {}) {
3
+ 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);
7
+ const property = { name: propertyName, kind: ReferenceKind.MANY_TO_ONE };
8
+ meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
+ };
10
+ }
@@ -0,0 +1,3 @@
1
+ import { type EntityName, type OneToManyOptions } from '@mikro-orm/core';
2
+ export declare function OneToMany<Target extends object, Owner extends object>(entity: string | ((e?: any) => EntityName<Target>), mappedBy: (string & keyof Target) | ((e: Target) => any), options?: Partial<OneToManyOptions<Owner, Target>>): (target: Owner, propertyName: string) => void;
3
+ export declare function OneToMany<Target extends object, Owner extends object>(options: OneToManyOptions<Owner, Target>): (target: Owner, propertyName: string) => void;
@@ -0,0 +1,10 @@
1
+ import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function OneToMany(entity, mappedBy, options = {}) {
3
+ 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);
7
+ const property = { name: propertyName, kind: ReferenceKind.ONE_TO_MANY };
8
+ meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ import { type EntityName, type OneToOneOptions } from '@mikro-orm/core';
2
+ export declare function OneToOne<Target, Owner>(entity?: OneToOneOptions<Owner, Target> | string | ((e: Owner) => EntityName<Target>), mappedByOrOptions?: (string & keyof Target) | ((e: Target) => any) | Partial<OneToOneOptions<Owner, Target>>, options?: Partial<OneToOneOptions<Owner, Target>>): (target: Owner, propertyName: string) => void;
@@ -0,0 +1,12 @@
1
+ import { MetadataStorage, MetadataValidator, ReferenceKind, Utils, } from '@mikro-orm/core';
2
+ export function OneToOne(entity, mappedByOrOptions, options = {}) {
3
+ const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
4
+ options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
5
+ 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);
9
+ const property = { name: propertyName, kind: ReferenceKind.ONE_TO_ONE };
10
+ meta.properties[propertyName] = Object.assign(meta.properties[propertyName] ?? {}, property, options);
11
+ };
12
+ }
@@ -0,0 +1,3 @@
1
+ import { type PrimaryKeyOptions, type SerializedPrimaryKeyOptions } from '@mikro-orm/core';
2
+ export declare function PrimaryKey<T extends object>(options?: PrimaryKeyOptions<T>): (target: T, propertyName: string) => void;
3
+ export declare function SerializedPrimaryKey<T extends object>(options?: SerializedPrimaryKeyOptions<T>): (target: T, propertyName: string) => void;
@@ -0,0 +1,16 @@
1
+ import { MetadataStorage, MetadataValidator, ReferenceKind, } from '@mikro-orm/core';
2
+ function createDecorator(options, serialized) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
6
+ const k = serialized ? 'serializedPrimaryKey' : 'primary';
7
+ options[k] = true;
8
+ meta.properties[propertyName] = { name: propertyName, kind: ReferenceKind.SCALAR, ...options };
9
+ };
10
+ }
11
+ export function PrimaryKey(options = {}) {
12
+ return createDecorator(options, false);
13
+ }
14
+ export function SerializedPrimaryKey(options = {}) {
15
+ return createDecorator(options, true);
16
+ }
@@ -0,0 +1,2 @@
1
+ import { type PropertyOptions } from '@mikro-orm/core';
2
+ export declare function Property<T extends object>(options?: PropertyOptions<T>): (target: T, propertyName: string) => void;
@@ -0,0 +1,28 @@
1
+ import { MetadataStorage, MetadataValidator, Utils, ReferenceKind, } from '@mikro-orm/core';
2
+ export function Property(options = {}) {
3
+ return function (target, propertyName) {
4
+ const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
5
+ const desc = Object.getOwnPropertyDescriptor(target, propertyName) || {};
6
+ MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
7
+ const name = options.name || propertyName;
8
+ if (propertyName !== name && !(desc.value instanceof Function)) {
9
+ Utils.renameKey(options, 'name', 'fieldName');
10
+ }
11
+ options.name = propertyName;
12
+ const { check, ...opts } = options;
13
+ const prop = { kind: ReferenceKind.SCALAR, ...opts };
14
+ prop.getter = !!desc.get;
15
+ prop.setter = !!desc.set;
16
+ if (desc.value instanceof Function) {
17
+ prop.getter = true;
18
+ prop.persist = false;
19
+ prop.type = 'method';
20
+ prop.getterName = propertyName;
21
+ prop.name = name;
22
+ }
23
+ if (check) {
24
+ meta.checks.push({ property: prop.name, expression: check });
25
+ }
26
+ meta.properties[prop.name] = prop;
27
+ };
28
+ }
@@ -0,0 +1,6 @@
1
+ import 'reflect-metadata';
2
+ import { type EntityMetadata, type EntityProperty, MetadataProvider } from '@mikro-orm/core';
3
+ export declare class ReflectMetadataProvider extends MetadataProvider {
4
+ loadEntityMetadata(meta: EntityMetadata): void;
5
+ protected initPropertyType(meta: EntityMetadata, prop: EntityProperty): void;
6
+ }
@@ -0,0 +1,39 @@
1
+ import 'reflect-metadata';
2
+ import { MetadataProvider, ReferenceKind, Utils } from '@mikro-orm/core';
3
+ export class ReflectMetadataProvider extends MetadataProvider {
4
+ loadEntityMetadata(meta) {
5
+ // load types and column names
6
+ for (const prop of meta.props) {
7
+ if (typeof prop.entity === 'string') {
8
+ prop.type = prop.entity;
9
+ }
10
+ else if (prop.entity) {
11
+ const tmp = prop.entity();
12
+ prop.type = Array.isArray(tmp) ? tmp.map(t => Utils.className(t)).sort().join(' | ') : Utils.className(tmp);
13
+ }
14
+ else {
15
+ this.initPropertyType(meta, prop);
16
+ }
17
+ }
18
+ }
19
+ initPropertyType(meta, prop) {
20
+ const type = Reflect.getMetadata('design:type', meta.prototype, prop.name);
21
+ if (!prop.type && (!type || (type === Object && prop.kind !== ReferenceKind.SCALAR)) && !(prop.enum && (prop.items?.length ?? 0) > 0)) {
22
+ throw new Error(`Please provide either 'type' or 'entity' attribute in ${meta.className}.${prop.name}. Make sure you have 'emitDecoratorMetadata' enabled in your tsconfig.json.`);
23
+ }
24
+ // Force mapping to UnknownType which is a string when we see just `Object`, as that often means failed inference.
25
+ // This is to prevent defaulting to JSON column type, which can often be hard to revert and cause hard to understand issues with PKs.
26
+ // If there are explicitly provided `columnTypes`, we use those instead for the inference, this way
27
+ // we can have things like `columnType: 'timestamp'` be respected as `type: 'Date'`.
28
+ if (prop.kind === ReferenceKind.SCALAR && type === Object && !prop.columnTypes) {
29
+ prop.type ??= 'any';
30
+ return;
31
+ }
32
+ let typeName = type?.name;
33
+ if (typeName && ['string', 'number', 'boolean', 'array', 'object'].includes(typeName.toLowerCase())) {
34
+ typeName = typeName.toLowerCase();
35
+ }
36
+ prop.type ??= typeName;
37
+ prop.runtimeType ??= typeName;
38
+ }
39
+ }
@@ -0,0 +1,15 @@
1
+ import { type TransactionOptions } from '@mikro-orm/core';
2
+ import { type ContextProvider } from '../resolveContextProvider.js';
3
+ type TransactionalOptions<T> = TransactionOptions & {
4
+ context?: ContextProvider<T>;
5
+ contextName?: string;
6
+ };
7
+ /**
8
+ * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
9
+ * The difference is that you can specify the context in which the transaction begins by providing `context` option,
10
+ * and if omitted, the transaction will begin in the current context implicitly.
11
+ * It works on async functions and can be nested with `em.transactional()`.
12
+ * Unlike `em.transactional()`, this decorator uses `REQUIRED` propagation by default, which means it will join existing transactions.
13
+ */
14
+ export declare function Transactional<T extends object>(options?: TransactionalOptions<T>): MethodDecorator;
15
+ export {};
@@ -0,0 +1,29 @@
1
+ import { TransactionPropagation, RequestContext, TransactionContext } from '@mikro-orm/core';
2
+ import { resolveContextProvider } from '../resolveContextProvider.js';
3
+ /**
4
+ * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
5
+ * The difference is that you can specify the context in which the transaction begins by providing `context` option,
6
+ * and if omitted, the transaction will begin in the current context implicitly.
7
+ * It works on async functions and can be nested with `em.transactional()`.
8
+ * Unlike `em.transactional()`, this decorator uses `REQUIRED` propagation by default, which means it will join existing transactions.
9
+ */
10
+ export function Transactional(options = {}) {
11
+ return function (target, propertyKey, descriptor) {
12
+ const originalMethod = descriptor.value;
13
+ if (originalMethod.constructor.name !== 'AsyncFunction') {
14
+ throw new Error('@Transactional() should be use with async functions');
15
+ }
16
+ descriptor.value = async function (...args) {
17
+ const { context, contextName, ...txOptions } = options;
18
+ txOptions.propagation ??= TransactionPropagation.REQUIRED;
19
+ const em = (await resolveContextProvider(this, context))
20
+ || TransactionContext.getEntityManager(contextName)
21
+ || RequestContext.getEntityManager(contextName);
22
+ if (!em) {
23
+ throw new Error(`@Transactional() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@Transactional(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
24
+ }
25
+ return em.transactional(() => originalMethod.apply(this, args), txOptions);
26
+ };
27
+ return descriptor;
28
+ };
29
+ }