@mikro-orm/core 7.1.0-dev.4 → 7.1.0-dev.41
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 +63 -12
- package/EntityManager.js +221 -40
- package/README.md +2 -1
- package/connections/Connection.d.ts +29 -0
- package/drivers/IDatabaseDriver.d.ts +45 -7
- package/entity/BaseEntity.d.ts +68 -1
- package/entity/BaseEntity.js +18 -0
- package/entity/Collection.d.ts +6 -3
- package/entity/Collection.js +15 -4
- package/entity/EntityAssigner.js +8 -0
- package/entity/EntityFactory.js +20 -1
- package/entity/EntityLoader.d.ts +8 -1
- package/entity/EntityLoader.js +89 -28
- package/entity/EntityRepository.d.ts +27 -9
- package/entity/EntityRepository.js +12 -0
- package/entity/Reference.d.ts +42 -1
- package/entity/Reference.js +9 -0
- package/entity/defineEntity.d.ts +99 -21
- package/entity/defineEntity.js +17 -6
- package/entity/utils.js +4 -5
- package/enums.d.ts +8 -1
- package/errors.d.ts +2 -0
- package/errors.js +4 -0
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/metadata/EntitySchema.js +3 -0
- package/metadata/MetadataDiscovery.d.ts +12 -0
- package/metadata/MetadataDiscovery.js +166 -20
- package/metadata/MetadataValidator.d.ts +24 -0
- package/metadata/MetadataValidator.js +202 -1
- package/metadata/types.d.ts +71 -4
- package/naming-strategy/AbstractNamingStrategy.d.ts +1 -1
- package/naming-strategy/NamingStrategy.d.ts +1 -1
- package/package.json +1 -1
- package/platforms/Platform.d.ts +18 -3
- package/platforms/Platform.js +58 -6
- package/serialization/EntitySerializer.js +2 -1
- package/typings.d.ts +202 -22
- package/typings.js +51 -14
- package/unit-of-work/UnitOfWork.js +15 -4
- package/utils/AbstractMigrator.d.ts +20 -5
- package/utils/AbstractMigrator.js +263 -28
- package/utils/AbstractSchemaGenerator.d.ts +1 -1
- package/utils/AbstractSchemaGenerator.js +4 -1
- package/utils/Configuration.d.ts +25 -0
- package/utils/Configuration.js +1 -0
- package/utils/DataloaderUtils.d.ts +10 -1
- package/utils/DataloaderUtils.js +78 -0
- package/utils/EntityComparator.js +1 -1
- package/utils/QueryHelper.d.ts +16 -0
- package/utils/QueryHelper.js +15 -0
- package/utils/TransactionManager.js +2 -0
- package/utils/Utils.js +1 -1
- package/utils/fs-utils.d.ts +2 -0
- package/utils/fs-utils.js +7 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
- package/utils/partition-utils.d.ts +17 -0
- package/utils/partition-utils.js +79 -0
- package/utils/upsert-utils.d.ts +2 -0
- package/utils/upsert-utils.js +26 -1
|
@@ -114,7 +114,12 @@ export class MetadataDiscovery {
|
|
|
114
114
|
}
|
|
115
115
|
else {
|
|
116
116
|
const name = prop.name;
|
|
117
|
-
|
|
117
|
+
// For to-one relations, only swap to the accessor for the documented
|
|
118
|
+
// backing-field convention (a `_`-prefixed property like `_draft` paired
|
|
119
|
+
// with `accessor: 'draft'`). The non-prefixed sibling-helper pattern
|
|
120
|
+
// (`author` + `accessor: 'authorId'`) keeps the property name as the
|
|
121
|
+
// canonical FK column source, so this stays non-breaking.
|
|
122
|
+
if (prop.kind === ReferenceKind.SCALAR || prop.kind === ReferenceKind.EMBEDDED || name.startsWith('_')) {
|
|
118
123
|
prop.name = prop.accessor;
|
|
119
124
|
}
|
|
120
125
|
this.initRelation(prop);
|
|
@@ -159,6 +164,7 @@ export class MetadataDiscovery {
|
|
|
159
164
|
forEachProp((m, p) => this.initGeneratedColumn(m, p));
|
|
160
165
|
filtered.forEach(meta => this.initAutoincrement(meta)); // once again after we init custom types
|
|
161
166
|
filtered.forEach(meta => this.initCheckConstraints(meta));
|
|
167
|
+
filtered.forEach(meta => this.initTriggers(meta));
|
|
162
168
|
forEachProp((_m, p) => {
|
|
163
169
|
this.initDefaultValue(p);
|
|
164
170
|
this.inferTypeFromDefault(p);
|
|
@@ -479,17 +485,28 @@ export class MetadataDiscovery {
|
|
|
479
485
|
prop.polymorphic = prop2.polymorphic;
|
|
480
486
|
prop.discriminator = prop2.discriminator;
|
|
481
487
|
prop.discriminatorColumn = prop2.discriminatorColumn;
|
|
482
|
-
|
|
488
|
+
// For a union-target pivot each inverse side sits on one specific target class, so its
|
|
489
|
+
// discriminator value is that class's tableName. For Rails-style, prop2 has a single fixed value.
|
|
490
|
+
prop.discriminatorValue = QueryHelper.isUnionTargetPolymorphic(prop2) ? meta.tableName : prop2.discriminatorValue;
|
|
483
491
|
}
|
|
484
492
|
prop.referencedColumnNames ??= Utils.flatten(meta.primaryKeys.map(primaryKey => meta.properties[primaryKey].fieldNames));
|
|
485
|
-
//
|
|
486
|
-
|
|
493
|
+
// Union-target polymorphic M:N: owner side is fixed (real FK), target side uses discriminator-derived names.
|
|
494
|
+
const isUnionTargetMN = QueryHelper.isUnionTargetPolymorphic(prop);
|
|
495
|
+
if (prop.polymorphic && prop.discriminator && !isUnionTargetMN) {
|
|
496
|
+
// Rails-style: owner side is polymorphic, uses discriminator base name (e.g. taggable_id instead of post_id)
|
|
487
497
|
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(prop.discriminator, referencedColumnName, prop.referencedColumnNames.length > 1));
|
|
488
498
|
}
|
|
489
499
|
else {
|
|
490
500
|
prop.joinColumns ??= prop.referencedColumnNames.map(referencedColumnName => this.#namingStrategy.joinKeyColumnName(meta.root.className, referencedColumnName, meta.compositePK));
|
|
491
501
|
}
|
|
492
|
-
|
|
502
|
+
if (isUnionTargetMN) {
|
|
503
|
+
// Target side uses discriminator base name (e.g. attachable_id — shared across Image/Video)
|
|
504
|
+
const targetPkCols = Utils.flatten(meta2.primaryKeys.map(pk => meta2.properties[pk].fieldNames));
|
|
505
|
+
prop.inverseJoinColumns ??= targetPkCols.map(fieldName => this.#namingStrategy.joinKeyColumnName(prop.discriminator, fieldName, targetPkCols.length > 1));
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
prop.inverseJoinColumns ??= this.initManyToOneFieldName(prop, meta2.root.className);
|
|
509
|
+
}
|
|
493
510
|
}
|
|
494
511
|
isExplicitTableName(meta) {
|
|
495
512
|
return meta.tableName !== this.#namingStrategy.classToTableName(meta.className);
|
|
@@ -556,6 +573,12 @@ export class MetadataDiscovery {
|
|
|
556
573
|
}
|
|
557
574
|
}
|
|
558
575
|
meta.forceConstructor ??= this.shouldForceConstructorUsage(meta);
|
|
576
|
+
// Fail fast when the platform rejects the metadata, so users see the platform-specific
|
|
577
|
+
// error (e.g. "SqlitePlatform does not support partitioned tables") instead of a
|
|
578
|
+
// downstream core validator message that assumes partitioning is supported.
|
|
579
|
+
if (meta.partitionBy && !this.#platform.supportsPartitionedTables()) {
|
|
580
|
+
this.#platform.validateMetadata(meta);
|
|
581
|
+
}
|
|
559
582
|
this.#validator.validateEntityDefinition(this.#metadata, meta.class, this.#config.get('discovery'));
|
|
560
583
|
for (const prop of Object.values(meta.properties)) {
|
|
561
584
|
this.initNullability(prop);
|
|
@@ -582,6 +605,28 @@ export class MetadataDiscovery {
|
|
|
582
605
|
prop.pivotEntity = pivotMeta.class;
|
|
583
606
|
if (prop.inversedBy) {
|
|
584
607
|
prop.targetMeta.properties[prop.inversedBy].pivotEntity = pivotMeta.class;
|
|
608
|
+
const targetRoot = prop.targetMeta.root;
|
|
609
|
+
if (targetRoot !== prop.targetMeta && targetRoot.properties[prop.inversedBy]) {
|
|
610
|
+
targetRoot.properties[prop.inversedBy].pivotEntity = pivotMeta.class;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
// Propagate pivotEntity to ALL inverse collections using mappedBy pointing at this
|
|
614
|
+
// owner prop. Covers three cases:
|
|
615
|
+
// - regular inverse (Tag.posts mappedBy Post.tags) — handled by inversedBy above
|
|
616
|
+
// - union-target inverse (Image.posts mappedBy Post.attachments) — on each polymorph target
|
|
617
|
+
// - merged inverse (Tag.owners mappedBy [Post,Video].tags) — union collection on the target
|
|
618
|
+
const inverseCandidates = QueryHelper.isUnionTargetPolymorphic(prop)
|
|
619
|
+
? prop.polymorphTargets
|
|
620
|
+
: [prop.targetMeta];
|
|
621
|
+
for (const targetMeta of inverseCandidates) {
|
|
622
|
+
for (const inverseProp of Object.values(targetMeta.properties)) {
|
|
623
|
+
if (inverseProp.kind === ReferenceKind.MANY_TO_MANY &&
|
|
624
|
+
inverseProp.mappedBy === prop.name &&
|
|
625
|
+
!inverseProp.pivotEntity) {
|
|
626
|
+
inverseProp.pivotEntity = pivotMeta.class;
|
|
627
|
+
inverseProp.pivotTable = pivotMeta.tableName;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
585
630
|
}
|
|
586
631
|
return pivotMeta;
|
|
587
632
|
});
|
|
@@ -721,8 +766,12 @@ export class MetadataDiscovery {
|
|
|
721
766
|
}
|
|
722
767
|
}
|
|
723
768
|
}
|
|
724
|
-
//
|
|
725
|
-
if (prop.
|
|
769
|
+
// Union-target polymorphic M:N: discriminator + target FK share the pivot across multiple target types
|
|
770
|
+
if (prop.discriminatorColumn && QueryHelper.isUnionTargetPolymorphic(prop)) {
|
|
771
|
+
this.defineUnionTargetPolymorphicPivotProperties(pivotMeta2, meta, prop);
|
|
772
|
+
}
|
|
773
|
+
else if (prop.polymorphic && prop.discriminatorColumn) {
|
|
774
|
+
// Rails-style polymorphic M:N: multiple owners share the pivot, single target type
|
|
726
775
|
this.definePolymorphicPivotProperties(pivotMeta2, meta, prop, targetMeta);
|
|
727
776
|
}
|
|
728
777
|
else {
|
|
@@ -809,6 +858,33 @@ export class MetadataDiscovery {
|
|
|
809
858
|
pivotMeta.polymorphicDiscriminatorMap ??= {};
|
|
810
859
|
pivotMeta.polymorphicDiscriminatorMap[prop.discriminatorValue] = meta.class;
|
|
811
860
|
}
|
|
861
|
+
/**
|
|
862
|
+
* Mirror of definePolymorphicPivotProperties for union-target M:N
|
|
863
|
+
* (e.g. Post.attachments -> Image | Video via shared pivot with a target-side discriminator).
|
|
864
|
+
*
|
|
865
|
+
* Pivot shape:
|
|
866
|
+
* (owner_fk..., discriminator_column, target_fk...)
|
|
867
|
+
* - owner side is a normal M:1 to the single owner entity
|
|
868
|
+
* - target side is a discriminator column + per-target-type virtual M:1 relations
|
|
869
|
+
*/
|
|
870
|
+
defineUnionTargetPolymorphicPivotProperties(pivotMeta, meta, prop) {
|
|
871
|
+
const discriminatorColumn = prop.discriminatorColumn;
|
|
872
|
+
const targets = prop.polymorphTargets;
|
|
873
|
+
pivotMeta.properties[meta.name + '_owner'] = this.definePivotProperty(prop, meta.name + '_owner', meta.class, prop.discriminator, true, false);
|
|
874
|
+
const discriminatorProp = this.createPivotScalarProperty(discriminatorColumn, [this.#platform.getVarcharTypeDeclarationSQL(prop)], [discriminatorColumn], { type: 'string', primary: true, nullable: false });
|
|
875
|
+
this.initFieldName(discriminatorProp);
|
|
876
|
+
pivotMeta.properties[discriminatorColumn] = discriminatorProp;
|
|
877
|
+
const firstTargetColumnTypes = this.getPrimaryKeyColumnTypes(targets[0]);
|
|
878
|
+
pivotMeta.properties[prop.discriminator] = this.createPivotScalarProperty(prop.discriminator, firstTargetColumnTypes, [...prop.inverseJoinColumns], { type: targets[0].className, primary: true, nullable: false });
|
|
879
|
+
pivotMeta.polymorphicDiscriminatorMap ??= {};
|
|
880
|
+
for (const targetMeta of targets) {
|
|
881
|
+
const relationName = `${prop.discriminator}_${targetMeta.tableName}`;
|
|
882
|
+
const relation = this.definePolymorphicOwnerRelation(prop, relationName, targetMeta);
|
|
883
|
+
relation.joinColumns = relation.fieldNames = relation.ownColumns = [...prop.inverseJoinColumns];
|
|
884
|
+
pivotMeta.properties[relationName] = relation;
|
|
885
|
+
pivotMeta.polymorphicDiscriminatorMap[targetMeta.tableName] = targetMeta.class;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
812
888
|
/**
|
|
813
889
|
* Create a virtual M:1 relation from pivot to a polymorphic owner entity.
|
|
814
890
|
* This enables single-query join loading for inverse-side polymorphic M:N.
|
|
@@ -958,9 +1034,14 @@ export class MetadataDiscovery {
|
|
|
958
1034
|
meta.propertyOrder.set(prop.name, (order += 0.01));
|
|
959
1035
|
});
|
|
960
1036
|
}
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
meta.
|
|
1037
|
+
// TPT children have their own tables that don't contain the parent's columns,
|
|
1038
|
+
// so propagating parent indexes/uniques/checks/triggers would target missing columns.
|
|
1039
|
+
if (meta.inheritanceType !== 'tpt' || !meta.tptParent) {
|
|
1040
|
+
meta.indexes = Utils.unique([...base.indexes, ...meta.indexes]);
|
|
1041
|
+
meta.uniques = Utils.unique([...base.uniques, ...meta.uniques]);
|
|
1042
|
+
meta.checks = Utils.unique([...base.checks, ...meta.checks]);
|
|
1043
|
+
meta.triggers = Utils.unique([...base.triggers, ...meta.triggers]);
|
|
1044
|
+
}
|
|
964
1045
|
const pks = Object.values(meta.properties)
|
|
965
1046
|
.filter(p => p.primary)
|
|
966
1047
|
.map(p => p.name);
|
|
@@ -1045,11 +1126,15 @@ export class MetadataDiscovery {
|
|
|
1045
1126
|
prop.discriminatorColumn ??= this.#namingStrategy.discriminatorColumnName(prop.discriminator);
|
|
1046
1127
|
prop.createForeignKeyConstraint = false;
|
|
1047
1128
|
const isToOne = [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind);
|
|
1048
|
-
|
|
1129
|
+
const isUnionTargetMN = prop.kind === ReferenceKind.MANY_TO_MANY && Array.isArray(prop.target);
|
|
1130
|
+
if (isToOne || isUnionTargetMN) {
|
|
1049
1131
|
const types = prop.type.split(/ ?\| ?/);
|
|
1050
1132
|
prop.polymorphTargets = discovered.filter(m => types.includes(m.className) && !m.embeddable);
|
|
1051
1133
|
prop.targetMeta = prop.polymorphTargets[0];
|
|
1052
1134
|
prop.referencedPKs = prop.targetMeta?.primaryKeys;
|
|
1135
|
+
if (isUnionTargetMN && prop.polymorphTargets.length < 2) {
|
|
1136
|
+
throw new MetadataError(`${meta.className}.${prop.name} union-target polymorphic M:N requires at least two target entity types; use a regular M:N relation for a single target.`);
|
|
1137
|
+
}
|
|
1053
1138
|
}
|
|
1054
1139
|
if (prop.discriminatorMap) {
|
|
1055
1140
|
const normalizedMap = {};
|
|
@@ -1065,7 +1150,7 @@ export class MetadataDiscovery {
|
|
|
1065
1150
|
}
|
|
1066
1151
|
prop.discriminatorMap = normalizedMap;
|
|
1067
1152
|
}
|
|
1068
|
-
else if (isToOne) {
|
|
1153
|
+
else if (isToOne || isUnionTargetMN) {
|
|
1069
1154
|
prop.discriminatorMap = {};
|
|
1070
1155
|
const tableNameToTarget = new Map();
|
|
1071
1156
|
for (const target of prop.polymorphTargets) {
|
|
@@ -1187,6 +1272,19 @@ export class MetadataDiscovery {
|
|
|
1187
1272
|
});
|
|
1188
1273
|
}
|
|
1189
1274
|
}
|
|
1275
|
+
sameRelationTargetRoot(rootProp, prop) {
|
|
1276
|
+
if (!rootProp || rootProp.kind !== prop.kind) {
|
|
1277
|
+
return false;
|
|
1278
|
+
}
|
|
1279
|
+
if (prop.kind !== ReferenceKind.MANY_TO_ONE &&
|
|
1280
|
+
prop.kind !== ReferenceKind.ONE_TO_ONE &&
|
|
1281
|
+
prop.kind !== ReferenceKind.ONE_TO_MANY &&
|
|
1282
|
+
prop.kind !== ReferenceKind.MANY_TO_MANY) {
|
|
1283
|
+
return false;
|
|
1284
|
+
}
|
|
1285
|
+
const aRoot = this.#metadata.getByClassName(prop.type, false)?.root;
|
|
1286
|
+
return aRoot != null && aRoot === this.#metadata.getByClassName(rootProp.type, false)?.root;
|
|
1287
|
+
}
|
|
1190
1288
|
initSingleTableInheritance(meta, metadata) {
|
|
1191
1289
|
if (meta.root !== meta && !meta.__processed) {
|
|
1192
1290
|
meta.root = metadata.find(m => m.class === meta.root.class);
|
|
@@ -1227,12 +1325,24 @@ export class MetadataDiscovery {
|
|
|
1227
1325
|
Object.values(meta.properties).forEach(prop => {
|
|
1228
1326
|
const newProp = { ...prop };
|
|
1229
1327
|
const rootProp = meta.root.properties[prop.name];
|
|
1328
|
+
// A child that narrows a relation to a subclass of the root's declared
|
|
1329
|
+
// target (same STI hierarchy) shares the FK column with the root; treat
|
|
1330
|
+
// that as matching so the rename branch below doesn't run (which would
|
|
1331
|
+
// crash — `targetMeta` is only populated later, in `initRelation`).
|
|
1332
|
+
const narrowedRelationOverride = rootProp != null && rootProp.type !== prop.type && this.sameRelationTargetRoot(rootProp, prop);
|
|
1230
1333
|
// stiMerged is set during inlineProperties when a property was merged
|
|
1231
1334
|
// from multiple polymorphic variants with different types. The flag is
|
|
1232
1335
|
// cleared implicitly when the first child claims the root property via
|
|
1233
1336
|
// addProperty below, so subsequent children correctly trigger renaming.
|
|
1234
|
-
const typesMatch = rootProp?.type === prop.type || rootProp?.stiMerged === true;
|
|
1337
|
+
const typesMatch = rootProp?.type === prop.type || rootProp?.stiMerged === true || narrowedRelationOverride;
|
|
1338
|
+
// Inverse-side relations (1:m, inverse m:n) have no FK column on this
|
|
1339
|
+
// entity, so the rename branch — which exists to disambiguate physical
|
|
1340
|
+
// columns shared across STI children — doesn't apply. Skipping it also
|
|
1341
|
+
// avoids crashing on `[...rootProp.fieldNames]` since fieldNames is
|
|
1342
|
+
// never populated for inverse-side properties.
|
|
1343
|
+
const isInverseSideRelation = prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner);
|
|
1235
1344
|
if (rootProp &&
|
|
1345
|
+
!isInverseSideRelation &&
|
|
1236
1346
|
(!typesMatch ||
|
|
1237
1347
|
(rootProp.fieldNames && prop.fieldNames && !compareArrays(rootProp.fieldNames, prop.fieldNames)))) {
|
|
1238
1348
|
const name = newProp.name;
|
|
@@ -1281,6 +1391,20 @@ export class MetadataDiscovery {
|
|
|
1281
1391
|
}
|
|
1282
1392
|
newProp.nullable = true;
|
|
1283
1393
|
newProp.inherited = !rootProp;
|
|
1394
|
+
// For narrowed relation overrides, keep the root's declaration intact so
|
|
1395
|
+
// the full target union (e.g. `Food`) is preserved for populates from the
|
|
1396
|
+
// abstract root — otherwise the last child processed would win.
|
|
1397
|
+
if (narrowedRelationOverride) {
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
// STI siblings can declare the same property name on inverse-side relations
|
|
1401
|
+
// pointing to disjoint targets (e.g. `Dog.items → DogItem` and
|
|
1402
|
+
// `Cat.items → CatItem` with no shared base). There's no physical column
|
|
1403
|
+
// to manage at root, and propagating would clobber the first child's
|
|
1404
|
+
// declaration; each child keeps its local typed property instead.
|
|
1405
|
+
if (isInverseSideRelation && rootProp && rootProp.type !== prop.type) {
|
|
1406
|
+
return;
|
|
1407
|
+
}
|
|
1284
1408
|
meta.root.addProperty(newProp);
|
|
1285
1409
|
});
|
|
1286
1410
|
meta.tableName = meta.root.tableName;
|
|
@@ -1296,10 +1420,10 @@ export class MetadataDiscovery {
|
|
|
1296
1420
|
if (meta.root !== meta) {
|
|
1297
1421
|
meta.root = metadata.find(m => m.class === meta.root.class);
|
|
1298
1422
|
}
|
|
1299
|
-
|
|
1300
|
-
if (inheritance === 'tpt' && meta.root
|
|
1301
|
-
meta.inheritanceType = 'tpt';
|
|
1302
|
-
meta.tptChildren
|
|
1423
|
+
// init the root eagerly so children iterated before the root (e.g. alphabetical glob order) still get linked
|
|
1424
|
+
if (meta.root.inheritance === 'tpt' && meta.root.inheritanceType !== 'tpt') {
|
|
1425
|
+
meta.root.inheritanceType = 'tpt';
|
|
1426
|
+
meta.root.tptChildren ??= [];
|
|
1303
1427
|
}
|
|
1304
1428
|
if (meta.root.inheritanceType !== 'tpt') {
|
|
1305
1429
|
return;
|
|
@@ -1487,7 +1611,7 @@ export class MetadataDiscovery {
|
|
|
1487
1611
|
const table = this.createSchemaTable(meta);
|
|
1488
1612
|
for (const check of meta.checks) {
|
|
1489
1613
|
const fieldNames = check.property ? meta.properties[check.property].fieldNames : [];
|
|
1490
|
-
check.name ??= this.#
|
|
1614
|
+
check.name ??= this.#platform.getIndexName(meta.tableName, fieldNames, 'check');
|
|
1491
1615
|
if (check.expression instanceof Function) {
|
|
1492
1616
|
check.expression = check.expression(columns, table);
|
|
1493
1617
|
}
|
|
@@ -1512,7 +1636,7 @@ export class MetadataDiscovery {
|
|
|
1512
1636
|
}
|
|
1513
1637
|
if (expression) {
|
|
1514
1638
|
meta.checks.push({
|
|
1515
|
-
name: this.#
|
|
1639
|
+
name: this.#platform.getIndexName(meta.tableName, prop.fieldNames, 'check'),
|
|
1516
1640
|
property: prop.name,
|
|
1517
1641
|
expression,
|
|
1518
1642
|
});
|
|
@@ -1520,6 +1644,27 @@ export class MetadataDiscovery {
|
|
|
1520
1644
|
}
|
|
1521
1645
|
}
|
|
1522
1646
|
}
|
|
1647
|
+
initTriggers(meta) {
|
|
1648
|
+
if (meta.triggers.length === 0) {
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
const columns = meta.createSchemaColumnMappingObject();
|
|
1652
|
+
const table = this.createSchemaTable(meta);
|
|
1653
|
+
for (const trigger of meta.triggers) {
|
|
1654
|
+
if (trigger.body && trigger.expression) {
|
|
1655
|
+
throw new MetadataError(`Trigger "${trigger.name ?? '(unnamed)'}" on entity ${meta.className} defines both 'body' and 'expression'. Use one or the other.`);
|
|
1656
|
+
}
|
|
1657
|
+
if (!trigger.body && !trigger.expression) {
|
|
1658
|
+
throw new MetadataError(`Trigger "${trigger.name ?? '(unnamed)'}" on entity ${meta.className} must define either 'body' or 'expression'.`);
|
|
1659
|
+
}
|
|
1660
|
+
trigger.name ??= this.#namingStrategy.indexName(meta.tableName, trigger.events, 'trigger');
|
|
1661
|
+
trigger.forEach ??= 'row';
|
|
1662
|
+
if (trigger.body instanceof Function) {
|
|
1663
|
+
trigger.body = trigger.body(columns, table);
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
meta.hasTriggers = true;
|
|
1667
|
+
}
|
|
1523
1668
|
initGeneratedColumn(meta, prop) {
|
|
1524
1669
|
if (!prop.generated && prop.columnTypes) {
|
|
1525
1670
|
const match = /(.*) generated always as (.*)/i.exec(prop.columnTypes[0]);
|
|
@@ -1667,6 +1812,7 @@ export class MetadataDiscovery {
|
|
|
1667
1812
|
prop.customType &&
|
|
1668
1813
|
!(prop.customType instanceof t.array) &&
|
|
1669
1814
|
!(prop.customType instanceof t.enumArray) &&
|
|
1815
|
+
!(prop.customType instanceof t.json) &&
|
|
1670
1816
|
prop.kind === ReferenceKind.SCALAR) {
|
|
1671
1817
|
const innerType = prop.customType;
|
|
1672
1818
|
innerType.platform = this.#platform;
|
|
@@ -1702,7 +1848,7 @@ export class MetadataDiscovery {
|
|
|
1702
1848
|
if (!prop.customType && prop.array && prop.items) {
|
|
1703
1849
|
prop.customType = new t.enumArray(`${meta.className}.${prop.name}`, prop.items);
|
|
1704
1850
|
}
|
|
1705
|
-
const isArray = prop.type?.toLowerCase() === 'array' || prop.type?.toString().endsWith('[]');
|
|
1851
|
+
const isArray = prop.array || prop.type?.toLowerCase() === 'array' || prop.type?.toString().endsWith('[]');
|
|
1706
1852
|
if (objectEmbeddable && !prop.customType && isArray) {
|
|
1707
1853
|
prop.customType = new t.json();
|
|
1708
1854
|
}
|
|
@@ -7,6 +7,30 @@ import type { MetadataStorage } from './MetadataStorage.js';
|
|
|
7
7
|
export declare class MetadataValidator {
|
|
8
8
|
validateEntityDefinition<T>(metadata: MetadataStorage, name: EntityName<T>, options: MetadataDiscoveryOptions): void;
|
|
9
9
|
validateDiscovered(discovered: EntityMetadata[], options: MetadataDiscoveryOptions): void;
|
|
10
|
+
private validatePartitioning;
|
|
11
|
+
/**
|
|
12
|
+
* Find the first partition name whose normalized form (case-folded for unquoted segments,
|
|
13
|
+
* quoted segments preserved) has already been seen. Returns the offending name in its
|
|
14
|
+
* original form for the error message.
|
|
15
|
+
*/
|
|
16
|
+
private findDuplicatePartitionName;
|
|
17
|
+
/**
|
|
18
|
+
* Partition names may be bare (`child`), schema-qualified (`schema.child`), or use quoted
|
|
19
|
+
* identifiers (`"my.schema"."child"`). Reject anything with more than one unquoted `.`.
|
|
20
|
+
*/
|
|
21
|
+
private hasValidPartitionName;
|
|
22
|
+
private hasPartitionExpression;
|
|
23
|
+
private validatePartitionKeyConstraints;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the list of physical field names that a partition expression references, or
|
|
26
|
+
* `undefined` when the expression is opaque (callback, or raw SQL like `date_trunc('day', x)`
|
|
27
|
+
* that we cannot statically parse). Opaque expressions intentionally bypass the primary-key /
|
|
28
|
+
* unique-constraint coverage checks — users are trusted to ensure the referenced columns are
|
|
29
|
+
* part of the partition key, since PostgreSQL will surface the violation at DDL execution.
|
|
30
|
+
*/
|
|
31
|
+
private getPartitionKeyFields;
|
|
32
|
+
private resolvePartitionKeyField;
|
|
33
|
+
private getConstraintFields;
|
|
10
34
|
private validateReference;
|
|
11
35
|
private validateTargetKey;
|
|
12
36
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Utils } from '../utils/Utils.js';
|
|
2
|
+
import { normalizePartitionNameForComparison, splitCommaSeparatedIdentifiers } from '../utils/partition-utils.js';
|
|
2
3
|
import { MetadataError } from '../errors.js';
|
|
3
4
|
import { ReferenceKind } from '../enums.js';
|
|
4
5
|
/**
|
|
@@ -28,6 +29,9 @@ export class MetadataValidator {
|
|
|
28
29
|
// Virtual entities (expression without view flag) have restrictions - no PKs, limited relation types
|
|
29
30
|
// Note: meta.virtual is set later in sync(), so we check for expression && !view here
|
|
30
31
|
if (meta.virtual || (meta.expression && !meta.view)) {
|
|
32
|
+
if (meta.partitionBy) {
|
|
33
|
+
throw new MetadataError(`Virtual entity ${meta.className} cannot define partitionBy`);
|
|
34
|
+
}
|
|
31
35
|
for (const prop of Utils.values(meta.properties)) {
|
|
32
36
|
if (![ReferenceKind.SCALAR, ReferenceKind.EMBEDDED, ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
33
37
|
throw new MetadataError(`Only scalars, embedded properties and to-many relations are allowed inside virtual entity. Found '${prop.kind}' in ${meta.className}.${prop.name}`);
|
|
@@ -46,6 +50,7 @@ export class MetadataValidator {
|
|
|
46
50
|
this.validateDuplicateFieldNames(meta, options);
|
|
47
51
|
this.validateIndexes(meta, meta.indexes ?? [], 'index');
|
|
48
52
|
this.validateIndexes(meta, meta.uniques ?? [], 'unique');
|
|
53
|
+
this.validatePartitioning(meta);
|
|
49
54
|
this.validatePropertyNames(meta);
|
|
50
55
|
for (const prop of Utils.values(meta.properties)) {
|
|
51
56
|
if (prop.kind !== ReferenceKind.SCALAR) {
|
|
@@ -111,6 +116,189 @@ export class MetadataValidator {
|
|
|
111
116
|
}
|
|
112
117
|
});
|
|
113
118
|
}
|
|
119
|
+
validatePartitioning(meta) {
|
|
120
|
+
if (!meta.partitionBy) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (!this.hasPartitionExpression(meta.partitionBy.expression)) {
|
|
124
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: missing expression`);
|
|
125
|
+
}
|
|
126
|
+
// Inheritance (STI/TPT) and partitioning both drive table layout, so combining them would
|
|
127
|
+
// require non-trivial DDL coordination that the schema generator does not produce today.
|
|
128
|
+
const hasInheritance = !!meta.root.discriminatorColumn || meta.root.inheritanceType === 'tpt' || meta.root.inheritance === 'tpt';
|
|
129
|
+
if (hasInheritance) {
|
|
130
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: combining partitioning with inheritance is not supported`);
|
|
131
|
+
}
|
|
132
|
+
this.validatePartitionKeyConstraints(meta);
|
|
133
|
+
if (meta.partitionBy.type === 'hash') {
|
|
134
|
+
const { partitions } = meta.partitionBy;
|
|
135
|
+
if (Array.isArray(partitions)) {
|
|
136
|
+
if (partitions.length === 0) {
|
|
137
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: hash partition name list must not be empty`);
|
|
138
|
+
}
|
|
139
|
+
const blank = partitions.find(name => typeof name !== 'string' || !name.trim());
|
|
140
|
+
if (blank !== undefined) {
|
|
141
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: hash partition names must be non-empty strings`);
|
|
142
|
+
}
|
|
143
|
+
const ambiguous = partitions.find(name => !this.hasValidPartitionName(name));
|
|
144
|
+
if (ambiguous) {
|
|
145
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: partition name '${ambiguous}' contains more than one '.' — use at most one '.' to separate schema from table`);
|
|
146
|
+
}
|
|
147
|
+
const duplicate = this.findDuplicatePartitionName(partitions);
|
|
148
|
+
if (duplicate !== undefined) {
|
|
149
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: duplicate hash partition name '${duplicate}'`);
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (typeof partitions !== 'number' || !Number.isInteger(partitions) || partitions < 1) {
|
|
154
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: hash partition count must be a positive integer`);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (!Array.isArray(meta.partitionBy.partitions) || meta.partitionBy.partitions.length === 0) {
|
|
159
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: list/range partitions must be a non-empty array`);
|
|
160
|
+
}
|
|
161
|
+
if (meta.partitionBy.partitions.some(partition => !partition.values?.trim())) {
|
|
162
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: every partition must define values`);
|
|
163
|
+
}
|
|
164
|
+
const ambiguousName = meta.partitionBy.partitions.find(partition => partition.name != null && !this.hasValidPartitionName(partition.name));
|
|
165
|
+
if (ambiguousName) {
|
|
166
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: partition name '${ambiguousName.name}' contains more than one '.' — use at most one '.' to separate schema from table`);
|
|
167
|
+
}
|
|
168
|
+
// Include auto-generated default names (`${tableName}_${index}`, matching
|
|
169
|
+
// `createExplicitPartitions` in the sql package) so an explicit name that collides with
|
|
170
|
+
// an unnamed peer's default is caught here rather than at DDL execution time.
|
|
171
|
+
const resolvedNames = meta.partitionBy.partitions.map((partition, index) => partition.name ?? `${meta.tableName}_${index}`);
|
|
172
|
+
const duplicate = this.findDuplicatePartitionName(resolvedNames);
|
|
173
|
+
if (duplicate !== undefined) {
|
|
174
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: duplicate partition name '${duplicate}'`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Find the first partition name whose normalized form (case-folded for unquoted segments,
|
|
179
|
+
* quoted segments preserved) has already been seen. Returns the offending name in its
|
|
180
|
+
* original form for the error message.
|
|
181
|
+
*/
|
|
182
|
+
findDuplicatePartitionName(names) {
|
|
183
|
+
const seen = new Set();
|
|
184
|
+
for (const name of names) {
|
|
185
|
+
const normalized = normalizePartitionNameForComparison(name);
|
|
186
|
+
if (seen.has(normalized)) {
|
|
187
|
+
return name;
|
|
188
|
+
}
|
|
189
|
+
seen.add(normalized);
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Partition names may be bare (`child`), schema-qualified (`schema.child`), or use quoted
|
|
195
|
+
* identifiers (`"my.schema"."child"`). Reject anything with more than one unquoted `.`.
|
|
196
|
+
*/
|
|
197
|
+
hasValidPartitionName(name) {
|
|
198
|
+
let depth = 0;
|
|
199
|
+
let dots = 0;
|
|
200
|
+
for (let i = 0; i < name.length; i++) {
|
|
201
|
+
const ch = name[i];
|
|
202
|
+
if (ch === '"') {
|
|
203
|
+
if (name[i + 1] === '"') {
|
|
204
|
+
i++;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
depth = depth === 0 ? 1 : 0;
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (ch === '.' && depth === 0) {
|
|
211
|
+
dots++;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return dots <= 1;
|
|
215
|
+
}
|
|
216
|
+
hasPartitionExpression(expression) {
|
|
217
|
+
if (expression == null) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
if (typeof expression === 'function') {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
if (Array.isArray(expression)) {
|
|
224
|
+
return expression.length > 0 && expression.every(key => typeof key === 'string' && key.trim().length > 0);
|
|
225
|
+
}
|
|
226
|
+
return String(expression).trim().length > 0;
|
|
227
|
+
}
|
|
228
|
+
validatePartitionKeyConstraints(meta) {
|
|
229
|
+
const partitionFields = this.getPartitionKeyFields(meta);
|
|
230
|
+
if (!partitionFields?.length) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const primaryKeyFields = meta.root.getPrimaryProps().flatMap(prop => prop.fieldNames);
|
|
234
|
+
if (partitionFields.some(field => !primaryKeyFields.includes(field))) {
|
|
235
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: primary key must include partition key columns '${partitionFields.join("', '")}'`);
|
|
236
|
+
}
|
|
237
|
+
for (const prop of Object.values(meta.root.properties)) {
|
|
238
|
+
if (!prop.unique || !prop.fieldNames?.length) {
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (partitionFields.some(field => !prop.fieldNames.includes(field))) {
|
|
242
|
+
throw new MetadataError(`Entity ${meta.root.className} has invalid partitionBy option: unique property ${meta.root.className}.${prop.name} must include partition key columns '${partitionFields.join("', '")}'`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
for (const unique of meta.root.uniques ?? []) {
|
|
246
|
+
const fields = this.getConstraintFields(meta.root, unique.properties);
|
|
247
|
+
if (!fields?.length) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (partitionFields.some(field => !fields.includes(field))) {
|
|
251
|
+
const constraint = unique.name ? `unique constraint '${unique.name}'` : 'unique constraint';
|
|
252
|
+
throw new MetadataError(`Entity ${meta.root.className} has invalid partitionBy option: ${constraint} must include partition key columns '${partitionFields.join("', '")}'`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Returns the list of physical field names that a partition expression references, or
|
|
258
|
+
* `undefined` when the expression is opaque (callback, or raw SQL like `date_trunc('day', x)`
|
|
259
|
+
* that we cannot statically parse). Opaque expressions intentionally bypass the primary-key /
|
|
260
|
+
* unique-constraint coverage checks — users are trusted to ensure the referenced columns are
|
|
261
|
+
* part of the partition key, since PostgreSQL will surface the violation at DDL execution.
|
|
262
|
+
*/
|
|
263
|
+
getPartitionKeyFields(meta) {
|
|
264
|
+
const expression = meta.partitionBy?.expression;
|
|
265
|
+
if (!expression || typeof expression === 'function') {
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
if (Array.isArray(expression)) {
|
|
269
|
+
return expression.map(key => this.resolvePartitionKeyField(meta, key));
|
|
270
|
+
}
|
|
271
|
+
const keys = splitCommaSeparatedIdentifiers(String(expression).trim());
|
|
272
|
+
if (!keys) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
return keys.map(key => this.resolvePartitionKeyField(meta, key));
|
|
276
|
+
}
|
|
277
|
+
resolvePartitionKeyField(meta, key) {
|
|
278
|
+
const trimmed = key.trim().replaceAll('"', '');
|
|
279
|
+
if (!trimmed) {
|
|
280
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: empty partition key`);
|
|
281
|
+
}
|
|
282
|
+
const prop = meta.root.properties[trimmed] ??
|
|
283
|
+
Object.values(meta.root.properties).find(candidate => candidate.fieldNames?.length === 1 && candidate.fieldNames[0] === trimmed);
|
|
284
|
+
if (!prop) {
|
|
285
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: unknown partition key '${key.trim()}'`);
|
|
286
|
+
}
|
|
287
|
+
if (prop.fieldNames?.length !== 1) {
|
|
288
|
+
throw new MetadataError(`Entity ${meta.className} has invalid partitionBy option: partition key '${key.trim()}' maps to multiple columns ('${prop.fieldNames?.join("', '")}'); list them explicitly as partition keys`);
|
|
289
|
+
}
|
|
290
|
+
return prop.fieldNames[0];
|
|
291
|
+
}
|
|
292
|
+
getConstraintFields(meta, properties) {
|
|
293
|
+
if (!properties) {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
const fields = Utils.asArray(properties).flatMap(propName => {
|
|
297
|
+
const prop = meta.root.properties[propName];
|
|
298
|
+
return prop?.fieldNames ?? [];
|
|
299
|
+
});
|
|
300
|
+
return fields.length > 0 ? fields : undefined;
|
|
301
|
+
}
|
|
114
302
|
validateReference(meta, prop, options) {
|
|
115
303
|
// references do have types
|
|
116
304
|
if (!prop.type) {
|
|
@@ -160,7 +348,7 @@ export class MetadataValidator {
|
|
|
160
348
|
* Composite unique constraints are not sufficient for targetKey.
|
|
161
349
|
*/
|
|
162
350
|
isPropertyUnique(prop, meta) {
|
|
163
|
-
if (prop.unique) {
|
|
351
|
+
if (prop.unique || (prop.primary && meta.primaryKeys.length === 1)) {
|
|
164
352
|
return true;
|
|
165
353
|
}
|
|
166
354
|
// Check for single-property unique constraint via @Unique decorator
|
|
@@ -171,6 +359,15 @@ export class MetadataValidator {
|
|
|
171
359
|
}
|
|
172
360
|
validatePolymorphicTargets(meta, prop) {
|
|
173
361
|
const targets = prop.polymorphTargets;
|
|
362
|
+
// Union-target M:N stores one scalar target FK per pivot row, so composite-PK targets
|
|
363
|
+
// can't round-trip through this schema.
|
|
364
|
+
if (prop.kind === ReferenceKind.MANY_TO_MANY && targets.length > 1) {
|
|
365
|
+
for (const target of targets) {
|
|
366
|
+
if (target.compositePK) {
|
|
367
|
+
throw MetadataError.incompatiblePolymorphicTargets(meta, prop, targets[0], target, `${target.className} has a composite primary key; union-target polymorphic M:N does not support composite-PK targets.`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
174
371
|
// Validate targetKey exists and is compatible across all targets
|
|
175
372
|
if (prop.targetKey) {
|
|
176
373
|
for (const target of targets) {
|
|
@@ -349,6 +546,10 @@ export class MetadataValidator {
|
|
|
349
546
|
if (!meta.expression) {
|
|
350
547
|
throw MetadataError.viewEntityWithoutExpression(meta);
|
|
351
548
|
}
|
|
549
|
+
// Views are not partitionable - reject explicitly instead of silently ignoring
|
|
550
|
+
if (meta.partitionBy) {
|
|
551
|
+
throw new MetadataError(`View entity ${meta.className} cannot define partitionBy`);
|
|
552
|
+
}
|
|
352
553
|
// Validate indexes if present
|
|
353
554
|
this.validateIndexes(meta, meta.indexes ?? [], 'index');
|
|
354
555
|
this.validateIndexes(meta, meta.uniques ?? [], 'unique');
|