mikro-orm icon indicating copy to clipboard operation
mikro-orm copied to clipboard

Add an "offset" option to "em.findOne()"

Open KevinNovak opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. I want to be able to find a single random child from a parent. In my app there are "Deck" and "Card" entities, and I want to be able to choose a random card from a deck.

Describe the solution you'd like I'd like to be able to do something like so:

let card = await em.findOne(
    Card,
    { deck: "my_deck", alreadyPlayed: false },
    { orderBy: { order: QueryOrder.ASC }, offset: randomNumber() }
);

However "offset" is not an available option.

Another solution could be if there was a such thing as "QueryOrder.Random".

Describe alternatives you've considered I can work around this by using em.find() with a limit instead:

let card = await em.find(
    Card,
    { deck: "my_deck", alreadyPlayed: false },
    { orderBy: { order: QueryOrder.ASC }, offset: randomNumber(), limit: 1 }
)?.[0];

However it would be more convenient if findOne() had an "offset" option.

KevinNovak avatar Jul 11 '22 00:07 KevinNovak

Less clunky workaround is via array destructing:

const [card] = await em.find(...);

But I dont mind having offset there, feel free to send a PR.

B4nan avatar Jul 11 '22 10:07 B4nan