Automattic / Automattic/mongoose

Rollback the implementation of .exists() method to be syntactically consistent (+ add similar method with correct naming)

Open
#12,211 5 comments 6 reactions 0 assignees View on GitHub
backwards-breaking discussion enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 7h
Merged PRs (30d)
35

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the feature has not already been requested

### 🚀 Feature Proposal

**Description**

The `exists` method in common frameworks and implementation implies the same thing : a boolean (or promise that resolve to a boolean).

Changing the meaning of this function can be extremely misleading for everyone that will make assumptions, this change produce a breaking change (even if the value is "truthy").

Consider the snippet bellow, even if code like this may be extremely rare, its remains inconsistent syntactically :
```js
const exists = await Model.exists({});

if (exists) {} // works
if (exists === false) {} // never trigger
if (exists === true) {} // never trigger
```

**Proposition**

I know the functionality provided by the new implementation is wanted, so bellow my proposition :

- revert the changes to this signature (as of `5.13.X` version) :
```js
prototype.exists({}): Promise
```
- add a method to keep the `exists` functionality :
```js
prototype.findOneId({}) : Promise<{_id: ObjectId}|null>
```

### Motivation

Keeping code consistent, understandable for anyone, with no misleading syntax or behaviours

Someone that doesn't know mongoose, should be able to read the code and understand it without documentation.

### Example

_No response_

**edit**: typo

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the current Model.exists({}) behavior and the stated 5.13.X signature. Compare the proposed boolean return with the proposed findOneId({}) result, then check how the existing API and its callers would be affected. Done means the naming and return values are consistent without leaving the breaking behavior ambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb
Domain
backend-api-design, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.