MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / validateModification

Method validateModification

packages/core/src/entity/Collection.ts:538–568  ·  view source on GitHub ↗
(items: T[])

Source from the content-addressed store, hash-verified

536 }
537
538 private validateModification(items: T[]): void {
539 if (this.#readonly) {
540 throw ValidationError.cannotModifyReadonlyCollection(this.owner, this.property);
541 }
542
543 const check = (item: AnyEntity) => {
544 if (!item) {
545 return false;
546 }
547
548 if (!Utils.isEntity(item)) {
549 throw ValidationError.notEntity(this.owner, this.property, item);
550 }
551
552 // currently we allow persisting to inverse sides only in SQL drivers
553 if (this.property.pivotTable || !this.property.mappedBy) {
554 return false;
555 }
556
557 if (helper(item).__initialized) {
558 return false;
559 }
560
561 return !item[this.property.mappedBy] && this.property.kind === ReferenceKind.MANY_TO_MANY;
562 };
563
564 // throw if we are modifying inverse side of M:N collection when owning side is initialized (would be ignored when persisting)
565 if (items.some(item => check(item))) {
566 throw ValidationError.cannotModifyInverseCollection(this.owner, this.property);
567 }
568 }
569
570 /** Converts all items in the collection to plain DTO objects. */
571 toArray<TT extends T>(): EntityDTO<TT>[] {

Callers 2

addMethod · 0.95
removeMethod · 0.95

Calls 3

checkFunction · 0.50

Tested by

no test coverage detected