Skip to content

Commit

Permalink
fix(core): throw when trying to call em.remove(..., null)
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Apr 23, 2020
1 parent aa87d8c commit f6e3a39
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/EntityManager.ts
Expand Up @@ -437,6 +437,8 @@ export class EntityManager<D extends IDatabaseDriver = IDatabaseDriver> {
return ret ? ret.then(() => 1) : ret;
}

this.validator.validateRemoveEmptyWhere(entityName, where);

return this.nativeDelete(entityName, where);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/entity/EntityValidator.ts
Expand Up @@ -83,6 +83,12 @@ export class EntityValidator {
}
}

validateRemoveEmptyWhere<T extends AnyEntity<T>>(className: string, where: FilterQuery<T>): void {
if (!Utils.isDefined(where, true)) {
throw new Error(`You cannot call 'EntityManager.remove()' with empty 'where' parameter. If you want to remove all entities, use 'em.remove(${className}, {})'.`);
}
}

private validateCollection<T extends AnyEntity<T>>(entity: T, prop: EntityProperty): void {
if (wrap(entity).isInitialized() && !entity[prop.name as keyof T]) {
throw ValidationError.fromCollectionNotInitialized(entity, prop);
Expand Down
5 changes: 5 additions & 0 deletions tests/EntityManager.mysql.test.ts
Expand Up @@ -1852,6 +1852,11 @@ describe('EntityManagerMySql', () => {
expect(mock.mock.calls[2][0]).toMatch('commit');
});

test('em.remove() with null or undefined in where parameter throws', async () => {
expect(() => orm.em.remove(Book2, undefined as any)).toThrowError(`You cannot call 'EntityManager.remove()' with empty 'where' parameter. If you want to remove all entities, use 'em.remove(Book2, {})'.`);
expect(() => orm.em.remove(Book2, null)).toThrowError(`You cannot call 'EntityManager.remove()' with empty 'where' parameter. If you want to remove all entities, use 'em.remove(Book2, {})'.`);
});

afterAll(async () => orm.close(true));

});

0 comments on commit f6e3a39

Please sign in to comment.