Skip to content

Commit

Permalink
feat(core): add callback parameter to Collection.remove()
Browse files Browse the repository at this point in the history
```ts
book.tags.remove(t => t.id === params.id);
```

Closes #2398
  • Loading branch information
B4nan committed Nov 13, 2021
1 parent ed1cf7d commit 0b37654
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions packages/core/src/entity/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,18 @@ export class Collection<T, O = unknown> extends ArrayCollection<T, O> {
}
}

remove(...items: (T | Reference<T>)[]): void {
const unwrapped = items.map(i => Reference.unwrapReference(i));
remove(...items: (T | Reference<T> | ((item: T) => boolean))[]): void {
if (items[0] instanceof Function) {
for (const item of this.items) {
if (items[0](item)) {
this.remove(item);
}
}

return;
}

const unwrapped = items.map(i => Reference.unwrapReference(i as T));
this.modify('remove', unwrapped);
const em = this.getEntityManager(unwrapped, false);

Expand Down
2 changes: 1 addition & 1 deletion tests/EntityManager.postgre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ describe('EntityManagerPostgre', () => {
// test collection CRUD
// remove
expect(book.tags.count()).toBe(2);
book.tags.remove(orm.em.getReference(BookTag2, tag1.id)); // we need to get reference as tag1 is detached from current EM
book.tags.remove(t => t.id === tag1.id); // we need to get reference as tag1 is detached from current EM
await orm.em.persistAndFlush(book);
orm.em.clear();
book = (await orm.em.findOne(Book2, book.uuid, { populate: ['tags'] }))!;
Expand Down
2 changes: 1 addition & 1 deletion tests/issues/GH949.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('GH issue 949', () => {

// Force refresh
expect(await aEntity.bItems.loadCount(true)).toEqual(1);
// Testing array collection implemntation
// Testing array collection implementation
await orm.em.flush();
orm.em.clear();

Expand Down

0 comments on commit 0b37654

Please sign in to comment.