Skip to content

Commit

Permalink
feat(core): allow calling Collection.set() on not initialized colle…
Browse files Browse the repository at this point in the history
…ctions

Normally we are diffing the collection snapshot with the current state, issueing
only the right insert/delete queries to sync the state. Now when the collection
is not initialied and we call set, we will remove all the items before adding the
new set of items.

Related #1048
  • Loading branch information
B4nan committed Nov 7, 2020
1 parent 7960932 commit 1d0bb85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
17 changes: 9 additions & 8 deletions packages/core/src/entity/Collection.ts
Expand Up @@ -99,6 +99,12 @@ export class Collection<T, O = unknown> extends ArrayCollection<T, O> {
const unwrapped = items.map(i => Reference.unwrapReference(i));
unwrapped.forEach(item => this.validateItemType(item));
this.validateModification(unwrapped);

if (!this.initialized) {
this.initialized = true;
this.snapshot = undefined;
}

super.set(unwrapped);
this.setDirty();
this.cancelOrphanRemoval(unwrapped);
Expand All @@ -107,15 +113,10 @@ export class Collection<T, O = unknown> extends ArrayCollection<T, O> {
/**
* @internal
*/
hydrate(items?: T[]): void {
hydrate(items: T[]): void {
this.initialized = true;

if (items) {
super.hydrate(items);
this.takeSnapshot();
} else {
this.snapshot = undefined;
}
super.hydrate(items);
this.takeSnapshot();
}

remove(...items: (T | Reference<T>)[]): void {
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/entity/EntityAssigner.ts
Expand Up @@ -120,10 +120,6 @@ export class EntityAssigner {
throw new Error(`Invalid collection values provided for '${name}.${prop.name}' in ${name}.assign(): ${inspect(invalid)}`);
}

if (!collection.isInitialized()) {
collection.hydrate();
}

collection.set(items);
}

Expand Down

0 comments on commit 1d0bb85

Please sign in to comment.