ORM: where({}) counts as a filter, so where({}).deleteAll() deletes every row

Open
#30,994 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
mongodb, postgresql, sqlite, typescript
Domain
databases

Research direction

Read ADR 265 and the related issue #30784/PR #30785 first. The issue points to collection.state.test.ts and identifies separate SQL and MongoDB behavior; trace their where and bulk write checks. Done means a design decision defines explicit opt-in and consistent compile-time and runtime checks for both ORM families, with clear error messages.

Written by the indexing model from the issue text.

Description

Problem

The ORM refuses a bulk write with no filter at compile time:

await db.orm.public.User.deleteAll();
// error: The 'this' context of type 'Collection<...>' is not assignable to method's 'this' of type 'HasWhere'

An empty filter gets past that check:

await db.orm.public.User.where({}).deleteAll(); // compiles; on Postgres and SQLite it runs DELETE FROM "user"

A call that deletes every row should be something the user clearly opts into. where({}) does not read as "delete everything", and it is easy to produce by accident, for example from a filter object built up from optional inputs that all turned out empty.

What happens today

  • The compile-time check. update, updateAll, updateAndCount, delete, deleteAll and deleteAndCount require a collection known to be filtered (A collection keeps its class through the chain). The same decision says where(...) always marks the collection as filtered, and nothing excludes an empty object. So where({}) satisfies the check, in both the SQL and the MongoDB ORM.
  • SQL ORM at run time. where({}) returns the same collection (collection.state.test.ts: expect(collection.where({})).toBe(collection)), so deleteAll() sends a DELETE with no WHERE. The SQL ORM has no run-time equivalent of the compile-time check: deleteAll() checks for limits, offsets and locks, but not for a filter.
  • MongoDB ORM at run time. where({}) adds no filter, and a separate check throws ORM.WHERE_MISSING: "deleteAll() requires a .where() filter. Call .where() before .deleteAll()". The user did call .where(), which is the confusion in #30784. No design doc records why MongoDB has this check and SQL does not.

So the two ORMs behave differently, and on SQL nothing catches this at run time.

What we want

  • Deleting or updating every row needs an explicit opt-in that reads as exactly that.
  • A filter with no conditions, such as where({}), does not count as a filter.
  • Both families check the same way, twice: the compile-time "is filtered" check, then a run-time check that refuses the same calls when the collection has no filter. The run-time check replaces MongoDB's own ORM.WHERE_MISSING check rather than sitting beside it.
  • The errors say how to opt in.

The shape of the opt-in is open. One possibility is a value the user imports and passes, such as where(ALL); that is an illustration, not a decision.

This changes what the decision above says where does, so it needs a design decision before implementation.

Related

  • #30784, and its PR #30785, which improves MongoDB's message without changing the behaviour.
Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.