Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FindQuery empty symbol #4873

Closed
Kalos-S opened this issue Oct 23, 2023 · 1 comment · Fixed by #4875
Closed

FindQuery empty symbol #4873

Kalos-S opened this issue Oct 23, 2023 · 1 comment · Fixed by #4875
Labels
enhancement New feature or request

Comments

@Kalos-S
Copy link

Kalos-S commented Oct 23, 2023

I need to implement a get multiple api endpoint that can be searched by multiple properties like status, user, and so on. If one of those properties is omitted from the request query it should be ignored by the find query.

Currently if either undefined or null gets passed the column gets queried for null which is not the wanted behaviour for an omitted filter. Adding the option to pass an empty symbol would fix that issue.

Example:

const query = {
  status: "active",
};

em.find(Foo, { status: query.status, user: query.user })

currently returns

SELECT * FROM FOO WHERE status = "active" AND user is null

With an Empty symbol added the code could look like this:

const query = {
  status: "active",
};

em.find(Foo, { status: query.status || Symbols.Empty, user: query.user || Symbols.Empty })

would return

SELECT * FROM FOO WHERE status = "active"

Expanding this same behaviour to query conditions like $gt or $lte would also be amazing.

@Kalos-S Kalos-S added the enhancement New feature or request label Oct 23, 2023
@B4nan
Copy link
Member

B4nan commented Oct 23, 2023

Numerous users were asking for undefined to be ignored this way, I am planning to have an option to trigger that behaviour. Do you think we really need some special symbol for that, or will it be enough if undefined behaves like that?

B4nan added a commit that referenced this issue Oct 24, 2023
The ORM will treat explicitly defined `undefined` values in your `em.find()` queries as `null`s. If you want to ignore them instead, use `ignoreUndefinedInQuery` option:

```ts
MikroORM.init({
  ignoreUndefinedInQuery: true,
});

// resolves to `em.find(User, {})`
await em.find(User, { email: undefined, { profiles: { foo: undefined } } });
```

Closes #4873
B4nan added a commit that referenced this issue Oct 24, 2023
…4875)

The ORM will treat explicitly defined `undefined` values in your
`em.find()` queries as `null`s. If you want to ignore them instead, use
`ignoreUndefinedInQuery` option:

```ts
MikroORM.init({
  ignoreUndefinedInQuery: true,
});

// resolves to `em.find(User, {})`
await em.find(User, { email: undefined, { profiles: { foo: undefined } } });
```

Closes #4873
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants