@mikro-orm/core 7.0.0-dev.3 → 7.0.0-dev.300
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/EntityManager.d.ts +114 -63
- package/EntityManager.js +385 -310
- package/MikroORM.d.ts +44 -35
- package/MikroORM.js +109 -143
- package/README.md +3 -2
- package/cache/FileCacheAdapter.d.ts +1 -1
- package/cache/FileCacheAdapter.js +17 -8
- package/cache/GeneratedCacheAdapter.d.ts +0 -1
- package/cache/GeneratedCacheAdapter.js +0 -2
- package/cache/index.d.ts +0 -1
- package/cache/index.js +0 -1
- package/connections/Connection.d.ts +16 -7
- package/connections/Connection.js +23 -14
- package/drivers/DatabaseDriver.d.ts +25 -16
- package/drivers/DatabaseDriver.js +119 -36
- package/drivers/IDatabaseDriver.d.ts +125 -23
- package/entity/BaseEntity.d.ts +63 -4
- package/entity/BaseEntity.js +0 -3
- package/entity/Collection.d.ts +102 -31
- package/entity/Collection.js +446 -108
- package/entity/EntityAssigner.d.ts +1 -1
- package/entity/EntityAssigner.js +26 -18
- package/entity/EntityFactory.d.ts +13 -1
- package/entity/EntityFactory.js +106 -60
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +65 -20
- package/entity/EntityLoader.d.ts +13 -11
- package/entity/EntityLoader.js +257 -107
- package/entity/EntityRepository.d.ts +28 -8
- package/entity/EntityRepository.js +8 -2
- package/entity/PolymorphicRef.d.ts +12 -0
- package/entity/PolymorphicRef.js +18 -0
- package/entity/Reference.d.ts +9 -12
- package/entity/Reference.js +34 -9
- package/entity/WrappedEntity.d.ts +3 -8
- package/entity/WrappedEntity.js +3 -8
- package/entity/defineEntity.d.ts +753 -0
- package/entity/defineEntity.js +537 -0
- package/entity/index.d.ts +4 -2
- package/entity/index.js +4 -2
- package/entity/utils.d.ts +13 -1
- package/entity/utils.js +49 -4
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/enums.d.ts +23 -8
- package/enums.js +15 -1
- package/errors.d.ts +25 -9
- package/errors.js +67 -21
- package/events/EventManager.d.ts +2 -1
- package/events/EventManager.js +19 -11
- package/events/EventSubscriber.d.ts +3 -1
- package/hydration/Hydrator.js +1 -2
- package/hydration/ObjectHydrator.d.ts +4 -4
- package/hydration/ObjectHydrator.js +89 -36
- package/index.d.ts +2 -2
- package/index.js +1 -2
- package/logging/DefaultLogger.d.ts +1 -1
- package/logging/DefaultLogger.js +1 -0
- package/logging/SimpleLogger.d.ts +1 -1
- package/logging/colors.d.ts +1 -1
- package/logging/colors.js +7 -6
- package/logging/index.d.ts +1 -0
- package/logging/index.js +1 -0
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +11 -0
- package/metadata/EntitySchema.d.ts +53 -27
- package/metadata/EntitySchema.js +125 -52
- package/metadata/MetadataDiscovery.d.ts +64 -10
- package/metadata/MetadataDiscovery.js +823 -344
- package/metadata/MetadataProvider.d.ts +11 -2
- package/metadata/MetadataProvider.js +66 -2
- package/metadata/MetadataStorage.d.ts +13 -11
- package/metadata/MetadataStorage.js +71 -38
- package/metadata/MetadataValidator.d.ts +32 -9
- package/metadata/MetadataValidator.js +198 -42
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +1 -1
- package/metadata/index.js +1 -1
- package/metadata/types.d.ts +577 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +16 -4
- package/naming-strategy/AbstractNamingStrategy.js +20 -2
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +3 -3
- package/naming-strategy/EntityCaseNamingStrategy.js +6 -5
- package/naming-strategy/MongoNamingStrategy.d.ts +3 -3
- package/naming-strategy/MongoNamingStrategy.js +6 -6
- package/naming-strategy/NamingStrategy.d.ts +28 -4
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +3 -3
- package/naming-strategy/UnderscoreNamingStrategy.js +6 -6
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +22 -11
- package/platforms/ExceptionConverter.js +1 -1
- package/platforms/Platform.d.ts +14 -16
- package/platforms/Platform.js +24 -44
- package/serialization/EntitySerializer.d.ts +8 -3
- package/serialization/EntitySerializer.js +47 -27
- package/serialization/EntityTransformer.js +33 -21
- package/serialization/SerializationContext.d.ts +6 -6
- package/serialization/SerializationContext.js +16 -13
- package/types/ArrayType.d.ts +1 -1
- package/types/ArrayType.js +2 -3
- package/types/BigIntType.d.ts +9 -6
- package/types/BigIntType.js +4 -1
- package/types/BlobType.d.ts +0 -1
- package/types/BlobType.js +0 -3
- package/types/BooleanType.d.ts +2 -1
- package/types/BooleanType.js +3 -0
- package/types/DecimalType.d.ts +6 -4
- package/types/DecimalType.js +3 -3
- package/types/DoubleType.js +2 -2
- package/types/EnumArrayType.js +1 -2
- package/types/JsonType.d.ts +1 -1
- package/types/JsonType.js +7 -2
- package/types/TinyIntType.js +1 -1
- package/types/Type.d.ts +2 -4
- package/types/Type.js +3 -3
- package/types/Uint8ArrayType.d.ts +0 -1
- package/types/Uint8ArrayType.js +1 -4
- package/types/index.d.ts +1 -1
- package/typings.d.ts +469 -175
- package/typings.js +120 -45
- package/unit-of-work/ChangeSet.d.ts +4 -6
- package/unit-of-work/ChangeSet.js +4 -5
- package/unit-of-work/ChangeSetComputer.d.ts +3 -8
- package/unit-of-work/ChangeSetComputer.js +44 -21
- package/unit-of-work/ChangeSetPersister.d.ts +15 -12
- package/unit-of-work/ChangeSetPersister.js +113 -45
- package/unit-of-work/CommitOrderCalculator.d.ts +12 -10
- package/unit-of-work/CommitOrderCalculator.js +13 -13
- package/unit-of-work/IdentityMap.d.ts +12 -0
- package/unit-of-work/IdentityMap.js +39 -1
- package/unit-of-work/UnitOfWork.d.ts +28 -3
- package/unit-of-work/UnitOfWork.js +315 -110
- package/utils/AbstractMigrator.d.ts +101 -0
- package/utils/AbstractMigrator.js +305 -0
- package/utils/AbstractSchemaGenerator.d.ts +5 -5
- package/utils/AbstractSchemaGenerator.js +32 -18
- package/utils/AsyncContext.d.ts +6 -0
- package/utils/AsyncContext.js +42 -0
- package/utils/Configuration.d.ts +801 -207
- package/utils/Configuration.js +150 -191
- package/utils/ConfigurationLoader.d.ts +1 -54
- package/utils/ConfigurationLoader.js +1 -352
- package/utils/Cursor.d.ts +3 -6
- package/utils/Cursor.js +27 -11
- package/utils/DataloaderUtils.d.ts +15 -5
- package/utils/DataloaderUtils.js +65 -17
- package/utils/EntityComparator.d.ts +21 -10
- package/utils/EntityComparator.js +243 -106
- package/utils/QueryHelper.d.ts +24 -6
- package/utils/QueryHelper.js +122 -26
- package/utils/RawQueryFragment.d.ts +60 -32
- package/utils/RawQueryFragment.js +69 -66
- package/utils/RequestContext.js +2 -2
- package/utils/TransactionContext.js +2 -2
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +15 -122
- package/utils/Utils.js +108 -376
- package/utils/clone.js +8 -23
- package/utils/env-vars.d.ts +7 -0
- package/utils/env-vars.js +97 -0
- package/utils/fs-utils.d.ts +34 -0
- package/utils/fs-utils.js +196 -0
- package/utils/index.d.ts +2 -3
- package/utils/index.js +2 -3
- package/utils/upsert-utils.d.ts +9 -4
- package/utils/upsert-utils.js +55 -4
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -13
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -32
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -11
- package/decorators/Embedded.d.ts +0 -18
- package/decorators/Embedded.js +0 -18
- package/decorators/Entity.d.ts +0 -18
- package/decorators/Entity.js +0 -13
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -16
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -8
- package/decorators/Formula.d.ts +0 -5
- package/decorators/Formula.js +0 -15
- package/decorators/Indexed.d.ts +0 -17
- package/decorators/Indexed.js +0 -20
- package/decorators/ManyToMany.d.ts +0 -40
- package/decorators/ManyToMany.js +0 -14
- package/decorators/ManyToOne.d.ts +0 -30
- package/decorators/ManyToOne.js +0 -14
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -17
- package/decorators/OneToOne.d.ts +0 -24
- package/decorators/OneToOne.js +0 -7
- package/decorators/PrimaryKey.d.ts +0 -9
- package/decorators/PrimaryKey.js +0 -20
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -32
- package/decorators/Transactional.d.ts +0 -13
- package/decorators/Transactional.js +0 -28
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -47
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -17
- package/entity/ArrayCollection.d.ts +0 -116
- package/entity/ArrayCollection.js +0 -395
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -150
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -44
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -28
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
import { Utils } from '../utils/Utils.js';
|
|
2
2
|
import { MetadataError } from '../errors.js';
|
|
3
3
|
import { ReferenceKind } from '../enums.js';
|
|
4
|
+
/**
|
|
5
|
+
* List of property names that could lead to prototype pollution vulnerabilities.
|
|
6
|
+
* These names should never be used as entity property names because they could
|
|
7
|
+
* allow malicious code to modify object prototypes when property values are assigned.
|
|
8
|
+
*
|
|
9
|
+
* - `__proto__`: Could modify the prototype chain
|
|
10
|
+
* - `constructor`: Could modify the constructor property
|
|
11
|
+
* - `prototype`: Could modify the prototype object
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
const DANGEROUS_PROPERTY_NAMES = ['__proto__', 'constructor', 'prototype'];
|
|
4
16
|
/**
|
|
5
17
|
* @internal
|
|
6
18
|
*/
|
|
7
19
|
export class MetadataValidator {
|
|
8
|
-
/**
|
|
9
|
-
* Validate there is only one property decorator. This disallows using `@Property()` together with e.g. `@ManyToOne()`
|
|
10
|
-
* on the same property. One should use only `@ManyToOne()` in such case.
|
|
11
|
-
* We allow the existence of the property in metadata if the reference type is the same, this should allow things like HMR to work.
|
|
12
|
-
*/
|
|
13
|
-
static validateSingleDecorator(meta, propertyName, reference) {
|
|
14
|
-
if (meta.properties[propertyName] && meta.properties[propertyName].kind !== reference) {
|
|
15
|
-
throw MetadataError.multipleDecorators(meta.className, propertyName);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
20
|
validateEntityDefinition(metadata, name, options) {
|
|
19
21
|
const meta = metadata.get(name);
|
|
20
|
-
|
|
22
|
+
// View entities (expression with view flag) behave like regular tables but are read-only
|
|
23
|
+
// They can have primary keys and are created as actual database views
|
|
24
|
+
if (meta.view) {
|
|
25
|
+
this.validateViewEntity(meta);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
// Virtual entities (expression without view flag) have restrictions - no PKs, limited relation types
|
|
29
|
+
// Note: meta.virtual is set later in sync(), so we check for expression && !view here
|
|
30
|
+
if (meta.virtual || (meta.expression && !meta.view)) {
|
|
21
31
|
for (const prop of Utils.values(meta.properties)) {
|
|
22
32
|
if (![ReferenceKind.SCALAR, ReferenceKind.EMBEDDED, ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
23
33
|
throw new MetadataError(`Only scalars, embedded properties and to-many relations are allowed inside virtual entity. Found '${prop.kind}' in ${meta.className}.${prop.name}`);
|
|
@@ -36,13 +46,14 @@ export class MetadataValidator {
|
|
|
36
46
|
this.validateDuplicateFieldNames(meta, options);
|
|
37
47
|
this.validateIndexes(meta, meta.indexes ?? [], 'index');
|
|
38
48
|
this.validateIndexes(meta, meta.uniques ?? [], 'unique');
|
|
49
|
+
this.validatePropertyNames(meta);
|
|
39
50
|
for (const prop of Utils.values(meta.properties)) {
|
|
40
51
|
if (prop.kind !== ReferenceKind.SCALAR) {
|
|
41
|
-
this.validateReference(meta, prop,
|
|
42
|
-
this.validateBidirectional(meta, prop
|
|
52
|
+
this.validateReference(meta, prop, options);
|
|
53
|
+
this.validateBidirectional(meta, prop);
|
|
43
54
|
}
|
|
44
|
-
else if (metadata.
|
|
45
|
-
throw MetadataError.propertyTargetsEntityType(meta, prop, metadata.
|
|
55
|
+
else if (metadata.getByClassName(prop.type, false)) {
|
|
56
|
+
throw MetadataError.propertyTargetsEntityType(meta, prop, metadata.getByClassName(prop.type));
|
|
46
57
|
}
|
|
47
58
|
}
|
|
48
59
|
}
|
|
@@ -50,17 +61,15 @@ export class MetadataValidator {
|
|
|
50
61
|
if (discovered.length === 0 && options.warnWhenNoEntities) {
|
|
51
62
|
throw MetadataError.noEntityDiscovered();
|
|
52
63
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
const tableNames = discovered.filter(meta => !meta.abstract && meta === meta.root && (meta.tableName || meta.collection) && meta.schema !== '*');
|
|
64
|
+
// Validate no mixing of STI and TPT in the same hierarchy
|
|
65
|
+
this.validateInheritanceStrategies(discovered);
|
|
66
|
+
const tableNames = discovered.filter(meta => !meta.abstract && !meta.embeddable && meta === meta.root && (meta.tableName || meta.collection) && meta.schema !== '*');
|
|
58
67
|
const duplicateTableNames = Utils.findDuplicates(tableNames.map(meta => {
|
|
59
68
|
const tableName = meta.tableName || meta.collection;
|
|
60
69
|
return (meta.schema ? '.' + meta.schema : '') + tableName;
|
|
61
70
|
}));
|
|
62
|
-
if (duplicateTableNames.length > 0 && options.checkDuplicateTableNames
|
|
63
|
-
throw MetadataError.duplicateEntityDiscovered(duplicateTableNames
|
|
71
|
+
if (duplicateTableNames.length > 0 && options.checkDuplicateTableNames) {
|
|
72
|
+
throw MetadataError.duplicateEntityDiscovered(duplicateTableNames);
|
|
64
73
|
}
|
|
65
74
|
// validate we found at least one entity (not just abstract/base entities)
|
|
66
75
|
if (discovered.filter(meta => meta.name).length === 0 && options.warnWhenNoEntities) {
|
|
@@ -71,7 +80,7 @@ export class MetadataValidator {
|
|
|
71
80
|
.replace(/\[]$/, '') // remove array suffix
|
|
72
81
|
.replace(/\((.*)\)/, '$1'); // unwrap union types
|
|
73
82
|
const name = (p) => {
|
|
74
|
-
if (typeof p === 'function') {
|
|
83
|
+
if (typeof p === 'function' && !p.prototype) {
|
|
75
84
|
return Utils.className(p());
|
|
76
85
|
}
|
|
77
86
|
return Utils.className(p);
|
|
@@ -95,46 +104,134 @@ export class MetadataValidator {
|
|
|
95
104
|
}
|
|
96
105
|
});
|
|
97
106
|
}
|
|
98
|
-
validateReference(meta, prop,
|
|
107
|
+
validateReference(meta, prop, options) {
|
|
99
108
|
// references do have types
|
|
100
109
|
if (!prop.type) {
|
|
101
110
|
throw MetadataError.fromWrongTypeDefinition(meta, prop);
|
|
102
111
|
}
|
|
112
|
+
// Polymorphic relations have multiple targets, validate PK compatibility
|
|
113
|
+
if (prop.polymorphic && prop.polymorphTargets) {
|
|
114
|
+
this.validatePolymorphicTargets(meta, prop);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const targetMeta = prop.targetMeta;
|
|
103
118
|
// references do have type of known entity
|
|
104
|
-
if (!
|
|
119
|
+
if (!targetMeta) {
|
|
105
120
|
throw MetadataError.fromWrongTypeDefinition(meta, prop);
|
|
106
121
|
}
|
|
107
|
-
if (
|
|
122
|
+
if (targetMeta.abstract && !targetMeta.root?.inheritanceType && !targetMeta.embeddable) {
|
|
108
123
|
throw MetadataError.targetIsAbstract(meta, prop);
|
|
109
124
|
}
|
|
110
|
-
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && prop.persist === false &&
|
|
125
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && prop.persist === false && targetMeta.compositePK && options.checkNonPersistentCompositeProps) {
|
|
111
126
|
throw MetadataError.nonPersistentCompositeProp(meta, prop);
|
|
112
127
|
}
|
|
128
|
+
this.validateTargetKey(meta, prop, targetMeta);
|
|
129
|
+
}
|
|
130
|
+
validateTargetKey(meta, prop, targetMeta) {
|
|
131
|
+
if (!prop.targetKey) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// targetKey is not supported for ManyToMany relations
|
|
135
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY) {
|
|
136
|
+
throw MetadataError.targetKeyOnManyToMany(meta, prop);
|
|
137
|
+
}
|
|
138
|
+
// targetKey must point to an existing property
|
|
139
|
+
const targetProp = targetMeta.properties[prop.targetKey];
|
|
140
|
+
if (!targetProp) {
|
|
141
|
+
throw MetadataError.targetKeyNotFound(meta, prop);
|
|
142
|
+
}
|
|
143
|
+
// targetKey must point to a unique property (composite unique is not sufficient)
|
|
144
|
+
if (!this.isPropertyUnique(targetProp, targetMeta)) {
|
|
145
|
+
throw MetadataError.targetKeyNotUnique(meta, prop);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Checks if a property has a unique constraint (either via `unique: true` or single-property `@Unique` decorator).
|
|
150
|
+
* Composite unique constraints are not sufficient for targetKey.
|
|
151
|
+
*/
|
|
152
|
+
isPropertyUnique(prop, meta) {
|
|
153
|
+
if (prop.unique) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
// Check for single-property unique constraint via @Unique decorator
|
|
157
|
+
return !!meta.uniques?.some(u => {
|
|
158
|
+
const props = Utils.asArray(u.properties);
|
|
159
|
+
return props.length === 1 && props[0] === prop.name && !u.options;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
validatePolymorphicTargets(meta, prop) {
|
|
163
|
+
const targets = prop.polymorphTargets;
|
|
164
|
+
// Validate targetKey exists and is compatible across all targets
|
|
165
|
+
if (prop.targetKey) {
|
|
166
|
+
for (const target of targets) {
|
|
167
|
+
const targetProp = target.properties[prop.targetKey];
|
|
168
|
+
if (!targetProp) {
|
|
169
|
+
throw MetadataError.targetKeyNotFound(meta, prop, target);
|
|
170
|
+
}
|
|
171
|
+
// targetKey must point to a unique property (composite unique is not sufficient)
|
|
172
|
+
if (!this.isPropertyUnique(targetProp, target)) {
|
|
173
|
+
throw MetadataError.targetKeyNotUnique(meta, prop, target);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const firstPKs = targets[0].getPrimaryProps();
|
|
178
|
+
for (let i = 1; i < targets.length; i++) {
|
|
179
|
+
const target = targets[i];
|
|
180
|
+
const targetPKs = target.getPrimaryProps();
|
|
181
|
+
if (targetPKs.length !== firstPKs.length) {
|
|
182
|
+
throw MetadataError.incompatiblePolymorphicTargets(meta, prop, targets[0], target, 'different number of primary keys');
|
|
183
|
+
}
|
|
184
|
+
for (let j = 0; j < firstPKs.length; j++) {
|
|
185
|
+
const firstPK = firstPKs[j];
|
|
186
|
+
const targetPK = targetPKs[j];
|
|
187
|
+
if (firstPK.runtimeType !== targetPK.runtimeType) {
|
|
188
|
+
throw MetadataError.incompatiblePolymorphicTargets(meta, prop, targets[0], target, `incompatible primary key types: ${firstPK.name} (${firstPK.runtimeType}) vs ${targetPK.name} (${targetPK.runtimeType})`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
113
192
|
}
|
|
114
|
-
validateBidirectional(meta, prop
|
|
193
|
+
validateBidirectional(meta, prop) {
|
|
115
194
|
if (prop.inversedBy) {
|
|
116
|
-
|
|
117
|
-
this.validateOwningSide(meta, prop, inverse, metadata);
|
|
195
|
+
this.validateOwningSide(meta, prop);
|
|
118
196
|
}
|
|
119
197
|
else if (prop.mappedBy) {
|
|
120
|
-
|
|
121
|
-
this.validateInverseSide(meta, prop, inverse, metadata);
|
|
198
|
+
this.validateInverseSide(meta, prop);
|
|
122
199
|
}
|
|
123
|
-
else {
|
|
200
|
+
else if (prop.kind === ReferenceKind.ONE_TO_MANY && !prop.mappedBy) {
|
|
124
201
|
// 1:m property has `mappedBy`
|
|
125
|
-
|
|
126
|
-
throw MetadataError.fromMissingOption(meta, prop, 'mappedBy');
|
|
127
|
-
}
|
|
202
|
+
throw MetadataError.fromMissingOption(meta, prop, 'mappedBy');
|
|
128
203
|
}
|
|
129
204
|
}
|
|
130
|
-
validateOwningSide(meta, prop
|
|
205
|
+
validateOwningSide(meta, prop) {
|
|
206
|
+
// For polymorphic relations, inversedBy may point to multiple entity types
|
|
207
|
+
if (prop.polymorphic && prop.polymorphTargets?.length) {
|
|
208
|
+
// For polymorphic relations, validate inversedBy against each target
|
|
209
|
+
// The inverse property should exist on the target entities and reference back to this property
|
|
210
|
+
for (const targetMeta of prop.polymorphTargets) {
|
|
211
|
+
const inverse = targetMeta.properties[prop.inversedBy];
|
|
212
|
+
// The inverse property is optional - some targets may not have it
|
|
213
|
+
if (!inverse) {
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
// Validate the inverse property
|
|
217
|
+
if (inverse.targetMeta?.root.class !== meta.root.class) {
|
|
218
|
+
throw MetadataError.fromWrongReference(meta, prop, 'inversedBy', inverse);
|
|
219
|
+
}
|
|
220
|
+
// inverse side is not defined as owner
|
|
221
|
+
if (inverse.inversedBy || inverse.owner) {
|
|
222
|
+
throw MetadataError.fromWrongOwnership(meta, prop, 'inversedBy');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const inverse = prop.targetMeta.properties[prop.inversedBy];
|
|
131
228
|
// has correct `inversedBy` on owning side
|
|
132
229
|
if (!inverse) {
|
|
133
230
|
throw MetadataError.fromWrongReference(meta, prop, 'inversedBy');
|
|
134
231
|
}
|
|
135
|
-
const
|
|
232
|
+
const targetClass = inverse.targetMeta?.root.class;
|
|
136
233
|
// has correct `inversedBy` reference type
|
|
137
|
-
if (inverse.type !== meta.className &&
|
|
234
|
+
if (inverse.type !== meta.className && targetClass !== meta.root.class) {
|
|
138
235
|
throw MetadataError.fromWrongReference(meta, prop, 'inversedBy', inverse);
|
|
139
236
|
}
|
|
140
237
|
// inverse side is not defined as owner
|
|
@@ -142,13 +239,16 @@ export class MetadataValidator {
|
|
|
142
239
|
throw MetadataError.fromWrongOwnership(meta, prop, 'inversedBy');
|
|
143
240
|
}
|
|
144
241
|
}
|
|
145
|
-
validateInverseSide(meta, prop
|
|
242
|
+
validateInverseSide(meta, prop) {
|
|
243
|
+
const owner = prop.targetMeta.properties[prop.mappedBy];
|
|
146
244
|
// has correct `mappedBy` on inverse side
|
|
147
245
|
if (prop.mappedBy && !owner) {
|
|
148
246
|
throw MetadataError.fromWrongReference(meta, prop, 'mappedBy');
|
|
149
247
|
}
|
|
150
248
|
// has correct `mappedBy` reference type
|
|
151
|
-
if
|
|
249
|
+
// For polymorphic relations, check if this entity is one of the polymorphic targets
|
|
250
|
+
const isValidPolymorphicInverse = owner.polymorphic && owner.polymorphTargets?.some(target => target.class === meta.root.class);
|
|
251
|
+
if (!isValidPolymorphicInverse && owner.type !== meta.className && owner.targetMeta?.root.class !== meta.root.class) {
|
|
152
252
|
throw MetadataError.fromWrongReference(meta, prop, 'mappedBy', owner);
|
|
153
253
|
}
|
|
154
254
|
// owning side is not defined as inverse
|
|
@@ -191,7 +291,7 @@ export class MetadataValidator {
|
|
|
191
291
|
return [prop.embedded ? prop.embedded.join('.') : prop.name, prop.fieldNames[0]];
|
|
192
292
|
});
|
|
193
293
|
});
|
|
194
|
-
throw MetadataError.duplicateFieldName(meta.
|
|
294
|
+
throw MetadataError.duplicateFieldName(meta.class, pairs);
|
|
195
295
|
}
|
|
196
296
|
}
|
|
197
297
|
validateVersionField(meta) {
|
|
@@ -208,4 +308,60 @@ export class MetadataValidator {
|
|
|
208
308
|
throw MetadataError.invalidVersionFieldType(meta);
|
|
209
309
|
}
|
|
210
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Validates that entity properties do not use dangerous names that could lead to
|
|
313
|
+
* prototype pollution vulnerabilities. This validation ensures that property names
|
|
314
|
+
* cannot be exploited to modify object prototypes when values are assigned during
|
|
315
|
+
* entity hydration or persistence operations.
|
|
316
|
+
*
|
|
317
|
+
* @internal
|
|
318
|
+
*/
|
|
319
|
+
validatePropertyNames(meta) {
|
|
320
|
+
for (const prop of Utils.values(meta.properties)) {
|
|
321
|
+
if (DANGEROUS_PROPERTY_NAMES.includes(prop.name)) {
|
|
322
|
+
throw MetadataError.dangerousPropertyName(meta, prop);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Validates view entity configuration.
|
|
328
|
+
* View entities must have an expression.
|
|
329
|
+
*/
|
|
330
|
+
validateViewEntity(meta) {
|
|
331
|
+
// View entities must have an expression
|
|
332
|
+
if (!meta.expression) {
|
|
333
|
+
throw MetadataError.viewEntityWithoutExpression(meta);
|
|
334
|
+
}
|
|
335
|
+
// Validate indexes if present
|
|
336
|
+
this.validateIndexes(meta, meta.indexes ?? [], 'index');
|
|
337
|
+
this.validateIndexes(meta, meta.uniques ?? [], 'unique');
|
|
338
|
+
// Validate property names
|
|
339
|
+
this.validatePropertyNames(meta);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Validates that STI and TPT are not mixed in the same inheritance hierarchy.
|
|
343
|
+
* An entity hierarchy can use either STI (discriminatorColumn) or TPT (inheritance: 'tpt'),
|
|
344
|
+
* but not both.
|
|
345
|
+
*
|
|
346
|
+
* Note: This validation runs before `initTablePerTypeInheritance` sets `inheritanceType`,
|
|
347
|
+
* so we check the raw `inheritance` option from the decorator/schema.
|
|
348
|
+
*/
|
|
349
|
+
validateInheritanceStrategies(discovered) {
|
|
350
|
+
const checkedRoots = new Set();
|
|
351
|
+
for (const meta of discovered) {
|
|
352
|
+
if (meta.embeddable) {
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
const root = meta.root;
|
|
356
|
+
if (checkedRoots.has(root)) {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
checkedRoots.add(root);
|
|
360
|
+
const hasSTI = !!root.discriminatorColumn;
|
|
361
|
+
const hasTPT = root.inheritanceType === 'tpt' || root.inheritance === 'tpt';
|
|
362
|
+
if (hasSTI && hasTPT) {
|
|
363
|
+
throw MetadataError.mixedInheritanceStrategies(root, meta);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
211
367
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { fs } from '../utils/fs-utils.js';
|
|
3
|
+
import { Utils } from '../utils/Utils.js';
|
|
4
|
+
import { MetadataStorage } from './MetadataStorage.js';
|
|
5
|
+
import { EntitySchema } from './EntitySchema.js';
|
|
6
|
+
async function getEntityClassOrSchema(filepath, allTargets, baseDir) {
|
|
7
|
+
const path = fs.normalizePath(baseDir, filepath);
|
|
8
|
+
const exports = await fs.dynamicImport(path);
|
|
9
|
+
const targets = Object.values(exports);
|
|
10
|
+
// ignore class implementations that are linked from an EntitySchema
|
|
11
|
+
for (const item of targets) {
|
|
12
|
+
if (item instanceof EntitySchema) {
|
|
13
|
+
for (const item2 of targets) {
|
|
14
|
+
if (item.meta.class === item2) {
|
|
15
|
+
targets.splice(targets.indexOf(item2), 1);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
for (const item of targets) {
|
|
21
|
+
const validTarget = item instanceof EntitySchema || (item instanceof Function && MetadataStorage.isKnownEntity(item.name));
|
|
22
|
+
if (validTarget && !allTargets.has(item)) {
|
|
23
|
+
allTargets.set(item, path);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function discoverEntities(paths, options) {
|
|
28
|
+
paths = Utils.asArray(paths).map(path => fs.normalizePath(path));
|
|
29
|
+
const baseDir = fs.absolutePath(options?.baseDir ?? process.cwd());
|
|
30
|
+
const files = fs.glob(paths, fs.normalizePath(baseDir));
|
|
31
|
+
const found = new Map();
|
|
32
|
+
for (const filepath of files) {
|
|
33
|
+
const filename = basename(filepath);
|
|
34
|
+
if (!filename.match(/\.[cm]?[jt]s$/) || filename.match(/\.d\.[cm]?ts/)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
await getEntityClassOrSchema(filepath, found, baseDir);
|
|
38
|
+
}
|
|
39
|
+
return found.keys();
|
|
40
|
+
}
|
package/metadata/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
export * from './types.js';
|
|
1
2
|
export * from './EntitySchema.js';
|
|
2
3
|
export * from './MetadataDiscovery.js';
|
|
3
4
|
export * from './MetadataStorage.js';
|
|
4
5
|
export * from './MetadataProvider.js';
|
|
5
6
|
export * from './MetadataValidator.js';
|
|
6
|
-
export * from './ReflectMetadataProvider.js';
|
package/metadata/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
export * from './types.js';
|
|
1
2
|
export * from './EntitySchema.js';
|
|
2
3
|
export * from './MetadataDiscovery.js';
|
|
3
4
|
export * from './MetadataStorage.js';
|
|
4
5
|
export * from './MetadataProvider.js';
|
|
5
6
|
export * from './MetadataValidator.js';
|
|
6
|
-
export * from './ReflectMetadataProvider.js';
|