@mikro-orm/decorators 7.0.0-dev.136 → 7.0.0-dev.138

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/es/Embedded.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
- import { validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext } from '../utils.js';
3
3
  export function Embedded(type = {}, options = {}) {
4
4
  return function (value, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.EMBEDDED);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.EMBEDDED);
8
6
  options = type instanceof Function ? { entity: type, ...options } : { ...type, ...options };
9
7
  Utils.defaultValue(options, 'prefix', true);
10
8
  meta.properties[context.name] = {
package/es/Enum.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { prepareMetadataContext } from '../utils.js';
2
3
  export function Enum(options = {}) {
3
4
  return function (target, context) {
4
- const meta = context.metadata;
5
- meta.properties ??= {};
5
+ const meta = prepareMetadataContext(context);
6
6
  options = options instanceof Function ? { items: options } : options;
7
7
  meta.properties[context.name] = {
8
8
  name: context.name,
package/es/Formula.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ReferenceKind, } from '@mikro-orm/core';
2
+ import { prepareMetadataContext } from '../utils.js';
2
3
  export function Formula(formula, options = {}) {
3
4
  return function (value, context) {
4
- const meta = context.metadata;
5
- meta.properties ??= {};
5
+ const meta = prepareMetadataContext(context);
6
6
  meta.properties[context.name] = {
7
7
  name: context.name,
8
8
  kind: ReferenceKind.SCALAR,
package/es/ManyToMany.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
- import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext, processDecoratorParameters } from '../utils.js';
3
3
  export function ManyToMany(entity, mappedBy, options = {}) {
4
4
  return function (_, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_MANY);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.MANY_TO_MANY);
8
6
  options = processDecoratorParameters({ entity, mappedBy, options });
9
7
  const property = { name: context.name, kind: ReferenceKind.MANY_TO_MANY };
10
8
  meta.properties[context.name] ??= {};
package/es/ManyToOne.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
- import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext, processDecoratorParameters } from '../utils.js';
3
3
  export function ManyToOne(entity = {}, options = {}) {
4
4
  return function (_, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.MANY_TO_ONE);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.MANY_TO_ONE);
8
6
  options = processDecoratorParameters({ entity, options });
9
7
  const property = { name: context.name, kind: ReferenceKind.MANY_TO_ONE };
10
8
  meta.properties[context.name] = Utils.mergeConfig(meta.properties[context.name] ?? {}, property, options);
package/es/OneToMany.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
- import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext, processDecoratorParameters } from '../utils.js';
3
3
  export function OneToMany(entity, mappedBy, options = {}) {
4
4
  return function (value, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_MANY);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.ONE_TO_MANY);
8
6
  options = processDecoratorParameters({ entity, mappedBy, options });
9
7
  const property = { name: context.name, kind: ReferenceKind.ONE_TO_MANY };
10
8
  meta.properties[context.name] ??= {};
package/es/OneToOne.js CHANGED
@@ -1,12 +1,10 @@
1
1
  import { ReferenceKind, } from '@mikro-orm/core';
2
- import { processDecoratorParameters, validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext, processDecoratorParameters } from '../utils.js';
3
3
  export function OneToOne(entity, mappedByOrOptions, options = {}) {
4
4
  const mappedBy = typeof mappedByOrOptions === 'object' ? mappedByOrOptions.mappedBy : mappedByOrOptions;
5
5
  options = typeof mappedByOrOptions === 'object' ? { ...mappedByOrOptions, ...options } : options;
6
6
  return function (_, context) {
7
- const meta = context.metadata;
8
- meta.properties ??= {};
9
- validateSingleDecorator(meta, context.name, ReferenceKind.ONE_TO_ONE);
7
+ const meta = prepareMetadataContext(context, ReferenceKind.ONE_TO_ONE);
10
8
  options = processDecoratorParameters({ entity, mappedBy, options });
11
9
  const property = { name: context.name, kind: ReferenceKind.ONE_TO_ONE };
12
10
  meta.properties[context.name] = Object.assign(meta.properties[context.name] ?? {}, property, options);
package/es/PrimaryKey.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, } from '@mikro-orm/core';
2
- import { validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext } from '../utils.js';
3
3
  function createDecorator(options, serialized) {
4
4
  return function (value, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.SCALAR);
8
6
  const key = serialized ? 'serializedPrimaryKey' : 'primary';
9
7
  options[key] = true;
10
8
  meta.properties[context.name] = { name: context.name, kind: ReferenceKind.SCALAR, ...options };
package/es/Property.js CHANGED
@@ -1,10 +1,8 @@
1
1
  import { ReferenceKind, Utils, } from '@mikro-orm/core';
2
- import { validateSingleDecorator } from '../utils.js';
2
+ import { prepareMetadataContext } from '../utils.js';
3
3
  export function Property(options = {}) {
4
4
  return function (value, context) {
5
- const meta = context.metadata;
6
- meta.properties ??= {};
7
- validateSingleDecorator(meta, context.name, ReferenceKind.SCALAR);
5
+ const meta = prepareMetadataContext(context, ReferenceKind.SCALAR);
8
6
  const { check, ...opts } = options;
9
7
  const prop = { kind: ReferenceKind.SCALAR, ...opts };
10
8
  const name = options.name ?? context.name;
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.136",
4
+ "version": "7.0.0-dev.138",
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.4"
56
56
  },
57
57
  "peerDependencies": {
58
- "@mikro-orm/core": "7.0.0-dev.136",
58
+ "@mikro-orm/core": "7.0.0-dev.138",
59
59
  "reflect-metadata": "^0.1.0 || ^0.2.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
package/utils.d.ts CHANGED
@@ -25,9 +25,15 @@ export declare function processDecoratorParameters<T>(params: Dictionary): T;
25
25
  /**
26
26
  * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
27
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.
28
+ * We allow the existence of the property in metadata if the reference kind is the same, this should allow things like HMR to work.
29
29
  */
30
- export declare function validateSingleDecorator(meta: EntityMetadata, propertyName: string, reference: ReferenceKind): void;
30
+ export declare function validateSingleDecorator(meta: EntityMetadata, propertyName: string, kind: ReferenceKind): void;
31
+ /**
32
+ * Prepares and returns a metadata context for an entity, ensuring default structure and validating proper usage of a single decorator.
33
+ * We need to use the `Object.hasOwn` here, since the metadata object respects inheritance, and the `properties` object might already
34
+ * exist for some base entity.
35
+ */
36
+ export declare function prepareMetadataContext<T>(context: ClassFieldDecoratorContext<T> | ClassGetterDecoratorContext<T> | ClassMethodDecoratorContext<T>, kind?: ReferenceKind): EntityMetadata<T>;
31
37
  /**
32
38
  * Uses some dark magic to get source path to caller where decorator is used.
33
39
  * Analyzes stack trace of error created inside the function call.
package/utils.js CHANGED
@@ -53,13 +53,28 @@ export function processDecoratorParameters(params) {
53
53
  /**
54
54
  * Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
55
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.
56
+ * We allow the existence of the property in metadata if the reference kind is the same, this should allow things like HMR to work.
57
57
  */
58
- export function validateSingleDecorator(meta, propertyName, reference) {
59
- if (meta.properties[propertyName] && meta.properties[propertyName].kind !== reference) {
58
+ export function validateSingleDecorator(meta, propertyName, kind) {
59
+ if (meta.properties[propertyName] && meta.properties[propertyName].kind !== kind) {
60
60
  throw MetadataError.multipleDecorators(meta.className, propertyName);
61
61
  }
62
62
  }
63
+ /**
64
+ * Prepares and returns a metadata context for an entity, ensuring default structure and validating proper usage of a single decorator.
65
+ * We need to use the `Object.hasOwn` here, since the metadata object respects inheritance, and the `properties` object might already
66
+ * exist for some base entity.
67
+ */
68
+ export function prepareMetadataContext(context, kind) {
69
+ const meta = context.metadata;
70
+ if (!Object.hasOwn(meta, 'properties')) {
71
+ meta.properties = {};
72
+ }
73
+ if (kind) {
74
+ validateSingleDecorator(meta, context.name, kind);
75
+ }
76
+ return meta;
77
+ }
63
78
  /**
64
79
  * Uses some dark magic to get source path to caller where decorator is used.
65
80
  * Analyzes stack trace of error created inside the function call.